mediawiki-extensions-Visual.../modules/ve/ui/dialogs/ve.ui.MWReferenceDialog.js
Trevor Parscal fc8c46dd74 Reference name and group editing
Objective:

* Allow editing reference groups and names in the reference dialog

Bonus:

* Modify attribute transaction builder to support multiple attribute
  changes in a single transaction

Changes:

ve.ui.MWReferenceDialog.js
* Load ref name and group from model
* Save ref name and group, if changed, to model

ve.ui.ListAction.js, ve.ui.Transaction.test.js, ve.ce.ResizableNode.js
* Update use of newFromAttributeChange to newFromAttributeChanges

ve.dm.SurfaceFragment.test.js
* Add test for new changeAttributes method

ve.dm.InternalList.js
* Missing new line at end of file

ve.dm.Transaction.js
* Change newFromAttributeChange to accept an list of attribute changes and
  produce a single transaction that applies one or more attribute changes
  at once

ve.dm.SurfaceFragment.js
* Fix bug in getCoveredNodes where the wrong mode name was being used
* Add changeAttributes method, which applies attributes to all covered
  nodes and allows filtering of which types of nodes the attributes are
  applied to

ve.dm.MWReferenceNode.js
* Actually write key and group back to DOM
* Separate onRoot functionality into addToInternalList so it can be called
  separately (similarly onUnroot/removeFromInternalList)

ve.ce.MWReferenceListNode.js
* Clone internal item CE node before appending to avoid rendering bug.

*.php
* Add links to messages and sort them

Change-Id: Ic4121e4fcfc09265d5863af6f078cdeb77926c8e
2013-06-14 15:29:56 -07:00

237 lines
7.1 KiB
JavaScript

