2015-02-25 22:56:28 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MWInternalLinkContextItem class.
|
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 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)
|
|
|
|
* @return {jQuery} The jQuery object of the link context item
|
2015-02-25 22:56:28 +00:00
|
|
|
*/
|
2017-08-09 15:18:39 +00:00
|
|
|
ve.ui.MWInternalLinkContextItem.static.generateBody = function ( linkCache, model, htmlDoc ) {
|
2015-04-25 00:36:15 +00:00
|
|
|
var icon, $description,
|
2017-08-09 15:18:39 +00:00
|
|
|
title = model.getAttribute( 'lookupTitle' ),
|
|
|
|
description = model.getAttribute( 'normalizedTitle' ),
|
|
|
|
href = model.getHref(),
|
|
|
|
fragment = model.getFragment(),
|
2015-04-25 00:36:15 +00:00
|
|
|
usePageImages = mw.config.get( 'wgVisualEditor' ).usePageImages,
|
|
|
|
usePageDescriptions = mw.config.get( 'wgVisualEditor' ).usePageDescriptions,
|
|
|
|
$wrapper = $( '<div>' ),
|
2015-03-04 16:17:06 +00:00
|
|
|
$link = $( '<a>' )
|
2015-04-25 00:36:15 +00:00
|
|
|
.addClass( 've-ui-mwInternalLinkContextItem-link' )
|
2017-08-09 15:18:39 +00:00
|
|
|
.text( description )
|
2015-03-04 16:17:06 +00:00
|
|
|
.attr( {
|
2017-08-09 15:18:39 +00:00
|
|
|
href: ve.resolveUrl( href, htmlDoc ),
|
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 );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
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,
|
|
|
|
this.context.getSurface().getModel().getDocument().getHtmlDocument()
|
|
|
|
) );
|
2015-02-25 22:56:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.contextItemFactory.register( ve.ui.MWInternalLinkContextItem );
|