Abstract mwExtension behaviour into MWExtensionWindow

Allows us to create an MWExtensionDialog base class that shares
the logic.

Change-Id: I53c8f1713a3513d2635cfd736ec8fc3f9616d864
This commit is contained in:
Ed Sanders 2015-09-05 15:23:59 +01:00
parent ecdcdcc69f
commit 8815d1397f
8 changed files with 368 additions and 154 deletions

View file

@ -13,7 +13,8 @@
{
"name": "User Interface",
"classes": [
"ve.ui.MW*Page"
"ve.ui.MW*Page",
"ve.ui.MW*Window"
]
},
{

View file

@ -13,7 +13,8 @@
{
"name": "User Interface",
"classes": [
"ve.ui.MW*Page"
"ve.ui.MW*Page",
"ve.ui.MW*Window"
]
},
{

View file

@ -952,11 +952,13 @@
"modules/ve-mw/ce/annotations/ve.ce.MWNowikiAnnotation.js",
"modules/ve-mw/ui/ve.ui.MWCommandRegistry.js",
"modules/ve-mw/ui/ve.ui.MWSequenceRegistry.js",
"modules/ve-mw/ui/ve.ui.MWExtensionWindow.js",
"modules/ve-mw/ui/commands/ve.ui.MWWikitextWarningCommand.js",
"modules/ve-mw/ui/datatransferhandlers/ve.ui.MWWikitextStringTransferHandler.js",
"modules/ve-mw/ui/widgets/ve.ui.MWTargetWidget.js",
"modules/ve-mw/ui/widgets/ve.ui.MWTocItemWidget.js",
"modules/ve-mw/ui/widgets/ve.ui.MWTocWidget.js",
"modules/ve-mw/ui/dialogs/ve.ui.MWExtensionDialog.js",
"modules/ve-mw/ui/dialogs/ve.ui.MWSaveDialog.js",
"modules/ve-mw/ui/dialogs/ve.ui.MWBetaWelcomeDialog.js",
"modules/ve-mw/ui/dialogs/ve.ui.MWCommandHelpDialog.js",

View file

@ -0,0 +1,121 @@
/*!
* VisualEditor UserInterface MWExtensionDialog class.
*
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Dialog for editing generic MediaWiki extensions.
*
* @class
* @abstract
* @extends ve.ui.NodeDialog
* @mixins ve.ui.MWExtensionWindow
*
* @constructor
* @param {Object} [config] Configuration options
*/
ve.ui.MWExtensionDialog = function VeUiMWExtensionDialog() {
// Parent constructor
ve.ui.MWExtensionDialog.super.apply( this, arguments );
// Mixin constructors
ve.ui.MWExtensionWindow.call( this );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWExtensionDialog, ve.ui.NodeDialog );
OO.mixinClass( ve.ui.MWExtensionDialog, ve.ui.MWExtensionWindow );
/* Static Properties */
ve.ui.MWExtensionDialog.static.actions = ve.ui.MWExtensionDialog.super.static.actions.concat( [
{
label: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ),
flags: [ 'safe', 'back' ],
modes: [ 'edit', 'insert' ]
},
{
action: 'done',
label: OO.ui.deferMsg( 'visualeditor-dialog-action-done' ),
flags: [ 'progressive', 'primary' ],
modes: 'edit'
},
{
action: 'done',
label: OO.ui.deferMsg( 'visualeditor-dialog-action-insert' ),
flags: [ 'constructive', 'primary' ],
modes: 'insert'
}
] );
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWExtensionDialog.prototype.initialize = function () {
// Parent method
ve.ui.MWExtensionDialog.super.prototype.initialize.call( this );
// Mixin method
ve.ui.MWExtensionWindow.prototype.initialize.call( this );
// Initialization
this.$element.addClass( 've-ui-mwExtensionDialog' );
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionDialog.prototype.getSetupProcess = function ( data ) {
var process;
data = data || {};
// Parent process
process = ve.ui.MWExtensionDialog.super.prototype.getSetupProcess.call( this, data );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getSetupProcess.call( this, data, process ).next( function () {
this.actions.setMode( this.fragment.getSelectedModels().length ? 'edit' : 'insert' );
}, this );
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionDialog.prototype.getReadyProcess = function ( data ) {
var process;
data = data || {};
// Parent process
process = ve.ui.MWExtensionDialog.super.prototype.getReadyProcess.call( this, data );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getReadyProcess.call( this, data, process );
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionDialog.prototype.getTeardownProcess = function ( data ) {
var process;
data = data || {};
// Parent process
process = ve.ui.MWExtensionDialog.super.prototype.getTeardownProcess.call( this, data );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getTeardownProcess.call( this, data, process );
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionDialog.prototype.getActionProcess = function ( action ) {
// Parent process
var process = ve.ui.MWExtensionDialog.super.prototype.getActionProcess.call( this, action );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getActionProcess.call( this, action, process ).next( function () {
if ( action === 'done' ) {
this.close( { action: 'done' } );
}
}, this );
};

View file

@ -11,6 +11,7 @@
* @class
* @abstract
* @extends ve.ui.NodeInspector
* @mixins ve.ui.MWExtensionWindow
*
* @constructor
* @param {Object} [config] Configuration options
@ -18,195 +19,79 @@
ve.ui.MWExtensionInspector = function VeUiMWExtensionInspector() {
// Parent constructor
ve.ui.MWExtensionInspector.super.apply( this, arguments );
// Mixin constructors
ve.ui.MWExtensionWindow.call( this );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWExtensionInspector, ve.ui.NodeInspector );
/* Static properties */
ve.ui.MWExtensionInspector.static.placeholder = null;
/**
* Extension is allowed to have empty contents
*
* @static
* @property {boolean}
* @inheritable
*/
ve.ui.MWExtensionInspector.static.allowedEmpty = false;
/**
* Inspector's directionality, 'ltr' or 'rtl'
*
* Leave as null to use the directionality of the current fragment.
*
* @static
* @property {string|null}
* @inheritable
*/
ve.ui.MWExtensionInspector.static.dir = null;
OO.mixinClass( ve.ui.MWExtensionInspector, ve.ui.MWExtensionWindow );
/* Methods */
/**
* Handle frame ready events.
*
* @method
* @inheritdoc
*/
ve.ui.MWExtensionInspector.prototype.initialize = function () {
// Parent method
ve.ui.MWExtensionInspector.super.prototype.initialize.call( this );
this.input = new ve.ui.WhitespacePreservingTextInputWidget( {
limit: 1,
multiline: true
} );
this.input.$element.addClass( 've-ui-mwExtensionInspector-input' );
// Mixin method
ve.ui.MWExtensionWindow.prototype.initialize.call( this );
// Initialization
this.$element.addClass( 've-ui-mwExtensionInspector' );
this.form.$element.append( this.input.$element );
};
/**
* Get the placeholder text for the content input area.
*
* @return {string} Placeholder text
*/
ve.ui.MWExtensionInspector.prototype.getInputPlaceholder = function () {
return '';
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionInspector.prototype.getSetupProcess = function ( data ) {
var process;
data = data || {};
return ve.ui.MWExtensionInspector.super.prototype.getSetupProcess.call( this, data )
.next( function () {
var dir;
// Initialization
this.whitespace = [ '', '' ];
if ( this.selectedNode ) {
this.input.setValueAndWhitespace( this.selectedNode.getAttribute( 'mw' ).body.extsrc );
} else {
if ( !this.constructor.static.modelClasses[ 0 ].static.isContent ) {
// New nodes should use linebreaks for blocks
this.input.setWhitespace( [ '\n', '\n' ] );
}
this.input.setValue( '' );
}
this.input.$input.attr( 'placeholder', this.getInputPlaceholder() );
dir = this.constructor.static.dir || data.dir;
this.input.setRTL( dir === 'rtl' );
}, this );
// Parent process
process = ve.ui.MWExtensionInspector.super.prototype.getSetupProcess.call( this, data );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getSetupProcess.call( this, data, process );
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionInspector.prototype.getReadyProcess = function ( data ) {
return ve.ui.MWExtensionInspector.super.prototype.getReadyProcess.call( this, data )
.next( function () {
// Focus the input
this.input.focus();
}, this );
var process;
data = data || {};
// Parent process
process = ve.ui.MWExtensionInspector.super.prototype.getReadyProcess.call( this, data );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getReadyProcess.call( this, data, process ).next( function () {
// Focus the input
this.input.focus();
}, this );
};
/**
* @inheritdoc
*/
ve.ui.MWExtensionInspector.prototype.getTeardownProcess = function ( data ) {
return ve.ui.MWExtensionInspector.super.prototype.getTeardownProcess.call( this, data )
.first( function () {
if ( data && data.action === 'done' ) {
if ( this.constructor.static.allowedEmpty || this.input.getInnerValue() !== '' ) {
this.insertOrUpdateNode();
} else if ( this.selectedNode && !this.constructor.static.allowedEmpty ) {
// Content has been emptied on a node which isn't allowed to
// be empty, so delete it.
this.removeNode();
}
}
}, this );
var process;
data = data || {};
// Parent process
process = ve.ui.MWExtensionInspector.super.prototype.getTeardownProcess.call( this, data );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getTeardownProcess.call( this, data, process );
};
/**
* Create an new data element for the model class associated with this inspector
*
* @return {Object} Element data
* @inheritdoc
*/
ve.ui.MWExtensionInspector.prototype.getNewElement = function () {
// Extension inspectors which create elements should either match
// a single modelClass or override this method.
var modelClass = this.constructor.static.modelClasses[ 0 ];
return {
type: modelClass.static.name,
attributes: {
mw: {
name: modelClass.static.extensionName,
attrs: {},
body: {
extsrc: ''
}
}
}
};
};
/**
* Insert or update the node in the document model from the new values
*/
ve.ui.MWExtensionInspector.prototype.insertOrUpdateNode = function () {
var mwData, element,
surfaceModel = this.getFragment().getSurface();
if ( this.selectedNode ) {
mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) );
this.updateMwData( mwData );
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
surfaceModel.getDocument(),
this.selectedNode.getOuterRange().start,
{ mw: mwData }
)
);
} else {
element = this.getNewElement();
this.updateMwData( element.attributes.mw );
// Collapse returns a new fragment, so update this.fragment
this.fragment = this.getFragment().collapseToEnd();
this.getFragment().insertContent( [
element,
{ type: '/' + element.type }
] );
}
};
/**
* Remove the node form the document model
*/
ve.ui.MWExtensionInspector.prototype.removeNode = function () {
this.getFragment().removeContent();
};
/**
* Update mwData object with the new values from the inspector
*
* @param {Object} mwData MediaWiki data object
*/
ve.ui.MWExtensionInspector.prototype.updateMwData = function ( mwData ) {
var tagName = mwData.name,
value = this.input.getValue();
// XML-like tags in wikitext are not actually XML and don't expect their contents to be escaped.
// This means that it is not possible for a tag '<foo>…</foo>' to contain the string '</foo>'.
// Prevent that by escaping the first angle bracket '<' to '&lt;'. (bug 57429)
value = value.replace( new RegExp( '<(/' + tagName + '\\s*>)', 'gi' ), '&lt;$1' );
mwData.body.extsrc = this.whitespace[ 0 ] + value + this.whitespace[ 1 ];
ve.ui.MWExtensionInspector.prototype.getActionProcess = function ( action ) {
// Parent process
var process = ve.ui.MWExtensionInspector.super.prototype.getActionProcess.call( this, action );
// Mixin process
return ve.ui.MWExtensionWindow.prototype.getActionProcess.call( this, action, process );
};

View file

@ -84,6 +84,8 @@ ve.ui.MWLiveExtensionInspector.prototype.insertOrUpdateNode = function () {
*/
ve.ui.MWLiveExtensionInspector.prototype.removeNode = function () {
this.getFragment().getSurface().popStaging();
// Parent method
ve.ui.MWLiveExtensionInspector.super.prototype.removeNode.call( this );
};

View file

@ -4,10 +4,10 @@
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
.ve-ui-mwExtensionInspector-input {
.ve-ui-mwExtensionInspector .ve-ui-mwExtensionWindow-input {
width: 100%;
}
.ve-ui-mwExtensionInspector-input textarea {
.ve-ui-mwExtensionInspector .ve-ui-mwExtensionWindow-input textarea {
height: 8em;
}

View file

@ -0,0 +1,202 @@
/*!
* VisualEditor UserInterface MWExtensionWindow class.
*
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Mixin for windows for editing generic MediaWiki extensions.
*
* @class
* @abstract
*
* @constructor
* @param {Object} [config] Configuration options
*/
ve.ui.MWExtensionWindow = function VeUiMWExtensionWindow() {
this.whitespace = null;
this.input = null;
};
/* Inheritance */
OO.initClass( ve.ui.MWExtensionWindow );
/* Static properties */
/**
* Extension is allowed to have empty contents
*
* @static
* @property {boolean}
* @inheritable
*/
ve.ui.MWExtensionWindow.static.allowedEmpty = false;
/**
* Inspector's directionality, 'ltr' or 'rtl'
*
* Leave as null to use the directionality of the current fragment.
*
* @static
* @property {string|null}
* @inheritable
*/
ve.ui.MWExtensionWindow.static.dir = null;
/* Methods */
/**
* @inheritdoc OO.ui.Window
*/
ve.ui.MWExtensionWindow.prototype.initialize = function () {
this.input = new ve.ui.WhitespacePreservingTextInputWidget( {
limit: 1,
multiline: true
} );
this.input.$element.addClass( 've-ui-mwExtensionWindow-input' );
};
/**
* Get the placeholder text for the content input area.
*
* @return {string} Placeholder text
*/
ve.ui.MWExtensionWindow.prototype.getInputPlaceholder = function () {
return '';
};
/**
* @inheritdoc OO.ui.Window
*/
ve.ui.MWExtensionWindow.prototype.getSetupProcess = function ( data, process ) {
data = data || {};
return process.next( function () {
var dir;
// Initialization
this.whitespace = [ '', '' ];
if ( this.selectedNode ) {
this.input.setValueAndWhitespace( this.selectedNode.getAttribute( 'mw' ).body.extsrc );
} else {
if ( !this.constructor.static.modelClasses[ 0 ].static.isContent ) {
// New nodes should use linebreaks for blocks
this.input.setWhitespace( [ '\n', '\n' ] );
}
this.input.setValue( '' );
}
this.input.$input.attr( 'placeholder', this.getInputPlaceholder() );
dir = this.constructor.static.dir || data.dir;
this.input.setRTL( dir === 'rtl' );
}, this );
};
/**
* @inheritdoc OO.ui.Window
*/
ve.ui.MWExtensionWindow.prototype.getReadyProcess = function ( data, process ) {
return process;
};
/**
* @inheritdoc OO.ui.Window
*/
ve.ui.MWExtensionWindow.prototype.getTeardownProcess = function ( data, process ) {
return process;
};
/**
* @inheritdoc OO.ui.Dialog
*/
ve.ui.MWExtensionWindow.prototype.getActionProcess = function ( action, process ) {
return process.first( function () {
if ( action === 'done' ) {
if ( this.constructor.static.allowedEmpty || this.input.getInnerValue() !== '' ) {
this.insertOrUpdateNode();
} else if ( this.selectedNode && !this.constructor.static.allowedEmpty ) {
// Content has been emptied on a node which isn't allowed to
// be empty, so delete it.
this.removeNode();
}
}
}, this );
};
/**
* Create an new data element for the model class associated with this inspector
*
* @return {Object} Element data
*/
ve.ui.MWExtensionWindow.prototype.getNewElement = function () {
// Extension inspectors which create elements should either match
// a single modelClass or override this method.
var modelClass = this.constructor.static.modelClasses[ 0 ];
return {
type: modelClass.static.name,
attributes: {
mw: {
name: modelClass.static.extensionName,
attrs: {},
body: {
extsrc: ''
}
}
}
};
};
/**
* Insert or update the node in the document model from the new values
*/
ve.ui.MWExtensionWindow.prototype.insertOrUpdateNode = function () {
var mwData, element,
surfaceModel = this.getFragment().getSurface();
if ( this.selectedNode ) {
mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) );
this.updateMwData( mwData );
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
surfaceModel.getDocument(),
this.selectedNode.getOuterRange().start,
{ mw: mwData }
)
);
} else {
element = this.getNewElement();
this.updateMwData( element.attributes.mw );
// Collapse returns a new fragment, so update this.fragment
this.fragment = this.getFragment().collapseToEnd();
this.getFragment().insertContent( [
element,
{ type: '/' + element.type }
] );
}
};
/**
* Remove the node form the document model
*/
ve.ui.MWExtensionWindow.prototype.removeNode = function () {
this.getFragment().removeContent();
};
/**
* Update mwData object with the new values from the inspector
*
* @param {Object} mwData MediaWiki data object
*/
ve.ui.MWExtensionWindow.prototype.updateMwData = function ( mwData ) {
var tagName = mwData.name,
value = this.input.getValue();
// XML-like tags in wikitext are not actually XML and don't expect their contents to be escaped.
// This means that it is not possible for a tag '<foo>…</foo>' to contain the string '</foo>'.
// Prevent that by escaping the first angle bracket '<' to '&lt;'. (bug 57429)
value = value.replace( new RegExp( '<(/' + tagName + '\\s*>)', 'gi' ), '&lt;$1' );
mwData.body.extsrc = this.whitespace[ 0 ] + value + this.whitespace[ 1 ];
};