Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/contextitems/ve.ui.MWInternalLinkContextItem.js

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