2013-08-03 14:17:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWExtensionInspector class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-08-03 14:17:16 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki extension inspector.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2013-11-05 00:29:50 +00:00
|
|
|
* @extends ve.ui.Inspector
|
2013-08-03 14:17:16 +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-08-03 14:17:16 +00:00
|
|
|
*/
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.MWExtensionInspector = function VeUiMWExtensionInspector( windowSet, config ) {
|
2013-08-03 14:17:16 +00:00
|
|
|
// Parent constructor
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.Inspector.call( this, windowSet, config );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-11-05 00:29:50 +00:00
|
|
|
OO.inheritClass( ve.ui.MWExtensionInspector, ve.ui.Inspector );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
2014-02-03 20:04:36 +00:00
|
|
|
ve.ui.MWExtensionInspector.static.placeholder = null;
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
ve.ui.MWExtensionInspector.static.nodeView = null;
|
|
|
|
|
|
|
|
ve.ui.MWExtensionInspector.static.nodeModel = null;
|
|
|
|
|
2013-12-06 20:24:36 +00:00
|
|
|
ve.ui.MWExtensionInspector.static.removable = false;
|
2013-08-14 14:39:56 +00:00
|
|
|
|
2014-02-04 12:17:43 +00:00
|
|
|
/**
|
|
|
|
* Extension is allowed to have empty contents
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {boolean}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionInspector.static.allowedEmpty = false;
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle frame ready events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.Inspector.prototype.initialize.call( this );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
this.input = new OO.ui.TextInputWidget( {
|
2013-11-01 19:45:59 +00:00
|
|
|
'$': this.$,
|
2013-08-03 14:17:16 +00:00
|
|
|
'overlay': this.surface.$localOverlay,
|
|
|
|
'multiline': true
|
|
|
|
} );
|
2013-11-01 19:45:59 +00:00
|
|
|
this.input.$element.addClass( 've-ui-mwExtensionInspector-input' );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
|
|
|
// Initialization
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$form.append( this.input.$element );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
2014-02-04 12:28:46 +00:00
|
|
|
/**
|
|
|
|
* Get the placeholder text for the content input area.
|
|
|
|
*
|
|
|
|
* @returns {string} Placeholder text
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionInspector.prototype.getInputPlaceholder = function () {
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* @inheritdoc
|
2013-08-03 14:17:16 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.setup = function ( data ) {
|
2013-12-12 05:36:35 +00:00
|
|
|
var dir,
|
|
|
|
fragment = this.surface.getModel().getFragment( null, true );
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
// Parent method
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.Inspector.prototype.setup.call( this, data );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
2013-11-05 00:29:50 +00:00
|
|
|
// Initialization
|
2013-08-03 14:17:16 +00:00
|
|
|
this.node = this.surface.getView().getFocusedNode();
|
2014-01-28 03:21:36 +00:00
|
|
|
// Make sure we're inspecting the right type of node
|
|
|
|
if ( !( this.node instanceof this.constructor.static.nodeView ) ) {
|
|
|
|
this.node = null;
|
|
|
|
}
|
2013-11-05 00:29:50 +00:00
|
|
|
this.input.setValue( this.node ? this.node.getModel().getAttribute( 'mw' ).body.extsrc : '' );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
2014-02-04 12:28:46 +00:00
|
|
|
this.input.$input.attr( 'placeholder', this.getInputPlaceholder() );
|
2014-02-03 20:04:36 +00:00
|
|
|
|
2013-12-12 05:36:35 +00:00
|
|
|
// By default, the direction of the input element should be the same
|
|
|
|
// as the direction of the content it applies to
|
2013-11-22 12:58:12 +00:00
|
|
|
if ( this.node ) {
|
2013-12-12 05:36:35 +00:00
|
|
|
// The node is being edited
|
|
|
|
dir = this.node.$element.css( 'direction' );
|
|
|
|
} else {
|
|
|
|
// New insertion, base direction on the fragment range
|
|
|
|
dir = this.surface.getView().documentView.getDirectionFromRange( fragment.getRange() );
|
2013-11-22 12:58:12 +00:00
|
|
|
}
|
2013-12-12 05:36:35 +00:00
|
|
|
this.input.setRTL( dir === 'rtl' );
|
2013-11-05 00:29:50 +00:00
|
|
|
};
|
2013-11-08 14:57:50 +00:00
|
|
|
|
2013-11-05 00:29:50 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionInspector.prototype.ready = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.Inspector.prototype.ready.call( this );
|
|
|
|
|
|
|
|
// Focus the input
|
|
|
|
this.input.$input.focus().select();
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* @inheritdoc
|
2013-08-03 14:17:16 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.teardown = function ( data ) {
|
2013-09-24 23:55:41 +00:00
|
|
|
var mwData,
|
2013-08-03 14:17:16 +00:00
|
|
|
surfaceModel = this.surface.getModel();
|
|
|
|
|
2014-02-04 12:17:43 +00:00
|
|
|
if ( this.constructor.static.allowedEmpty || this.input.getValue() !== '' ) {
|
|
|
|
if ( this.node instanceof this.constructor.static.nodeView ) {
|
|
|
|
mwData = ve.copy( this.node.getModel().getAttribute( 'mw' ) );
|
2013-12-06 14:52:26 +00:00
|
|
|
this.updateMwData( mwData );
|
2014-02-04 12:17:43 +00:00
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChanges(
|
|
|
|
surfaceModel.getDocument(), this.node.getOuterRange().start, { 'mw': mwData }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
mwData = {
|
|
|
|
'name': this.constructor.static.nodeModel.static.extensionName,
|
|
|
|
'attrs': {},
|
2013-12-06 14:52:26 +00:00
|
|
|
'body': {}
|
2014-02-04 12:17:43 +00:00
|
|
|
};
|
2013-12-06 14:52:26 +00:00
|
|
|
this.updateMwData( mwData );
|
2014-02-04 12:17:43 +00:00
|
|
|
surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
|
|
|
|
{
|
|
|
|
'type': this.constructor.static.nodeModel.static.name,
|
|
|
|
'attributes': {
|
|
|
|
'mw': mwData
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ 'type': '/' + this.constructor.static.nodeModel.static.name }
|
|
|
|
] );
|
|
|
|
}
|
2013-08-03 14:17:16 +00:00
|
|
|
}
|
2013-11-05 00:29:50 +00:00
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.ui.Inspector.prototype.teardown.call( this, data );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
2013-12-06 14:52:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update mwData object with the new values from the inspector
|
|
|
|
*
|
|
|
|
* @param {Object} mwData MediaWiki data object
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionInspector.prototype.updateMwData = function ( mwData ) {
|
|
|
|
mwData.body.extsrc = this.input.getValue();
|
|
|
|
};
|