2013-07-03 21:44:15 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWMathInspector class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki math inspector.
|
|
|
|
*
|
|
|
|
* @class
|
2013-08-03 14:17:16 +00:00
|
|
|
* @extends ve.ui.MWExtensionInspector
|
2013-07-03 21:44:15 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-11-05 00:29:50 +00:00
|
|
|
* @param {ve.ui.WindowSet} windowSet Window set this inspector is part of
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-07-03 21:44:15 +00:00
|
|
|
*/
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.MWMathInspector = function VeUiMWMathInspector( windowSet, config ) {
|
2013-07-03 21:44:15 +00:00
|
|
|
// Parent constructor
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.MWExtensionInspector.call( this, windowSet, config );
|
2013-09-24 18:57:03 +00:00
|
|
|
|
|
|
|
this.onChangeHandler = ve.debounce( ve.bind( this.updatePreview, this ), 250 );
|
2013-07-03 21:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ui.MWMathInspector, ve.ui.MWExtensionInspector );
|
2013-07-03 21:44:15 +00:00
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
ve.ui.MWMathInspector.static.name = 'math';
|
|
|
|
|
2013-07-18 22:48:21 +00:00
|
|
|
ve.ui.MWMathInspector.static.icon = 'math';
|
2013-07-03 21:44:15 +00:00
|
|
|
|
|
|
|
ve.ui.MWMathInspector.static.titleMessage = 'visualeditor-mwmathinspector-title';
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
ve.ui.MWMathInspector.static.nodeView = ve.ce.MWMathNode;
|
|
|
|
|
|
|
|
ve.ui.MWMathInspector.static.nodeModel = ve.dm.MWMathNode;
|
|
|
|
|
2013-07-03 21:44:15 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2013-09-24 18:57:03 +00:00
|
|
|
/**
|
|
|
|
* Update the math node rendering to reflect the content entered into the inspector.
|
|
|
|
*/
|
|
|
|
ve.ui.MWMathInspector.prototype.updatePreview = function () {
|
|
|
|
var newsrc = this.input.getValue();
|
|
|
|
if ( this.visible ) {
|
|
|
|
this.node.update( { 'extsrc': newsrc } );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* @inheritdoc
|
2013-09-24 18:57:03 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.MWMathInspector.prototype.setup = function ( data ) {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWExtensionInspector.prototype.setup.call( this, data );
|
|
|
|
|
2013-09-24 18:57:03 +00:00
|
|
|
var mw, surfaceModel = this.surface.getModel();
|
|
|
|
|
|
|
|
this.node = this.surface.getView().getFocusedNode();
|
|
|
|
if ( !this.node ) {
|
|
|
|
// Create a dummy node, needed for live preview
|
|
|
|
mw = {
|
|
|
|
'name': 'math',
|
|
|
|
'attrs': {},
|
|
|
|
'body': {
|
|
|
|
'extsrc': ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
|
|
|
|
{
|
|
|
|
'type': 'mwMath',
|
|
|
|
'attributes': {
|
|
|
|
'mw': mw
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ 'type': '/mwMath' }
|
|
|
|
] );
|
|
|
|
this.node = this.surface.getView().getFocusedNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.input.on( 'change', this.onChangeHandler );
|
|
|
|
|
2013-11-08 14:57:50 +00:00
|
|
|
// Override directionality settings, inspector's input
|
|
|
|
// should always be LTR:
|
|
|
|
this.input.setRTL( false );
|
2013-09-24 18:57:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* @inheritdoc
|
2013-09-24 18:57:03 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.MWMathInspector.prototype.teardown = function ( data ) {
|
2013-09-24 18:57:03 +00:00
|
|
|
var newsrc = this.input.getValue(),
|
|
|
|
surfaceModel = this.surface.getModel();
|
|
|
|
|
2013-11-05 00:29:50 +00:00
|
|
|
this.input.off( 'change', this.onChangeHandler );
|
|
|
|
|
2013-09-24 18:57:03 +00:00
|
|
|
if ( newsrc !== '' ) {
|
|
|
|
// Parent method
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.teardown.call( this, data );
|
2013-09-24 18:57:03 +00:00
|
|
|
} else {
|
|
|
|
// The user tried to empty the node, remove it
|
|
|
|
surfaceModel.change( ve.dm.Transaction.newFromRemoval(
|
|
|
|
surfaceModel.getDocument(), this.node.getOuterRange()
|
|
|
|
) );
|
2013-11-05 00:29:50 +00:00
|
|
|
// Grandparent method; we're overriding the parent behavior in this case
|
|
|
|
ve.ui.Inspector.prototype.teardown.call( this, data );
|
2013-09-24 18:57:03 +00:00
|
|
|
}
|
2013-07-03 21:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
ve.ui.inspectorFactory.register( ve.ui.MWMathInspector );
|