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 const hasOwn = Object.prototype.hasOwnProperty,
9 NS_CATEGORY = mw.config.get( 'wgNamespaceIds' ).category;
12 * @classdesc Displays an {@link OO.ui.MenuTagMultiselectWidget}
13 * and autocompletes with available categories.
16 * mw.loader.using( 'mediawiki.widgets.CategoryMultiselectWidget', function () {
17 * let selector = new mw.widgets.CategoryMultiselectWidget( {
19 * mw.widgets.CategoryMultiselectWidget.SearchType.OpenSearch,
20 * mw.widgets.CategoryMultiselectWidget.SearchType.InternalSearch
24 * $( document.body ).append( selector.$element );
26 * selector.setSearchTypes( [ mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories ] );
29 * @class mw.widgets.CategoryMultiselectWidget
31 * @extends OO.ui.MenuTagMultiselectWidget
32 * @mixes OO.ui.mixin.PendingElement
35 * @description Create an instance of `mw.widgets.CategoryMultiselectWidget`.
36 * @param {Object} [config] Configuration options
37 * @param {mw.Api} [config.api] Instance of mw.Api (or subclass thereof) to use for queries
38 * @param {number} [config.limit=10] Maximum number of results to load
39 * @param {mw.widgets.CategoryMultiselectWidget.SearchType[]} [config.searchTypes=[mw.widgets.CategoryMultiselectWidget.SearchType.OpenSearch]]
40 * Default search API to use when searching.
42 mw.widgets.CategoryMultiselectWidget = function MWCategoryMultiselectWidget( config ) {
43 // Config initialization
44 config = Object.assign( {
46 searchTypes: [ mw.widgets.CategoryMultiselectWidget.SearchType.OpenSearch ],
47 placeholder: mw.msg( 'mw-widgets-categoryselector-add-category-placeholder' )
49 this.limit = config.limit;
50 this.searchTypes = config.searchTypes;
51 this.validateSearchTypes();
54 mw.widgets.CategoryMultiselectWidget.super.call( this, $.extend( true, {}, config, {
56 filterFromInput: false
58 // This allows the user to both select non-existent categories, and prevents the selector from
59 // being wiped from #onMenuItemsChange when we change the available options in the dropdown
64 OO.ui.mixin.PendingElement.call( this, Object.assign( {}, config, { $pending: this.$handle } ) );
66 // Event handler to call the autocomplete methods
67 this.input.$input.on( 'change input cut paste', OO.ui.debounce( this.updateMenuItems.bind( this ), 100 ) );
70 this.api = config.api || new mw.Api();
71 this.searchCache = {};
76 OO.inheritClass( mw.widgets.CategoryMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
77 OO.mixinClass( mw.widgets.CategoryMultiselectWidget, OO.ui.mixin.PendingElement );
82 * Gets new items based on the input by calling
83 * {@link #getNewMenuItems getNewItems} and updates the menu
84 * after removing duplicates based on the data value.
89 mw.widgets.CategoryMultiselectWidget.prototype.updateMenuItems = function () {
90 this.getMenu().clearItems();
91 this.getNewMenuItems( this.input.$input.val() ).then( ( items ) => {
92 const menu = this.getMenu();
94 // Never show the menu if the input lost focus in the meantime
95 if ( !this.input.$input.is( ':focus' ) ) {
99 // Array of strings of the data of OO.ui.MenuOptionsWidgets
100 const existingItems = menu.getItems().map( ( item ) => item.data );
102 // Remove if items' data already exists
103 let filteredItems = items.filter( ( item ) => existingItems.indexOf( item ) === -1 );
105 // Map to an array of OO.ui.MenuOptionWidgets
106 filteredItems = filteredItems.map( ( item ) => new OO.ui.MenuOptionWidget( {
111 menu.addItems( filteredItems ).toggle( true );
118 mw.widgets.CategoryMultiselectWidget.prototype.clearInput = function () {
119 mw.widgets.CategoryMultiselectWidget.super.prototype.clearInput.call( this );
120 // Abort all pending requests, we won't need their results
125 * Searches for categories based on the input.
129 * @param {string} input The input used to prefix search categories
130 * @return {jQuery.Promise} Resolves with an array of categories
132 mw.widgets.CategoryMultiselectWidget.prototype.getNewMenuItems = function ( input ) {
133 const deferred = $.Deferred();
135 if ( input.trim() === '' ) {
136 deferred.resolve( [] );
137 return deferred.promise();
140 // Abort all pending requests, we won't need their results
143 for ( let i = 0; i < this.searchTypes.length; i++ ) {
144 promises.push( this.searchCategories( input, this.searchTypes[ i ] ) );
149 $.when( ...promises ).done( ( ...dataSets ) => {
151 const allData = [].concat( ...dataSets );
153 const categoryNames = allData
155 .filter( ( value, index, arr ) => arr.indexOf( value ) === index )
157 .map( ( name ) => mw.Title.newFromText( name ) )
158 // Keep only titles from 'Category' namespace
159 .filter( ( title ) => title && title.getNamespaceId() === NS_CATEGORY )
160 // Convert back to strings, strip 'Category:' prefix
161 .map( ( title ) => title.getMainText() );
163 deferred.resolve( categoryNames );
165 } ).always( this.popPending.bind( this ) );
167 return deferred.promise();
173 mw.widgets.CategoryMultiselectWidget.prototype.isAllowedData = function ( data ) {
174 const title = mw.Title.makeTitle( NS_CATEGORY, data );
178 return mw.widgets.CategoryMultiselectWidget.super.prototype.isAllowedData.call( this, data );
184 mw.widgets.CategoryMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
185 const title = mw.Title.makeTitle( NS_CATEGORY, data );
187 return new mw.widgets.CategoryTagItemWidget( {
188 apiUrl: this.api.apiUrl || undefined,
196 mw.widgets.CategoryMultiselectWidget.prototype.findItemFromData = function ( data ) {
197 // This is a bit of a hack... We have to canonicalize the data in the same way that
198 // #createItemWidget and CategoryTagItemWidget will do, otherwise we won't find duplicates.
199 const title = mw.Title.makeTitle( NS_CATEGORY, data );
203 return OO.ui.mixin.GroupElement.prototype.findItemFromData.call( this, title.getMainText() );
207 * Validates the values in `this.searchType`.
212 mw.widgets.CategoryMultiselectWidget.prototype.validateSearchTypes = function () {
213 let validSearchTypes = false;
215 const searchTypeEnumCount = Object.keys( mw.widgets.CategoryMultiselectWidget.SearchType ).length;
217 // Check if all values are in the SearchType enum
218 validSearchTypes = this.searchTypes.every( ( searchType ) => searchType > -1 && searchType < searchTypeEnumCount );
220 if ( validSearchTypes === false ) {
221 throw new Error( 'Unknown searchType in searchTypes' );
224 // If the searchTypes has mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories
225 // it can be the only search type.
226 if ( this.searchTypes.indexOf( mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories ) > -1 &&
227 this.searchTypes.length > 1
229 throw new Error( 'Can\'t have additional search types with mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories' );
232 // If the searchTypes has mw.widgets.CategoryMultiselectWidget.SearchType.ParentCategories
233 // it can be the only search type.
234 if ( this.searchTypes.indexOf( mw.widgets.CategoryMultiselectWidget.SearchType.ParentCategories ) > -1 &&
235 this.searchTypes.length > 1
237 throw new Error( 'Can\'t have additional search types with mw.widgets.CategoryMultiselectWidget.SearchType.ParentCategories' );
244 * Sets and validates the value of `this.searchType`.
246 * @param {mw.widgets.CategoryMultiselectWidget.SearchType[]} searchTypes
248 mw.widgets.CategoryMultiselectWidget.prototype.setSearchTypes = function ( searchTypes ) {
249 this.searchTypes = searchTypes;
250 this.validateSearchTypes();
254 * Searches categories based on input and searchType.
258 * @param {string} input The input used to prefix search categories
259 * @param {mw.widgets.CategoryMultiselectWidget.SearchType} searchType
260 * @return {jQuery.Promise} Resolves with an array of categories
262 mw.widgets.CategoryMultiselectWidget.prototype.searchCategories = function ( input, searchType ) {
263 const deferred = $.Deferred(),
264 cacheKey = input + searchType.toString();
267 if ( hasOwn.call( this.searchCache, cacheKey ) ) {
268 return this.searchCache[ cacheKey ];
271 switch ( searchType ) {
272 case mw.widgets.CategoryMultiselectWidget.SearchType.OpenSearch:
275 action: 'opensearch',
276 namespace: NS_CATEGORY,
279 } ).done( ( res ) => {
280 const categories = res[ 1 ];
281 deferred.resolve( categories );
282 } ).fail( deferred.reject.bind( deferred ) );
285 case mw.widgets.CategoryMultiselectWidget.SearchType.InternalSearch:
290 apnamespace: NS_CATEGORY,
294 } ).done( ( res ) => {
295 const categories = res.query.allpages.map( ( page ) => page.title );
296 deferred.resolve( categories );
297 } ).fail( deferred.reject.bind( deferred ) );
300 case mw.widgets.CategoryMultiselectWidget.SearchType.Exists:
301 if ( input.indexOf( '|' ) > -1 ) {
302 deferred.resolve( [] );
310 titles: 'Category:' + input
311 } ).done( ( res ) => {
312 const categories = [];
314 res.query.pages.forEach( ( page ) => {
315 if ( !page.missing ) {
316 categories.push( page.title );
320 deferred.resolve( categories );
321 } ).fail( deferred.reject.bind( deferred ) );
324 case mw.widgets.CategoryMultiselectWidget.SearchType.SubCategories:
325 if ( input.indexOf( '|' ) > -1 ) {
326 deferred.resolve( [] );
333 list: 'categorymembers',
336 cmtitle: 'Category:' + input
337 } ).done( ( res ) => {
338 const categories = res.query.categorymembers.map( ( category ) => category.title );
339 deferred.resolve( categories );
340 } ).fail( deferred.reject.bind( deferred ) );
343 case mw.widgets.CategoryMultiselectWidget.SearchType.ParentCategories:
344 if ( input.indexOf( '|' ) > -1 ) {
345 deferred.resolve( [] );
354 titles: 'Category:' + input
355 } ).done( ( res ) => {
356 const categories = [];
358 res.query.pages.forEach( ( page ) => {
359 if ( !page.missing && Array.isArray( page.categories ) ) {
360 categories.push( ...page.categories.map( ( category ) => category.title ) );
364 deferred.resolve( categories );
365 } ).fail( deferred.reject.bind( deferred ) );
369 throw new Error( 'Unknown searchType' );
373 this.searchCache[ cacheKey ] = deferred.promise();
375 return deferred.promise();
379 * @enum mw.widgets.CategoryMultiselectWidget.SearchType
380 * Types of search available.
382 mw.widgets.CategoryMultiselectWidget.SearchType = {
383 /** Search using action=opensearch */
386 /** Search using action=query */
389 /** Search for existing categories with the exact title */
392 /** Search only subcategories */
395 /** Search only parent categories */