Merge "Corrected grammatical error."
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.TitleWidget.js
index c256f1f..22bac08 100644 (file)
         * @cfg {boolean} [showImages] Show page images
         * @cfg {boolean} [showDescriptions] Show page descriptions
         * @cfg {boolean} [showMissing=true] Show missing pages
+        * @cfg {boolean} [showInterwikis=false] Show pages with a valid interwiki prefix
         * @cfg {boolean} [addQueryInput=true] Add exact user's input query to results
         * @cfg {boolean} [excludeCurrentPage] Exclude the current page from suggestions
+        * @cfg {boolean} [excludeDynamicNamespaces] Exclude pages whose namespace is negative
         * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title
         * @cfg {boolean} [required=false] Whether the input must not be empty
+        * @cfg {boolean} [highlightSearchQuery=true] Highlight the partial query the user used for this title
         * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
         * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
         */
                this.showImages = !!config.showImages;
                this.showDescriptions = !!config.showDescriptions;
                this.showMissing = config.showMissing !== false;
+               this.showInterwikis = !!config.showInterwikis;
                this.addQueryInput = config.addQueryInput !== false;
                this.excludeCurrentPage = !!config.excludeCurrentPage;
+               this.excludeDynamicNamespaces = !!config.excludeDynamicNamespaces;
                this.validateTitle = config.validateTitle !== undefined ? config.validateTitle : true;
+               this.highlightSearchQuery = config.highlightSearchQuery === undefined ? true : !!config.highlightSearchQuery;
                this.cache = config.cache;
                this.api = config.api || new mw.Api();
                // Supports: IE10, FF28, Chrome23
        };
 
        mw.widgets.TitleWidget.prototype.getInterwikiPrefixesPromise = function () {
-               var api = this.getApi(),
-                       cache = this.constructor.static.interwikiPrefixesPromiseCache,
-                       key = api.defaults.ajax.url;
+               var api, cache, key;
+
+               if ( !this.showInterwikis ) {
+                       return $.Deferred().resolve( [] ).promise();
+               }
+
+               api = this.getApi();
+               cache = this.constructor.static.interwikiPrefixesPromiseCache;
+               key = api.defaults.ajax.url;
+
                if ( !Object.prototype.hasOwnProperty.call( cache, key ) ) {
                        cache[ key ] = api.get( {
                                action: 'query',
                }
 
                return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes ) {
-                       var interwiki = query.substring( 0, query.indexOf( ':' ) );
-                       if (
-                               interwiki && interwiki !== '' &&
-                               interwikiPrefixes.indexOf( interwiki ) !== -1
-                       ) {
-                               return $.Deferred().resolve( { query: {
-                                       pages: [ {
-                                               title: query
-                                       } ]
-                               } } ).promise( promiseAbortObject );
-                       } else {
-                               req = api.get( widget.getApiParams( query ) );
-                               promiseAbortObject.abort = req.abort.bind( req ); // TODO ew
-                               return req.then( function ( ret ) {
-                                       if ( widget.showMissing && ret.query === undefined ) {
-                                               ret = api.get( { action: 'query', titles: query } );
-                                               promiseAbortObject.abort = ret.abort.bind( ret );
-                                       }
-                                       return ret;
-                               } );
+                       var interwiki;
+                       // Optimization: check we have any prefixes.
+                       if ( interwikiPrefixes.length ) {
+                               interwiki = query.substring( 0, query.indexOf( ':' ) );
+                               if (
+                                       interwiki && interwiki !== '' &&
+                                       interwikiPrefixes.indexOf( interwiki ) !== -1
+                               ) {
+                                       // Interwiki prefix is valid: return the original query as a valid title
+                                       // NB: This doesn't check if the title actually exists on the other wiki
+                                       return $.Deferred().resolve( { query: {
+                                               pages: [ {
+                                                       title: query
+                                               } ]
+                                       } } ).promise( promiseAbortObject );
+                               }
                        }
+                       // Not a interwiki: do an API lookup of the query
+                       req = api.get( widget.getApiParams( query ) );
+                       promiseAbortObject.abort = req.abort.bind( req ); // TODO ew
+                       return req.then( function ( ret ) {
+                               if ( widget.showMissing && ret.query === undefined ) {
+                                       ret = api.get( { action: 'query', titles: query } );
+                                       promiseAbortObject.abort = ret.abort.bind( ret );
+                               }
+                               return ret;
+                       } );
                } ).promise( promiseAbortObject );
        };
 
                        if ( this.excludeCurrentPage && suggestionPage.title === currentPageName && suggestionPage.title !== titleObj.getPrefixedText() ) {
                                continue;
                        }
+
+                       // When excludeDynamicNamespaces is set, ignore all pages with negative namespace
+                       if ( this.excludeDynamicNamespaces && suggestionPage.ns < 0 ) {
+                               continue;
+                       }
                        pageData[ suggestionPage.title ] = {
                                known: suggestionPage.known !== undefined,
                                missing: suggestionPage.missing !== undefined,
                        missing: data.missing,
                        redirect: data.redirect,
                        disambiguation: data.disambiguation,
-                       query: this.getQueryValue(),
+                       query: this.highlightSearchQuery ? this.getQueryValue() : null,
                        compare: this.compare
                };
        };