2015-02-25 22:56:28 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MWInternalLinkContextItem class.
|
|
|
|
*
|
2017-01-03 16:58:33 +00:00
|
|
|
* @copyright 2011-2017 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 ];
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkContextItem.prototype.getDescription = function () {
|
|
|
|
return this.model.getAttribute( 'normalizedTitle' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkContextItem.prototype.renderBody = function () {
|
2015-04-25 00:36:15 +00:00
|
|
|
var icon, $description,
|
|
|
|
usePageImages = mw.config.get( 'wgVisualEditor' ).usePageImages,
|
|
|
|
usePageDescriptions = mw.config.get( 'wgVisualEditor' ).usePageDescriptions,
|
|
|
|
title = this.model.getAttribute( 'lookupTitle' ),
|
|
|
|
htmlDoc = this.context.getSurface().getModel().getDocument().getHtmlDocument(),
|
|
|
|
$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' )
|
2015-03-04 16:17:06 +00:00
|
|
|
.text( this.getDescription() )
|
|
|
|
.attr( {
|
|
|
|
href: ve.resolveUrl( this.model.getHref(), htmlDoc ),
|
|
|
|
target: '_blank'
|
|
|
|
} );
|
2015-02-25 22:56:28 +00:00
|
|
|
|
|
|
|
// Style based on link cache information
|
2017-04-11 17:00:49 +00:00
|
|
|
ve.init.platform.linkCache.styleElement( title, $link, this.model.getFragment() );
|
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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$body.empty().append( $wrapper );
|
|
|
|
|
|
|
|
if ( usePageImages || usePageDescriptions ) {
|
|
|
|
ve.init.platform.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 );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2015-02-25 22:56:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.contextItemFactory.register( ve.ui.MWInternalLinkContextItem );
|