mw.widgets.CategorySelector: Don't lose namespace-like prefixes from category names
authorBartosz Dziewoński <matma.rex@gmail.com>
Tue, 22 Dec 2015 18:01:12 +0000 (19:01 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 20 Jan 2016 14:42:23 +0000 (14:42 +0000)
Also, handle queries which return non-category results.

Bug: T122221
Change-Id: I205d2fe52a10bd3adcf0235108005dd96811ae65

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

index 1e65da5..ec38e2b 100644 (file)
                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 );
        };