NS_CATEGORY = mw.config.get( 'wgNamespaceIds' ).category;
/**
- * Category selector widget. Displays an OO.ui.CapsuleMultiSelectWidget
+ * Category selector widget. Displays an OO.ui.CapsuleMultiselectWidget
* and autocompletes with available categories.
*
- * var selector = new mw.widgets.CategorySelector( {
- * searchTypes: [
- * mw.widgets.CategorySelector.SearchType.OpenSearch,
- * mw.widgets.CategorySelector.SearchType.InternalSearch
- * ]
- * } );
+ * mw.loader.using( 'mediawiki.widgets.CategorySelector', function () {
+ * var selector = new mw.widgets.CategorySelector( {
+ * searchTypes: [
+ * mw.widgets.CategorySelector.SearchType.OpenSearch,
+ * mw.widgets.CategorySelector.SearchType.InternalSearch
+ * ]
+ * } );
*
- * $( '#content' ).append( selector.$element );
+ * $( 'body' ).append( selector.$element );
*
- * selector.setSearchTypes( [ mw.widgets.CategorySelector.SearchType.SubCategories ] );
+ * selector.setSearchTypes( [ mw.widgets.CategorySelector.SearchType.SubCategories ] );
+ * } );
*
* @class mw.widgets.CategorySelector
* @uses mw.Api
- * @extends OO.ui.CapsuleMultiSelectWidget
+ * @extends OO.ui.CapsuleMultiselectWidget
* @mixins OO.ui.mixin.PendingElement
*
* @constructor
// Initialize
this.api = config.api || new mw.Api();
+ this.searchCache = {};
}
/* Setup */
- OO.inheritClass( CategorySelector, OO.ui.CapsuleMultiSelectWidget );
+ OO.inheritClass( CategorySelector, OO.ui.CapsuleMultiselectWidget );
OO.mixinClass( CategorySelector, OO.ui.mixin.PendingElement );
CSP = CategorySelector.prototype;
* @inheritdoc
*/
CSP.createItemWidget = function ( data ) {
+ var title = mw.Title.makeTitle( NS_CATEGORY, data );
+ if ( !title ) {
+ return null;
+ }
return new mw.widgets.CategoryCapsuleItemWidget( {
apiUrl: this.api.apiUrl || undefined,
- title: mw.Title.makeTitle( NS_CATEGORY, data )
+ title: title
} );
};
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.makeTitle( NS_CATEGORY, data ).getMainText();
- return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, data );
+ var title = mw.Title.makeTitle( NS_CATEGORY, data );
+ if ( !title ) {
+ return null;
+ }
+ return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, title.getMainText() );
};
/**
* @return {jQuery.Promise} Resolves with an array of categories
*/
CSP.searchCategories = function ( input, searchType ) {
- var deferred = $.Deferred();
+ var deferred = $.Deferred(),
+ cacheKey = input + searchType.toString();
+
+ // Check cache
+ if ( this.searchCache[ cacheKey ] !== undefined ) {
+ return this.searchCache[ cacheKey ];
+ }
switch ( searchType ) {
case CategorySelector.SearchType.OpenSearch:
this.api.get( {
+ formatversion: 2,
action: 'opensearch',
namespace: NS_CATEGORY,
limit: this.limit,
case CategorySelector.SearchType.InternalSearch:
this.api.get( {
+ formatversion: 2,
action: 'query',
list: 'allpages',
apnamespace: NS_CATEGORY,
}
this.api.get( {
+ formatversion: 2,
action: 'query',
prop: 'info',
titles: 'Category:' + input
} ).done( function ( res ) {
- var page,
- categories = [];
+ var categories = [];
- for ( page in res.query.pages ) {
- if ( parseInt( page, 10 ) > -1 ) {
- categories.push( res.query.pages[ page ].title );
+ $.each( res.query.pages, function ( index, page ) {
+ if ( !page.missing ) {
+ categories.push( page.title );
}
- }
+ } );
deferred.resolve( categories );
} ).fail( deferred.reject.bind( deferred ) );
}
this.api.get( {
+ formatversion: 2,
action: 'query',
list: 'categorymembers',
cmtype: 'subcat',
}
this.api.get( {
+ formatversion: 2,
action: 'query',
prop: 'categories',
cllimit: this.limit,
titles: 'Category:' + input
} ).done( function ( res ) {
- var page,
- categories = [];
-
- for ( page in res.query.pages ) {
- if ( parseInt( page, 10 ) > -1 ) {
- if ( $.isArray( res.query.pages[ page ].categories ) ) {
- categories.push.apply( categories, res.query.pages[ page ].categories.map( function ( category ) {
- return category.title;
- } ) );
- }
+ var categories = [];
+
+ $.each( res.query.pages, function ( index, page ) {
+ if ( !page.missing && $.isArray( page.categories ) ) {
+ categories.push.apply( categories, page.categories.map( function ( category ) {
+ return category.title;
+ } ) );
}
- }
+ } );
deferred.resolve( categories );
} ).fail( deferred.reject.bind( deferred ) );
throw new Error( 'Unknown searchType' );
}
+ // Cache the result
+ this.searchCache[ cacheKey ] = deferred.promise();
+
return deferred.promise();
};