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-10-09 19:59:03 +00:00
|
|
|
* @param {ve.ui.SurfaceWindowSet} 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 } );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the inspector being opened.
|
|
|
|
*/
|
|
|
|
ve.ui.MWMathInspector.prototype.onOpen = function () {
|
|
|
|
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-07-03 21:44:15 +00:00
|
|
|
// Parent method
|
2013-09-24 18:57:03 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.onOpen.call( this );
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the inspector being closed.
|
|
|
|
*
|
|
|
|
* @param {string} action Action that caused the window to be closed
|
|
|
|
*/
|
|
|
|
ve.ui.MWMathInspector.prototype.onClose = function ( action ) {
|
|
|
|
var newsrc = this.input.getValue(),
|
|
|
|
surfaceModel = this.surface.getModel();
|
|
|
|
|
|
|
|
if ( newsrc !== '' ) {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWExtensionInspector.prototype.onClose.call( this, action );
|
|
|
|
} else {
|
|
|
|
// Grandparent method; we're overriding the parent behavior in this case
|
2013-10-04 17:51:44 +00:00
|
|
|
ve.ui.SurfaceInspector.prototype.onClose.call( this, action );
|
2013-09-24 18:57:03 +00:00
|
|
|
|
|
|
|
// The user tried to empty the node, remove it
|
|
|
|
surfaceModel.change( ve.dm.Transaction.newFromRemoval(
|
|
|
|
surfaceModel.getDocument(), this.node.getOuterRange()
|
|
|
|
) );
|
|
|
|
}
|
2013-07-03 21:44:15 +00:00
|
|
|
|
2013-09-24 18:57:03 +00:00
|
|
|
this.input.off( 'change', this.onChangeHandler );
|
2013-07-03 21:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
ve.ui.inspectorFactory.register( ve.ui.MWMathInspector );
|