2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryWidget class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; 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
|
2014-02-15 00:17:15 +00:00
|
|
|
* @mixins OO.ui.GroupElement
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget = function VeUiMWCategoryWidget( config ) {
|
|
|
|
// Config intialization
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.Widget.call( this, config );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2013-05-03 18:30:33 +00:00
|
|
|
// Mixin constructors
|
2013-11-01 19:45:59 +00:00
|
|
|
OO.ui.GroupElement.call( this, this.$( '<div>' ), config );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
// Properties
|
|
|
|
this.categories = {};
|
2014-02-14 20:03:42 +00:00
|
|
|
this.categoryHiddenStatus = {};
|
2014-03-11 19:02:16 +00:00
|
|
|
this.categoryRedirects = {}; // Source -> target
|
2013-04-29 21:01:56 +00:00
|
|
|
this.popupState = false;
|
|
|
|
this.savedPopupState = false;
|
|
|
|
this.popup = new ve.ui.MWCategoryPopupWidget( {
|
2013-11-01 19:45:59 +00:00
|
|
|
'$': this.$, '$overlay': config.$overlay
|
2013-04-29 21:01:56 +00:00
|
|
|
} );
|
2013-05-03 18:30:33 +00:00
|
|
|
this.input = new ve.ui.MWCategoryInputWidget( this, {
|
2013-11-01 19:45:59 +00:00
|
|
|
'$': this.$, '$overlay': config.$overlay
|
2013-04-29 21:01:56 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Events
|
2013-05-03 18:30:33 +00:00
|
|
|
this.input.$input.on( 'keydown', ve.bind( this.onLookupInputKeyDown, this ) );
|
2014-04-07 19:15:01 +00:00
|
|
|
this.input.lookupMenu.connect( this, { 'choose': 'onLookupMenuItemChoose' } );
|
2013-05-01 22:21:32 +00:00
|
|
|
this.popup.connect( this, {
|
2013-04-29 21:01:56 +00:00
|
|
|
'removeCategory': 'onRemoveCategory',
|
|
|
|
'updateSortkey': 'onUpdateSortkey',
|
|
|
|
'hide': 'onPopupHide'
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Initialization
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwCategoryWidget' )
|
2013-04-29 21:01:56 +00:00
|
|
|
.append(
|
2013-07-25 12:34:25 +00:00
|
|
|
this.$group.addClass( 've-ui-mwCategoryWidget-items' ),
|
2013-11-01 19:45:59 +00:00
|
|
|
this.input.$element,
|
|
|
|
this.$( '<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
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.mixinClass( ve.ui.MWCategoryWidget, OO.ui.GroupElement );
|
2013-06-10 23:41:12 +00:00
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event 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)
|
|
|
|
* @param {Object} item.metaItem Category meta item
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event 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)
|
|
|
|
* @param {Object} item.metaItem Category meta item
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input key down event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input key down event
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onLookupInputKeyDown = function ( e ) {
|
2013-05-03 18:30:33 +00:00
|
|
|
if ( this.input.getValue() !== '' && e.which === 13 ) {
|
2014-02-14 20:03:42 +00:00
|
|
|
var item = this.input.getCategoryItemFromValue( this.input.getValue() ),
|
|
|
|
categoryWidget = this;
|
|
|
|
this.queryCategoryHiddenStatus( [item.name] ).done( function () {
|
|
|
|
categoryWidget.emit( 'newCategory', item );
|
|
|
|
} );
|
2013-05-03 18:30:33 +00:00
|
|
|
this.input.setValue( '' );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle menu item select event.
|
|
|
|
*
|
|
|
|
* @method
|
2013-10-09 20:09:59 +00:00
|
|
|
* @param {OO.ui.MenuItemWidget} item Selected item
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
2014-04-07 19:15:01 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.onLookupMenuItemChoose = function ( item ) {
|
2014-02-14 20:03:42 +00:00
|
|
|
var categoryItem,
|
|
|
|
value = item && item.getData(),
|
|
|
|
categoryWidget = this;
|
2013-07-01 23:01:34 +00:00
|
|
|
|
|
|
|
if ( value && value !== '' ) {
|
|
|
|
// Remove existing items by value
|
|
|
|
if ( value in this.categories ) {
|
|
|
|
this.categories[value].metaItem.remove();
|
|
|
|
}
|
|
|
|
// Add new item
|
2014-02-14 20:03:42 +00:00
|
|
|
categoryItem = this.input.getCategoryItemFromValue( value );
|
|
|
|
this.queryCategoryHiddenStatus( [categoryItem.name] ).done( function () {
|
|
|
|
categoryWidget.emit( 'newCategory', categoryItem );
|
|
|
|
} );
|
|
|
|
|
2013-07-01 23:01:34 +00:00
|
|
|
// Reset input
|
2013-05-03 18:30:33 +00:00
|
|
|
this.input.setValue( '' );
|
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
|
|
|
*
|
2013-06-10 22:22:30 +00:00
|
|
|
* @method
|
2013-05-06 11:34:32 +00:00
|
|
|
* @param {string} name category name
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onRemoveCategory = function ( name ) {
|
|
|
|
this.categories[name].metaItem.remove();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-06-10 22:22:30 +00:00
|
|
|
* Update sortkey value, emit updateSortkey event
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
2013-06-10 22:22:30 +00:00
|
|
|
* @method
|
2013-05-06 11:34:32 +00:00
|
|
|
* @param {string} name
|
|
|
|
* @param {string} value
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onUpdateSortkey = function ( name, value ) {
|
|
|
|
this.categories[name].sortKey = value;
|
|
|
|
this.emit( 'updateSortkey', this.categories[name] );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets popup state when popup is hidden
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onPopupHide = function () {
|
|
|
|
this.popupState = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves current popup state
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.onSavePopupState = function () {
|
|
|
|
this.savedPopupState = this.popupState;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles popup menu per category item
|
|
|
|
* @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.
|
|
|
|
if ( this.savedPopupState === false || item.value !== this.popup.category ) {
|
|
|
|
this.popup.openPopup( item );
|
|
|
|
} else {
|
|
|
|
// Handle toggle
|
|
|
|
this.popup.closePopup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-31 22:53:29 +00:00
|
|
|
/** */
|
2013-05-03 22:17:35 +00:00
|
|
|
ve.ui.MWCategoryWidget.prototype.setDefaultSortKey = function ( value ) {
|
|
|
|
this.popup.setDefaultSortKey( value );
|
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* Get list of category names.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string[]} List of category names
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.getCategories = function () {
|
|
|
|
return ve.getObjectKeys( this.categories );
|
|
|
|
};
|
|
|
|
|
2014-02-14 20:03:42 +00:00
|
|
|
/**
|
|
|
|
* Starts a request to update categoryHiddenStatus for the given titles.
|
|
|
|
* The returned promise will be resolved with an API result if an API call was made,
|
|
|
|
* or no arguments if it was unnecessary.
|
|
|
|
*
|
|
|
|
* @param {string[]} categoryNames
|
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.queryCategoryHiddenStatus = function ( categoryNames ) {
|
|
|
|
var categoryWidget = this, categoryNamesToQuery = [];
|
|
|
|
// Get rid of any we already know the hidden status of.
|
|
|
|
categoryNamesToQuery = $.grep( categoryNames, function ( categoryTitle ) {
|
|
|
|
return !Object.prototype.hasOwnProperty.call( categoryWidget.categoryHiddenStatus, categoryTitle );
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !categoryNamesToQuery.length ) {
|
|
|
|
return $.Deferred().resolve().promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*global mw*/
|
|
|
|
return new mw.Api().get( {
|
|
|
|
action: 'query',
|
|
|
|
prop: 'pageprops',
|
|
|
|
titles: categoryNamesToQuery.join( '|' ),
|
2014-03-11 19:02:16 +00:00
|
|
|
ppprop: 'hiddencat',
|
|
|
|
redirects: ''
|
2014-02-14 20:03:42 +00:00
|
|
|
} ).then( function ( result ) {
|
|
|
|
if ( result && result.query && result.query.pages ) {
|
|
|
|
$.each( result.query.pages, function ( index, pageInfo ) {
|
|
|
|
var hiddenStatus = !!( pageInfo.pageprops && pageInfo.pageprops.hiddencat !== undefined );
|
|
|
|
categoryWidget.categoryHiddenStatus[pageInfo.title] = hiddenStatus;
|
|
|
|
} );
|
|
|
|
}
|
2014-03-11 19:02:16 +00:00
|
|
|
if ( result && result.query && result.query.redirects ) {
|
|
|
|
$.each( result.query.redirects, function ( index, redirectInfo ) {
|
|
|
|
categoryWidget.categoryRedirects[redirectInfo.from] = redirectInfo.to;
|
|
|
|
} );
|
|
|
|
}
|
2014-02-14 20:03:42 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* Adds category items.
|
|
|
|
*
|
|
|
|
* @method
|
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 ) {
|
|
|
|
var i, len, item, categoryItem,
|
|
|
|
categoryItems = [],
|
2014-02-14 20:03:42 +00:00
|
|
|
existingCategoryItem = null,
|
|
|
|
categoryNames = $.map( items, function ( item ) {
|
|
|
|
return item.name;
|
|
|
|
} ),
|
|
|
|
categoryWidget = this;
|
|
|
|
|
|
|
|
return this.queryCategoryHiddenStatus( categoryNames ).then( function () {
|
2014-03-11 19:02:16 +00:00
|
|
|
var itemTitle, config;
|
2014-02-14 20:03:42 +00:00
|
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
|
|
|
item = items[i];
|
|
|
|
|
2014-03-11 19:02:16 +00:00
|
|
|
itemTitle = new mw.Title( item.name, mw.config.get( 'wgNamespaceIds' ).category ).getPrefixedText();
|
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-02-14 20:03:42 +00:00
|
|
|
'$': categoryWidget.$,
|
|
|
|
'item': item,
|
|
|
|
'hidden': categoryWidget.categoryHiddenStatus[item.name]
|
2014-03-11 19:02:16 +00:00
|
|
|
};
|
|
|
|
if ( Object.prototype.hasOwnProperty.call( categoryWidget.categoryRedirects, itemTitle ) ) {
|
|
|
|
config.redirectTo = new mw.Title(
|
|
|
|
categoryWidget.categoryRedirects[itemTitle],
|
|
|
|
mw.config.get( 'wgNamespaceIds' ).category
|
|
|
|
).getMainText();
|
|
|
|
}
|
|
|
|
categoryItem = new ve.ui.MWCategoryItemWidget( config );
|
2014-02-14 20:03:42 +00:00
|
|
|
categoryItem.connect( categoryWidget, {
|
|
|
|
'savePopupState': 'onSavePopupState',
|
|
|
|
'togglePopupMenu': 'onTogglePopupMenu'
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Index item by value
|
|
|
|
categoryWidget.categories[item.value] = categoryItem;
|
|
|
|
// Copy sortKey from old item when "moving"
|
|
|
|
if ( existingCategoryItem ) {
|
|
|
|
categoryItem.sortKey = existingCategoryItem.sortKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
categoryItems.push( categoryItem );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2014-02-14 20:03:42 +00:00
|
|
|
OO.ui.GroupElement.prototype.addItems.call( categoryWidget, categoryItems, index );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2014-02-14 20:03:42 +00:00
|
|
|
categoryWidget.fitInput();
|
|
|
|
} );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove category items.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string[]} names Names of categories to remove
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.removeItems = function ( names ) {
|
2013-05-03 18:30:33 +00:00
|
|
|
var i, len, categoryItem,
|
|
|
|
items = [];
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
for ( i = 0, len = names.length; i < len; i++ ) {
|
2013-05-03 18:30:33 +00:00
|
|
|
categoryItem = this.categories[names[i]];
|
|
|
|
categoryItem.disconnect( this );
|
|
|
|
items.push( categoryItem );
|
2013-04-29 21:01:56 +00:00
|
|
|
delete this.categories[names[i]];
|
|
|
|
}
|
2013-05-03 18:30:33 +00:00
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.GroupElement.prototype.removeItems.call( this, items );
|
2013-05-03 18:30:33 +00:00
|
|
|
|
|
|
|
this.fitInput();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-fit the input.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryWidget.prototype.fitInput = function () {
|
2013-06-12 23:25:28 +00:00
|
|
|
var gap, min, $lastItem,
|
2013-11-01 19:45:59 +00:00
|
|
|
$input = this.input.$element;
|
2013-05-03 18:30:33 +00:00
|
|
|
|
|
|
|
if ( !$input.is( ':visible') ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$input.css( { 'width': 'inherit' } );
|
|
|
|
min = $input.outerWidth();
|
|
|
|
|
|
|
|
$input.css( { 'width': '100%' } );
|
2013-11-01 19:45:59 +00:00
|
|
|
$lastItem = this.$element.find( '.ve-ui-mwCategoryItemWidget:last' );
|
2013-05-03 18:30:33 +00:00
|
|
|
if ( $lastItem.length ) {
|
|
|
|
// Try to fit to the right of the last item
|
|
|
|
gap = ( $input.offset().left + $input.outerWidth() ) -
|
|
|
|
( $lastItem.offset().left + $lastItem.outerWidth() );
|
|
|
|
if ( gap >= min ) {
|
2013-06-12 23:25:28 +00:00
|
|
|
$input.css( { 'width': gap } );
|
2013-05-03 18:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|