2013-12-02 20:10:55 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor user interface MWCategoriesPage class.
|
|
|
|
*
|
2017-01-03 16:58:33 +00:00
|
|
|
* @copyright 2011-2017 VisualEditor Team and others; see AUTHORS.txt
|
2013-12-02 20:10:55 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki meta dialog categories page.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.PageLayout
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {string} name Unique symbolic name of page
|
|
|
|
* @param {Object} [config] Configuration options
|
2014-10-14 22:48:24 +00:00
|
|
|
* @cfg {jQuery} [$overlay] Overlay to render dropdowns in
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2014-04-04 17:42:13 +00:00
|
|
|
ve.ui.MWCategoriesPage = function VeUiMWCategoriesPage( name, config ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
// Configuration initialization
|
2014-02-15 01:37:32 +00:00
|
|
|
config = config || {};
|
2013-12-02 20:10:55 +00:00
|
|
|
|
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWCategoriesPage.super.apply( this, arguments );
|
2013-12-02 20:10:55 +00:00
|
|
|
|
|
|
|
// Properties
|
2014-04-04 17:42:13 +00:00
|
|
|
this.metaList = null;
|
2013-12-02 20:10:55 +00:00
|
|
|
this.defaultSortKeyTouched = false;
|
|
|
|
this.fallbackDefaultSortKey = mw.config.get( 'wgTitle' );
|
|
|
|
this.categoriesFieldset = new OO.ui.FieldsetLayout( {
|
2014-08-22 20:50:48 +00:00
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-data-label' ),
|
|
|
|
icon: 'tag'
|
2013-12-02 20:10:55 +00:00
|
|
|
} );
|
|
|
|
this.categoryOptionsFieldset = new OO.ui.FieldsetLayout( {
|
2014-08-22 20:50:48 +00:00
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-options' ),
|
|
|
|
icon: 'settings'
|
2013-12-02 20:10:55 +00:00
|
|
|
} );
|
|
|
|
this.categoryWidget = new ve.ui.MWCategoryWidget( {
|
2015-04-09 23:47:15 +00:00
|
|
|
$overlay: config.$overlay
|
2013-12-02 20:10:55 +00:00
|
|
|
} );
|
|
|
|
this.defaultSortInput = new OO.ui.TextInputWidget( {
|
2015-04-09 23:47:15 +00:00
|
|
|
placeholder: this.fallbackDefaultSortKey
|
2013-12-02 20:10:55 +00:00
|
|
|
} );
|
2014-03-17 18:30:47 +00:00
|
|
|
|
|
|
|
this.defaultSortInput.$element.addClass( 've-ui-mwCategoriesPage-defaultsort' );
|
|
|
|
|
2014-01-22 20:13:59 +00:00
|
|
|
this.defaultSort = new OO.ui.FieldLayout(
|
|
|
|
this.defaultSortInput,
|
|
|
|
{
|
2014-08-22 20:50:48 +00:00
|
|
|
align: 'top',
|
2014-09-15 01:18:03 +00:00
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-defaultsort-label' ),
|
|
|
|
help: ve.msg( 'visualeditor-dialog-meta-categories-defaultsort-help' )
|
2014-01-22 20:13:59 +00:00
|
|
|
}
|
|
|
|
);
|
2013-12-02 20:10:55 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.categoryWidget.connect( this, {
|
2014-08-22 20:50:48 +00:00
|
|
|
newCategory: 'onNewCategory',
|
|
|
|
updateSortkey: 'onUpdateSortKey'
|
2013-12-02 20:10:55 +00:00
|
|
|
} );
|
|
|
|
this.defaultSortInput.connect( this, {
|
2014-08-22 20:50:48 +00:00
|
|
|
change: 'onDefaultSortChange'
|
2013-12-02 20:10:55 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.categoriesFieldset.$element.append( this.categoryWidget.$element );
|
2014-01-22 20:13:59 +00:00
|
|
|
this.categoryOptionsFieldset.addItems( [ this.defaultSort ] );
|
2013-12-02 20:10:55 +00:00
|
|
|
this.$element.append( this.categoriesFieldset.$element, this.categoryOptionsFieldset.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWCategoriesPage, OO.ui.PageLayout );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2014-02-15 01:37:32 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWCategoriesPage.prototype.setOutlineItem = function () {
|
2014-02-15 01:37:32 +00:00
|
|
|
// Parent method
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWCategoriesPage.super.prototype.setOutlineItem.apply( this, arguments );
|
2014-02-15 01:37:32 +00:00
|
|
|
|
|
|
|
if ( this.outlineItem ) {
|
|
|
|
this.outlineItem
|
|
|
|
.setIcon( 'tag' )
|
|
|
|
.setLabel( ve.msg( 'visualeditor-dialog-meta-categories-section' ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
/**
|
|
|
|
* Handle category default sort change events.
|
|
|
|
*
|
|
|
|
* @param {string} value Default sort value
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.onDefaultSortChange = function ( value ) {
|
|
|
|
this.categoryWidget.setDefaultSortKey( value === '' ? this.fallbackDefaultSortKey : value );
|
|
|
|
this.defaultSortKeyTouched = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts new category into meta list
|
|
|
|
*
|
|
|
|
* @param {Object} item
|
2014-11-14 20:44:55 +00:00
|
|
|
* @param {ve.dm.MWCategoryMetaItem} [beforeMetaItem] Meta item to insert before,
|
|
|
|
* or undefined to go at the end
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2014-11-14 20:44:55 +00:00
|
|
|
ve.ui.MWCategoriesPage.prototype.onNewCategory = function ( item, beforeMetaItem ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
// Insert new metaList item
|
2014-11-14 20:44:55 +00:00
|
|
|
if ( beforeMetaItem ) {
|
|
|
|
this.insertMetaListItem(
|
|
|
|
this.getCategoryItemForInsertion( item ),
|
|
|
|
beforeMetaItem.getOffset(),
|
|
|
|
beforeMetaItem.getIndex()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.insertMetaListItem( this.getCategoryItemForInsertion( item ) );
|
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes and re-inserts updated category widget item
|
|
|
|
*
|
|
|
|
* @param {Object} item
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.onUpdateSortKey = function ( item ) {
|
|
|
|
// Replace meta item with updated one
|
|
|
|
item.metaItem.replaceWith( this.getCategoryItemForInsertion( item, item.metaItem.getElement() ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bound to MetaList insert event for adding meta dialog components.
|
|
|
|
*
|
2014-02-20 20:36:52 +00:00
|
|
|
* @param {ve.dm.MetaItem} metaItem
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.onMetaListInsert = function ( metaItem ) {
|
|
|
|
// Responsible for adding UI components
|
|
|
|
if ( metaItem.element.type === 'mwCategory' ) {
|
|
|
|
this.categoryWidget.addItems(
|
|
|
|
[ this.getCategoryItemFromMetaListItem( metaItem ) ],
|
|
|
|
this.metaList.findItem( metaItem.getOffset(), metaItem.getIndex(), 'mwCategory' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bound to MetaList insert event for removing meta dialog components.
|
|
|
|
*
|
2014-02-20 20:36:52 +00:00
|
|
|
* @param {ve.dm.MetaItem} metaItem
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.onMetaListRemove = function ( metaItem ) {
|
|
|
|
var item;
|
|
|
|
|
|
|
|
if ( metaItem.element.type === 'mwCategory' ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
item = this.categoryWidget.categories[ this.getCategoryItemFromMetaListItem( metaItem ).value ];
|
2015-07-22 22:13:09 +00:00
|
|
|
this.categoryWidget.removeItems( [ item ] );
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default sort key item.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Default sort key item
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.getDefaultSortKeyItem = function () {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.metaList.getItemsInGroup( 'mwDefaultSort' )[ 0 ] || null;
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array of category items from meta list
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object[]} items
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.getCategoryItems = function () {
|
|
|
|
var i,
|
|
|
|
items = [],
|
|
|
|
categories = this.metaList.getItemsInGroup( 'mwCategory' );
|
|
|
|
|
|
|
|
// Loop through MwCategories and build out items
|
|
|
|
for ( i = 0; i < categories.length; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
items.push( this.getCategoryItemFromMetaListItem( categories[ i ] ) );
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets category item from meta list item
|
|
|
|
*
|
2014-02-20 20:36:52 +00:00
|
|
|
* @param {ve.dm.MWCategoryMetaItem} metaItem
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object} item
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.getCategoryItemFromMetaListItem = function ( metaItem ) {
|
|
|
|
var title = mw.Title.newFromText( metaItem.element.attributes.category ),
|
|
|
|
value = title ? title.getMainText() : '';
|
|
|
|
|
|
|
|
return {
|
2014-08-22 20:50:48 +00:00
|
|
|
name: metaItem.element.attributes.category,
|
|
|
|
value: value,
|
2013-12-02 20:10:55 +00:00
|
|
|
// TODO: sortkey is lcase, make consistent throughout CategoryWidget
|
2014-08-22 20:50:48 +00:00
|
|
|
sortKey: metaItem.element.attributes.sortkey,
|
|
|
|
metaItem: metaItem
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get metaList like object to insert from item
|
|
|
|
*
|
|
|
|
* @param {Object} item category widget item
|
|
|
|
* @param {Object} [oldData] Metadata object that was previously associated with this item, if any
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object} metaBase
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.getCategoryItemForInsertion = function ( item, oldData ) {
|
|
|
|
var newData = {
|
2014-08-22 20:50:48 +00:00
|
|
|
attributes: { category: item.name, sortkey: item.sortKey || '' },
|
|
|
|
type: 'mwCategory'
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
if ( oldData ) {
|
|
|
|
return ve.extendObject( {}, oldData, newData );
|
|
|
|
}
|
|
|
|
return newData;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a meta list item
|
|
|
|
*
|
|
|
|
* @param {Object} metaBase meta list insert object
|
2014-11-14 20:44:55 +00:00
|
|
|
* @param {number} [offset] Offset of the meta items within the document
|
|
|
|
* @param {number} [index] Index of the meta item within the group of meta items at this offset
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2014-11-14 20:44:55 +00:00
|
|
|
ve.ui.MWCategoriesPage.prototype.insertMetaListItem = function ( metaBase, offset, index ) {
|
|
|
|
this.metaList.insertMeta( metaBase, offset, index );
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-04-04 17:42:13 +00:00
|
|
|
* Setup categories page.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MetaList} metaList Meta list
|
|
|
|
* @param {Object} [data] Dialog setup data
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2014-04-04 17:42:13 +00:00
|
|
|
ve.ui.MWCategoriesPage.prototype.setup = function ( metaList ) {
|
2014-12-16 21:14:01 +00:00
|
|
|
var defaultSortKeyItem,
|
|
|
|
page = this;
|
|
|
|
|
2014-04-04 17:42:13 +00:00
|
|
|
this.metaList = metaList;
|
|
|
|
this.metaList.connect( this, {
|
2014-08-22 20:50:48 +00:00
|
|
|
insert: 'onMetaListInsert',
|
|
|
|
remove: 'onMetaListRemove'
|
2014-04-04 17:42:13 +00:00
|
|
|
} );
|
|
|
|
|
2014-12-16 21:14:01 +00:00
|
|
|
defaultSortKeyItem = this.getDefaultSortKeyItem();
|
2013-12-02 20:10:55 +00:00
|
|
|
|
2014-04-04 17:42:13 +00:00
|
|
|
this.categoryWidget.addItems( this.getCategoryItems() );
|
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
this.defaultSortInput.setValue(
|
|
|
|
defaultSortKeyItem ? defaultSortKeyItem.getAttribute( 'content' ) : ''
|
|
|
|
);
|
|
|
|
this.defaultSortKeyTouched = false;
|
|
|
|
|
2016-02-18 17:09:53 +00:00
|
|
|
// Update input position after transition
|
2014-07-08 22:33:32 +00:00
|
|
|
setTimeout( function () {
|
2014-12-16 21:14:01 +00:00
|
|
|
page.categoryWidget.fitInput();
|
2016-07-12 12:28:13 +00:00
|
|
|
}, OO.ui.theme.getDialogTransitionDuration() );
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
2016-02-19 17:34:41 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoriesPage.prototype.focus = function () {
|
|
|
|
this.categoryWidget.focus();
|
|
|
|
};
|
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
/**
|
|
|
|
* Tear down the page. This is called when the MWMetaDialog is torn down.
|
2015-07-30 23:21:52 +00:00
|
|
|
*
|
|
|
|
* @param {Object} [data] Dialog tear down data
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2015-07-30 23:21:52 +00:00
|
|
|
ve.ui.MWCategoriesPage.prototype.teardown = function ( data ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
var currentDefaultSortKeyItem = this.getDefaultSortKeyItem(),
|
|
|
|
newDefaultSortKey = this.defaultSortInput.getValue(),
|
|
|
|
newDefaultSortKeyData = {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: 'mwDefaultSort',
|
|
|
|
attributes: { content: newDefaultSortKey }
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
2016-07-02 16:38:53 +00:00
|
|
|
if ( data && data.action === 'apply' ) {
|
|
|
|
// Alter the default sort key iff it's been touched & is actually different
|
|
|
|
if ( this.defaultSortKeyTouched ) {
|
|
|
|
if ( newDefaultSortKey === '' ) {
|
|
|
|
if ( currentDefaultSortKeyItem ) {
|
|
|
|
currentDefaultSortKeyItem.remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( !currentDefaultSortKeyItem ) {
|
|
|
|
this.metaList.insertMeta( newDefaultSortKeyData );
|
|
|
|
} else if ( currentDefaultSortKeyItem.getAttribute( 'content' ) !== newDefaultSortKey ) {
|
|
|
|
currentDefaultSortKeyItem.replaceWith(
|
|
|
|
ve.extendObject( true, {},
|
|
|
|
currentDefaultSortKeyItem.getElement(),
|
|
|
|
newDefaultSortKeyData
|
|
|
|
) );
|
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-04 17:42:13 +00:00
|
|
|
|
|
|
|
this.categoryWidget.clearItems();
|
|
|
|
this.metaList.disconnect( this );
|
|
|
|
this.metaList = null;
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|