mediawiki-extensions-Visual.../modules/ve-mw/dm/models/ve.dm.MWReferenceModel.js

281 lines
7.4 KiB
JavaScript
Raw Normal View History

Ultra-mega-hyper-citation editing on crack Objectives: * Allow users on-wiki to create tools and dialogs for citation templates of their choosing * Allow editing of citation templates directly, without having to go through the reference dialog * Provide citation template tools within reference editing that use the same titles and icons as the citation tools do, but don't wrap the inserted content in a ref tag Changes: * Reference list was cloning the DOM element it was inserting into its view before the generated content node could finish rendering, so it never ended up showing the finished rendering in the reference list * Documenting hack about use of reference list node's destroy method, and how we are depending on destroy not canceling generated content rendering * Introduced reference model * Added saving/updating method to transclusion model * Added getPartsList method to dm transclusion node, which caches the result and invalidates the cache on update * Added citation dialog, which extends transclusion dialog * Added cite group to toolbars, cite-template in reference dialog toolbar * Factored out getting the node to edit and saving changes procedures in transclusion dialog so they could be extended in citation dialog * Updated uses of autoAdd as per changes in oojs-ui (Ic353f91) * Renamed MWDialogTool file since there was only one tool in it * Expanded TransclusionDialogTool file out since there is now more logic to it * Switched to using ve.dm.MWReferenceModel instead of plain objects in reference search widget Configuration: If you add to MediaWiki:Visualeditor-cite-tool-definition.json the following code you will magically be presented with a delightful array of citation options: [ { "name": "web", "icon": "ref-cite-web", "template": "Cite web" }, { "name": "book", "icon": "ref-cite-book", "template": "Cite book" }, { "name": "news", "icon": "ref-cite-news", "template": "Cite news" }, { "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" } ] ...or... [ { "name": "any-name", "icon": "any-ooui-icon", "template": "Any template", "title": "Any title text" } ] The title text is derived either from the title property or from the name property by pre-pending the string 'visualeditor-cite-tool-name-' to generate a message key. Titles for 'web', 'book', 'news' and 'journal' are provided. The icon is a normal oo-ui-icon name, and more icons can be added, as usual, by adding a class called .oo-ui-icon-{icon name} to MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news' and 'ref-cite-journal' are provided. The template name is simply the name of the template without its namespace prefix. Depends on Ic353f91 in oojs-ui Bug: 50110 Bug: 50768 Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-21 18:56:46 +00:00
/*!
* VisualEditor DataModel MWReferenceModel class.
*
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* MediaWiki reference model.
*
* @class
* @mixins OO.EventEmitter
*
* @constructor
*/
ve.dm.MWReferenceModel = function VeDmMWReferenceModel() {
// Mixin constructors
OO.EventEmitter.call( this );
// Properties
this.listKey = '';
this.listGroup = '';
this.listIndex = null;
this.group = '';
this.doc = null;
};
/* Inheritance */
OO.mixinClass( ve.dm.MWReferenceModel, OO.EventEmitter );
/* Static Methods */
/**
* Create a reference model from a reference internal item.
*
* @param {ve.dm.MWReferenceNode} node Reference node
* @return {ve.dm.MWReferenceModel} Reference model
*/
ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) {
var doc = node.getDocument(),
internalList = doc.getInternalList(),
attr = node.getAttributes(),
ref = new ve.dm.MWReferenceModel();
ref.setListKey( attr.listKey );
ref.setListGroup( attr.listGroup );
ref.setListIndex( attr.listIndex );
ref.setGroup( attr.refGroup );
ref.setDocument( doc.cloneFromRange( internalList.getItemNode( attr.listIndex ).getRange() ) );
return ref;
};
/* Methods */
/**
* Find matching item in a surface.
*
* @param {ve.dm.Surface} surfaceModel Surface reference is in
* @returns {ve.dm.InternalItemNode|null} Internal reference item, null if none exists
*/
ve.dm.MWReferenceModel.prototype.findInternalItem = function ( surfaceModel ) {
if ( this.listIndex !== null ) {
return surfaceModel.getDocument().getInternalList().getItemNode( this.listIndex );
}
return null;
};
/**
* Insert reference internal item into a surface.
*
* If the internal item for this reference doesn't exist, use this method to create one.
* The inserted reference is empty and auto-numbered.
*
* @param {ve.dm.Surface} surfaceModel Surface model of main document
*/
ve.dm.MWReferenceModel.prototype.insertInternalItem = function ( surfaceModel ) {
// Create new internal item
var item,
doc = surfaceModel.getDocument(),
internalList = doc.getInternalList();
// Fill in data
this.setListKey( 'auto/' + internalList.getNextUniqueNumber() );
this.setListGroup( 'mwReference/' + this.refGroup );
// Insert internal reference item into document
item = internalList.getItemInsertion( this.listGroup, this.listKey, [] );
surfaceModel.change( item.transaction );
this.setListIndex( item.index );
// Inject reference document into internal reference item
surfaceModel.change(
ve.dm.Transaction.newFromDocumentInsertion(
doc,
internalList.getItemNode( item.index ).getRange().start,
this.getDocument()
)
);
};
/**
* Update an internal reference item.
*
* An internal item for the reference will be created if no `ref` argument is given.
*
* @param {ve.dm.Surface} surfaceModel Surface model of main document
*/
ve.dm.MWReferenceModel.prototype.updateInternalItem = function ( surfaceModel ) {
var i, len, txs, group, refNodes, keyIndex, itemNodeRange,
doc = surfaceModel.getDocument(),
internalList = doc.getInternalList(),
listGroup = 'mwReference/' + this.group;
// Group/key has changed
if ( this.listGroup !== listGroup ) {
// Get all reference nodes with the same group and key
group = internalList.getNodeGroup( this.listGroup );
refNodes = group.keyedNodes[this.listKey] ?
group.keyedNodes[this.listKey].slice() :
[ group.firstNodes[this.listIndex] ];
// Check for name collision when moving items between groups
keyIndex = internalList.getKeyIndex( this.listGroup, this.listKey );
if ( keyIndex !== undefined ) {
// Resolve name collision by generating a new list key
this.listKey = 'auto/' + internalList.getNextUniqueNumber();
}
// Update the group name of all references nodes with the same group and key
txs = [];
for ( i = 0, len = refNodes.length; i < len; i++ ) {
// HACK: Removing and re-inserting nodes to/from the internal list is done
// because internal list doesn't yet support attribute changes
refNodes[i].removeFromInternalList();
txs.push( ve.dm.Transaction.newFromAttributeChanges(
doc,
refNodes[i].getOuterRange().start,
{ 'refGroup': this.group, 'listGroup': listGroup }
) );
}
surfaceModel.change( txs );
// HACK: Same as above, internal list issues
for ( i = 0, len = refNodes.length; i < len; i++ ) {
refNodes[i].addToInternalList();
}
this.listGroup = listGroup;
}
// Update internal node content
itemNodeRange = internalList.getItemNode( this.listIndex ).getRange();
surfaceModel.change( ve.dm.Transaction.newFromRemoval( doc, itemNodeRange, true ) );
surfaceModel.change(
ve.dm.Transaction.newFromDocumentInsertion( doc, itemNodeRange.start, this.doc )
);
};
/**
* Insert reference into a surface.
*
* Reference is inserted at the current cursor position in `surfaceModel`.
*
* @param {ve.dm.Surface} surfaceModel Surface model of main document
* @param {ve.Range} [at] Location to insert at
*/
ve.dm.MWReferenceModel.prototype.insertReferenceNode = function ( surfaceModel, at ) {
surfaceModel
.getFragment( at || surfaceModel.getSelection().clone(), true )
.collapseRangeToEnd()
.insertContent( [
{
'type': 'mwReference',
'attributes': {
'listKey': this.listKey,
'listGroup': this.listGroup,
'listIndex': this.listIndex,
'refGroup': this.refGroup
}
},
{ 'type': '/mwReference' }
] );
};
/**
* Get key of reference in list.
*
* @returns {string} Reference list key
*/
ve.dm.MWReferenceModel.prototype.getListKey = function () {
return this.listKey;
};
/**
* Get name of group reference list is in.
*
* @returns {string} Reference list group
*/
ve.dm.MWReferenceModel.prototype.getListGroup = function () {
return this.listGroup;
};
/**
* Get index of reference in list.
*
* @returns {string} Reference list group
*/
ve.dm.MWReferenceModel.prototype.getListIndex = function () {
return this.listIndex;
};
/**
* Get name of group reference is in.
*
* @returns {string} Reference group
*/
ve.dm.MWReferenceModel.prototype.getGroup = function () {
return this.group;
};
/**
* Get reference document.
*
* Auto-generates a blank document if no document exists.
*
* @returns {ve.dm.Document} Reference document
*/
ve.dm.MWReferenceModel.prototype.getDocument = function () {
if ( !this.doc ) {
this.doc = new ve.dm.Document( [
{ 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } },
{ 'type': '/paragraph' },
{ 'type': 'internalList' },
{ 'type': '/internalList' }
] );
}
return this.doc;
};
/**
* Set key of reference in list.
*
* @param {string} Reference list key
*/
ve.dm.MWReferenceModel.prototype.setListKey = function ( listKey ) {
this.listKey = listKey;
};
/**
* Set name of group reference list is in.
*
* @param {string} Reference list group
*/
ve.dm.MWReferenceModel.prototype.setListGroup = function ( listGroup ) {
this.listGroup = listGroup;
};
/**
* Set index of reference in list.
*
* @param {string} Reference list group
*/
ve.dm.MWReferenceModel.prototype.setListIndex = function ( listIndex ) {
this.listIndex = listIndex;
};
/**
* Set name of group reference is in.
*
* @param {string} group Reference group
*/
ve.dm.MWReferenceModel.prototype.setGroup = function ( group ) {
this.group = group;
};
/**
* Set reference document.
*
* @param {ve.dm.Document} Reference document
*/
ve.dm.MWReferenceModel.prototype.setDocument = function ( doc ) {
this.doc = doc;
};