2014-04-17 02:16:24 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWLiveExtensionInspector class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-07-14 21:32:49 +00:00
|
|
|
* Inspector for editing generic MediaWiki extensions with dynamic rendering.
|
2014-04-17 02:16:24 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ui.MWExtensionInspector
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2014-08-21 00:50:54 +00:00
|
|
|
ve.ui.MWLiveExtensionInspector = function VeUiMWLiveExtensionInspector( config ) {
|
2014-04-17 02:16:24 +00:00
|
|
|
// Parent constructor
|
2014-08-21 00:50:54 +00:00
|
|
|
ve.ui.MWExtensionInspector.call( this, config );
|
2014-04-17 02:16:24 +00:00
|
|
|
|
|
|
|
// Late bind onChangeHanlder to a debounced updatePreview
|
2014-07-08 22:33:32 +00:00
|
|
|
this.onChangeHandler = ve.debounce( this.updatePreview.bind( this ), 250 );
|
2014-04-17 02:16:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWLiveExtensionInspector, ve.ui.MWExtensionInspector );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of extension in the mw data, if different from inspector name
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.static.mwName = null;
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an MW data object for a new node
|
|
|
|
* @returns {Object} MW data
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.getNewMwData = function () {
|
|
|
|
return {
|
2014-08-22 20:50:48 +00:00
|
|
|
name: this.constructor.static.mwName || this.constructor.static.name,
|
|
|
|
attrs: {},
|
|
|
|
body: {
|
|
|
|
extsrc: ''
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
|
|
|
};
|
2014-04-17 02:16:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.getSetupProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLiveExtensionInspector.super.prototype.getSetupProcess.call( this, data )
|
|
|
|
.next( function () {
|
|
|
|
// Initialization
|
|
|
|
this.getFragment().getSurface().pushStaging();
|
|
|
|
|
|
|
|
if ( !this.node ) {
|
|
|
|
// Create a new node
|
|
|
|
// collapseRangeToEnd returns a new fragment
|
|
|
|
this.fragment = this.getFragment().collapseRangeToEnd().insertContent( [
|
|
|
|
{
|
2014-08-22 20:50:48 +00:00
|
|
|
type: this.constructor.static.nodeModel.static.name,
|
|
|
|
attributes: { mw: this.getNewMwData() }
|
2014-05-31 04:47:08 +00:00
|
|
|
},
|
2014-08-22 20:50:48 +00:00
|
|
|
{ type: '/' + this.constructor.static.nodeModel.static.name }
|
2014-05-31 04:47:08 +00:00
|
|
|
] );
|
|
|
|
// Check if the node was inserted at a structural offset and wrapped in a paragraph
|
|
|
|
if ( this.getFragment().getRange().getLength() === 4 ) {
|
|
|
|
this.fragment = this.getFragment().adjustRange( 1, -1 );
|
|
|
|
}
|
|
|
|
this.getFragment().select();
|
|
|
|
this.node = this.getFragment().getSelectedNode();
|
|
|
|
}
|
|
|
|
this.input.on( 'change', this.onChangeHandler );
|
|
|
|
}, this );
|
2014-04-17 02:16:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2014-08-15 16:40:25 +00:00
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.insertOrUpdateNode = function () {
|
|
|
|
// No need to call parent method as changes have already been made
|
|
|
|
// to the model in staging, just need to apply them.
|
|
|
|
this.input.off( 'change', this.onChangeHandler );
|
|
|
|
this.getFragment().getSurface().applyStaging();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.removeNode = function () {
|
|
|
|
this.getFragment().getSurface().popStaging();
|
|
|
|
ve.ui.MWLiveExtensionInspector.super.prototype.removeNode.call( this );
|
2014-04-17 02:16:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the node rendering to reflect the current content in the inspector.
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.updatePreview = function () {
|
|
|
|
var mwData = ve.copy( this.node.getAttribute( 'mw' ) );
|
|
|
|
|
|
|
|
mwData.body.extsrc = this.input.getValue();
|
|
|
|
|
|
|
|
if ( this.visible ) {
|
2014-08-22 20:50:48 +00:00
|
|
|
this.getFragment().changeAttributes( { mw: mwData } );
|
2014-04-17 02:16:24 +00:00
|
|
|
}
|
|
|
|
};
|