2 * MediaWiki Widgets - CategoryMultiselectWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
8 var NS_CATEGORY
= mw
.config
.get( 'wgNamespaceIds' ).category
;
11 * Category selector widget. Displays an OO.ui.CapsuleMultiselectWidget
12 * and autocompletes with available categories.
14 * mw.loader.using( 'mediawiki.widgets.CategoryMultiselectWidget', function () {
15 * var selector = new mw.widgets.CategoryMultiselectWidget( {
17 * mw.widgets.CategoryMultiselectWidget.SearchType.OpenSearch,
18 * mw.widgets.CategoryMultiselectWidget.SearchType.InternalSearch
22 * $( 'body' ).append( selector.$element );
24 * selector.setSearchTypes( [ mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories ] );
27 * @class mw.widgets.CategoryMultiselectWidget
29 * @extends OO.ui.CapsuleMultiselectWidget
30 * @mixins OO.ui.mixin.PendingElement
33 * @param {Object} [config] Configuration options
34 * @cfg {mw.Api} [api] Instance of mw.Api (or subclass thereof) to use for queries
35 * @cfg {number} [limit=10] Maximum number of results to load
36 * @cfg {mw.widgets.CategoryMultiselectWidget.SearchType[]} [searchTypes=[mw.widgets.CategoryMultiselectWidget.SearchType.OpenSearch]]
37 * Default search API to use when searching.
39 mw
.widgets
.CategoryMultiselectWidget
= function MWCategoryMultiselectWidget( config
) {
40 // Config initialization
43 searchTypes
: [ mw
.widgets
.CategoryMultiselectWidget
.SearchType
.OpenSearch
]
45 this.limit
= config
.limit
;
46 this.searchTypes
= config
.searchTypes
;
47 this.validateSearchTypes();
50 mw
.widgets
.CategoryMultiselectWidget
.parent
.call( this, $.extend( true, {}, config
, {
52 filterFromInput
: false
54 placeholder
: mw
.msg( 'mw-widgets-categoryselector-add-category-placeholder' ),
55 // This allows the user to both select non-existent categories, and prevents the selector from
56 // being wiped from #onMenuItemsChange when we change the available options in the dropdown
61 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$handle
} ) );
63 // Event handler to call the autocomplete methods
64 this.$input
.on( 'change input cut paste', OO
.ui
.debounce( this.updateMenuItems
.bind( this ), 100 ) );
67 this.api
= config
.api
|| new mw
.Api();
68 this.searchCache
= {};
73 OO
.inheritClass( mw
.widgets
.CategoryMultiselectWidget
, OO
.ui
.CapsuleMultiselectWidget
);
74 OO
.mixinClass( mw
.widgets
.CategoryMultiselectWidget
, OO
.ui
.mixin
.PendingElement
);
79 * Gets new items based on the input by calling
80 * {@link #getNewMenuItems getNewItems} and updates the menu
81 * after removing duplicates based on the data value.
86 mw
.widgets
.CategoryMultiselectWidget
.prototype.updateMenuItems = function () {
87 this.getMenu().clearItems();
88 this.getNewMenuItems( this.$input
.val() ).then( function ( items
) {
89 var existingItems
, filteredItems
,
90 menu
= this.getMenu();
92 // Never show the menu if the input lost focus in the meantime
93 if ( !this.$input
.is( ':focus' ) ) {
97 // Array of strings of the data of OO.ui.MenuOptionsWidgets
98 existingItems
= menu
.getItems().map( function ( item
) {
102 // Remove if items' data already exists
103 filteredItems
= items
.filter( function ( item
) {
104 return existingItems
.indexOf( item
) === -1;
107 // Map to an array of OO.ui.MenuOptionWidgets
108 filteredItems
= filteredItems
.map( function ( item
) {
109 return new OO
.ui
.MenuOptionWidget( {
115 menu
.addItems( filteredItems
).toggle( true );
122 mw
.widgets
.CategoryMultiselectWidget
.prototype.clearInput = function () {
123 mw
.widgets
.CategoryMultiselectWidget
.parent
.prototype.clearInput
.call( this );
124 // Abort all pending requests, we won't need their results
129 * Searches for categories based on the input.
133 * @param {string} input The input used to prefix search categories
134 * @return {jQuery.Promise} Resolves with an array of categories
136 mw
.widgets
.CategoryMultiselectWidget
.prototype.getNewMenuItems = function ( input
) {
139 deferred
= $.Deferred();
141 if ( $.trim( input
) === '' ) {
142 deferred
.resolve( [] );
143 return deferred
.promise();
146 // Abort all pending requests, we won't need their results
148 for ( i
= 0; i
< this.searchTypes
.length
; i
++ ) {
149 promises
.push( this.searchCategories( input
, this.searchTypes
[ i
] ) );
154 $.when
.apply( $, promises
).done( function () {
157 dataSets
= Array
.prototype.slice
.apply( arguments
);
159 // Collect values from all results
160 allData
= allData
.concat
.apply( allData
, dataSets
);
162 categoryNames
= allData
164 .filter( function ( value
, index
, self
) {
165 return self
.indexOf( value
) === index
;
168 .map( function ( name
) {
169 return mw
.Title
.newFromText( name
);
171 // Keep only titles from 'Category' namespace
172 .filter( function ( title
) {
173 return title
&& title
.getNamespaceId() === NS_CATEGORY
;
175 // Convert back to strings, strip 'Category:' prefix
176 .map( function ( title
) {
177 return title
.getMainText();
180 deferred
.resolve( categoryNames
);
182 } ).always( this.popPending
.bind( this ) );
184 return deferred
.promise();
190 mw
.widgets
.CategoryMultiselectWidget
.prototype.createItemWidget = function ( data
) {
191 var title
= mw
.Title
.makeTitle( NS_CATEGORY
, data
);
195 return new mw
.widgets
.CategoryCapsuleItemWidget( {
196 apiUrl
: this.api
.apiUrl
|| undefined,
204 mw
.widgets
.CategoryMultiselectWidget
.prototype.getItemFromData = function ( data
) {
205 // This is a bit of a hack... We have to canonicalize the data in the same way that
206 // #createItemWidget and CategoryCapsuleItemWidget will do, otherwise we won't find duplicates.
207 var title
= mw
.Title
.makeTitle( NS_CATEGORY
, data
);
211 return OO
.ui
.mixin
.GroupElement
.prototype.getItemFromData
.call( this, title
.getMainText() );
215 * Validates the values in `this.searchType`.
220 mw
.widgets
.CategoryMultiselectWidget
.prototype.validateSearchTypes = function () {
221 var validSearchTypes
= false,
222 searchTypeEnumCount
= Object
.keys( mw
.widgets
.CategoryMultiselectWidget
.SearchType
).length
;
224 // Check if all values are in the SearchType enum
225 validSearchTypes
= this.searchTypes
.every( function ( searchType
) {
226 return searchType
> -1 && searchType
< searchTypeEnumCount
;
229 if ( validSearchTypes
=== false ) {
230 throw new Error( 'Unknown searchType in searchTypes' );
233 // If the searchTypes has mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories
234 // it can be the only search type.
235 if ( this.searchTypes
.indexOf( mw
.widgets
.CategoryMultiselectWidget
.SearchType
.SubCategories
) > -1 &&
236 this.searchTypes
.length
> 1
238 throw new Error( 'Can\'t have additional search types with mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories' );
241 // If the searchTypes has mw.widgets.CategoryMultiselectWidget.SearchType.ParentCategories
242 // it can be the only search type.
243 if ( this.searchTypes
.indexOf( mw
.widgets
.CategoryMultiselectWidget
.SearchType
.ParentCategories
) > -1 &&
244 this.searchTypes
.length
> 1
246 throw new Error( 'Can\'t have additional search types with mw.widgets.CategoryMultiselectWidget.SearchType.ParentCategories' );
253 * Sets and validates the value of `this.searchType`.
255 * @param {mw.widgets.CategoryMultiselectWidget.SearchType[]} searchTypes
257 mw
.widgets
.CategoryMultiselectWidget
.prototype.setSearchTypes = function ( searchTypes
) {
258 this.searchTypes
= searchTypes
;
259 this.validateSearchTypes();
263 * Searches categories based on input and searchType.
267 * @param {string} input The input used to prefix search categories
268 * @param {mw.widgets.CategoryMultiselectWidget.SearchType} searchType
269 * @return {jQuery.Promise} Resolves with an array of categories
271 mw
.widgets
.CategoryMultiselectWidget
.prototype.searchCategories = function ( input
, searchType
) {
272 var deferred
= $.Deferred(),
273 cacheKey
= input
+ searchType
.toString();
276 if ( this.searchCache
[ cacheKey
] !== undefined ) {
277 return this.searchCache
[ cacheKey
];
280 switch ( searchType
) {
281 case mw
.widgets
.CategoryMultiselectWidget
.SearchType
.OpenSearch
:
284 action
: 'opensearch',
285 namespace: NS_CATEGORY
,
288 } ).done( function ( res
) {
289 var categories
= res
[ 1 ];
290 deferred
.resolve( categories
);
291 } ).fail( deferred
.reject
.bind( deferred
) );
294 case mw
.widgets
.CategoryMultiselectWidget
.SearchType
.InternalSearch
:
299 apnamespace
: NS_CATEGORY
,
303 } ).done( function ( res
) {
304 var categories
= res
.query
.allpages
.map( function ( page
) {
307 deferred
.resolve( categories
);
308 } ).fail( deferred
.reject
.bind( deferred
) );
311 case mw
.widgets
.CategoryMultiselectWidget
.SearchType
.Exists
:
312 if ( input
.indexOf( '|' ) > -1 ) {
313 deferred
.resolve( [] );
321 titles
: 'Category:' + input
322 } ).done( function ( res
) {
325 $.each( res
.query
.pages
, function ( index
, page
) {
326 if ( !page
.missing
) {
327 categories
.push( page
.title
);
331 deferred
.resolve( categories
);
332 } ).fail( deferred
.reject
.bind( deferred
) );
335 case mw
.widgets
.CategoryMultiselectWidget
.SearchType
.SubCategories
:
336 if ( input
.indexOf( '|' ) > -1 ) {
337 deferred
.resolve( [] );
344 list
: 'categorymembers',
347 cmtitle
: 'Category:' + input
348 } ).done( function ( res
) {
349 var categories
= res
.query
.categorymembers
.map( function ( category
) {
350 return category
.title
;
352 deferred
.resolve( categories
);
353 } ).fail( deferred
.reject
.bind( deferred
) );
356 case mw
.widgets
.CategoryMultiselectWidget
.SearchType
.ParentCategories
:
357 if ( input
.indexOf( '|' ) > -1 ) {
358 deferred
.resolve( [] );
367 titles
: 'Category:' + input
368 } ).done( function ( res
) {
371 $.each( res
.query
.pages
, function ( index
, page
) {
372 if ( !page
.missing
&& Array
.isArray( page
.categories
) ) {
373 categories
.push
.apply( categories
, page
.categories
.map( function ( category
) {
374 return category
.title
;
379 deferred
.resolve( categories
);
380 } ).fail( deferred
.reject
.bind( deferred
) );
384 throw new Error( 'Unknown searchType' );
388 this.searchCache
[ cacheKey
] = deferred
.promise();
390 return deferred
.promise();
394 * @enum mw.widgets.CategoryMultiselectWidget.SearchType
395 * Types of search available.
397 mw
.widgets
.CategoryMultiselectWidget
.SearchType
= {
398 /** Search using action=opensearch */
401 /** Search using action=query */
404 /** Search for existing categories with the exact title */
407 /** Search only subcategories */
410 /** Search only parent categories */
414 // For backwards compatibility. See T161285.
415 mw
.widgets
.CategorySelector
= mw
.widgets
.CategoryMultiselectWidget
;
416 }( jQuery
, mediaWiki
) );