Make mw.widget.TitleWidget more flexible
authorEd Sanders <esanders@wikimedia.org>
Tue, 26 Sep 2017 16:31:38 +0000 (17:31 +0100)
committerEd Sanders <esanders@wikimedia.org>
Tue, 26 Sep 2017 16:31:38 +0000 (17:31 +0100)
* 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

resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js

index 9703eea..8e7afd7 100644 (file)
 
                if ( mw.Title.newFromText( query ) ) {
                        return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes ) {
 
                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
                                if (
                                        interwiki && interwiki !== '' &&
                                        interwikiPrefixes.indexOf( interwiki ) !== -1
                                                } ]
                                        } } ).promise( promiseAbortObject );
                                } else {
                                                } ]
                                        } } ).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 ) {
                                        promiseAbortObject.abort = req.abort.bind( req ); // TODO ew
                                        return req.then( function ( ret ) {
                                                if ( ret.query === undefined ) {
                }
        };
 
                }
        };
 
+       /**
+        * 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
         *
        /**
         * Get the API object for title requests
         *
                                imageUrl: OO.getProp( suggestionPage, 'thumbnail', 'source' ),
                                description: OO.getProp( suggestionPage, 'terms', 'description' ),
                                // Sort index
                                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
                        };
 
                        // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
                                        disambiguation: false,
                                        description: mw.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage.title ),
                                        // Sort index, just below its target
                                        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 ] );
                        }
                                };
                                titles.push( redirects[ i ] );
                        }
 
                for ( i = 0, len = titles.length; i < len; i++ ) {
                        page = pageData[ titles[ i ] ] || {};
 
                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;
        };
 
                }
 
                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
         *
        /**
         * Get menu option widget data from the title and page data
         *