/*!
* VisualEditor user interface MWReferenceDialog class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Document dialog.
*
* @class
* @extends ve.ui.Dialog
*
* @constructor
* @param {ve.ui.Surface} surface
* @param {Object} [config] Config options
*/
ve.ui.MWReferenceDialog = function VeUiMWReferenceDialog( surface, config ) {
// Parent constructor
ve.ui.Dialog.call( this, surface, config );
// Properties
this.internalItem = null;
};
/* Inheritance */
ve.inheritClass( ve.ui.MWReferenceDialog, ve.ui.Dialog );
/* Static Properties */
ve.ui.MWReferenceDialog.static.titleMessage = 'visualeditor-dialog-reference-title';
ve.ui.MWReferenceDialog.static.icon = 'reference';
ve.ui.MWReferenceDialog.static.modelClasses = [ ve.dm.MWReferenceNode ];
ve.ui.MWReferenceDialog.static.toolbarTools = [
{ 'items': ['undo', 'redo'] },
{ 'items': ['mwFormat'] },
{ 'items': ['bold', 'italic', 'mwLink', 'clear', 'mwMediaInsert'] }
];
ve.ui.MWReferenceDialog.static.surfaceCommands = [
'bold', 'italic', 'mwLink', 'undo', 'redo'
];
/* Static Initialization */
ve.ui.MWReferenceDialog.static.addLocalStylesheets( [
've.ce.Node.css',
've.ce.Surface.css',
've.ui.Surface.css',
've.ui.Context.css',
've.ui.Tool.css',
've.ui.Toolbar.css'
] );
/* Methods */
/**
* Handle frame ready events.
*
* @method
*/
ve.ui.MWReferenceDialog.prototype.initialize = function () {
// Call parent method
ve.ui.Dialog.prototype.initialize.call( this );
// Properties
this.contentFieldset = new ve.ui.FieldsetLayout( {
'$$': this.frame.$$,
'label': ve.msg( 'visualeditor-dialog-reference-content-section' ),
'icon': 'reference'
} );
this.optionsFieldset = new ve.ui.FieldsetLayout( {
'$$': this.frame.$$,
'label': ve.msg( 'visualeditor-dialog-reference-options-section' ),
'icon': 'settings'
} );
this.nameInput = new ve.ui.TextInputWidget( { '$$': this.frame.$$ } );
this.nameLabel = new ve.ui.InputLabelWidget( {
'$$': this.frame.$$,
'input': this.nameInput,
'label': ve.msg( 'visualeditor-dialog-reference-options-name-label' )
} );
this.groupInput = new ve.ui.TextInputWidget( { '$$': this.frame.$$ } );
this.groupLabel = new ve.ui.InputLabelWidget( {
'$$': this.frame.$$,
'input': this.groupInput,
'label': ve.msg( 'visualeditor-dialog-reference-options-group-label' )
} );
// Initialization
this.$body.addClass( 've-ui-mwReferenceDialog-body' );
this.$body.append( this.contentFieldset.$, this.optionsFieldset.$ );
this.optionsFieldset.$.append(
this.nameLabel.$, this.nameInput.$, this.groupLabel.$, this.groupInput.$
);
};
/**
* Handle frame ready events.
*
* @method
*/
ve.ui.MWReferenceDialog.prototype.onOpen = function () {
var focusedNode, data, refGroup, listKey,
doc = this.surface.getModel().getDocument();
// Parent method
ve.ui.Dialog.prototype.onOpen.call( this );
// Get reference content
focusedNode = this.surface.getView().getFocusedNode();
if ( focusedNode instanceof ve.ce.MWReferenceNode ) {
this.internalItem = focusedNode.getModel().getInternalItem();
data = doc.getData( this.internalItem.getRange(), true );
listKey = focusedNode.getModel().getAttribute( 'listKey' );
refGroup = focusedNode.getModel().getAttribute( 'refGroup' );
} else {
data = [
{ 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } },
{ 'type': '/paragraph' }
];
}
this.referenceSurface = new ve.ui.Surface(
new ve.dm.ElementLinearData( doc.getStore(), data ), { '$$': this.frame.$$ }
);
this.referenceToolbar = new ve.ui.Toolbar( this.referenceSurface, { '$$': this.frame.$$ } );
// Initialization
this.nameInput.setValue( listKey );
this.groupInput.setValue( refGroup );
this.referenceToolbar.$.addClass( 've-ui-mwReferenceDialog-toolbar' );
this.contentFieldset.$.append( this.referenceToolbar.$, this.referenceSurface.$ );
this.referenceToolbar.addTools( this.constructor.static.toolbarTools );
this.referenceSurface.addCommands( this.constructor.static.surfaceCommands );
this.referenceSurface.initialize();
this.referenceSurface.view.documentView.documentNode.$.focus();
};
/**
* Handle frame ready events.
*
* @method
* @param {string} action Action that caused the window to be closed
*/
ve.ui.MWReferenceDialog.prototype.onClose = function ( action ) {
var data, doc, listIndex, listGroup, listKey, refGroup, newItem, refNode, oldListGroup,
oldListKey, oldNodes, internalList, attrChanges,
surfaceModel = this.surface.getModel();
// Parent method
ve.ui.Dialog.prototype.onOpen.call( this );
// Save changes
if ( action === 'apply' ) {
data = this.referenceSurface.getModel().getDocument().getData();
doc = surfaceModel.getDocument();
listKey = this.nameInput.getValue() !== '' ? this.nameInput.getValue() : null;
refGroup = this.groupInput.getValue();
listGroup = 'mwReference/' + refGroup;
if ( this.internalItem ) {
// Edit reference: handle various replacement cases
refNode = this.surface.getView().getFocusedNode().getModel();
oldListGroup = refNode.getAttribute( 'listGroup' );
oldListKey = refNode.getAttribute( 'listKey' );
// Group/key has changed
if ( listGroup !== oldListGroup || listKey !== oldListKey ) {
internalList = doc.getInternalList();
oldNodes = internalList.getNodeGroup( oldListGroup ).keyedNodes[oldListKey] || [];
listIndex = internalList.getKeyIndex( listGroup, listKey );
attrChanges = {};
if ( listIndex !== undefined ) {
// If the new key exists, reuse its internal node
attrChanges.listIndex = listIndex;
} else if ( oldNodes.length > 1 ) {
// If the old internal node was being shared, create a new one
newItem = internalList.getItemInsertion( listGroup, listKey, data );
attrChanges.listIndex = newItem.index;
}
attrChanges.listGroup = listGroup;
attrChanges.listKey = listKey;
attrChanges.refGroup = refGroup;
// Manually re-register the node before and after change
refNode.removeFromInternalList();
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
doc, refNode.getOuterRange().start, attrChanges
)
);
refNode.addToInternalList();
}
// Process the internal node create/edit transaction
if ( !newItem ) {
surfaceModel.change(
ve.dm.Transaction.newFromNodeReplacement( doc, this.internalItem, data )
);
} else {
surfaceModel.change( newItem.transaction );
}
} else {
// Create reference: just create new nodes
newItem = doc.getInternalList().getItemInsertion( listGroup, listKey, data );
surfaceModel.change( newItem.transaction );
surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
{
'type': 'mwReference',
'attributes': {
'mw': { 'name': 'ref' },
'listIndex': newItem.index,
'listGroup': listGroup,
'listKey': listKey,
'refGroup': refGroup
}
},
{ 'type': '/mwReference' }
] );
}
}
// Cleanup
this.internalItem = null;
this.referenceSurface.destroy();
this.referenceToolbar.destroy();
};
/* Registration */
ve.ui.dialogFactory.register( 'mwReference', ve.ui.MWReferenceDialog );
ve.ui.viewRegistry.register( 'mwReference', ve.ui.MWReferenceDialog );