2 * MediaWiki Widgets - CategorySelector class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * Category selector widget. Displays an OO.ui.CapsuleMultiSelectWidget
11 * and autocompletes with available categories.
15 * @extends OO.ui.CapsuleMultiSelectWidget
18 * @param {Object} [config] Configuration options
19 * @cfg {number} [limit=10] Maximum number of results to load
21 mw
.widgets
.CategorySelector = function ( config
) {
22 // Config initialization
23 config
= $.extend( { limit
: 10 }, config
);
24 this.limit
= config
.limit
;
27 mw
.widgets
.CategorySelector
.parent
.call( this, config
);
29 // Event handler to call the autocomplete methods
30 this.$input
.on( 'change input cut paste', OO
.ui
.debounce( this.updateMenuItems
.bind( this ), 100 ) );
35 OO
.inheritClass( mw
.widgets
.CategorySelector
, OO
.ui
.CapsuleMultiSelectWidget
);
40 * Gets new items based on the input by calling
41 * {@link #getNewMenuItems getNewItems} and updates the menu
42 * after removing duplicates based on the data value.
47 mw
.widgets
.CategorySelector
.prototype.updateMenuItems = function () {
48 this.getNewMenuItems( this.$input
.val() ).then( function ( items
) {
49 var existingItems
, filteredItems
,
50 menu
= this.getMenu();
52 // Array of strings of the data of OO.ui.MenuOptionsWidgets
53 existingItems
= menu
.getItems().map( function ( item
) {
57 // Remove if items' data already exists
58 filteredItems
= items
.filter( function ( item
) {
59 return existingItems
.indexOf( item
) === -1;
62 // Map to an array of OO.ui.MenuOptionWidgets
63 filteredItems
= filteredItems
.map( function ( item
) {
64 return new OO
.ui
.MenuOptionWidget( {
70 menu
.addItems( filteredItems
).updateItemVisibility();
75 * Searches for categories based on the input.
79 * @param {string} input The input used to prefix search categories
80 * @return {jQuery.Promise} Resolves with an array of categories
82 mw
.widgets
.CategorySelector
.prototype.getNewMenuItems = function ( input
) {
83 var deferred
= new $.Deferred(),
84 catNsId
= mw
.config
.get( 'wgNamespaceIds' ).category
,
92 } ).done( function ( res
) {
93 var categoryNames
= res
[ 1 ].map( function ( name
) {
94 return mw
.Title
.newFromText( name
, catNsId
).getMainText();
97 deferred
.resolve( categoryNames
);
100 return deferred
.promise();
102 }( jQuery
, mediaWiki
) );