From ee40877e8304ebf3023160e56f8b4476c4efdf08 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Sun, 20 Nov 2016 22:55:14 +0000 Subject: [PATCH] Re-use target widgets Depends-On: I5ed0591048a17e84077bde5490e05c0467d0394a Depends-On: I5decb918f398704d4b6c108a16fbc1cc073ef077 Change-Id: Icba13d84e10cf18a6c68e26448b2efe93b8c42b8 --- modules/ve-cite/ve.ui.MWReferenceDialog.js | 55 +++++++++------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/modules/ve-cite/ve.ui.MWReferenceDialog.js b/modules/ve-cite/ve.ui.MWReferenceDialog.js index 65044717d..4bf6c1006 100644 --- a/modules/ve-cite/ve.ui.MWReferenceDialog.js +++ b/modules/ve-cite/ve.ui.MWReferenceDialog.js @@ -151,14 +151,14 @@ ve.ui.MWReferenceDialog.prototype.documentHasContent = function () { */ ve.ui.MWReferenceDialog.prototype.canApply = function () { return this.documentHasContent() && - ( this.referenceTarget.getSurface().getModel().hasBeenModified() || + ( this.referenceTarget.hasBeenModified() || this.referenceGroupInput.getValue() !== this.originalGroup ); }; /** - * Handle reference surface change events + * Handle reference target widget change events */ -ve.ui.MWReferenceDialog.prototype.onSurfaceHistory = function () { +ve.ui.MWReferenceDialog.prototype.onTargetChange = function () { var hasContent = this.documentHasContent(); this.actions.setAbilities( { @@ -229,7 +229,7 @@ ve.ui.MWReferenceDialog.prototype.getBodyHeight = function () { * @chainable */ ve.ui.MWReferenceDialog.prototype.useReference = function ( ref ) { - var group, citeCommands; + var group; // Properties if ( ref instanceof ve.dm.MWReferenceModel ) { @@ -240,31 +240,7 @@ ve.ui.MWReferenceDialog.prototype.useReference = function ( ref ) { this.referenceModel = new ve.dm.MWReferenceModel( this.getFragment().getDocument() ); } - // Cleanup - if ( this.referenceTarget ) { - this.referenceTarget.destroy(); - } - - citeCommands = Object.keys( ve.init.target.getSurface().commandRegistry.registry ).filter( function ( command ) { - return command.indexOf( 'cite-' ) !== -1; - } ); - - // Properties - this.referenceTarget = ve.init.target.createTargetWidget( - this.referenceModel.getDocument(), - { - tools: ve.copy( ve.init.target.constructor.static.toolbarGroups ), - includeCommands: this.constructor.static.includeCommands, - excludeCommands: this.constructor.static.excludeCommands.concat( citeCommands ), - importRules: this.constructor.static.getImportRules(), - inDialog: this.constructor.static.name - } - ); - - // Events - this.referenceTarget.getSurface().getModel().connect( this, { - history: this.onSurfaceHistory.bind( this ) - } ); + this.referenceTarget.setDocument( this.referenceModel.getDocument() ); // Initialization this.originalGroup = this.referenceModel.getGroup(); @@ -272,7 +248,6 @@ ve.ui.MWReferenceDialog.prototype.useReference = function ( ref ) { this.referenceGroupInput.setDisabled( true ); this.referenceGroupInput.setValue( this.originalGroup ); this.referenceGroupInput.setDisabled( false ); - this.contentFieldset.$element.append( this.referenceTarget.$element ); this.referenceTarget.initialize(); group = this.getFragment().getDocument().getInternalList() @@ -294,6 +269,10 @@ ve.ui.MWReferenceDialog.prototype.useReference = function ( ref ) { * @inheritdoc */ ve.ui.MWReferenceDialog.prototype.initialize = function () { + var citeCommands = Object.keys( ve.init.target.getSurface().commandRegistry.registry ).filter( function ( command ) { + return command.indexOf( 'cite-' ) !== -1; + } ); + // Parent method ve.ui.MWReferenceDialog.super.prototype.initialize.call( this ); @@ -308,11 +287,23 @@ ve.ui.MWReferenceDialog.prototype.initialize = function () { this.$reuseWarningText = $( '' ); this.$reuseWarning = $( '' ).append( this.reuseWarningIcon.$element, this.$reuseWarningText ); + this.referenceTarget = ve.init.target.createTargetWidget( + { + tools: ve.copy( ve.init.target.constructor.static.toolbarGroups ), + includeCommands: this.constructor.static.includeCommands, + excludeCommands: this.constructor.static.excludeCommands.concat( citeCommands ), + importRules: this.constructor.static.getImportRules(), + inDialog: this.constructor.static.name + } + ); + this.contentFieldset = new OO.ui.FieldsetLayout(); this.optionsFieldset = new OO.ui.FieldsetLayout( { label: ve.msg( 'cite-ve-dialog-reference-options-section' ), icon: 'settings' } ); + this.contentFieldset.$element.append( this.referenceTarget.$element ); + this.referenceGroupInput = new ve.ui.MWReferenceGroupInputWidget( { $overlay: this.$overlay, emptyGroupName: ve.msg( 'cite-ve-dialog-reference-options-group-placeholder' ) @@ -326,6 +317,7 @@ ve.ui.MWReferenceDialog.prototype.initialize = function () { // Events this.search.getResults().connect( this, { choose: 'onSearchResultsChoose' } ); + this.referenceTarget.connect( this, { change: 'onTargetChange' } ); // Initialization this.panels.addItems( [ this.editPanel, this.searchPanel ] ); @@ -431,8 +423,7 @@ ve.ui.MWReferenceDialog.prototype.getTeardownProcess = function ( data ) { .first( function () { this.referenceTarget.getSurface().getModel().disconnect( this ); this.search.getQuery().setValue( '' ); - this.referenceTarget.destroy(); - this.referenceTarget = null; + this.referenceTarget.clear(); this.referenceModel = null; }, this ); };