* 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
menu: {
filterFromInput: false
},
+ placeholder: mw.msg( 'mw-widgets-categoryselector-add-category-placeholder' ),
// This allows the user to both select non-existent categories, and prevents the selector from
// being wiped from #onMenuItemsChange when we change the available options in the dropdown
allowArbitrary: true
// Initialize
this.api = config.api || new mw.Api();
+ this.searchCache = {};
}
/* Setup */
* @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:
prop: 'categories',
cllimit: this.limit,
titles: 'Category:' + input
- } ).done( function ( res ) {
+ } ).done( function ( res ) {
var categories = [];
$.each( res.query.pages, function ( index, page ) {
- if ( !page.missing ) {
- if ( $.isArray( page.categories ) ) {
- categories.push.apply( categories, page.categories.map( function ( category ) {
- return category.title;
- } ) );
- }
+ if ( !page.missing && $.isArray( page.categories ) ) {
+ categories.push.apply( categories, page.categories.map( function ( category ) {
+ return category.title;
+ } ) );
}
} );
throw new Error( 'Unknown searchType' );
}
+ // Cache the result
+ this.searchCache[ cacheKey ] = deferred.promise();
+
return deferred.promise();
};