mediawiki-extensions-Visual.../modules/ve-mw/ui/dialogs/ve.ui.MWCitationDialog.js
Trevor Parscal 926700fbca Re-split the transclusion dialog
We used to have two dialogs; a complex one, and a subclass that dumbed it
down - this was rubbish.

Then we merged them together - this was a'ight.

Now we have two dialogs again; a simple one, and a subclass that fancies
it up a bit - this is wicked keen broham.

Change-Id: I4b2ba31bed5c4f80940623702d635cacd19e0a66
2014-04-17 01:39:18 +00:00

155 lines
4.3 KiB
JavaScript

/*
* VisualEditor user interface MWCitationDialog class.
*
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Dialog for inserting and editing MediaWiki transclusions.
*
* @class
* @extends ve.ui.MWTemplateDialog
*
* @constructor
* @param {Object} [config] Configuration options
*/
ve.ui.MWCitationDialog = function VeUiMWCitationDialog( config ) {
// Parent constructor
ve.ui.MWCitationDialog.super.call( this, config );
// Properties
this.referenceModel = null;
this.referenceNode = null;
};
/* Inheritance */
OO.inheritClass( ve.ui.MWCitationDialog, ve.ui.MWTemplateDialog );
/* Static Properties */
ve.ui.MWCitationDialog.static.name = 'citation';
ve.ui.MWCitationDialog.static.icon = 'reference';
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWCitationDialog.prototype.getApplyButtonLabel = function () {
return ve.msg(
this.inserting ?
'visualeditor-dialog-citation-insert-citation' :
'visualeditor-dialog-action-apply'
);
};
/**
* Get the reference node to be edited.
*
* @returns {ve.dm.MWReferenceNode|null} Reference node to be edited, null if none exists
*/
ve.ui.MWCitationDialog.prototype.getReferenceNode = function () {
var focusedNode = this.getFragment().getSelectedNode();
return focusedNode instanceof ve.dm.MWReferenceNode ? focusedNode : null;
};
/**
* @inheritdoc
*/
ve.ui.MWCitationDialog.prototype.getTransclusionNode = function () {
var branches, leaves, transclusionNode,
referenceNode = this.getReferenceNode();
if ( referenceNode instanceof ve.dm.MWReferenceNode ) {
branches = referenceNode.getInternalItem().getChildren();
leaves = branches &&
branches.length === 1 &&
branches[0].canContainContent() &&
branches[0].getChildren();
transclusionNode = leaves &&
leaves.length === 1 &&
leaves[0] instanceof ve.dm.MWTransclusionNode &&
leaves[0];
}
return transclusionNode || null;
};
/**
* @inheritdoc
*/
ve.ui.MWCitationDialog.prototype.saveChanges = function () {
var item,
surfaceFragment = this.getFragment(),
surfaceModel = surfaceFragment.getSurface(),
doc = surfaceModel.getDocument(),
internalList = doc.getInternalList(),
obj = this.transclusion.getPlainObject();
if ( !this.referenceModel ) {
this.getFragment().collapseRangeToEnd();
this.referenceModel = new ve.dm.MWReferenceModel();
this.referenceModel.insertInternalItem( surfaceModel );
this.referenceModel.insertReferenceNode( surfaceFragment );
}
item = this.referenceModel.findInternalItem( surfaceModel );
if ( item ) {
if ( this.transclusionNode instanceof ve.dm.MWTransclusionNode ) {
this.transclusion.updateTransclusionNode( surfaceModel, this.transclusionNode );
} else if ( obj !== null ) {
this.transclusion.insertTransclusionNode(
// HACK: This is trying to place the cursor inside the first content branch node
// but this theoretically not a safe assumption - in practice, the citation dialog
// will only reach this code if we are inserting (not updating) a transclusion, so
// the referenceModel will have already initialized the internal node with a
// paragraph - getting the range of the item covers the entire paragraph so we have
// to get the range of it's first (and empty) child
surfaceFragment.clone( item.getChildren()[0].getRange() )
);
}
}
// HACK: Scorch the earth - this is only needed because without it, the reference list won't
// re-render properly, and can be removed once someone fixes that
this.referenceModel.setDocument(
doc.cloneFromRange(
internalList.getItemNode( this.referenceModel.getListIndex() ).getRange()
)
);
this.referenceModel.updateInternalItem( surfaceModel );
};
/**
* @inheritdoc
*/
ve.ui.MWCitationDialog.prototype.setup = function ( data ) {
// Parent method
ve.ui.MWCitationDialog.super.prototype.setup.call( this, data );
// Initialization
if ( this.transclusionNode ) {
this.referenceNode = this.getReferenceNode();
if ( this.referenceNode instanceof ve.dm.MWReferenceNode ) {
this.referenceModel = ve.dm.MWReferenceModel.static.newFromReferenceNode(
this.referenceNode
);
}
}
};
/**
* @inheritdoc
*/
ve.ui.MWCitationDialog.prototype.teardown = function ( data ) {
// Parent method
ve.ui.MWCitationDialog.super.prototype.teardown.call( this, data );
// Cleanup
this.referenceModel = null;
this.referenceNode = null;
};