Merge "mw.widgets.CategorySelector: Don't lose namespace-like prefixes from category...
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 20 Jan 2016 19:38:57 +0000 (19:38 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 20 Jan 2016 19:38:57 +0000 (19:38 +0000)
1  2 
resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js

        CSP.getNewMenuItems = function ( input ) {
                var i,
                        promises = [],
 -                      deferred = new $.Deferred();
 +                      deferred = $.Deferred();
  
                if ( $.trim( input ) === '' ) {
                        deferred.resolve( [] );
                this.pushPending();
  
                $.when.apply( $, promises ).done( function () {
-                       var categories, categoryNames,
+                       var categoryNames,
                                allData = [],
                                dataSets = Array.prototype.slice.apply( arguments );
  
                        // Collect values from all results
                        allData = allData.concat.apply( allData, dataSets );
  
-                       // Remove duplicates
-                       categories = allData.filter( function ( value, index, self ) {
-                               return self.indexOf( value ) === index;
-                       } );
-                       // Get titles
-                       categoryNames = categories.map( function ( name ) {
-                               return mw.Title.newFromText( name, NS_CATEGORY ).getMainText();
-                       } );
+                       categoryNames = allData
+                               // Remove duplicates
+                               .filter( function ( value, index, self ) {
+                                       return self.indexOf( value ) === index;
+                               } )
+                               // Get Title objects
+                               .map( function ( name ) {
+                                       return mw.Title.newFromText( name );
+                               } )
+                               // Keep only titles from 'Category' namespace
+                               .filter( function ( title ) {
+                                       return title && title.getNamespaceId() === NS_CATEGORY;
+                               } )
+                               // Convert back to strings, strip 'Category:' prefix
+                               .map( function ( title ) {
+                                       return title.getMainText();
+                               } );
  
                        deferred.resolve( categoryNames );
  
        CSP.createItemWidget = function ( data ) {
                return new mw.widgets.CategoryCapsuleItemWidget( {
                        apiUrl: this.api.apiUrl || undefined,
-                       title: mw.Title.newFromText( data, NS_CATEGORY )
+                       title: mw.Title.makeTitle( NS_CATEGORY, data )
                } );
        };
  
        CSP.getItemFromData = function ( data ) {
                // This is a bit of a hack... We have to canonicalize the data in the same way that
                // #createItemWidget and CategoryCapsuleItemWidget will do, otherwise we won't find duplicates.
-               data = mw.Title.newFromText( data, NS_CATEGORY ).getMainText();
+               data = mw.Title.makeTitle( NS_CATEGORY, data ).getMainText();
                return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, data );
        };
  
         * @return {jQuery.Promise} Resolves with an array of categories
         */
        CSP.searchCategories = function ( input, searchType ) {
 -              var deferred = new $.Deferred();
 +              var deferred = $.Deferred();
  
                switch ( searchType ) {
                        case CategorySelector.SearchType.OpenSearch: