2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MediaWiki MWReferenceDialog class.
|
|
|
|
*
|
2018-01-03 01:05:45 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
|
2017-12-29 12:12:35 +00:00
|
|
|
* @license MIT
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2024-07-02 09:34:21 +00:00
|
|
|
* Dialog for inserting, editing and re-using MediaWiki references.
|
2016-02-03 21:03:41 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2024-02-28 08:57:24 +00:00
|
|
|
* @extends ve.ui.NodeDialog
|
2016-02-03 21:03:41 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog = function VeUiMWReferenceDialog( config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWReferenceDialog.super.call( this, config );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.referenceModel = null;
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseReference = false;
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWReferenceDialog, ve.ui.NodeDialog );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.MWReferenceDialog.static.name = 'reference';
|
|
|
|
|
|
|
|
ve.ui.MWReferenceDialog.static.title =
|
|
|
|
OO.ui.deferMsg( 'cite-ve-dialog-reference-title' );
|
|
|
|
|
|
|
|
ve.ui.MWReferenceDialog.static.actions = [
|
|
|
|
{
|
2019-02-21 21:56:51 +00:00
|
|
|
action: 'done',
|
2016-02-03 21:03:41 +00:00
|
|
|
label: OO.ui.deferMsg( 'visualeditor-dialog-action-apply' ),
|
|
|
|
flags: [ 'progressive', 'primary' ],
|
|
|
|
modes: 'edit'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'insert',
|
|
|
|
label: OO.ui.deferMsg( 'visualeditor-dialog-action-insert' ),
|
2017-09-26 19:35:55 +00:00
|
|
|
flags: [ 'progressive', 'primary' ],
|
2016-02-03 21:03:41 +00:00
|
|
|
modes: 'insert'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ),
|
2019-06-10 15:00:38 +00:00
|
|
|
flags: [ 'safe', 'close' ],
|
2019-02-15 14:53:54 +00:00
|
|
|
modes: [ 'readonly', 'insert', 'edit', 'insert-select' ]
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
ve.ui.MWReferenceDialog.static.modelClasses = [ ve.dm.MWReferenceNode ];
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the reference document we're editing has any content.
|
|
|
|
*
|
|
|
|
* @return {boolean} Document has content
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.documentHasContent = function () {
|
|
|
|
// TODO: Check for other types of empty, e.g. only whitespace?
|
2019-11-28 18:49:16 +00:00
|
|
|
return this.referenceModel && this.referenceModel.getDocument().data.hasContent();
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
2024-07-21 18:09:27 +00:00
|
|
|
/**
|
2016-02-03 21:03:41 +00:00
|
|
|
* Determine whether any changes have been made (and haven't been undone).
|
|
|
|
*
|
2019-02-21 21:56:51 +00:00
|
|
|
* @return {boolean} Changes have been made
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
2019-02-21 21:56:51 +00:00
|
|
|
ve.ui.MWReferenceDialog.prototype.isModified = function () {
|
2024-07-21 18:09:27 +00:00
|
|
|
return this.documentHasContent() && this.editPanel.isModified();
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-11-20 22:55:14 +00:00
|
|
|
* Handle reference target widget change events
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
2016-11-20 22:55:14 +00:00
|
|
|
ve.ui.MWReferenceDialog.prototype.onTargetChange = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const hasContent = this.documentHasContent();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
this.actions.setAbilities( {
|
2019-02-21 21:56:51 +00:00
|
|
|
done: this.isModified(),
|
2019-01-03 19:42:44 +00:00
|
|
|
insert: hasContent
|
2016-02-03 21:03:41 +00:00
|
|
|
} );
|
2019-07-08 16:04:22 +00:00
|
|
|
|
|
|
|
if ( !this.trackedInputChange ) {
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'input' } );
|
|
|
|
this.trackedInputChange = true;
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle reference group input change events.
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.onReferenceGroupInputChange = function () {
|
|
|
|
this.actions.setAbilities( {
|
2019-02-21 21:56:51 +00:00
|
|
|
done: this.isModified()
|
2016-02-03 21:03:41 +00:00
|
|
|
} );
|
2019-07-08 16:04:22 +00:00
|
|
|
|
|
|
|
if ( !this.trackedInputChange ) {
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'input' } );
|
|
|
|
this.trackedInputChange = true;
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle search results choose events.
|
|
|
|
*
|
|
|
|
* @param {ve.ui.MWReferenceResultWidget} item Chosen item
|
|
|
|
*/
|
2024-07-02 09:34:21 +00:00
|
|
|
ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsChoose = function ( item ) {
|
2024-02-16 03:54:34 +00:00
|
|
|
const ref = item.getData();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) {
|
|
|
|
this.getFragment().removeContent();
|
|
|
|
this.selectedNode = null;
|
|
|
|
}
|
2024-06-06 12:54:05 +00:00
|
|
|
|
|
|
|
this.referenceModel = ref;
|
2024-07-21 18:09:27 +00:00
|
|
|
this.editPanel.setFormFieldsFromRef( ref );
|
2016-02-03 21:03:41 +00:00
|
|
|
this.executeAction( 'insert' );
|
2019-07-08 16:04:22 +00:00
|
|
|
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'reuse-choose' } );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.getReadyProcess = function ( data ) {
|
|
|
|
return ve.ui.MWReferenceDialog.super.prototype.getReadyProcess.call( this, data )
|
2024-05-31 14:27:11 +00:00
|
|
|
.next( () => {
|
2024-07-02 09:34:21 +00:00
|
|
|
if ( this.reuseReference ) {
|
|
|
|
this.reuseSearch.getQuery().focus().select();
|
2016-02-03 21:03:41 +00:00
|
|
|
} else {
|
2024-07-17 17:51:31 +00:00
|
|
|
this.editPanel.referenceTarget.focus();
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
2024-05-31 14:27:11 +00:00
|
|
|
} );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.getBodyHeight = function () {
|
|
|
|
// Clamp value to between 300 and 400px height, preferring the actual height if available
|
|
|
|
return Math.min(
|
|
|
|
400,
|
|
|
|
Math.max(
|
|
|
|
300,
|
|
|
|
Math.ceil( this.panels.getCurrentItem().$element[ 0 ].scrollHeight )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWReferenceDialog.super.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.panels = new OO.ui.StackLayout();
|
2024-07-17 17:51:31 +00:00
|
|
|
this.editPanel = new ve.ui.MWReferenceEditPanel( { $overlay: this.$overlay } );
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseSearchPanel = new OO.ui.PanelLayout();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseSearch = new ve.ui.MWReferenceSearchWidget();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Events
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseSearch.getResults().connect( this, { choose: 'onReuseSearchResultsChoose' } );
|
2024-07-17 17:51:31 +00:00
|
|
|
this.editPanel.referenceTarget.connect( this, { change: 'onTargetChange' } );
|
|
|
|
this.editPanel.referenceGroupInput.connect( this, { change: 'onReferenceGroupInputChange' } );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Initialization
|
2024-03-15 18:36:42 +00:00
|
|
|
this.$content.addClass( 've-ui-mwReferenceDialog' );
|
|
|
|
|
2024-07-02 09:34:21 +00:00
|
|
|
this.panels.addItems( [ this.editPanel, this.reuseSearchPanel ] );
|
|
|
|
this.reuseSearchPanel.$element.append( this.reuseSearch.$element );
|
2016-02-03 21:03:41 +00:00
|
|
|
this.$body.append( this.panels.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switches dialog to use existing reference mode.
|
|
|
|
*/
|
2024-07-02 09:34:21 +00:00
|
|
|
ve.ui.MWReferenceDialog.prototype.openReusePanel = function () {
|
2019-01-03 19:42:44 +00:00
|
|
|
this.actions.setMode( 'insert-select' );
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseSearch.buildIndex();
|
|
|
|
this.panels.setItem( this.reuseSearchPanel );
|
|
|
|
this.reuseSearch.getQuery().focus().select();
|
2024-04-22 09:34:46 +00:00
|
|
|
|
|
|
|
// https://phabricator.wikimedia.org/T362347
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'dialog-open-reuse' } );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.getActionProcess = function ( action ) {
|
2019-02-21 21:56:51 +00:00
|
|
|
if ( action === 'insert' || action === 'done' ) {
|
2024-05-31 14:27:11 +00:00
|
|
|
return new OO.ui.Process( () => {
|
2023-08-21 08:08:23 +00:00
|
|
|
const surfaceModel = this.getFragment().getSurface();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2024-07-17 17:51:31 +00:00
|
|
|
this.referenceModel.setGroup( this.editPanel.referenceGroupInput.getValue() );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Insert reference (will auto-create an internal item if needed)
|
|
|
|
if ( !( this.selectedNode instanceof ve.dm.MWReferenceNode ) ) {
|
|
|
|
if ( !this.referenceModel.findInternalItem( surfaceModel ) ) {
|
|
|
|
this.referenceModel.insertInternalItem( surfaceModel );
|
|
|
|
}
|
|
|
|
// Collapse returns a new fragment, so update this.fragment
|
|
|
|
this.fragment = this.getFragment().collapseToEnd();
|
|
|
|
this.referenceModel.insertReferenceNode( this.getFragment() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update internal item
|
|
|
|
this.referenceModel.updateInternalItem( surfaceModel );
|
|
|
|
|
|
|
|
this.close( { action: action } );
|
2024-05-31 14:27:11 +00:00
|
|
|
} );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
return ve.ui.MWReferenceDialog.super.prototype.getActionProcess.call( this, action );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
* @param {Object} [data] Setup data
|
2024-07-02 09:34:21 +00:00
|
|
|
* @param {boolean} [data.reuseReference=false] Open the dialog in "use existing reference" mode
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.getSetupProcess = function ( data ) {
|
|
|
|
data = data || {};
|
|
|
|
return ve.ui.MWReferenceDialog.super.prototype.getSetupProcess.call( this, data )
|
2024-05-31 14:27:11 +00:00
|
|
|
.next( () => {
|
2016-02-03 21:03:41 +00:00
|
|
|
this.panels.setItem( this.editPanel );
|
2024-07-21 18:09:27 +00:00
|
|
|
this.editPanel.setInternalList( this.getFragment().getDocument().getInternalList() );
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) {
|
2024-07-21 18:09:27 +00:00
|
|
|
this.referenceModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.selectedNode );
|
2016-02-03 21:03:41 +00:00
|
|
|
} else {
|
2024-07-21 18:09:27 +00:00
|
|
|
this.referenceModel = new ve.dm.MWReferenceModel( this.getFragment().getDocument() );
|
2019-02-21 21:56:51 +00:00
|
|
|
this.actions.setAbilities( { done: false, insert: false } );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
2024-07-21 18:09:27 +00:00
|
|
|
this.editPanel.setReferenceForEditing( this.referenceModel );
|
|
|
|
this.editPanel.setReadOnly( this.isReadOnly() );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseSearch.setInternalList( this.getFragment().getDocument().getInternalList() );
|
2019-02-15 14:53:54 +00:00
|
|
|
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseReference = !!data.reuseReference;
|
|
|
|
if ( this.reuseReference ) {
|
|
|
|
this.openReusePanel();
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
this.actions.setAbilities( {
|
2019-02-21 21:56:51 +00:00
|
|
|
done: false
|
2016-02-03 21:03:41 +00:00
|
|
|
} );
|
|
|
|
|
2019-07-08 16:04:22 +00:00
|
|
|
this.trackedInputChange = false;
|
2024-05-31 14:27:11 +00:00
|
|
|
} );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.getTeardownProcess = function ( data ) {
|
|
|
|
return ve.ui.MWReferenceDialog.super.prototype.getTeardownProcess.call( this, data )
|
2024-05-31 14:27:11 +00:00
|
|
|
.first( () => {
|
2024-07-17 17:51:31 +00:00
|
|
|
this.editPanel.referenceTarget.getSurface().getModel().disconnect( this );
|
2024-07-21 18:09:27 +00:00
|
|
|
this.editPanel.clear();
|
2024-07-16 14:06:07 +00:00
|
|
|
this.reuseSearch.clearSearch();
|
2016-02-03 21:03:41 +00:00
|
|
|
this.referenceModel = null;
|
2024-05-31 14:27:11 +00:00
|
|
|
} );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWReferenceDialog );
|