From c03d69b761f0429677f43fee95d0c277206721bc Mon Sep 17 00:00:00 2001 From: David Chan Date: Wed, 9 Aug 2017 23:18:39 +0800 Subject: [PATCH] Expose ve.ui.MWInternalLinkContextItem generateBody logic Change-Id: I94912ac2c471cc5d227fa8b9cb29c302ca29814d --- .../ve.ui.MWInternalLinkContextItem.js | 55 +++++++++++++------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/modules/ve-mw/ui/contextitems/ve.ui.MWInternalLinkContextItem.js b/modules/ve-mw/ui/contextitems/ve.ui.MWInternalLinkContextItem.js index 03072ef6ca..0c269da2ca 100644 --- a/modules/ve-mw/ui/contextitems/ve.ui.MWInternalLinkContextItem.js +++ b/modules/ve-mw/ui/contextitems/ve.ui.MWInternalLinkContextItem.js @@ -33,35 +33,35 @@ ve.ui.MWInternalLinkContextItem.static.name = 'link/internal'; ve.ui.MWInternalLinkContextItem.static.modelClasses = [ ve.dm.MWInternalLinkAnnotation ]; -/* Methods */ +/* Static methods */ /** - * @inheritdoc + * 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 */ -ve.ui.MWInternalLinkContextItem.prototype.getDescription = function () { - return this.model.getAttribute( 'normalizedTitle' ); -}; - -/** - * @inheritdoc - */ -ve.ui.MWInternalLinkContextItem.prototype.renderBody = function () { +ve.ui.MWInternalLinkContextItem.static.generateBody = function ( linkCache, model, htmlDoc ) { var icon, $description, + title = model.getAttribute( 'lookupTitle' ), + description = model.getAttribute( 'normalizedTitle' ), + href = model.getHref(), + fragment = model.getFragment(), 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 = $( '
' ), $link = $( '' ) .addClass( 've-ui-mwInternalLinkContextItem-link' ) - .text( this.getDescription() ) + .text( description ) .attr( { - href: ve.resolveUrl( this.model.getHref(), htmlDoc ), + href: ve.resolveUrl( href, htmlDoc ), target: '_blank' } ); // Style based on link cache information - ve.init.platform.linkCache.styleElement( title, $link, this.model.getFragment() ); + 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' ); @@ -78,10 +78,8 @@ ve.ui.MWInternalLinkContextItem.prototype.renderBody = function () { $wrapper.addClass( 've-ui-mwInternalLinkContextItem-withDescription' ); } - this.$body.empty().append( $wrapper ); - if ( usePageImages || usePageDescriptions ) { - ve.init.platform.linkCache.get( title ).then( function ( linkData ) { + linkCache.get( title ).then( function ( linkData ) { if ( usePageImages ) { if ( linkData.imageUrl ) { icon.$element @@ -99,6 +97,27 @@ ve.ui.MWInternalLinkContextItem.prototype.renderBody = function () { } } ); } + 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() + ) ); }; /* Registration */