2014-04-17 02:16:24 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWLiveExtensionInspector class.
|
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2014-04-17 02:16:24 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2015-03-23 15:09:49 +00:00
|
|
|
ve.ui.MWLiveExtensionInspector = function VeUiMWLiveExtensionInspector() {
|
2014-04-17 02:16:24 +00:00
|
|
|
// Parent constructor
|
2015-03-23 15:09:49 +00:00
|
|
|
ve.ui.MWLiveExtensionInspector.super.apply( this, arguments );
|
2014-04-17 02:16:24 +00:00
|
|
|
|
2014-09-23 21:54:10 +00:00
|
|
|
// Late bind onChangeHandler 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 );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2015-10-04 22:14:39 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWLiveExtensionInspector.super.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Elements for displaying errors
|
|
|
|
this.$generatedContentsErrorContainer = $( '<div>', {
|
|
|
|
'class': 've-ui-mwLiveExtensionInspector-error-container-hidden'
|
|
|
|
} );
|
|
|
|
this.generatedContentsErrorLabel = new OO.ui.LabelWidget( {
|
|
|
|
classes: [
|
|
|
|
've-ui-mwLiveExtensionInspector-error ve-ui-mwLiveExtensionInspector-error-collapsed'
|
|
|
|
]
|
|
|
|
} );
|
|
|
|
this.generatedContentsErrorButton = new OO.ui.ButtonWidget( {
|
|
|
|
framed: false,
|
|
|
|
classes: [ 've-ui-mwLiveExtensionInspector-error-button' ],
|
|
|
|
icon: 'expand'
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.$generatedContentsErrorContainer.append(
|
|
|
|
this.generatedContentsErrorButton.$element,
|
|
|
|
this.generatedContentsErrorLabel.$element
|
|
|
|
);
|
|
|
|
this.form.$element.append( this.$generatedContentsErrorContainer );
|
|
|
|
};
|
|
|
|
|
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 () {
|
2015-03-23 15:09:49 +00:00
|
|
|
var element = this.getNewElement();
|
2014-05-31 04:47:08 +00:00
|
|
|
// Initialization
|
|
|
|
this.getFragment().getSurface().pushStaging();
|
|
|
|
|
2015-03-23 15:09:49 +00:00
|
|
|
if ( !this.selectedNode ) {
|
2014-05-31 04:47:08 +00:00
|
|
|
// Create a new node
|
2014-09-30 16:34:33 +00:00
|
|
|
// collapseToEnd returns a new fragment
|
|
|
|
this.fragment = this.getFragment().collapseToEnd().insertContent( [
|
2015-03-23 15:09:49 +00:00
|
|
|
element,
|
|
|
|
{ type: '/' + element.type }
|
2014-05-31 04:47:08 +00:00
|
|
|
] );
|
2015-10-04 22:14:39 +00:00
|
|
|
// Check if the node was inserted at a structural offset and
|
|
|
|
// wrapped in a paragraph
|
2014-09-30 16:34:33 +00:00
|
|
|
if ( this.getFragment().getSelection().getRange().getLength() === 4 ) {
|
|
|
|
this.fragment = this.getFragment().adjustLinearSelection( 1, -1 );
|
2014-05-31 04:47:08 +00:00
|
|
|
}
|
|
|
|
this.getFragment().select();
|
2015-03-23 15:09:49 +00:00
|
|
|
this.selectedNode = this.getFragment().getSelectedNode();
|
2014-05-31 04:47:08 +00:00
|
|
|
}
|
|
|
|
this.input.on( 'change', this.onChangeHandler );
|
2015-10-04 22:14:39 +00:00
|
|
|
this.selectedNode.connect( this, {
|
|
|
|
generatedContentsError: this.renderGeneratedContentsError
|
|
|
|
} );
|
2014-05-31 04:47:08 +00:00
|
|
|
}, this );
|
2014-04-17 02:16:24 +00:00
|
|
|
};
|
|
|
|
|
2014-11-12 19:11:06 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.getTeardownProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLiveExtensionInspector.super.prototype.getTeardownProcess.call( this, data )
|
|
|
|
.first( function () {
|
2015-10-04 22:14:39 +00:00
|
|
|
this.removeGeneratedContentsError();
|
2014-11-12 19:11:06 +00:00
|
|
|
this.input.off( 'change', this.onChangeHandler );
|
2015-10-04 22:14:39 +00:00
|
|
|
this.selectedNode.disconnect( this );
|
2015-09-02 01:32:11 +00:00
|
|
|
if ( data === undefined ) { // cancel
|
|
|
|
this.getFragment().getSurface().popStaging();
|
|
|
|
}
|
2014-11-12 19:11:06 +00:00
|
|
|
}, 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.
|
2015-10-01 14:58:20 +00:00
|
|
|
this.updatePreview();
|
2014-08-15 16:40:25 +00:00
|
|
|
this.getFragment().getSurface().applyStaging();
|
2015-10-02 13:10:12 +00:00
|
|
|
// Force the selected node to re-render after staging has finished
|
|
|
|
this.selectedNode.emit( 'update', false );
|
2014-08-15 16:40:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.removeNode = function () {
|
|
|
|
this.getFragment().getSurface().popStaging();
|
2015-09-05 14:23:59 +00:00
|
|
|
|
|
|
|
// Parent method
|
2014-08-15 16:40:25 +00:00
|
|
|
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 () {
|
2015-03-23 15:09:49 +00:00
|
|
|
var mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) );
|
2014-04-17 02:16:24 +00:00
|
|
|
|
2015-05-27 14:47:11 +00:00
|
|
|
this.updateMwData( mwData );
|
2014-04-17 02:16:24 +00:00
|
|
|
|
2015-10-04 22:14:39 +00:00
|
|
|
this.removeGeneratedContentsError();
|
|
|
|
|
2014-04-17 02:16:24 +00:00
|
|
|
if ( this.visible ) {
|
2014-08-22 20:50:48 +00:00
|
|
|
this.getFragment().changeAttributes( { mw: mwData } );
|
2014-04-17 02:16:24 +00:00
|
|
|
}
|
|
|
|
};
|
2015-10-04 22:14:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the error container and set the error label to contain the error.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $element Element containing the error
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.renderGeneratedContentsError = function ( $element ) {
|
|
|
|
// Display the error
|
|
|
|
this.$generatedContentsErrorContainer
|
|
|
|
.removeClass( 've-ui-mwLiveExtensionInspector-error-container-hidden' );
|
|
|
|
this.generatedContentsErrorLabel.setLabel( this.formatGeneratedContentsError( $element ) );
|
|
|
|
this.updateSize();
|
|
|
|
|
|
|
|
this.generatedContentsErrorButton.connect( this, { click: 'toggleGeneratedContentsError' } );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the error and collapse the error container.
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.removeGeneratedContentsError = function () {
|
|
|
|
this.$generatedContentsErrorContainer
|
|
|
|
.addClass( 've-ui-mwLiveExtensionInspector-error-container-hidden' );
|
|
|
|
this.generatedContentsErrorButton.setIcon( 'expand' ).disconnect( this );
|
|
|
|
this.generatedContentsErrorLabel.setLabel( null );
|
|
|
|
this.toggleGeneratedContentsError( true );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the error.
|
|
|
|
*
|
|
|
|
* Default behaviour returns the error with no modification.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $element Element containing the error
|
|
|
|
* @return {jQuery} $element Element containing the error
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.formatGeneratedContentsError = function ( $element ) {
|
|
|
|
return $element;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the error between collapsed and expanded.
|
|
|
|
*
|
|
|
|
* @param {boolean} expanded Treat the error as expanded without checking
|
|
|
|
*/
|
|
|
|
ve.ui.MWLiveExtensionInspector.prototype.toggleGeneratedContentsError = function ( expanded ) {
|
|
|
|
// Set the correct icon on the expand/collapse button
|
|
|
|
expanded = expanded || this.generatedContentsErrorButton.getIcon() === 'collapse';
|
|
|
|
this.generatedContentsErrorButton.setIcon( expanded ? 'expand' : 'collapse' );
|
|
|
|
|
|
|
|
// Expand or collapse the error message
|
|
|
|
this.generatedContentsErrorLabel.$element
|
|
|
|
.removeClass(
|
|
|
|
've-ui-mwLiveExtensionInspector-error-expanded ve-ui-mwLiveExtensionInspector-error-collapsed'
|
|
|
|
)
|
|
|
|
.addClass( 've-ui-mwLiveExtensionInspector-error-' + ( expanded ? 'collapsed' : 'expanded' ) );
|
|
|
|
|
|
|
|
this.updateSize();
|
|
|
|
};
|