All files / src/dm ve.dm.MetaItem.js

88.46% Statements 23/26
75% Branches 3/4
71.43% Functions 5/7
88.46% Lines 23/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                    1x   306x   306x   306x         1x   1x       1x   1x   1x                 1x                 1x                 1x 1x     1x                   1x 1x                 1x                 1x 96x               1x 18x 10x                 1x      
/*!
 * VisualEditor DataModel MetaItem class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * DataModel meta item.
 * TODO: rename to MetaNode to reflect the fact it is now a node
 *
 * @class
 * @abstract
 * @extends ve.dm.LeafNode
 * @mixins OO.EventEmitter
 *
 * @constructor
 * @param {Object} element Reference to element in meta-linmod
 */
ve.dm.MetaItem = function VeDmMetaItem() {
	// Parent constructor
	ve.dm.MetaItem.super.apply( this, arguments );
	// Mixin
	OO.EventEmitter.call( this );
	// Properties
	this.list = null;
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.MetaItem, ve.dm.LeafNode );
 
OO.mixinClass( ve.dm.MetaItem, OO.EventEmitter );
 
/* Static members */
 
ve.dm.MetaItem.static.isContent = false;
 
ve.dm.MetaItem.static.isMetaData = true;
 
ve.dm.MetaItem.static.canSerializeAsContent = true;
 
/**
 * Symbolic name for the group this meta item type will be grouped in in ve.dm.MetaList.
 *
 * @static
 * @property {string}
 * @inheritable
 */
ve.dm.MetaItem.static.group = 'misc';
 
/**
 * If the metaitem can be removed by regular remove operations
 *
 * @static
 * @property {boolean}
 * @inheritable
 */
ve.dm.MetaItem.static.removable = false;
 
/* Methods */
 
/**
 * 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 () {
	Iif ( !this.list ) {
		throw new Error( 'Cannot remove detached item' );
	}
	this.list.removeMeta( this );
};
 
/**
 * Replace item with another in-place.
 *
 * 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
 */
ve.dm.MetaItem.prototype.replaceWith = function ( item ) {
	this.list.replaceMeta( this, item );
};
 
/**
 * Get the group this meta item belongs to.
 *
 * @see #static-group
 * @return {string} Group
 */
ve.dm.MetaItem.prototype.getGroup = function () {
	return this.constructor.static.group;
};
 
/**
 * Attach this item to a MetaList.
 *
 * @param {ve.dm.MetaList} list Parent list to attach to
 */
ve.dm.MetaItem.prototype.attachToMetaList = function ( list ) {
	this.list = list;
};
 
/**
 * Detach this item from its parent list.
 *
 * @param {ve.dm.MetaList} list List to detach from
 */
ve.dm.MetaItem.prototype.detachFromMetaList = function ( list ) {
	if ( this.list === list ) {
		this.list = null;
	}
};
 
/**
 * Check whether this item is attached to a MetaList.
 *
 * @return {boolean} Whether item is attached
 */
ve.dm.MetaItem.prototype.isAttached = function () {
	return this.list !== null;
};