1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*!
* VisualEditor MWInternalLinkContextItem class.
*
* @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
*/
/**
* 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
*/
ve.ui.MWInternalLinkContextItem = function VeUiMWInternalLinkContextItem() {
// Parent constructor
ve.ui.MWInternalLinkContextItem.super.apply( this, arguments );
// 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 ];
/* Static methods */
/**
* 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)
* @param {ve.ui.Context} context Context (for resizing)
* @return {jQuery} The jQuery object of the link context item
*/
ve.ui.MWInternalLinkContextItem.static.generateBody = function ( linkCache, model, htmlDoc, context ) {
var title = model.getAttribute( 'lookupTitle' ),
normalizedTitle = model.getAttribute( 'normalizedTitle' ),
href = model.getHref(),
titleObj = mw.Title.newFromText( mw.libs.ve.normalizeParsoidResourceName( href ) ),
fragment = model.getFragment(),
usePageImages = mw.config.get( 'wgVisualEditorConfig' ).usePageImages,
usePageDescriptions = mw.config.get( 'wgVisualEditorConfig' ).usePageDescriptions,
$wrapper = $( '<div>' ),
$link = $( '<a>' )
.addClass( 've-ui-linkContextItem-link' )
.text( normalizedTitle )
.attr( {
href: titleObj.getUrl(),
target: '_blank',
rel: 'noopener'
} );
// Style based on link cache information
ve.init.platform.linkCache.styleElement( title, $link, fragment );
// Don't style as a self-link in the context menu (but do elsewhere)
$link.removeClass( 'mw-selflink' );
var icon;
if ( usePageImages ) {
icon = new OO.ui.IconWidget( { icon: 'page-existing' } );
$wrapper
.addClass( 've-ui-mwInternalLinkContextItem-withImage' )
.append( icon.$element );
}
$wrapper.append( $link );
if ( usePageDescriptions ) {
$wrapper.addClass( 've-ui-mwInternalLinkContextItem-withDescription' );
}
if ( usePageImages || usePageDescriptions ) {
linkCache.get( title ).then( function ( linkData ) {
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 ) );
}
}
if ( usePageDescriptions && linkData.description ) {
var $description = $( '<span>' )
.addClass( 've-ui-mwInternalLinkContextItem-description' )
.text( linkData.description );
$wrapper.append( $description );
// Multiline descriptions may make the context bigger (T183650)
context.updateDimensions();
}
} );
}
return $wrapper;
};
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWInternalLinkContextItem.prototype.getDescription = function () {
return this.model.getAttribute( 'normalizedTitle' );
};
/**
* @inheritdoc
*/
ve.ui.MWInternalLinkContextItem.prototype.renderBody = function () {
var $body = this.constructor.static.generateBody(
ve.init.platform.linkCache,
this.model,
this.context.getSurface().getModel().getDocument().getHtmlDocument(),
this.context
);
this.$body.empty().append( $body );
if ( !this.context.isMobile() ) {
this.$body.append( this.$labelLayout );
}
this.updateLabelPreview();
};
/* Registration */
ve.ui.contextItemFactory.register( ve.ui.MWInternalLinkContextItem );
|