mediawiki-extensions-Visual.../modules/ve-mw/ui/inspectors/ve.ui.MWLiveExtensionInspector.js
Ed Sanders 03231e5a1c Fix teardown of live extension nodes
* Separate teardown into (insertOrUpdate|remove)Node
* Override insertOrUpdate completely to avoid generating
  extra transactions. Surface#applyStaging creates all the
  transactions we need.
* Override remove to popStaging before calling parent.
* Fix isEmpty check to use getInnerValue (ignoring whitepsace)

Change-Id: I903302af178d6f923de00aff9c58e5330d682813
2014-08-15 17:40:25 +01:00

118 lines
3.3 KiB
JavaScript

/*!
* VisualEditor UserInterface MWLiveExtensionInspector class.
*
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Inspector for editing generic MediaWiki extensions with dynamic rendering.
*
* @class
* @abstract
* @extends ve.ui.MWExtensionInspector
*
* @constructor
* @param {OO.ui.WindowManager} manager Manager of window
* @param {Object} [config] Configuration options
*/
ve.ui.MWLiveExtensionInspector = function VeUiMWLiveExtensionInspector( manager, config ) {
// Parent constructor
ve.ui.MWExtensionInspector.call( this, manager, config );
// Late bind onChangeHanlder to a debounced updatePreview
this.onChangeHandler = ve.debounce( this.updatePreview.bind( this ), 250 );
};
/* 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 {
'name': this.constructor.static.mwName || this.constructor.static.name,
'attrs': {},
'body': {
'extsrc': ''
}
};
};
/**
* @inheritdoc
*/
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( [
{
'type': this.constructor.static.nodeModel.static.name,
'attributes': { 'mw': this.getNewMwData() }
},
{ 'type': '/' + this.constructor.static.nodeModel.static.name }
] );
// 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 );
};
/**
* @inheritdoc
*/
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 );
};
/**
* 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 ) {
this.getFragment().changeAttributes( { 'mw': mwData } );
}
};