2013-04-17 17:53:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWReferenceNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki reference node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.LeafNode
|
|
|
|
* @constructor
|
|
|
|
* @param {number} [length] Length of content data in document; ignored and overridden to 0
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode = function VeDmMWReferenceNode( length, element ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.dm.LeafNode.call( this, 0, element );
|
2013-05-29 14:46:52 +00:00
|
|
|
|
|
|
|
// Event handlers
|
|
|
|
this.connect( this, {
|
|
|
|
'root': 'onRoot',
|
|
|
|
'unroot': 'onUnroot'
|
|
|
|
} );
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode );
|
|
|
|
|
|
|
|
/* Static members */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWReferenceNode.static.name = 'mwReference';
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.matchTagNames = null;
|
|
|
|
|
2013-06-03 20:22:35 +00:00
|
|
|
ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ];
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.isContent = true;
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
|
2013-06-03 20:22:35 +00:00
|
|
|
var dataElement,
|
2013-04-17 17:53:26 +00:00
|
|
|
about = domElements[0].getAttribute( 'about' ),
|
|
|
|
mw = JSON.parse( domElements[0].getAttribute( 'data-mw' ) || '{}' ),
|
2013-06-03 20:22:35 +00:00
|
|
|
body = mw.body ? mw.body.html : '',
|
|
|
|
refGroup = mw.attrs.group || '',
|
|
|
|
listGroup = this.name + '/' + refGroup,
|
2013-06-07 16:58:34 +00:00
|
|
|
listKey = mw.attrs && mw.attrs.name !== undefined ? mw.attrs.name : null,
|
2013-06-06 15:42:55 +00:00
|
|
|
queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body ),
|
|
|
|
listIndex = queueResult.index,
|
|
|
|
contentsUsed = ( body !== '' && queueResult.isNew );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
dataElement = {
|
|
|
|
'type': this.name,
|
|
|
|
'attributes': {
|
|
|
|
'mw': mw,
|
|
|
|
'about': about,
|
2013-05-29 14:46:52 +00:00
|
|
|
'listIndex': listIndex,
|
|
|
|
'listGroup': listGroup,
|
2013-06-03 20:22:35 +00:00
|
|
|
'listKey': listKey,
|
2013-06-06 15:42:55 +00:00
|
|
|
'refGroup': refGroup,
|
|
|
|
'contentsUsed': contentsUsed
|
2013-04-17 17:53:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return dataElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
2013-06-07 16:58:34 +00:00
|
|
|
var itemNodeHtml, mwAttr, i, iLen, keyedNodes, setContents,
|
2013-04-17 17:53:26 +00:00
|
|
|
span = doc.createElement( 'span' ),
|
|
|
|
itemNodeWrapper = doc.createElement( 'div' ),
|
|
|
|
itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex ),
|
|
|
|
itemNodeRange = itemNode.getRange();
|
|
|
|
|
|
|
|
span.setAttribute( 'about', dataElement.attributes.about );
|
2013-06-03 20:22:35 +00:00
|
|
|
span.setAttribute( 'typeof', 'mw:Extension/ref' );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-06-06 15:42:55 +00:00
|
|
|
mwAttr = ve.copyObject( dataElement.attributes.mw ) || {};
|
|
|
|
|
2013-06-10 22:01:17 +00:00
|
|
|
setContents = dataElement.attributes.contentsUsed ||
|
|
|
|
dataElement.attributes.listKey === null;
|
2013-06-06 15:42:55 +00:00
|
|
|
|
|
|
|
if ( !setContents ) {
|
|
|
|
// Check if any other nodes with this key provided content. If not
|
|
|
|
// then we attach the contents to the first reference with this key
|
2013-06-07 16:58:34 +00:00
|
|
|
keyedNodes = converter.internalList
|
2013-06-06 15:42:55 +00:00
|
|
|
.getNodeGroup( dataElement.attributes.listGroup )
|
2013-06-07 16:58:34 +00:00
|
|
|
.keyedNodes[dataElement.attributes.listKey];
|
2013-06-06 15:42:55 +00:00
|
|
|
// Check that this the first reference with its key
|
2013-06-07 16:58:34 +00:00
|
|
|
if ( dataElement === keyedNodes[0].element ) {
|
2013-06-06 15:42:55 +00:00
|
|
|
setContents = true;
|
|
|
|
// Check no other reference originally defined the contents
|
2013-06-07 16:58:34 +00:00
|
|
|
// As this is keyedNodes[0] we can start at 1
|
|
|
|
for ( i = 1, iLen = keyedNodes.length; i < iLen; i++ ) {
|
|
|
|
if ( keyedNodes[i].element.attributes.contentsUsed ) {
|
2013-06-06 15:42:55 +00:00
|
|
|
setContents = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( setContents ) {
|
|
|
|
converter.getDomSubtreeFromData(
|
|
|
|
itemNode.getDocument().getData().slice( itemNodeRange.start, itemNodeRange.end ),
|
|
|
|
itemNodeWrapper
|
|
|
|
),
|
|
|
|
itemNodeHtml = $( itemNodeWrapper ).html();
|
|
|
|
ve.setProp( mwAttr, 'body', 'html', itemNodeHtml );
|
|
|
|
}
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-05-26 12:34:47 +00:00
|
|
|
span.setAttribute( 'data-mw', JSON.stringify( mwAttr ) );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
return [ span ];
|
|
|
|
};
|
|
|
|
|
2013-05-22 14:58:21 +00:00
|
|
|
ve.dm.MWReferenceNode.static.remapInternalListIndexes = function ( dataElement, mapping ) {
|
|
|
|
dataElement.attributes.listIndex = mapping[dataElement.attributes.listIndex];
|
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the internal item node associated with this node
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.InternalItemNode} Item node
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getInternalItem = function () {
|
2013-05-29 14:46:52 +00:00
|
|
|
return this.getDocument().getInternalList().getItemNode( this.getAttribute( 'listIndex' ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the node being attached to the root
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.onRoot = function () {
|
|
|
|
if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
|
|
|
|
this.getDocument().getInternalList().addNode(
|
2013-06-07 16:58:34 +00:00
|
|
|
this.element.attributes.listGroup,
|
|
|
|
this.element.attributes.listKey,
|
|
|
|
this.element.attributes.listIndex,
|
|
|
|
this
|
2013-05-29 14:46:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the node being detatched from the root
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.onUnroot = function () {
|
|
|
|
this.getDocument().getInternalList().removeNode(
|
2013-06-07 16:58:34 +00:00
|
|
|
this.element.attributes.listGroup,
|
|
|
|
this.element.attributes.listKey,
|
|
|
|
this.element.attributes.listIndex,
|
|
|
|
this
|
2013-05-29 14:46:52 +00:00
|
|
|
);
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
2013-06-06 15:42:55 +00:00
|
|
|
ve.dm.MWReferenceNode.prototype.getClonedElement = function () {
|
|
|
|
var clone = ve.dm.LeafNode.prototype.getClonedElement.call( this );
|
|
|
|
delete clone.element.attributes.contentsUsed;
|
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferenceNode );
|