2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryWidget class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2013-04-29 21:01:56 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.MWCategoryWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.Widget
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes OO.ui.mixin.GroupElement
|
|
|
|
* @mixes OO.ui.mixin.DraggableGroupElement
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2014-10-14 22:48:24 +00:00
|
|
|
* @cfg {jQuery} [$overlay] Overlay to render dropdowns in
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget = function VeUiMWCategoryWidget( config ) {
|
2014-11-21 13:00:50 +00:00
|
|
|
// Config initialization
|
2013-04-29 21:01:56 +00:00
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWCategoryWidget.super.call( this, config );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2013-05-03 18:30:33 +00:00
|
|
|
// Mixin constructors
|
2015-06-05 17:59:00 +00:00
|
|
|
OO.ui.mixin.GroupElement.call( this, config );
|
2021-11-15 17:25:51 +00:00
|
|
|
OO.ui.mixin.DraggableGroupElement.call( this, ve.extendObject( {}, config, { orientation: 'horizontal' } ) );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var categoryNamespace = mw.config.get( 'wgNamespaceIds' ).category;
|
2013-04-29 21:01:56 +00:00
|
|
|
// Properties
|
2022-01-19 14:25:39 +00:00
|
|
|
this.fragment = null;
|
2013-04-29 21:01:56 +00:00
|
|
|
this.categories = {};
|
2016-02-18 16:47:26 +00:00
|
|
|
// Source -> target
|
|
|
|
this.categoryRedirects = {};
|
|
|
|
// Title cache - will contain entries even if title is already normalized
|
|
|
|
this.normalizedTitles = {};
|
2015-04-09 23:47:15 +00:00
|
|
|
this.popup = new ve.ui.MWCategoryPopupWidget();
|
|
|
|
this.input = new ve.ui.MWCategoryInputWidget( this, { $overlay: config.$overlay } );
|
2016-02-18 11:36:31 +00:00
|
|
|
this.forceCapitalization = mw.config.get( 'wgCaseSensitiveNamespaces' ).indexOf( categoryNamespace ) === -1;
|
|
|
|
this.categoryPrefix = mw.config.get( 'wgFormattedNamespaces' )[ categoryNamespace ] + ':';
|
2020-04-02 04:20:54 +00:00
|
|
|
this.expandedItem = null;
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Events
|
2015-01-15 22:07:34 +00:00
|
|
|
this.input.connect( this, { choose: 'onInputChoose' } );
|
2013-05-01 22:21:32 +00:00
|
|
|
this.popup.connect( this, {
|
2014-08-22 20:50:48 +00:00
|
|
|
removeCategory: 'onRemoveCategory',
|
2020-04-02 04:20:54 +00:00
|
|
|
updateSortkey: 'onUpdateSortkey',
|
|
|
|
ready: 'onPopupOpened',
|
|
|
|
closing: 'onPopupClosing'
|
2013-04-29 21:01:56 +00:00
|
|
|
} );
|
2016-02-19 17:34:41 +00:00
|
|
|
this.connect( this, {
|
|
|
|
drag: 'onDrag'
|
|
|
|
} );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Initialization
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwCategoryWidget' )
|
2013-04-29 21:01:56 +00:00
|
|
|
.append(
|
2016-02-19 17:34:41 +00:00
|
|
|
this.$group.addClass( 've-ui-mwCategoryWidget-items' ).append(
|
|
|
|
this.input.$element
|
|
|
|
),
|
2014-10-16 05:52:15 +00:00
|
|
|
this.popup.$element,
|
2015-04-09 23:47:15 +00:00
|
|
|
$( '<div>' ).css( 'clear', 'both' )
|
2013-04-29 21:01:56 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ui.MWCategoryWidget, OO.ui.Widget );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2015-06-10 19:28:09 +00:00
|
|
|
OO.mixinClass( ve.ui.MWCategoryWidget, OO.ui.mixin.GroupElement );
|
|
|
|
OO.mixinClass( ve.ui.MWCategoryWidget, OO.ui.mixin.DraggableGroupElement );
|
2013-06-10 23:41:12 +00:00
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.ui.MWCategoryWidget#newCategory
|
2013-06-10 22:22:30 +00:00
|
|
|
* @param {Object} item Category item
|
|
|
|
* @param {string} item.name Fully prefixed category name
|
|
|
|
* @param {string} item.value Category value (name without prefix)
|
2021-09-11 08:05:31 +00:00
|
|
|
* @param {ve.dm.MWCategoryMetaItem} item.metaItem
|
2016-02-19 17:34:41 +00:00
|
|
|
* @param {ve.dm.MetaItem} [beforeCategory] Insert after this category; if unset, insert at the end
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.ui.MWCategoryWidget#updateSortkey
|
2013-06-10 22:22:30 +00:00
|
|
|
* @param {Object} item Category item
|
|
|
|
* @param {string} item.name Fully prefixed category name
|
|
|
|
* @param {string} item.value Category value (name without prefix)
|
2021-09-11 08:05:31 +00:00
|
|
|
* @param {ve.dm.MWCategoryMetaItem} item.metaItem
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2022-01-19 14:25:39 +00:00
|
|
|
/**
|
|
|
|
* Surface fragment for modifying meta list
|
|
|
|
*
|
|
|
|
* @param {ve.dm.SurfaceFragment|null} fragment Surface fragment
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.setFragment = function ( fragment ) {
|
|
|
|
this.fragment = fragment;
|
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
2015-01-15 22:07:34 +00:00
|
|
|
* Handle input 'choose' event.
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
2015-03-27 10:51:06 +00:00
|
|
|
* @param {OO.ui.MenuOptionWidget} item Selected item
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
2015-01-15 22:07:34 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.onInputChoose = function ( item ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var value = item.getData(),
|
2015-01-15 22:07:34 +00:00
|
|
|
widget = this;
|
2013-07-01 23:01:34 +00:00
|
|
|
|
|
|
|
if ( value && value !== '' ) {
|
|
|
|
// Add new item
|
2022-02-21 18:07:07 +00:00
|
|
|
var categoryItem = this.getCategoryItemFromValue( value );
|
2024-04-30 16:44:25 +00:00
|
|
|
this.queryCategoryStatus( [ categoryItem.name ] ).done( () => {
|
2014-10-15 20:07:39 +00:00
|
|
|
// Remove existing items by name
|
|
|
|
var toRemove = mw.Title.newFromText( categoryItem.name ).getMainText();
|
2015-01-15 22:07:34 +00:00
|
|
|
if ( Object.prototype.hasOwnProperty.call( widget.categories, toRemove ) ) {
|
2022-01-19 14:25:39 +00:00
|
|
|
widget.fragment.removeMeta( widget.categories[ toRemove ].metaItem );
|
2014-10-15 20:07:39 +00:00
|
|
|
}
|
2016-02-18 16:47:26 +00:00
|
|
|
categoryItem.name = widget.normalizedTitles[ categoryItem.name ];
|
2015-01-15 22:07:34 +00:00
|
|
|
widget.emit( 'newCategory', categoryItem );
|
2014-02-14 20:03:42 +00:00
|
|
|
} );
|
2015-01-15 22:07:34 +00:00
|
|
|
}
|
|
|
|
};
|
2014-02-14 20:03:42 +00:00
|
|
|
|
2020-04-02 04:20:54 +00:00
|
|
|
/**
|
|
|
|
* Hanle popup open event
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onPopupOpened = function () {
|
|
|
|
this.popup.removeButton.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle popup closing dialog
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onPopupClosing = function () {
|
|
|
|
this.expandedItem.focus();
|
|
|
|
};
|
|
|
|
|
2015-01-15 22:07:34 +00:00
|
|
|
/**
|
|
|
|
* Get a category item.
|
|
|
|
*
|
|
|
|
* @param {string} value Category name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object} Category item with name, value and metaItem properties
|
2015-01-15 22:07:34 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.getCategoryItemFromValue = function ( value ) {
|
|
|
|
// Normalize
|
2022-02-21 18:07:07 +00:00
|
|
|
var title = mw.Title.newFromText( this.categoryPrefix + value );
|
2015-01-15 22:07:34 +00:00
|
|
|
if ( title ) {
|
|
|
|
return {
|
|
|
|
name: title.getPrefixedText(),
|
|
|
|
value: title.getMainText(),
|
|
|
|
metaItem: {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.forceCapitalization ) {
|
|
|
|
value = value.slice( 0, 1 ).toUpperCase() + value.slice( 1 );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
2015-01-15 22:07:34 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: this.categoryPrefix + value,
|
|
|
|
value: value,
|
|
|
|
metaItem: {}
|
|
|
|
};
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
2016-02-19 17:34:41 +00:00
|
|
|
/**
|
|
|
|
* Focus the widget
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.focus = function () {
|
|
|
|
this.input.$input[ 0 ].focus();
|
|
|
|
};
|
|
|
|
|
2014-11-14 20:44:55 +00:00
|
|
|
/**
|
2016-02-18 16:47:26 +00:00
|
|
|
* @param {ve.ui.MWCategoryItemWidget} item Item that was moved
|
2014-11-14 20:44:55 +00:00
|
|
|
* @param {number} newIndex The new index of the item
|
|
|
|
*/
|
2016-02-19 17:34:41 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.onDrag = function () {
|
|
|
|
this.fitInput();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.mixin.DraggableGroupElement
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.ui.MWCategoryWidget#newCategory
|
2016-02-19 17:34:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.reorder = function ( item, newIndex ) {
|
|
|
|
// Compute beforeCategory before removing, otherwise newIndex
|
2014-11-14 20:44:55 +00:00
|
|
|
// could be off by one
|
2016-02-25 16:26:12 +00:00
|
|
|
var beforeCategory = this.items[ newIndex ] && this.items[ newIndex ].metaItem;
|
2014-11-14 20:44:55 +00:00
|
|
|
if ( Object.prototype.hasOwnProperty.call( this.categories, item.value ) ) {
|
2022-01-19 14:25:39 +00:00
|
|
|
this.fragment.removeMeta( this.categories[ item.value ].metaItem );
|
2014-11-14 20:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 17:34:41 +00:00
|
|
|
this.emit( 'newCategory', item, beforeCategory );
|
2014-11-14 20:44:55 +00:00
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
2013-06-10 22:22:30 +00:00
|
|
|
* Removes category from model.
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
2016-02-18 16:47:26 +00:00
|
|
|
* @param {string} name Removed category name
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onRemoveCategory = function ( name ) {
|
2022-01-19 14:25:39 +00:00
|
|
|
this.fragment.removeMeta( this.categories[ name ].metaItem );
|
2015-08-19 17:33:02 +00:00
|
|
|
delete this.categories[ name ];
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-06-10 22:22:30 +00:00
|
|
|
* Update sortkey value, emit updateSortkey event
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
2013-05-06 11:34:32 +00:00
|
|
|
* @param {string} name
|
|
|
|
* @param {string} value
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.ui.MWCategoryWidget#updateSortkey
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onUpdateSortkey = function ( name, value ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.categories[ name ].sortKey = value;
|
|
|
|
this.emit( 'updateSortkey', this.categories[ name ] );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
2015-02-27 22:04:39 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.clearItems = function () {
|
2015-06-05 17:59:00 +00:00
|
|
|
OO.ui.mixin.GroupElement.prototype.clearItems.call( this );
|
2015-02-27 22:04:39 +00:00
|
|
|
this.categories = {};
|
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* Toggles popup menu per category item
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2013-04-29 21:01:56 +00:00
|
|
|
* @param {Object} item
|
|
|
|
*/
|
2013-05-25 12:53:33 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.onTogglePopupMenu = function ( item ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
// Close open popup.
|
2016-09-13 03:17:20 +00:00
|
|
|
if ( item.value !== this.popup.category ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
this.popup.openPopup( item );
|
2020-04-02 04:20:54 +00:00
|
|
|
this.expandedItem = item;
|
|
|
|
this.popup
|
|
|
|
.$element
|
|
|
|
.attr( 'aria-label',
|
|
|
|
ve.msg( 'visualeditor-dialog-meta-categories-category' )
|
|
|
|
);
|
2013-04-29 21:01:56 +00:00
|
|
|
} else {
|
|
|
|
// Handle toggle
|
|
|
|
this.popup.closePopup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-28 19:02:36 +00:00
|
|
|
/**
|
|
|
|
* Set the default sort key.
|
|
|
|
*
|
|
|
|
* @param {string} value Default sort key value
|
|
|
|
*/
|
2013-05-03 22:17:35 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.setDefaultSortKey = function ( value ) {
|
|
|
|
this.popup.setDefaultSortKey( value );
|
|
|
|
};
|
|
|
|
|
2019-02-25 11:48:53 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.setDisabled = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWCategoryWidget.super.prototype.setDisabled.apply( this, arguments );
|
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var isDisabled = this.isDisabled();
|
2019-02-25 11:48:53 +00:00
|
|
|
|
|
|
|
if ( this.input ) {
|
|
|
|
this.input.setDisabled( isDisabled );
|
|
|
|
}
|
|
|
|
if ( this.items ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
this.items.forEach( ( item ) => {
|
2019-02-25 11:48:53 +00:00
|
|
|
item.setDisabled( isDisabled );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( this.popup ) {
|
|
|
|
this.popup.closePopup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* Get list of category names.
|
|
|
|
*
|
2016-10-28 19:02:36 +00:00
|
|
|
* @return {string[]} List of category names
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.getCategories = function () {
|
2015-03-10 12:50:42 +00:00
|
|
|
return Object.keys( this.categories );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
2014-02-14 20:03:42 +00:00
|
|
|
/**
|
2014-10-15 20:07:39 +00:00
|
|
|
* Starts a request to update the link cache's hidden and missing status for
|
|
|
|
* the given titles, following normalisation responses as necessary.
|
2014-02-14 20:03:42 +00:00
|
|
|
*
|
|
|
|
* @param {string[]} categoryNames
|
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
2014-10-15 20:07:39 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.queryCategoryStatus = function ( categoryNames ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var widget = this;
|
2016-02-18 16:47:26 +00:00
|
|
|
|
|
|
|
// Get rid of any we already know the hidden status of, or have an entry
|
2017-04-20 21:47:16 +00:00
|
|
|
// if normalizedTitles (i.e. have been fetched before)
|
2024-04-30 16:44:25 +00:00
|
|
|
var categoryNamesToQuery = categoryNames.filter( ( name ) => {
|
2016-02-18 16:47:26 +00:00
|
|
|
var cacheEntry;
|
|
|
|
if ( widget.normalizedTitles[ name ] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cacheEntry = ve.init.platform.linkCache.getCached( name );
|
|
|
|
if ( cacheEntry && cacheEntry.hidden ) {
|
|
|
|
// As we aren't doing an API request for this category, mark it in the cache.
|
|
|
|
widget.normalizedTitles[ name ] = name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-02-14 20:03:42 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !categoryNamesToQuery.length ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
return ve.createDeferred().resolve( {} ).promise();
|
2014-02-14 20:03:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var index = 0, batchSize = 50, promises = [];
|
2017-04-20 21:47:16 +00:00
|
|
|
// Batch this up into groups of 50
|
|
|
|
while ( index < categoryNamesToQuery.length ) {
|
2018-05-04 13:30:10 +00:00
|
|
|
promises.push( ve.init.target.getContentApi().get( {
|
2017-04-20 21:47:16 +00:00
|
|
|
action: 'query',
|
|
|
|
prop: 'pageprops',
|
|
|
|
titles: categoryNamesToQuery.slice( index, index + batchSize ),
|
|
|
|
ppprop: 'hiddencat',
|
|
|
|
redirects: ''
|
2024-04-30 16:44:25 +00:00
|
|
|
} ).then( ( result ) => {
|
2017-04-20 21:47:16 +00:00
|
|
|
var linkCacheUpdate = {},
|
|
|
|
normalizedTitles = {};
|
|
|
|
if ( result && result.query && result.query.pages ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
result.query.pages.forEach( ( pageInfo ) => {
|
2017-04-20 21:47:16 +00:00
|
|
|
linkCacheUpdate[ pageInfo.title ] = {
|
|
|
|
missing: Object.prototype.hasOwnProperty.call( pageInfo, 'missing' ),
|
|
|
|
hidden: pageInfo.pageprops &&
|
|
|
|
Object.prototype.hasOwnProperty.call( pageInfo.pageprops, 'hiddencat' )
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( result && result.query && result.query.redirects ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
result.query.redirects.forEach( ( redirectInfo ) => {
|
2017-04-20 21:47:16 +00:00
|
|
|
widget.categoryRedirects[ redirectInfo.from ] = redirectInfo.to;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
ve.init.platform.linkCache.set( linkCacheUpdate );
|
2014-10-15 20:07:39 +00:00
|
|
|
|
2017-04-20 21:47:16 +00:00
|
|
|
if ( result.query && result.query.normalized ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
result.query.normalized.forEach( ( normalisation ) => {
|
2017-04-20 21:47:16 +00:00
|
|
|
normalizedTitles[ normalisation.from ] = normalisation.to;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
categoryNames.forEach( ( name ) => {
|
2017-04-20 21:47:16 +00:00
|
|
|
widget.normalizedTitles[ name ] = normalizedTitles[ name ] || name;
|
2014-10-15 20:07:39 +00:00
|
|
|
} );
|
2017-04-20 21:47:16 +00:00
|
|
|
} ) );
|
|
|
|
index += batchSize;
|
|
|
|
}
|
2014-10-15 20:07:39 +00:00
|
|
|
|
2019-11-02 16:29:11 +00:00
|
|
|
return ve.promiseAll( promises );
|
2014-02-14 20:03:42 +00:00
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* Adds category items.
|
|
|
|
*
|
2013-05-03 18:30:33 +00:00
|
|
|
* @param {Object[]} items Items to add
|
|
|
|
* @param {number} [index] Index to insert items after
|
2014-02-14 20:03:42 +00:00
|
|
|
* @return {jQuery.Promise}
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
2013-05-03 18:30:33 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.addItems = function ( items, index ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var categoryItems = [],
|
2014-11-14 20:44:55 +00:00
|
|
|
existingCategoryItems = [],
|
2019-02-20 20:23:43 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-map-util
|
2024-04-30 16:44:25 +00:00
|
|
|
categoryNames = $.map( items, ( item ) => item.name ),
|
2016-02-18 16:47:26 +00:00
|
|
|
widget = this;
|
2014-02-14 20:03:42 +00:00
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
return this.queryCategoryStatus( categoryNames ).then( () => {
|
2022-02-21 18:07:07 +00:00
|
|
|
var config;
|
|
|
|
var checkValueMatches = function ( existingCategoryItem ) {
|
|
|
|
return config.item.value === existingCategoryItem.value;
|
|
|
|
};
|
2014-10-15 20:07:39 +00:00
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
items.forEach( ( item ) => {
|
2016-02-18 16:47:26 +00:00
|
|
|
item.name = widget.normalizedTitles[ item.name ];
|
2014-02-14 20:03:42 +00:00
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var itemTitle = new mw.Title( item.name, mw.config.get( 'wgNamespaceIds' ).category );
|
2014-02-14 20:03:42 +00:00
|
|
|
// Create a widget using the item data
|
2014-03-11 19:02:16 +00:00
|
|
|
config = {
|
2014-09-24 22:18:09 +00:00
|
|
|
item: item
|
2014-03-11 19:02:16 +00:00
|
|
|
};
|
2022-02-21 18:07:07 +00:00
|
|
|
var cachedData;
|
2016-02-18 16:47:26 +00:00
|
|
|
if ( Object.prototype.hasOwnProperty.call( widget.categoryRedirects, itemTitle.getPrefixedText() ) ) {
|
2014-03-11 19:02:16 +00:00
|
|
|
config.redirectTo = new mw.Title(
|
2016-02-18 16:47:26 +00:00
|
|
|
widget.categoryRedirects[ itemTitle.getPrefixedText() ],
|
2014-03-11 19:02:16 +00:00
|
|
|
mw.config.get( 'wgNamespaceIds' ).category
|
|
|
|
).getMainText();
|
2016-02-18 16:47:26 +00:00
|
|
|
cachedData = ve.init.platform.linkCache.getCached( widget.categoryRedirects[ itemTitle.getPrefixedText() ] );
|
2014-09-24 22:18:09 +00:00
|
|
|
} else {
|
2014-11-05 22:45:09 +00:00
|
|
|
cachedData = ve.init.platform.linkCache.getCached( item.name );
|
2014-03-11 19:02:16 +00:00
|
|
|
}
|
2014-10-15 20:07:39 +00:00
|
|
|
config.hidden = cachedData.hidden;
|
|
|
|
config.missing = cachedData.missing;
|
2019-02-25 11:48:53 +00:00
|
|
|
config.disabled = widget.disabled;
|
2014-10-15 20:07:39 +00:00
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var categoryItem = new ve.ui.MWCategoryItemWidget( config );
|
2016-02-18 16:47:26 +00:00
|
|
|
categoryItem.connect( widget, {
|
2014-08-22 20:50:48 +00:00
|
|
|
togglePopupMenu: 'onTogglePopupMenu'
|
2014-02-14 20:03:42 +00:00
|
|
|
} );
|
|
|
|
|
2014-10-15 20:07:39 +00:00
|
|
|
// Index item
|
2016-02-18 16:47:26 +00:00
|
|
|
widget.categories[ itemTitle.getMainText() ] = categoryItem;
|
2014-02-14 20:03:42 +00:00
|
|
|
// Copy sortKey from old item when "moving"
|
2018-08-24 22:11:30 +00:00
|
|
|
existingCategoryItems = widget.items.filter( checkValueMatches );
|
2014-11-14 20:44:55 +00:00
|
|
|
if ( existingCategoryItems.length ) {
|
|
|
|
// There should only be one element in existingCategoryItems
|
2015-08-19 17:33:02 +00:00
|
|
|
categoryItem.sortKey = existingCategoryItems[ 0 ].sortKey;
|
2016-02-18 16:47:26 +00:00
|
|
|
widget.removeItems( [ existingCategoryItems[ 0 ] ] );
|
2014-02-14 20:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
categoryItems.push( categoryItem );
|
2020-08-18 12:16:49 +00:00
|
|
|
} );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2016-02-18 16:47:26 +00:00
|
|
|
OO.ui.mixin.DraggableGroupElement.prototype.addItems.call( widget, categoryItems, index );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2016-02-19 17:34:41 +00:00
|
|
|
// Ensure the input remains the last item in the list, and preserve focus
|
2022-02-21 18:07:07 +00:00
|
|
|
var hadFocus = widget.getElementDocument().activeElement === widget.input.$input[ 0 ];
|
2016-02-19 17:34:41 +00:00
|
|
|
widget.$group.append( widget.input.$element );
|
|
|
|
if ( hadFocus ) {
|
|
|
|
widget.input.$input[ 0 ].focus();
|
|
|
|
}
|
2016-02-18 16:47:26 +00:00
|
|
|
widget.fitInput();
|
2014-02-14 20:03:42 +00:00
|
|
|
} );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-11-14 20:44:55 +00:00
|
|
|
* @inheritdoc
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
2014-11-14 20:44:55 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.removeItems = function ( items ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
for ( var i = 0, len = items.length; i < len; i++ ) {
|
|
|
|
var categoryItem = items[ i ];
|
2014-09-30 18:11:09 +00:00
|
|
|
if ( categoryItem ) {
|
|
|
|
categoryItem.disconnect( this );
|
|
|
|
items.push( categoryItem );
|
2015-08-19 17:33:02 +00:00
|
|
|
delete this.categories[ categoryItem.value ];
|
2014-09-30 18:11:09 +00:00
|
|
|
}
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2015-06-05 17:59:00 +00:00
|
|
|
OO.ui.mixin.DraggableGroupElement.prototype.removeItems.call( this, items );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
|
|
|
this.fitInput();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-fit the input.
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.fitInput = function () {
|
2022-02-21 18:07:07 +00:00
|
|
|
var $input = this.input.$element;
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2019-08-01 01:48:24 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-sizzle
|
2016-02-19 17:34:41 +00:00
|
|
|
if ( !this.items.length || !$input.is( ':visible' ) ) {
|
2013-05-03 18:30:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:34:41 +00:00
|
|
|
// Measure the input's natural size
|
|
|
|
$input.css( 'width', '' );
|
2022-02-21 18:07:07 +00:00
|
|
|
var inputWidth = $input.outerWidth( true );
|
2016-02-19 17:34:41 +00:00
|
|
|
|
|
|
|
// this.items hasn't been updated if this was triggered by a drag event,
|
|
|
|
// so look at document order
|
2022-02-21 18:07:07 +00:00
|
|
|
var $lastItem = this.$group.find( '.ve-ui-mwCategoryItemWidget' ).last();
|
2016-02-19 17:34:41 +00:00
|
|
|
// Try to fit to the right of the last item
|
2022-02-21 18:07:07 +00:00
|
|
|
var availableSpace = Math.floor( this.$group.width() - ( $lastItem.position().left + $lastItem.outerWidth( true ) ) );
|
2016-02-19 17:34:41 +00:00
|
|
|
if ( availableSpace > inputWidth ) {
|
|
|
|
$input.css( 'width', availableSpace );
|
2013-05-03 18:30:33 +00:00
|
|
|
}
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|