2013-02-21 23:01:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MetaItem class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel meta item.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2013-04-02 18:28:42 +00:00
|
|
|
* @extends ve.dm.Model
|
2013-10-15 11:58:04 +00:00
|
|
|
* @mixins OO.EventEmitter
|
2013-04-02 19:33:22 +00:00
|
|
|
*
|
2013-02-21 23:01:04 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} element Reference to element in meta-linmod
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem = function VeDmMetaItem( element ) {
|
|
|
|
// Parent constructor
|
2013-04-02 18:28:42 +00:00
|
|
|
ve.dm.Model.call( this, element );
|
|
|
|
// Mixin
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.EventEmitter.call( this );
|
2013-02-21 23:01:04 +00:00
|
|
|
|
|
|
|
// Properties
|
2013-03-15 04:07:23 +00:00
|
|
|
this.list = null;
|
|
|
|
this.offset = null;
|
|
|
|
this.index = null;
|
2013-04-06 16:45:26 +00:00
|
|
|
this.move = null;
|
2013-02-21 23:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MetaItem, ve.dm.Model );
|
2013-02-21 23:01:04 +00:00
|
|
|
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.mixinClass( ve.dm.MetaItem, OO.EventEmitter );
|
2013-02-21 23:01:04 +00:00
|
|
|
|
2013-04-02 18:28:42 +00:00
|
|
|
/* Static members */
|
2013-02-21 23:01:04 +00:00
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
/**
|
|
|
|
* Symbolic name for the group this meta item type will be grouped in in ve.dm.MetaList.
|
|
|
|
*
|
|
|
|
* @static
|
2013-11-19 08:32:37 +00:00
|
|
|
* @property
|
2013-03-15 04:07:23 +00:00
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.static.group = 'misc';
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2013-03-21 01:35:38 +00:00
|
|
|
/**
|
|
|
|
* Remove this item from the document. Only works if the item is attached to a MetaList.
|
|
|
|
* @throws {Error} Cannot remove detached item
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.remove = function () {
|
|
|
|
if ( !this.list ) {
|
|
|
|
throw new Error( 'Cannot remove detached item' );
|
|
|
|
}
|
|
|
|
this.list.removeMeta( this );
|
|
|
|
};
|
|
|
|
|
2013-05-03 22:17:35 +00:00
|
|
|
/**
|
|
|
|
* Replace item with another in-place.
|
|
|
|
*
|
2013-11-18 08:37:05 +00:00
|
|
|
* Pass a plain object rather than a MetaItem into this function unless you know what you're doing.
|
|
|
|
*
|
|
|
|
* @param {Object|ve.dm.MetaItem} item Item to replace this item with
|
2013-05-03 22:17:35 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.replaceWith = function ( item ) {
|
|
|
|
var offset = this.getOffset(),
|
|
|
|
index = this.getIndex(),
|
|
|
|
list = this.list;
|
|
|
|
|
|
|
|
list.removeMeta( this );
|
|
|
|
list.insertMeta( item, offset, index );
|
|
|
|
};
|
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
/**
|
|
|
|
* Get the group this meta item belongs to.
|
2013-11-19 08:32:37 +00:00
|
|
|
* @see #static-group
|
2013-03-15 04:07:23 +00:00
|
|
|
* @returns {string} Group
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.getGroup = function () {
|
|
|
|
return this.constructor.static.group;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the MetaList this item is attached to.
|
|
|
|
* @returns {ve.dm.MetaList|null} Reference to the parent list, or null if not attached
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.getParentList = function () {
|
|
|
|
return this.list;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this item's offset in the linear model.
|
|
|
|
*
|
|
|
|
* This is only known if the item is attached to a MetaList.
|
|
|
|
*
|
|
|
|
* @returns {number|null} Offset, or null if not attached
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.getOffset = function () {
|
|
|
|
return this.offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this item's index in the metadata array at the offset.
|
|
|
|
*
|
|
|
|
* This is only known if the item is attached to a MetaList.
|
|
|
|
*
|
|
|
|
* @returns {number|null} Index, or null if not attached
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.getIndex = function () {
|
|
|
|
return this.index;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the offset. This is used by the parent list to synchronize the item with the document state.
|
|
|
|
* @param {number} offset New offset
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.setOffset = function ( offset ) {
|
|
|
|
this.offset = offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the index. This is used by the parent list to synchronize the item with the document state.
|
|
|
|
* @param {number} index New index
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.setIndex = function ( index ) {
|
|
|
|
this.index = index;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach this item to a MetaList.
|
|
|
|
* @param {ve.dm.MetaList} list Parent list to attach to
|
|
|
|
* @param {number} offset Offset of this item in the parent list's document
|
|
|
|
* @param {number} index Index of this item in the metadata array at the offset
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.attach = function ( list, offset, index ) {
|
|
|
|
this.list = list;
|
|
|
|
this.offset = offset;
|
|
|
|
this.index = index;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach this item from its parent list.
|
|
|
|
*
|
|
|
|
* This clears the stored offset and index, unless the item has already been attached to another list.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MetaList} list List to detach from
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.detach = function ( list ) {
|
|
|
|
if ( this.list === list ) {
|
|
|
|
this.list = null;
|
|
|
|
this.offset = null;
|
|
|
|
this.index = null;
|
|
|
|
}
|
|
|
|
};
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether this item is attached to a MetaList.
|
|
|
|
* @returns {boolean} Whether item is attached
|
|
|
|
*/
|
|
|
|
ve.dm.MetaItem.prototype.isAttached = function () {
|
|
|
|
return this.list !== null;
|
|
|
|
};
|