mediawiki-extensions-Visual.../modules/ve-mw/ui/inspectors/ve.ui.MWExtensionInspector.js
Ed Sanders 671c7d3b25 Create base MWExtensionNode for simple extension support
MWMath and other simple extensions all behave in a similar way, e.g.
<tagname>Foreign syntax</tagname>.

This creates a base class that should make supporting such extensions,
and editing their contents in a plain text box, very simple.

Change-Id: Icc0acb33fe32704f71dacb552d9dfa3142eaef2b
2013-08-07 17:39:31 +08:00

119 lines
2.7 KiB
JavaScript

/*!
* VisualEditor UserInterface MWExtensionInspector class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* MediaWiki extension inspector.
*
* @class
* @abstract
* @extends ve.ui.Inspector
*
* @constructor
* @param {ve.ui.Surface} surface
* @param {Object} [config] Config options
*/
ve.ui.MWExtensionInspector = function VeUiMWExtensionInspector( surface, config ) {
// Parent constructor
ve.ui.Inspector.call( this, surface, config );
};
/* Inheritance */
ve.inheritClass( ve.ui.MWExtensionInspector, ve.ui.Inspector );
/* Static properties */
ve.ui.MWExtensionInspector.static.nodeView = null;
ve.ui.MWExtensionInspector.static.nodeModel = null;
/* Methods */
/**
* Handle frame ready events.
*
* @method
*/
ve.ui.MWExtensionInspector.prototype.initialize = function () {
// Parent method
ve.ui.Inspector.prototype.initialize.call( this );
this.input = new ve.ui.TextInputWidget( {
'$$': this.frame.$$,
'overlay': this.surface.$localOverlay,
'multiline': true
} );
this.input.$.addClass( 've-ui-mwExtensionInspector-input' );
// Initialization
this.$form.append( this.input.$ );
};
/**
* Handle the inspector being opened.
*/
ve.ui.MWExtensionInspector.prototype.onOpen = function () {
var extsrc = '';
// Parent method
ve.ui.Inspector.prototype.onOpen.call( this );
this.node = this.surface.getView().getFocusedNode();
if ( this.node ) {
extsrc = this.node.getModel().getAttribute( 'mw' ).body.extsrc;
}
// Wait for animation to complete
setTimeout( ve.bind( function () {
// Setup input text
this.input.setValue( extsrc );
this.input.$input.focus().select();
}, this ), 200 );
};
/**
* Handle the inspector being closed.
*
* @param {string} action Action that caused the window to be closed
*/
ve.ui.MWExtensionInspector.prototype.onClose = function ( action ) {
var mw,
surfaceModel = this.surface.getModel();
// Parent method
ve.ui.Inspector.prototype.onClose.call( this, action );
if ( this.node instanceof this.constructor.static.nodeView ) {
mw = this.node.getModel().getAttribute( 'mw' );
mw.body.extsrc = this.input.getValue();
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
surfaceModel.getDocument(), this.node.getOuterRange().start, { 'mw': mw }
)
);
} else {
mw = {
'name': this.constructor.static.nodeModel.static.extensionName,
'attrs': {},
'body': {
'extsrc': this.input.getValue()
}
};
surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
{
'type': this.constructor.static.nodeModel.static.name,
'attributes': {
'mw': mw
}
},
{ 'type': '/' + this.constructor.static.nodeModel.static.name }
] );
}
};