mediawiki-extensions-Visual.../modules/ve/ui/dialogs/ve.ui.MWReferenceDialog.js
Trevor Parscal 2d044518bd References dialog cleanup
Objectives:

* Remove the whole toolbar subset thing, it's up to the creator of the
  subsurface to know what is and is now allowed, and the commands were
  still passing through unfiltered
* Correctly separate initialization from opening - fix issue where opening
  the reference dialog multiple times will keep adding more and more
  controls

Changes:

ve.init.Target.js
* Remove getToolbarSubset method

ve.ui.MWReferenceDialog.js
* Add toolbar tools and surface commands configs to reference dialog,
  including the things that are safe to use in references
* Move creation of field sets and reused controls to initialize method
  to prevent re-creation each time the dialog is opened
* Move static initialization stuff to the top near the other static stuff

Change-Id: I1c8577d17c506bac76e61b2b036655c59ef5a218
2013-06-12 11:27:49 -07:00

192 lines
5.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( {
// TODO: use message string
'$$': this.frame.$$, 'label': 'Content', 'icon': 'parameter'
} );
this.nameFieldset = new ve.ui.FieldsetLayout( {
// TODO: use message string
'$$': this.frame.$$, 'label': 'Name', 'icon': 'parameter'
} );
this.nameInput = new ve.ui.TextInputWidget( { '$$': this.frame.$$ } );
this.nameLabel = new ve.ui.InputLabelWidget( {
'$$': this.frame.$$,
'input': this.nameInput,
// TODO: use message string
'label': 'Reuse this reference by this name'
} );
// Initialization
this.$body.addClass( 've-ui-mwReferenceDialog-body' );
this.$body.append( this.contentFieldset.$, this.nameFieldset.$ );
this.nameFieldset.$.append( this.nameLabel.$, this.nameInput.$ );
};
/**
* Handle frame ready events.
*
* @method
*/
ve.ui.MWReferenceDialog.prototype.onOpen = function () {
var focusedNode, data,
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 );
} else {
data = [
{ 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } },
{ 'type': '/paragraph' }
];
}
// Properties
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.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, groupName, key, newItem,
txs = [];
// Parent method
ve.ui.Dialog.prototype.onOpen.call( this );
// Save changes
if ( action === 'apply' ) {
data = this.referenceSurface.getModel().getDocument().getData();
doc = this.surface.getModel().getDocument();
if ( this.internalItem ) {
txs.push(
ve.dm.Transaction.newFromNodeReplacement(
doc, this.internalItem.getRange(), data
)
);
this.surface.getModel().change( txs );
} else {
// TODO: pass in group and key from UI if they exist
groupName = '';
key = null;
newItem = doc.getInternalList().getItemInsertion( groupName, key, data );
if ( newItem.transaction ) {
txs.push( newItem.transaction );
}
this.surface.getModel().change( txs );
this.surface.getModel().getFragment().collapseRangeToEnd().insertContent( [
{
'type': 'mwReference',
'attributes': {
'mw': { 'name': 'ref' },
'listIndex': newItem.index,
'listGroup': 'mwReference/' + groupName,
'listKey': key,
'refGroup': groupName
}
},
{ '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 );