mediawiki-extensions-Visual.../modules/ve-mw/ui/inspectors/ve.ui.MWLiveExtensionInspector.js

158 lines
4.6 KiB
JavaScript
Raw Normal View History

/*!
* VisualEditor UserInterface MWLiveExtensionInspector class.
*
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
Update VE core submodule to master (f2277ea) New changes: 56de6f5 Localisation updates from https://translatewiki.net. f8bda64 Widgetise demo menu 6ac48d8 Localisation updates from https://translatewiki.net. 365e131 builderloader: Omit value for boolean "disabled" attribute per HTML5 706e4b3 Prevent double counting of DM nodes in getNodeAndOffset b141a7d Update OOjs UI to v0.1.0-pre (d2451ac748) c5b3921 Localisation updates from https://translatewiki.net. 1606983 Update reference to ConfirmationDialog to use MessageDialog Deletions: * Styles for ve.ui.MWBetaWelcomeDialog - not needed anymore because OO.ui.MessageDialog provides them * Styles for ve.ui.MWGalleryInspector - not needed anymore because ve.ui.MWExtensionInspector provides part of them and the rest are being replaced by programatic sizing Modifications: * ve.ui.MWLinkTargetInputWidget - Added support for validation and href getter * Split message between tool and dialog title for ve.ui.MWEditModeTool and ve.ui.MWWikitextSwitchConfirmDialog General changes: * Updated inheritance. * Added manager param to constructors of dialogs and inspectors. * Updated use of show/hide with toggle. * Added meaningful descriptions of dialog and inspector classes. * Configured dialog and inspector sizes statically. * Configured dialog action buttons statically. * Interfaced with OO.ui.ActionSet to control action buttons. * Moved applyChanges code into getActionProcess methods. * Always using .next in setup/ready process getters and .first in hold/teardown process getters. Change-Id: Ia74732e6e32c0808eee021f0a26225b9e6c3f971
2014-07-14 21:32:49 +00:00
* Inspector for editing generic MediaWiki extensions with dynamic rendering.
*
* @class
* @abstract
* @extends ve.ui.MWExtensionInspector
*
* @constructor
* @param {Object} [config] Configuration options
*/
ve.ui.MWLiveExtensionInspector = function VeUiMWLiveExtensionInspector() {
// Parent constructor
ve.ui.MWLiveExtensionInspector.super.apply( this, arguments );
// Late bind onChangeHandler to a debounced updatePreview
this.onChangeHandler = ve.debounce( this.updatePreview.bind( this ), 250 );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWLiveExtensionInspector, ve.ui.MWExtensionInspector );
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWLiveExtensionInspector.prototype.initialize = function () {
// Parent method
ve.ui.MWLiveExtensionInspector.super.prototype.initialize.call( this );
this.generatedContentsError = new ve.ui.MWExpandableErrorElement();
this.form.$element.append( this.generatedContentsError.$element );
};
/**
* @inheritdoc
*/
ve.ui.MWLiveExtensionInspector.prototype.getSetupProcess = function ( data ) {
return ve.ui.MWLiveExtensionInspector.super.prototype.getSetupProcess.call( this, data )
.next( function () {
var element = this.getNewElement();
// Initialization
this.getFragment().getSurface().pushStaging();
if ( !this.selectedNode ) {
// Create a new node
// collapseToEnd returns a new fragment
this.fragment = this.getFragment().collapseToEnd().insertContent( [
element,
{ type: '/' + element.type }
] );
// Check if the node was inserted at a structural offset and
// wrapped in a paragraph
if ( this.getFragment().getSelection().getRange().getLength() === 4 ) {
this.fragment = this.getFragment().adjustLinearSelection( 1, -1 );
}
this.getFragment().select();
this.selectedNode = this.getFragment().getSelectedNode();
}
this.input.on( 'change', this.onChangeHandler );
this.generatedContentsError.connect( this, {
update: 'updateSize'
} );
this.selectedNode.connect( this, {
generatedContentsError: 'showGeneratedContentsError'
} );
}, this );
};
/**
* @inheritdoc
*/
ve.ui.MWLiveExtensionInspector.prototype.getTeardownProcess = function ( data ) {
return ve.ui.MWLiveExtensionInspector.super.prototype.getTeardownProcess.call( this, data )
.first( function () {
this.input.off( 'change', this.onChangeHandler );
this.generatedContentsError.clear();
this.generatedContentsError.disconnect( this );
this.selectedNode.disconnect( this );
if ( data === undefined ) { // cancel
this.getFragment().getSurface().popStaging();
}
}, 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.updatePreview();
this.getFragment().getSurface().applyStaging();
// Force the selected node to re-render after staging has finished
this.selectedNode.emit( 'update', false );
};
/**
* @inheritdoc
*/
ve.ui.MWLiveExtensionInspector.prototype.removeNode = function () {
this.getFragment().getSurface().popStaging();
// Parent method
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.selectedNode.getAttribute( 'mw' ) );
this.updateMwData( mwData );
this.hideGeneratedContentsError();
if ( this.visible ) {
this.getFragment().changeAttributes( { mw: mwData } );
}
};
/**
* Show the error container and set the error label to contain the error.
*
* @param {jQuery} $element Element containing the error
*/
ve.ui.MWLiveExtensionInspector.prototype.showGeneratedContentsError = function ( $element ) {
this.generatedContentsError.show( this.formatGeneratedContentsError( $element ) );
};
/**
* Hide the error and collapse the error container.
*/
ve.ui.MWLiveExtensionInspector.prototype.hideGeneratedContentsError = function () {
this.generatedContentsError.clear();
};
/**
* 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;
};