Add option to hide missing pages while searching
authorpetarpetkovic <ppetkovic@wikimedia.org>
Mon, 9 Oct 2017 18:58:42 +0000 (20:58 +0200)
committerpetarpetkovic <ppetkovic@wikimedia.org>
Mon, 16 Oct 2017 10:40:25 +0000 (12:40 +0200)
Bug: T177469
Change-Id: I9e7dd451c61a6a88679628acbbf28d45c0a1e0e8

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

index db56bd3..83a2612 100644 (file)
@@ -22,6 +22,7 @@
         * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
         * @cfg {boolean} [showImages] Show page images
         * @cfg {boolean} [showDescriptions] Show page descriptions
+        * @cfg {boolean} [showMissing=true] Show missing pages
         * @cfg {boolean} [excludeCurrentPage] Exclude the current page from suggestions
         * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true,
         *  the widget will marks itself red for invalid inputs, including an empty query).
@@ -44,6 +45,7 @@
                this.showRedirectTargets = config.showRedirectTargets !== false;
                this.showImages = !!config.showImages;
                this.showDescriptions = !!config.showDescriptions;
+               this.showMissing = config.showMissing !== false;
                this.excludeCurrentPage = !!config.excludeCurrentPage;
                this.validateTitle = config.validateTitle !== undefined ? config.validateTitle : true;
                this.cache = config.cache;
                                // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
                        } };
 
-               if ( mw.Title.newFromText( 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 ( ret.query === undefined ) {
-                                                       ret = api.get( { action: 'query', titles: query } );
-                                                       promiseAbortObject.abort = ret.abort.bind( ret );
-                                               }
-                                               return ret;
-                                       } );
-                               }
-                       } ).promise( promiseAbortObject );
-               } else {
+               if ( !mw.Title.newFromText( query ) ) {
                        // Don't send invalid titles to the API.
                        // Just pretend it returned nothing so we can show the 'invalid title' section
                        return $.Deferred().resolve( {} ).promise( promiseAbortObject );
                }
+
+               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;
+                               } );
+                       }
+               } ).promise( promiseAbortObject );
        };
 
        /**
 
                for ( index in data.pages ) {
                        suggestionPage = data.pages[ index ];
+
                        // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
                        if ( this.excludeCurrentPage && suggestionPage.title === currentPageName && suggestionPage.title !== titleObj.getPrefixedText() ) {
                                continue;