mediawiki-extensions-Visual.../modules/ve/dm/nodes/ve.dm.MWReferenceNode.js
Trevor Parscal 8039b1c2f8 Insert Reference
Objective:

* Allow opening reference dialog with arbitrary selection
* Auto-insert reference when selection is not a reference node

Changes:

ve.init.mw.ViewPageTarget.js
* Added reference button to toolbar

ve.init.Target.js
* Add getToolbarSubset so we can exclude the reference button from
  the toolbar in the reference dialog (nested references are not
  allowed).

ve.ui.MWReferenceDialog.js
* Stop storing referenceNode (not needed)
* Only store internalItem on open if there's a focused node that's a
  reference
* Use wrapper paragraph when creating a new reference
* Create new reference on dialog close if required

ve.dm.InternalList.js
* Major rewrite to support key less references.
* Add new method for creating a transaction to insert a new iternal
  item. Also returns the index of the new item to be passed to the
  reference node.

Change-Id: I839ae165c299248484ce93d4ab087318a95fbb94
2013-06-10 20:14:08 +01:00

169 lines
4.8 KiB
JavaScript

/*!
* 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 );
// Event handlers
this.connect( this, {
'root': 'onRoot',
'unroot': 'onUnroot'
} );
};
/* Inheritance */
ve.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode );
/* Static members */
ve.dm.MWReferenceNode.static.name = 'mwReference';
ve.dm.MWReferenceNode.static.matchTagNames = null;
ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ];
ve.dm.MWReferenceNode.static.isContent = true;
ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
var dataElement,
about = domElements[0].getAttribute( 'about' ),
mw = JSON.parse( domElements[0].getAttribute( 'data-mw' ) || '{}' ),
body = mw.body ? mw.body.html : '',
refGroup = mw.attrs.group || '',
listGroup = this.name + '/' + refGroup,
listKey = mw.attrs && mw.attrs.name !== undefined ? mw.attrs.name : null,
queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body ),
listIndex = queueResult.index,
contentsUsed = ( body !== '' && queueResult.isNew );
dataElement = {
'type': this.name,
'attributes': {
'mw': mw,
'about': about,
'listIndex': listIndex,
'listGroup': listGroup,
'listKey': listKey,
'refGroup': refGroup,
'contentsUsed': contentsUsed
}
};
return dataElement;
};
ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) {
var itemNodeHtml, mwAttr, i, iLen, keyedNodes, setContents,
span = doc.createElement( 'span' ),
itemNodeWrapper = doc.createElement( 'div' ),
itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex ),
itemNodeRange = itemNode.getRange();
span.setAttribute( 'about', dataElement.attributes.about );
span.setAttribute( 'typeof', 'mw:Extension/ref' );
mwAttr = ve.copyObject( dataElement.attributes.mw ) || {};
setContents = dataElement.attributes.contentsUsed;
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
keyedNodes = converter.internalList
.getNodeGroup( dataElement.attributes.listGroup )
.keyedNodes[dataElement.attributes.listKey];
// Check that this the first reference with its key
if ( dataElement === keyedNodes[0].element ) {
setContents = true;
// Check no other reference originally defined the contents
// 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 ) {
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 );
}
span.setAttribute( 'data-mw', JSON.stringify( mwAttr ) );
return [ span ];
};
ve.dm.MWReferenceNode.static.remapInternalListIndexes = function ( dataElement, mapping ) {
dataElement.attributes.listIndex = mapping[dataElement.attributes.listIndex];
};
/* Methods */
/**
* Gets the internal item node associated with this node
* @method
* @returns {ve.dm.InternalItemNode} Item node
*/
ve.dm.MWReferenceNode.prototype.getInternalItem = function () {
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(
this.element.attributes.listGroup,
this.element.attributes.listKey,
this.element.attributes.listIndex,
this
);
}
};
/**
* Handle the node being detatched from the root
* @method
*/
ve.dm.MWReferenceNode.prototype.onUnroot = function () {
this.getDocument().getInternalList().removeNode(
this.element.attributes.listGroup,
this.element.attributes.listKey,
this.element.attributes.listIndex,
this
);
};
ve.dm.MWReferenceNode.prototype.getClonedElement = function () {
var clone = ve.dm.LeafNode.prototype.getClonedElement.call( this );
delete clone.element.attributes.contentsUsed;
return clone;
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.MWReferenceNode );