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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-07-14 21:32:49 +00:00
|
|
|
* Inspector for editing generic MediaWiki extensions.
|
2013-08-03 14:17:16 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2014-07-14 21:32:49 +00:00
|
|
|
* @extends ve.ui.FragmentInspector
|
2013-08-03 14:17:16 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-07-14 21:32:49 +00:00
|
|
|
* @param {OO.ui.WindowManager} manager Manager of window
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-08-03 14:17:16 +00:00
|
|
|
*/
|
2014-07-14 21:32:49 +00:00
|
|
|
ve.ui.MWExtensionInspector = function VeUiMWExtensionInspector( manager, config ) {
|
2013-08-03 14:17:16 +00:00
|
|
|
// Parent constructor
|
2014-07-14 21:32:49 +00:00
|
|
|
ve.ui.FragmentInspector.call( this, manager, config );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
OO.inheritClass( ve.ui.MWExtensionInspector, ve.ui.FragmentInspector );
|
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.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;
|
|
|
|
|
2014-04-17 01:22:36 +00:00
|
|
|
/**
|
2014-04-17 21:08:25 +00:00
|
|
|
* Inspector's directionality, 'ltr' or 'rtl'
|
|
|
|
*
|
|
|
|
* Leave as null to use the directionality of the current fragment.
|
2014-04-17 01:22:36 +00:00
|
|
|
*
|
|
|
|
* @static
|
2014-04-17 21:08:25 +00:00
|
|
|
* @property {string|null}
|
2014-04-17 01:22:36 +00:00
|
|
|
* @inheritable
|
|
|
|
*/
|
2014-04-17 21:08:25 +00:00
|
|
|
ve.ui.MWExtensionInspector.static.dir = null;
|
2014-04-17 01:22:36 +00:00
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle frame ready events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.ui.MWExtensionInspector.super.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
|
|
|
'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
|
|
|
|
2014-05-22 18:07:18 +00:00
|
|
|
this.isBlock = !this.constructor.static.nodeModel.static.isContent;
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
// Initialization
|
2014-07-14 21:32:49 +00:00
|
|
|
this.form.$element.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
|
|
|
*/
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.getSetupProcess = function ( data ) {
|
|
|
|
return ve.ui.MWExtensionInspector.super.prototype.getSetupProcess.call( this, data )
|
|
|
|
.next( function () {
|
2014-05-22 18:07:18 +00:00
|
|
|
var value, dir;
|
|
|
|
|
2014-05-31 04:47:08 +00:00
|
|
|
// Initialization
|
|
|
|
this.node = this.getFragment().getSelectedNode();
|
2014-05-22 18:07:18 +00:00
|
|
|
this.whitespace = [ '', '' ];
|
|
|
|
|
2014-05-31 04:47:08 +00:00
|
|
|
// Make sure we're inspecting the right type of node
|
|
|
|
if ( !( this.node instanceof this.constructor.static.nodeModel ) ) {
|
|
|
|
this.node = null;
|
|
|
|
}
|
2014-05-22 18:07:18 +00:00
|
|
|
if ( this.node ) {
|
|
|
|
value = this.node.getAttribute( 'mw' ).body.extsrc;
|
|
|
|
if ( this.isBlock ) {
|
|
|
|
// Trim leading/trailing linebreaks but remember them
|
|
|
|
if ( value.slice( 0, 1 ) === '\n' ) {
|
|
|
|
this.whitespace[0] = '\n';
|
|
|
|
value = value.slice( 1 );
|
|
|
|
}
|
|
|
|
if ( value.slice( -1 ) === '\n' ) {
|
|
|
|
this.whitespace[1] = '\n';
|
|
|
|
value = value.slice( 0, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.input.setValue( value );
|
|
|
|
} else {
|
|
|
|
if ( this.isBlock ) {
|
|
|
|
// New nodes should use linebreaks for blocks
|
|
|
|
this.whitespace = [ '\n', '\n' ];
|
|
|
|
}
|
|
|
|
this.input.setValue( '' );
|
|
|
|
}
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
this.input.$input.attr( 'placeholder', this.getInputPlaceholder() );
|
|
|
|
|
2014-05-22 18:07:18 +00:00
|
|
|
dir = this.constructor.static.dir || data.dir;
|
2014-05-31 04:47:08 +00:00
|
|
|
this.input.setRTL( dir === 'rtl' );
|
|
|
|
}, this );
|
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
|
|
|
|
*/
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.getReadyProcess = function ( data ) {
|
|
|
|
return ve.ui.MWExtensionInspector.super.prototype.getReadyProcess.call( this, data )
|
|
|
|
.next( function () {
|
|
|
|
// Focus the input
|
2014-06-11 13:26:38 +00:00
|
|
|
this.input.focus();
|
2014-05-31 04:47:08 +00:00
|
|
|
}, this );
|
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
|
|
|
*/
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.ui.MWExtensionInspector.prototype.getTeardownProcess = function ( data ) {
|
|
|
|
return ve.ui.MWExtensionInspector.super.prototype.getTeardownProcess.call( this, data )
|
|
|
|
.first( function () {
|
|
|
|
var mwData,
|
2014-06-16 13:28:54 +00:00
|
|
|
surfaceModel = this.getFragment().getSurface();
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
if ( this.constructor.static.allowedEmpty || this.input.getValue() !== '' ) {
|
|
|
|
if ( this.node ) {
|
|
|
|
mwData = ve.copy( this.node.getAttribute( 'mw' ) );
|
|
|
|
this.updateMwData( mwData );
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChanges(
|
|
|
|
surfaceModel.getDocument(),
|
|
|
|
this.node.getOuterRange().start,
|
|
|
|
{ 'mw': mwData }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
mwData = {
|
|
|
|
'name': this.constructor.static.nodeModel.static.extensionName,
|
|
|
|
'attrs': {},
|
|
|
|
'body': {}
|
|
|
|
};
|
|
|
|
this.updateMwData( mwData );
|
2014-06-16 13:28:54 +00:00
|
|
|
// Collapse returns a new fragment, so update this.fragment
|
|
|
|
this.fragment = this.getFragment().collapseRangeToEnd();
|
|
|
|
this.getFragment().insertContent( [
|
2014-05-31 04:47:08 +00:00
|
|
|
{
|
|
|
|
'type': this.constructor.static.nodeModel.static.name,
|
|
|
|
'attributes': {
|
|
|
|
'mw': mwData
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ 'type': '/' + this.constructor.static.nodeModel.static.name }
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
} else if ( this.node && !this.constructor.static.allowedEmpty ) {
|
|
|
|
// Content has been emptied on a node which isn't allowed to
|
|
|
|
// be empty, so delete it.
|
2014-06-16 13:28:54 +00:00
|
|
|
this.getFragment().removeContent();
|
2014-05-31 04:47:08 +00:00
|
|
|
}
|
|
|
|
}, this );
|
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 ) {
|
2014-05-22 18:07:18 +00:00
|
|
|
mwData.body.extsrc = this.whitespace[0] + this.input.getValue() + this.whitespace[1];
|
2013-12-06 14:52:26 +00:00
|
|
|
};
|