2013-04-17 17:53:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel InternalList class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel meta item.
|
|
|
|
*
|
|
|
|
* @class
|
2013-05-01 22:21:32 +00:00
|
|
|
* @mixins ve.EventEmitter
|
|
|
|
*
|
2013-04-17 17:53:26 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.Document} doc Document model
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList = function VeDmInternalList( doc ) {
|
2013-05-01 22:21:32 +00:00
|
|
|
// Mixin constructors
|
2013-04-17 17:53:26 +00:00
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.document = doc;
|
2013-05-29 14:46:52 +00:00
|
|
|
this.itemHtmlQueue = [];
|
2013-04-17 17:53:26 +00:00
|
|
|
this.listNode = null;
|
2013-05-29 14:46:52 +00:00
|
|
|
this.nodes = {};
|
2013-06-03 20:22:35 +00:00
|
|
|
this.groupsChanged = [];
|
2013-06-07 16:58:34 +00:00
|
|
|
this.keyIndexes = {};
|
|
|
|
this.keys = [];
|
2013-09-19 22:57:08 +00:00
|
|
|
this.nextUniqueNumber = 0;
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
// Event handlers
|
2013-07-01 20:02:10 +00:00
|
|
|
if ( doc ) {
|
|
|
|
doc.connect( this, { 'transact': 'onTransact' } );
|
|
|
|
}
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-05-01 22:21:32 +00:00
|
|
|
ve.mixinClass( ve.dm.InternalList, ve.EventEmitter );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-06-03 20:22:35 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event update
|
|
|
|
* @param {string[]} groupsChanged List of groups changed since the last transaction
|
|
|
|
*/
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2013-05-28 13:07:46 +00:00
|
|
|
* Queues up an item's html for parsing later.
|
2013-04-17 17:53:26 +00:00
|
|
|
*
|
2013-06-06 15:42:55 +00:00
|
|
|
* If an item with the specified group and key already exists it will be ignored, unless
|
|
|
|
* the data already stored is an empty string.
|
2013-04-17 17:53:26 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-05-29 14:46:52 +00:00
|
|
|
* @param {string} groupName Item group
|
2013-04-17 17:53:26 +00:00
|
|
|
* @param {string} key Item key
|
2013-05-28 13:07:46 +00:00
|
|
|
* @param {string} html Item contents
|
2013-06-06 15:42:55 +00:00
|
|
|
* @returns {Object} Object containing index of the item in the index-value store
|
|
|
|
* (and also its index in the internal list node), and a flag indicating if it is a new item.
|
2013-04-17 17:53:26 +00:00
|
|
|
*/
|
2013-05-29 14:46:52 +00:00
|
|
|
ve.dm.InternalList.prototype.queueItemHtml = function ( groupName, key, html ) {
|
2013-06-07 16:58:34 +00:00
|
|
|
var isNew = false,
|
|
|
|
index = this.getKeyIndex( groupName, key );
|
|
|
|
|
|
|
|
if ( index === undefined ) {
|
|
|
|
index = this.itemHtmlQueue.length;
|
2013-08-27 20:51:48 +00:00
|
|
|
this.keyIndexes[groupName + '/' + key] = index;
|
2013-06-07 16:58:34 +00:00
|
|
|
this.itemHtmlQueue.push( html );
|
2013-06-06 15:42:55 +00:00
|
|
|
isNew = true;
|
2013-06-07 16:58:34 +00:00
|
|
|
} else if ( this.itemHtmlQueue[index] === '' ) {
|
|
|
|
// Previous value with this key was empty, overwrite value in queue
|
|
|
|
this.itemHtmlQueue[index] = html;
|
2013-06-06 15:42:55 +00:00
|
|
|
isNew = true;
|
2013-04-17 17:53:26 +00:00
|
|
|
}
|
2013-06-06 15:42:55 +00:00
|
|
|
return {
|
|
|
|
'index': index,
|
|
|
|
'isNew': isNew
|
|
|
|
};
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the item's HTML strings
|
|
|
|
* @method
|
|
|
|
* @returns {Object} Name-indexed object containing HTMLElements
|
|
|
|
*/
|
2013-05-29 14:46:52 +00:00
|
|
|
ve.dm.InternalList.prototype.getItemHtmlQueue = function () {
|
2013-06-07 16:58:34 +00:00
|
|
|
return this.itemHtmlQueue;
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the internal list's document model
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.Document} Document model
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getDocument = function () {
|
|
|
|
return this.document;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list node
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.InternalListNode} List node
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getListNode = function () {
|
|
|
|
var i, nodes;
|
|
|
|
// find listNode if not set, or unattached
|
|
|
|
if ( !this.listNode || !this.listNode.doc ) {
|
|
|
|
nodes = this.getDocument().documentNode.children;
|
|
|
|
for ( i = nodes.length; i >= 0; i-- ) {
|
|
|
|
if ( nodes[i] instanceof ve.dm.InternalListNode ) {
|
|
|
|
this.listNode = nodes[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.listNode;
|
|
|
|
};
|
|
|
|
|
2013-06-07 16:58:34 +00:00
|
|
|
/**
|
2013-06-27 23:17:59 +00:00
|
|
|
* Get the number it internal items in the internal list.
|
|
|
|
*
|
2013-06-07 16:58:34 +00:00
|
|
|
* @method
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getItemNodeCount = function () {
|
|
|
|
return this.getListNode().children.length;
|
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/**
|
2013-06-27 23:17:59 +00:00
|
|
|
* Get the item node from a specific index.
|
|
|
|
*
|
2013-04-17 17:53:26 +00:00
|
|
|
* @method
|
2013-06-07 16:58:34 +00:00
|
|
|
* @param {number} index Item index
|
2013-04-17 17:53:26 +00:00
|
|
|
* @returns {ve.dm.InternalItemNode} Item node
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getItemNode = function ( index ) {
|
|
|
|
return this.getListNode().children[index];
|
|
|
|
};
|
|
|
|
|
2013-06-27 23:17:59 +00:00
|
|
|
/**
|
|
|
|
* Get all node groups.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Object} Node groups, keyed by group name
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getNodeGroups = function () {
|
|
|
|
return this.nodes;
|
|
|
|
};
|
|
|
|
|
2013-06-03 20:22:35 +00:00
|
|
|
/**
|
|
|
|
* Get the node group object for a specified group name.
|
2013-06-27 23:17:59 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-06-03 20:22:35 +00:00
|
|
|
* @param {string} groupName Name of the group
|
|
|
|
* @returns {Object} Node group object, containing nodes and key order array
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getNodeGroup = function ( groupName ) {
|
|
|
|
return this.nodes[groupName];
|
|
|
|
};
|
|
|
|
|
2013-06-27 23:17:59 +00:00
|
|
|
/**
|
|
|
|
* Get a unique list key for a given group.
|
|
|
|
*
|
2013-09-19 22:57:08 +00:00
|
|
|
* The returned list key is added to the list of unique list keys used in this group so that it
|
|
|
|
* won't be allocated again. It will also be associated to oldListKey so that if the same oldListKey
|
|
|
|
* is passed in again later, the previously allocated name will be returned.
|
2013-06-27 23:17:59 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} groupName Name of the group
|
2013-09-19 22:57:08 +00:00
|
|
|
* @param {string} oldListKey Current list key to associate the generated list key with
|
|
|
|
* @param {string} prefix Prefix to distinguish generated keys from non-generated ones
|
|
|
|
* @returns {string} Generated unique list key, or existing unique key associated with oldListKey
|
2013-06-27 23:17:59 +00:00
|
|
|
*/
|
2013-09-19 22:57:08 +00:00
|
|
|
ve.dm.InternalList.prototype.getUniqueListKey = function ( groupName, oldListKey, prefix ) {
|
2013-06-27 23:17:59 +00:00
|
|
|
var group = this.getNodeGroup( groupName ),
|
|
|
|
num = 0;
|
|
|
|
|
2013-09-19 22:57:08 +00:00
|
|
|
if ( group.uniqueListKeys[oldListKey] !== undefined ) {
|
|
|
|
return group.uniqueListKeys[oldListKey];
|
2013-06-27 23:17:59 +00:00
|
|
|
}
|
2013-08-27 20:51:48 +00:00
|
|
|
|
2013-09-19 22:57:08 +00:00
|
|
|
while ( group.keyedNodes[prefix + num] || group.uniqueListKeysInUse[prefix + num] ) {
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
group.uniqueListKeys[oldListKey] = prefix + num;
|
|
|
|
group.uniqueListKeysInUse[prefix + num] = true;
|
|
|
|
return prefix + num;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next number in a monotonically increasing series.
|
|
|
|
* @returns {number} One higher than the return value of the previous call, or 0 on the first call
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getNextUniqueNumber = function () {
|
|
|
|
return this.nextUniqueNumber++;
|
2013-06-27 23:17:59 +00:00
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/**
|
2013-05-28 13:07:46 +00:00
|
|
|
* Converts stored item HTML into linear data.
|
2013-04-17 17:53:26 +00:00
|
|
|
*
|
|
|
|
* Each item is an InternalItem, and they are wrapped in an InternalList.
|
|
|
|
* If there are no items an empty array is returned.
|
|
|
|
*
|
2013-05-28 13:07:46 +00:00
|
|
|
* Stored HTML is deleted after conversion.
|
|
|
|
*
|
2013-04-17 17:53:26 +00:00
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Converter} converter Converter object
|
|
|
|
* @returns {Array} Linear model data
|
|
|
|
*/
|
2013-05-28 13:07:46 +00:00
|
|
|
ve.dm.InternalList.prototype.convertToData = function ( converter ) {
|
2013-04-17 17:53:26 +00:00
|
|
|
var i, length, itemData,
|
2013-05-29 14:46:52 +00:00
|
|
|
itemHtmlQueue = this.getItemHtmlQueue(), list = [];
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-06-10 23:05:42 +00:00
|
|
|
list.push( { 'type': 'internalList' } );
|
|
|
|
for ( i = 0, length = itemHtmlQueue.length; i < length; i++ ) {
|
2013-06-13 08:30:13 +00:00
|
|
|
if ( itemHtmlQueue[i] !== '' ) {
|
|
|
|
itemData = converter.getDataFromDomRecursion( $( '<div>' ).html( itemHtmlQueue[i] )[0] );
|
|
|
|
list = list.concat(
|
|
|
|
[{ 'type': 'internalItem' }],
|
|
|
|
itemData,
|
|
|
|
[{ 'type': '/internalItem' }]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
list = list.concat( [ { 'type': 'internalItem' }, { 'type': '/internalItem' } ] );
|
|
|
|
}
|
2013-04-17 17:53:26 +00:00
|
|
|
}
|
2013-06-10 23:05:42 +00:00
|
|
|
list.push( { 'type': '/internalList' } );
|
2013-05-28 13:07:46 +00:00
|
|
|
// After conversion we no longer need the HTML
|
2013-05-29 14:46:52 +00:00
|
|
|
this.itemHtmlQueue = [];
|
2013-04-17 17:53:26 +00:00
|
|
|
return list;
|
2013-05-22 14:48:41 +00:00
|
|
|
};
|
|
|
|
|
2013-06-07 16:58:34 +00:00
|
|
|
/**
|
|
|
|
* Generate a transaction for inserting a new internal item node
|
|
|
|
* @param {string} groupName Item group
|
|
|
|
* @param {string} key Item key
|
|
|
|
* @param {Array} data Linear model data
|
|
|
|
* @returns {Object} Object containing the transaction (or null if none required)
|
|
|
|
* and the new item's index within the list
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getItemInsertion = function ( groupName, key, data ) {
|
|
|
|
var tx, itemData,
|
|
|
|
index = this.getKeyIndex( groupName, key );
|
|
|
|
|
|
|
|
if ( index === undefined ) {
|
|
|
|
index = this.getItemNodeCount();
|
2013-08-27 20:51:48 +00:00
|
|
|
this.keyIndexes[groupName + '/' + key] = index;
|
2013-06-07 16:58:34 +00:00
|
|
|
|
|
|
|
itemData = [{ 'type': 'internalItem' }].concat( data, [{ 'type': '/internalItem' }] );
|
|
|
|
tx = ve.dm.Transaction.newFromInsertion(
|
|
|
|
this.getDocument(),
|
|
|
|
this.getListNode().getRange().end,
|
|
|
|
itemData
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
tx = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'transaction': tx,
|
|
|
|
'index': index
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-06-03 20:22:35 +00:00
|
|
|
/**
|
|
|
|
* Get position of a key within a group
|
|
|
|
* @param {string} groupName Name of the group
|
|
|
|
* @param {string} key Name of the key
|
|
|
|
* @returns {number} Position within the key ordering for that group
|
|
|
|
*/
|
2013-06-07 16:58:34 +00:00
|
|
|
ve.dm.InternalList.prototype.getIndexPosition = function ( groupName, index ) {
|
|
|
|
return ve.indexOf( index, this.nodes[groupName].indexOrder );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the internal item index of a group key if it already exists
|
|
|
|
* @param {string} groupName Item group
|
|
|
|
* @param {string} key Item name
|
|
|
|
* @returns {number|undefined} The index of the group key, or undefined if it doesn't exist yet
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.getKeyIndex = function ( groupName, key ) {
|
2013-08-27 20:51:48 +00:00
|
|
|
return this.keyIndexes[groupName + '/' + key];
|
2013-06-03 20:22:35 +00:00
|
|
|
};
|
|
|
|
|
2013-05-29 14:46:52 +00:00
|
|
|
/**
|
|
|
|
* Add a node.
|
|
|
|
* @method
|
|
|
|
* @param {string} groupName Item group
|
|
|
|
* @param {string} key Item name
|
2013-06-07 16:58:34 +00:00
|
|
|
* @param {number} index Item index
|
2013-05-29 14:46:52 +00:00
|
|
|
* @param {ve.dm.Node} node Item node
|
|
|
|
*/
|
2013-06-07 16:58:34 +00:00
|
|
|
ve.dm.InternalList.prototype.addNode = function ( groupName, key, index, node ) {
|
|
|
|
var i, len, start, keyedNodes, group = this.nodes[groupName];
|
2013-05-29 14:46:52 +00:00
|
|
|
// The group may not exist yet
|
|
|
|
if ( group === undefined ) {
|
|
|
|
group = this.nodes[groupName] = {
|
2013-06-07 16:58:34 +00:00
|
|
|
'keyedNodes': {},
|
|
|
|
'firstNodes': [],
|
2013-09-19 22:57:08 +00:00
|
|
|
'indexOrder': [],
|
|
|
|
'uniqueListKeys': {},
|
|
|
|
'uniqueListKeysInUse': {}
|
2013-05-29 14:46:52 +00:00
|
|
|
};
|
|
|
|
}
|
2013-08-27 20:51:48 +00:00
|
|
|
keyedNodes = group.keyedNodes[key];
|
|
|
|
this.keys[index] = key;
|
|
|
|
// The key may not exist yet
|
|
|
|
if ( keyedNodes === undefined ) {
|
|
|
|
keyedNodes = group.keyedNodes[key] = [];
|
|
|
|
}
|
|
|
|
if ( node.getDocument().buildingNodeTree ) {
|
|
|
|
// If the document is building the original node tree
|
|
|
|
// then every item is being added in order, so we don't
|
|
|
|
// need to worry about sorting.
|
|
|
|
keyedNodes.push( node );
|
|
|
|
if ( keyedNodes.length === 1 ) {
|
|
|
|
group.firstNodes[index] = node;
|
2013-06-07 16:58:34 +00:00
|
|
|
}
|
2013-08-27 20:51:48 +00:00
|
|
|
} else {
|
|
|
|
// TODO: We could use binary search insertion sort
|
|
|
|
start = node.getRange().start;
|
|
|
|
for ( i = 0, len = keyedNodes.length; i < len; i++ ) {
|
|
|
|
if ( start < keyedNodes[i].getRange().start ) {
|
|
|
|
break;
|
2013-05-29 14:46:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 20:51:48 +00:00
|
|
|
// 'i' is now the insertion point, so add the node here
|
|
|
|
keyedNodes.splice( i, 0, node );
|
|
|
|
if ( i === 0 ) {
|
|
|
|
group.firstNodes[index] = node;
|
|
|
|
}
|
2013-06-07 16:58:34 +00:00
|
|
|
}
|
|
|
|
if ( ve.indexOf( index, group.indexOrder ) === -1 ) {
|
|
|
|
group.indexOrder.push( index );
|
2013-06-03 20:22:35 +00:00
|
|
|
}
|
|
|
|
this.markGroupAsChanged( groupName );
|
|
|
|
};
|
2013-05-29 14:46:52 +00:00
|
|
|
|
2013-06-03 20:22:35 +00:00
|
|
|
/**
|
|
|
|
* Mark a node group as having been changed since the last transaction.
|
|
|
|
* @param {string} groupName Name of group which has changed
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.markGroupAsChanged = function ( groupName ) {
|
|
|
|
if ( ve.indexOf( groupName, this.groupsChanged ) === -1 ) {
|
|
|
|
this.groupsChanged.push( groupName );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle document transaction events
|
|
|
|
* @emits update
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.onTransact = function () {
|
|
|
|
var i;
|
|
|
|
if ( this.groupsChanged.length > 0 ) {
|
|
|
|
// length will almost always be 1, so probably better to not cache it
|
|
|
|
for ( i = 0; i < this.groupsChanged.length; i++ ) {
|
2013-06-07 16:58:34 +00:00
|
|
|
this.sortGroupIndexes( this.nodes[this.groupsChanged[i]] );
|
2013-06-03 20:22:35 +00:00
|
|
|
}
|
|
|
|
this.emit( 'update', this.groupsChanged );
|
|
|
|
this.groupsChanged = [];
|
2013-05-29 14:46:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a node.
|
|
|
|
* @method
|
|
|
|
* @param {string} groupName Item group
|
|
|
|
* @param {string} key Item name
|
2013-06-07 16:58:34 +00:00
|
|
|
* @param {number} index Item index
|
2013-05-29 14:46:52 +00:00
|
|
|
* @param {ve.dm.Node} node Item node
|
|
|
|
*/
|
2013-06-07 16:58:34 +00:00
|
|
|
ve.dm.InternalList.prototype.removeNode = function ( groupName, key, index, node ) {
|
|
|
|
var i, len, j, keyedNodes,
|
|
|
|
group = this.nodes[groupName];
|
|
|
|
|
2013-08-27 20:51:48 +00:00
|
|
|
keyedNodes = group.keyedNodes[key];
|
|
|
|
for ( i = 0, len = keyedNodes.length; i < len; i++ ) {
|
|
|
|
if ( keyedNodes[i] === node ) {
|
|
|
|
keyedNodes.splice( i, 1 );
|
|
|
|
if ( i === 0 ) {
|
|
|
|
group.firstNodes[index] = keyedNodes[0];
|
2013-05-29 14:46:52 +00:00
|
|
|
}
|
2013-08-27 20:51:48 +00:00
|
|
|
break;
|
2013-05-29 14:46:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 20:51:48 +00:00
|
|
|
// If the all the items in this key have been removed
|
2013-06-07 16:58:34 +00:00
|
|
|
// then remove this index from indexOrder and firstNodes
|
2013-08-27 20:51:48 +00:00
|
|
|
if ( keyedNodes.length === 0 ) {
|
|
|
|
delete group.keyedNodes[key];
|
2013-06-07 16:58:34 +00:00
|
|
|
delete group.firstNodes[index];
|
|
|
|
j = ve.indexOf( index, group.indexOrder );
|
|
|
|
group.indexOrder.splice( j, 1 );
|
|
|
|
}
|
2013-06-03 20:22:35 +00:00
|
|
|
this.markGroupAsChanged( groupName );
|
2013-05-29 14:46:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-06-07 16:58:34 +00:00
|
|
|
* Sort the indexOrder array within a group object.
|
2013-05-29 14:46:52 +00:00
|
|
|
* @param {Object} group Group object
|
|
|
|
*/
|
2013-06-07 16:58:34 +00:00
|
|
|
ve.dm.InternalList.prototype.sortGroupIndexes = function ( group ) {
|
|
|
|
// Sort indexOrder
|
|
|
|
group.indexOrder.sort( function ( index1, index2 ) {
|
|
|
|
return group.firstNodes[index1].getRange().start - group.firstNodes[index2].getRange().start;
|
2013-05-29 14:46:52 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2013-05-22 14:48:41 +00:00
|
|
|
/**
|
|
|
|
* Clone this internal list.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Document} [doc] The new list's document. Defaults to this list's document.
|
|
|
|
* @returns {ve.dm.InternalList} Clone of this internal
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.clone = function ( doc ) {
|
2013-10-03 21:43:20 +00:00
|
|
|
var clone = new this.constructor( doc || this.getDocument() );
|
|
|
|
// Most properties don't need to be copied, because addNode() will be invoked when the new
|
|
|
|
// document tree is built. But some do need copying:
|
|
|
|
clone.nextUniqueNumber = this.nextUniqueNumber;
|
|
|
|
clone.itemHtmlQueue = ve.copy( this.itemHtmlQueue );
|
|
|
|
return clone;
|
2013-05-22 14:58:21 +00:00
|
|
|
};
|
Introduce newFromDocumentReplace() transaction builder
Replaces newFromNodeReplacement(). newFromNodeReplacement was very
simplistic and didn't support metadata or internal list items, so
if you had comments or references inside of the data you were editing
(reference contents or an image caption), they'd get mangled.
With this, you can do:
newDoc = doc.getDocumentSlice( node );
// Edit newDoc
tx = ve.dm.Transaction.newFromDocumentReplace( doc, node, newDoc );
surface.change( newDoc );
and that takes care of metadata, internal list items, and things like
references that reference internal list items.
ve.dm.Document.js:
* In getDocumentSlice(), store a reference to the original document
and the number of items in its InternalList at the time of slicing
in the created slice. This is used for reconciliation when the
modified slice is injected back into the parent document with
newFromDocumentReplace().
ve.dm.InternalList.js:
* Add a method for merging in another InternalList. This provides a
mapping from old to new InternalList indexes so the linear model data
being injected by newFromDocumentReplace() can have its InternalList
indexes remapped.
ve.dm.Transaction.js:
* Replace newFromNodeReplacement() with newFromDocumentReplace()
ve.ui.MWMediaEditDialog.js, ve.ui.MWReferenceDialog.js:
* Use getDocumentSlice/newFromDocumentReplace for editing captions/refs
* Change insertion code path to insert an empty internalItem/caption, then
newFromDocumentReplace into that
* Add empty internalList to new mini-documents
ve/test/dm/ve.dm.Transaction.test.js:
* Replace newFromNodeReplacement tests with newFromDocumentReplace tests
ve-mw/test/dm/ve.dm.Transaction.test.js (new):
* Add tests for newFromDocumentReplace with mwReference nodes
ve.dm.mwExample.js:
* Add data for newFromDocumentReplace with mwReference tests
VisualEditor.hooks.php:
* Add new test file
Bug: 52102
Change-Id: I4aa980780114b391924f04df588e81c990c32983
2013-09-05 01:05:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge another internal list into this one.
|
|
|
|
*
|
|
|
|
* This function updates the state of this list, and returns a mapping from indexes in list to
|
|
|
|
* indexes in this, as well as a set of ranges that should be copied from list's linear model
|
|
|
|
* into this list's linear model by the caller.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.InternalList} list Internal list to merge into this list
|
|
|
|
* @param {number} commonLength The number of elements, counted from the beginning, that the lists have in common
|
|
|
|
* @returns {Object} 'mapping' is an object mapping indexes in list to indexes in this; newItemRanges is an array
|
|
|
|
* of ranges of internal nodes in list's document that should be copied into our document
|
|
|
|
*/
|
|
|
|
ve.dm.InternalList.prototype.merge = function ( list, commonLength ) {
|
|
|
|
var i, k, key,
|
|
|
|
listLen = list.getItemNodeCount(),
|
|
|
|
nextIndex = this.getItemNodeCount(),
|
|
|
|
newItemRanges = [],
|
|
|
|
mapping = {};
|
|
|
|
for ( i = 0; i < commonLength; i++ ) {
|
|
|
|
mapping[i] = i;
|
|
|
|
}
|
|
|
|
for ( i = commonLength; i < listLen; i++ ) {
|
|
|
|
// Try to find i in list.keyIndexes
|
|
|
|
key = undefined;
|
|
|
|
for ( k in list.keyIndexes ) {
|
|
|
|
if ( list.keyIndexes[k] === i ) {
|
|
|
|
key = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.keyIndexes[key] !== undefined ) {
|
|
|
|
// We already have this key in this internal list. Ignore the duplicate that the other
|
|
|
|
// list is trying to merge in.
|
|
|
|
// NOTE: This case cannot occur in VE currently, but may be possible in the future with
|
|
|
|
// collaborative editing, which is why this code needs to be rewritten before we do
|
|
|
|
// collaborative editing.
|
|
|
|
mapping[i] = this.keyIndexes[key];
|
|
|
|
} else {
|
|
|
|
mapping[i] = nextIndex;
|
|
|
|
if ( key !== undefined ) {
|
|
|
|
this.keyIndexes[key] = nextIndex;
|
|
|
|
}
|
|
|
|
nextIndex++;
|
|
|
|
newItemRanges.push( list.getItemNode( i ).getOuterRange() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
'mapping': mapping,
|
|
|
|
'newItemRanges': newItemRanges
|
|
|
|
};
|
|
|
|
};
|