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
9 NS_CATEGORY
= mw
.config
.get( 'wgNamespaceIds' ).category
;
12 * Category selector widget. Displays an OO.ui.CapsuleMultiSelectWidget
13 * and autocompletes with available categories.
15 * var selector = new mw.widgets.CategorySelector( {
17 * mw.widgets.CategorySelector.SearchType.OpenSearch,
18 * mw.widgets.CategorySelector.SearchType.InternalSearch
22 * $( '#content' ).append( selector.$element );
24 * selector.setSearchType( [ mw.widgets.CategorySelector.SearchType.SubCategories ] );
27 * @class mw.widgets.CategorySelector
29 * @extends OO.ui.CapsuleMultiSelectWidget
32 * @param {Object} [config] Configuration options
33 * @cfg {number} [limit=10] Maximum number of results to load
34 * @cfg {mw.widgets.CategorySelector.SearchType[]} [searchTypes=[mw.widgets.CategorySelector.SearchType.OpenSearch]]
35 * Default search API to use when searching.
37 function CategorySelector( config
) {
38 // Config initialization
41 searchTypes
: [ CategorySelector
.SearchType
.OpenSearch
]
43 this.limit
= config
.limit
;
44 this.searchTypes
= config
.searchTypes
;
45 this.validateSearchTypes();
48 mw
.widgets
.CategorySelector
.parent
.call( this, $.extend( true, {}, config
, {
50 filterFromInput
: false
52 // This allows the user to both select non-existent categories, and prevents the selector from
53 // being wiped from #onMenuItemsChange when we change the available options in the dropdown
57 // Event handler to call the autocomplete methods
58 this.$input
.on( 'change input cut paste', OO
.ui
.debounce( this.updateMenuItems
.bind( this ), 100 ) );
61 this.api
= new mw
.Api();
67 OO
.inheritClass( CategorySelector
, OO
.ui
.CapsuleMultiSelectWidget
);
68 CSP
= CategorySelector
.prototype;
73 * Gets new items based on the input by calling
74 * {@link #getNewMenuItems getNewItems} and updates the menu
75 * after removing duplicates based on the data value.
80 CSP
.updateMenuItems = function () {
81 this.getMenu().clearItems();
82 this.getNewMenuItems( this.$input
.val() ).then( function ( items
) {
83 var existingItems
, filteredItems
,
84 menu
= this.getMenu();
86 // Array of strings of the data of OO.ui.MenuOptionsWidgets
87 existingItems
= menu
.getItems().map( function ( item
) {
91 // Remove if items' data already exists
92 filteredItems
= items
.filter( function ( item
) {
93 return existingItems
.indexOf( item
) === -1;
96 // Map to an array of OO.ui.MenuOptionWidgets
97 filteredItems
= filteredItems
.map( function ( item
) {
98 return new OO
.ui
.MenuOptionWidget( {
104 menu
.addItems( filteredItems
).toggle( true );
109 * Searches for categories based on the input.
113 * @param {string} input The input used to prefix search categories
114 * @return {jQuery.Promise} Resolves with an array of categories
116 CSP
.getNewMenuItems = function ( input
) {
119 deferred
= new $.Deferred();
121 for ( i
= 0; i
< this.searchTypes
.length
; i
++ ) {
122 promises
.push( this.searchCategories( input
, this.searchTypes
[ i
] ) );
125 $.when
.apply( $, promises
).done( function () {
126 var categories
, categoryNames
,
128 dataSets
= Array
.prototype.slice
.apply( arguments
);
130 // Collect values from all results
131 allData
= allData
.concat
.apply( allData
, dataSets
);
134 categories
= allData
.filter( function ( value
, index
, self
) {
135 return self
.indexOf( value
) === index
;
139 categoryNames
= categories
.map( function ( name
) {
140 return mw
.Title
.newFromText( name
, NS_CATEGORY
).getMainText();
143 deferred
.resolve( categoryNames
);
147 return deferred
.promise();
153 CSP
.createItemWidget = function ( data
) {
154 return new mw
.widgets
.CategoryCapsuleItemWidget( {
155 title
: mw
.Title
.newFromText( data
, NS_CATEGORY
)
160 * Validates the values in `this.searchType`.
165 CSP
.validateSearchTypes = function () {
166 var validSearchTypes
= false,
167 searchTypeEnumCount
= Object
.keys( CategorySelector
.SearchType
).length
;
169 // Check if all values are in the SearchType enum
170 validSearchTypes
= this.searchTypes
.every( function ( searchType
) {
171 return searchType
> -1 && searchType
< searchTypeEnumCount
;
174 if ( validSearchTypes
=== false ) {
175 throw new Error( 'Unknown searchType in searchTypes' );
178 // If the searchTypes has CategorySelector.SearchType.SubCategories
179 // it can be the only search type.
180 if ( this.searchTypes
.indexOf( CategorySelector
.SearchType
.SubCategories
) > -1 &&
181 this.searchTypes
.length
> 1
183 throw new Error( 'Can\'t have additional search types with CategorySelector.SearchType.SubCategories' );
186 // If the searchTypes has CategorySelector.SearchType.ParentCategories
187 // it can be the only search type.
188 if ( this.searchTypes
.indexOf( CategorySelector
.SearchType
.ParentCategories
) > -1 &&
189 this.searchTypes
.length
> 1
191 throw new Error( 'Can\'t have additional search types with CategorySelector.SearchType.ParentCategories' );
198 * Sets and validates the value of `this.searchType`.
200 * @param {mw.widgets.CategorySelector.SearchType[]} searchTypes
202 CSP
.setSearchTypes = function ( searchTypes
) {
203 this.searchTypes
= searchTypes
;
204 this.validateSearchTypes();
208 * Searches categories based on input and searchType.
212 * @param {string} input The input used to prefix search categories
213 * @param {mw.widgets.CategorySelector.SearchType} searchType
214 * @return {jQuery.Promise} Resolves with an array of categories
216 CSP
.searchCategories = function ( input
, searchType
) {
217 var deferred
= new $.Deferred();
219 switch ( searchType
) {
220 case CategorySelector
.SearchType
.OpenSearch
:
222 action
: 'opensearch',
223 namespace: NS_CATEGORY
,
226 } ).done( function ( res
) {
227 var categories
= res
[ 1 ];
228 deferred
.resolve( categories
);
232 case CategorySelector
.SearchType
.InternalSearch
:
236 apnamespace
: NS_CATEGORY
,
240 } ).done( function ( res
) {
241 var categories
= res
.query
.allpages
.map( function ( page
) {
244 deferred
.resolve( categories
);
248 case CategorySelector
.SearchType
.Exists
:
249 if ( input
.indexOf( '|' ) > -1 ) {
250 deferred
.resolve( [] );
257 titles
: 'Category:' + input
258 } ).done( function ( res
) {
262 for ( page
in res
.query
.pages
) {
263 if ( parseInt( page
, 10 ) > -1 ) {
264 categories
.push( res
.query
.pages
[ page
].title
);
268 deferred
.resolve( categories
);
272 case CategorySelector
.SearchType
.SubCategories
:
273 if ( input
.indexOf( '|' ) > -1 ) {
274 deferred
.resolve( [] );
280 list
: 'categorymembers',
283 cmtitle
: 'Category:' + input
284 } ).done( function ( res
) {
285 var categories
= res
.query
.categorymembers
.map( function ( category
) {
286 return category
.title
;
288 deferred
.resolve( categories
);
292 case CategorySelector
.SearchType
.ParentCategories
:
293 if ( input
.indexOf( '|' ) > -1 ) {
294 deferred
.resolve( [] );
302 titles
: 'Category:' + input
303 } ).done( function ( res
) {
307 for ( page
in res
.query
.pages
) {
308 if ( parseInt( page
, 10 ) > -1 ) {
309 if ( $.isArray( res
.query
.pages
[ page
].categories
) ) {
310 categories
.push
.apply( categories
, res
.query
.pages
[ page
].categories
.map( function ( category
) {
311 return category
.title
;
317 deferred
.resolve( categories
);
322 throw new Error( 'Unknown searchType' );
325 return deferred
.promise();
329 * @enum mw.widgets.CategorySelector.SearchType
330 * Types of search available.
332 CategorySelector
.SearchType
= {
333 /** Search using action=opensearch */
336 /** Search using action=query */
339 /** Search for existing categories with the exact title */
342 /** Search only subcategories */
345 /** Search only parent categories */
349 mw
.widgets
.CategorySelector
= CategorySelector
;
350 }( jQuery
, mediaWiki
) );