2015-02-25 22:56:28 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MWInternalLinkContextItem class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
|
2015-02-25 22:56:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context item for a MWInternalLink.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.LinkContextItem
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Context} context Context item is in
|
|
|
|
* @param {ve.dm.Model} model Model item is related to
|
|
|
|
* @param {Object} config Configuration options
|
|
|
|
*/
|
2015-08-18 12:54:51 +00:00
|
|
|
ve.ui.MWInternalLinkContextItem = function VeUiMWInternalLinkContextItem() {
|
2015-02-25 22:56:28 +00:00
|
|
|
// Parent constructor
|
2015-03-27 18:56:16 +00:00
|
|
|
ve.ui.MWInternalLinkContextItem.super.apply( this, arguments );
|
2015-02-25 22:56:28 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$element.addClass( 've-ui-mwInternalLinkContextItem' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWInternalLinkContextItem, ve.ui.LinkContextItem );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.MWInternalLinkContextItem.static.name = 'link/internal';
|
|
|
|
|
|
|
|
ve.ui.MWInternalLinkContextItem.static.modelClasses = [ ve.dm.MWInternalLinkAnnotation ];
|
|
|
|
|
2017-08-09 15:18:39 +00:00
|
|
|
/* Static methods */
|
2015-02-25 22:56:28 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-09 15:18:39 +00:00
|
|
|
* Generate the body of the link context item
|
|
|
|
*
|
|
|
|
* @param {ve.init.mw.LinkCache} linkCache The link cache to use
|
|
|
|
* @param {ve.dm.MWInternalLinkAnnotation} model The annotation model
|
|
|
|
* @param {HTMLDocument} htmlDoc The HTML document (for URL resolution)
|
2018-01-17 16:06:28 +00:00
|
|
|
* @param {ve.ui.Context} context Context (for resizing)
|
2017-08-09 15:18:39 +00:00
|
|
|
* @return {jQuery} The jQuery object of the link context item
|
2015-02-25 22:56:28 +00:00
|
|
|
*/
|
2018-01-17 16:06:28 +00:00
|
|
|
ve.ui.MWInternalLinkContextItem.static.generateBody = function ( linkCache, model, htmlDoc, context ) {
|
2015-04-25 00:36:15 +00:00
|
|
|
var icon, $description,
|
2017-08-09 15:18:39 +00:00
|
|
|
title = model.getAttribute( 'lookupTitle' ),
|
2019-09-18 20:54:24 +00:00
|
|
|
normalizedTitle = model.getAttribute( 'normalizedTitle' ),
|
2017-08-09 15:18:39 +00:00
|
|
|
href = model.getHref(),
|
2020-03-20 17:08:24 +00:00
|
|
|
titleObj = mw.Title.newFromText( mw.libs.ve.normalizeParsoidResourceName( href ) ),
|
2017-08-09 15:18:39 +00:00
|
|
|
fragment = model.getFragment(),
|
2019-04-16 23:09:23 +00:00
|
|
|
usePageImages = mw.config.get( 'wgVisualEditorConfig' ).usePageImages,
|
|
|
|
usePageDescriptions = mw.config.get( 'wgVisualEditorConfig' ).usePageDescriptions,
|
2015-04-25 00:36:15 +00:00
|
|
|
$wrapper = $( '<div>' ),
|
2015-03-04 16:17:06 +00:00
|
|
|
$link = $( '<a>' )
|
2019-08-10 13:55:15 +00:00
|
|
|
.addClass( 've-ui-linkContextItem-link' )
|
2019-09-18 20:54:24 +00:00
|
|
|
.text( normalizedTitle )
|
2015-03-04 16:17:06 +00:00
|
|
|
.attr( {
|
2019-09-18 20:54:24 +00:00
|
|
|
href: titleObj.getUrl(),
|
2017-11-30 19:15:38 +00:00
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener'
|
2015-03-04 16:17:06 +00:00
|
|
|
} );
|
2015-02-25 22:56:28 +00:00
|
|
|
|
|
|
|
// Style based on link cache information
|
2017-08-09 15:18:39 +00:00
|
|
|
ve.init.platform.linkCache.styleElement( title, $link, fragment );
|
2017-04-11 16:20:38 +00:00
|
|
|
// Don't style as a self-link in the context menu (but do elsewhere)
|
|
|
|
$link.removeClass( 'mw-selflink' );
|
2015-04-25 00:36:15 +00:00
|
|
|
|
|
|
|
if ( usePageImages ) {
|
2015-04-28 15:26:42 +00:00
|
|
|
icon = new OO.ui.IconWidget( { icon: 'page-existing' } );
|
2015-04-25 00:36:15 +00:00
|
|
|
$wrapper
|
|
|
|
.addClass( 've-ui-mwInternalLinkContextItem-withImage' )
|
|
|
|
.append( icon.$element );
|
|
|
|
}
|
|
|
|
|
|
|
|
$wrapper.append( $link );
|
|
|
|
|
|
|
|
if ( usePageDescriptions ) {
|
|
|
|
$wrapper.addClass( 've-ui-mwInternalLinkContextItem-withDescription' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( usePageImages || usePageDescriptions ) {
|
2017-08-09 15:18:39 +00:00
|
|
|
linkCache.get( title ).then( function ( linkData ) {
|
2015-04-28 15:26:42 +00:00
|
|
|
if ( usePageImages ) {
|
|
|
|
if ( linkData.imageUrl ) {
|
|
|
|
icon.$element
|
|
|
|
.addClass( 've-ui-mwInternalLinkContextItem-hasImage' )
|
|
|
|
.css( 'background-image', 'url(' + linkData.imageUrl + ')' );
|
|
|
|
} else {
|
|
|
|
icon.setIcon( ve.init.platform.linkCache.constructor.static.getIconForLink( linkData ) );
|
|
|
|
}
|
2015-04-25 00:36:15 +00:00
|
|
|
}
|
|
|
|
if ( usePageDescriptions && linkData.description ) {
|
|
|
|
$description = $( '<span>' )
|
|
|
|
.addClass( 've-ui-mwInternalLinkContextItem-description' )
|
|
|
|
.text( linkData.description );
|
|
|
|
$wrapper.append( $description );
|
2018-01-17 16:06:28 +00:00
|
|
|
// Multiline descriptions may make the context bigger (T183650)
|
|
|
|
context.updateDimensions();
|
2015-04-25 00:36:15 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2017-08-09 15:18:39 +00:00
|
|
|
return $wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkContextItem.prototype.getDescription = function () {
|
|
|
|
return this.model.getAttribute( 'normalizedTitle' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkContextItem.prototype.renderBody = function () {
|
|
|
|
this.$body.empty().append( this.constructor.static.generateBody(
|
|
|
|
ve.init.platform.linkCache,
|
|
|
|
this.model,
|
2018-01-17 16:06:28 +00:00
|
|
|
this.context.getSurface().getModel().getDocument().getHtmlDocument(),
|
|
|
|
this.context
|
2019-07-05 16:53:10 +00:00
|
|
|
) );
|
|
|
|
if ( !this.context.isMobile() ) {
|
|
|
|
this.$body.append( this.$labelLayout );
|
|
|
|
}
|
2018-05-30 16:57:03 +00:00
|
|
|
this.updateLabelPreview();
|
2015-02-25 22:56:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.contextItemFactory.register( ve.ui.MWInternalLinkContextItem );
|