From: Ed Sanders Date: Tue, 26 Sep 2017 16:31:38 +0000 (+0100) Subject: Make mw.widget.TitleWidget more flexible X-Git-Tag: 1.31.0-rc.0~1954^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmes_infos.php?a=commitdiff_plain;h=8edf301a9a1e5ca71aa90fa5665c06b49d79661c;p=lhc%2Fweb%2Fwiklou.git Make mw.widget.TitleWidget more flexible * Factor out #createOptionWidget so different option widget subclasses can be constructed * Factor out #getApiParams so extra data can be fetched from the API * Pass through raw API data to pageData, so extra API data can be used in option widgets Change-Id: I150c513e4144ad5b57643e98dd48866ce2d37850 --- diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js index 9703eeac73..8e7afd7f19 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js @@ -129,8 +129,7 @@ if ( mw.Title.newFromText( query ) ) { return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes ) { - var params, - interwiki = query.substring( 0, query.indexOf( ':' ) ); + var interwiki = query.substring( 0, query.indexOf( ':' ) ); if ( interwiki && interwiki !== '' && interwikiPrefixes.indexOf( interwiki ) !== -1 @@ -141,28 +140,7 @@ } ] } } ).promise( promiseAbortObject ); } else { - params = { - action: 'query', - prop: [ 'info', 'pageprops' ], - generator: 'prefixsearch', - gpssearch: query, - gpsnamespace: widget.namespace !== null ? widget.namespace : undefined, - gpslimit: widget.limit, - ppprop: 'disambiguation' - }; - if ( widget.showRedirectTargets ) { - params.redirects = true; - } - if ( widget.showImages ) { - params.prop.push( 'pageimages' ); - params.pithumbsize = 80; - params.pilimit = widget.limit; - } - if ( widget.showDescriptions ) { - params.prop.push( 'pageterms' ); - params.wbptterms = 'description'; - } - req = api.get( params ); + req = api.get( widget.getApiParams( query ) ); promiseAbortObject.abort = req.abort.bind( req ); // TODO ew return req.then( function ( ret ) { if ( ret.query === undefined ) { @@ -180,6 +158,37 @@ } }; + /** + * Get API params for a given query + * + * @param {string} query User query + * @return {Object} API params + */ + mw.widgets.TitleWidget.prototype.getApiParams = function ( query ) { + var params = { + action: 'query', + prop: [ 'info', 'pageprops' ], + generator: 'prefixsearch', + gpssearch: query, + gpsnamespace: this.namespace !== null ? this.namespace : undefined, + gpslimit: this.limit, + ppprop: 'disambiguation' + }; + if ( this.showRedirectTargets ) { + params.redirects = true; + } + if ( this.showImages ) { + params.prop.push( 'pageimages' ); + params.pithumbsize = 80; + params.pilimit = this.limit; + } + if ( this.showDescriptions ) { + params.prop.push( 'pageterms' ); + params.wbptterms = 'description'; + } + return params; + }; + /** * Get the API object for title requests * @@ -226,7 +235,8 @@ imageUrl: OO.getProp( suggestionPage, 'thumbnail', 'source' ), description: OO.getProp( suggestionPage, 'terms', 'description' ), // Sort index - index: suggestionPage.index + index: suggestionPage.index, + originalData: suggestionPage }; // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true @@ -244,7 +254,8 @@ disambiguation: false, description: mw.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage.title ), // Sort index, just below its target - index: suggestionPage.index + 0.5 + index: suggestionPage.index + 0.5, + originalData: suggestionPage }; titles.push( redirects[ i ] ); } @@ -284,12 +295,22 @@ for ( i = 0, len = titles.length; i < len; i++ ) { page = pageData[ titles[ i ] ] || {}; - items.push( new mw.widgets.TitleOptionWidget( this.getOptionWidgetData( titles[ i ], page ) ) ); + items.push( this.createOptionWidget( this.getOptionWidgetData( titles[ i ], page ) ) ); } return items; }; + /** + * Create a menu option widget with specified data + * + * @param {Object} data Data for option widget + * @return {OO.ui.MenuOptionWidget} Data for option widget + */ + mw.widgets.TitleWidget.prototype.createOptionWidget = function ( data ) { + return new mw.widgets.TitleOptionWidget( data ); + }; + /** * Get menu option widget data from the title and page data *