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
|
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 */
|
2024-07-21 18:09:27 +00:00
|
|
|
/**
|
2024-07-27 19:15:04 +00:00
|
|
|
* Handle ve.ui.MWReferenceEditPanel#change events
|
2016-02-03 21:03:41 +00:00
|
|
|
*
|
2024-07-27 19:15:04 +00:00
|
|
|
* @param {Object} change
|
|
|
|
* @param {boolean} [change.isModified] If changes to the original content or values have been made
|
|
|
|
* @param {boolean} [change.hasContent] If there's non empty content set
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
2024-07-27 19:15:04 +00:00
|
|
|
ve.ui.MWReferenceDialog.prototype.onEditPanelInputChange = function ( change ) {
|
2016-02-03 21:03:41 +00:00
|
|
|
this.actions.setAbilities( {
|
2024-07-27 19:15:04 +00:00
|
|
|
done: change.isModified,
|
|
|
|
insert: change.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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-09-30 09:17:36 +00:00
|
|
|
* Handle search results ref reuse events.
|
2016-02-03 21:03:41 +00:00
|
|
|
*
|
2024-09-30 09:17:36 +00:00
|
|
|
* @param {ve.dm.MWReferenceModel} ref
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
2024-09-30 09:17:36 +00:00
|
|
|
ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsReuse = function ( ref ) {
|
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
|
|
|
|
2024-08-01 15:08:33 +00:00
|
|
|
this.insertReference( ref );
|
2019-07-08 16:04:22 +00:00
|
|
|
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'reuse-choose' } );
|
2024-08-01 15:08:33 +00:00
|
|
|
|
|
|
|
this.close( { action: 'insert' } );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
2024-09-23 09:49:21 +00:00
|
|
|
/**
|
|
|
|
* Handle search results popup menu extends events.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWReferenceModel} originalRef
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsExtends = function ( originalRef ) {
|
|
|
|
const newRef = new ve.dm.MWReferenceModel( this.getFragment().getDocument() );
|
|
|
|
newRef.extendsRef = originalRef.getListKey();
|
|
|
|
newRef.group = originalRef.getGroup();
|
|
|
|
|
2024-10-06 15:17:48 +00:00
|
|
|
this.actions.setMode( 'insert' );
|
|
|
|
this.panels.setItem( this.editPanel );
|
|
|
|
this.title.setLabel( ve.msg( 'cite-ve-dialog-reference-title-add-details' ) );
|
|
|
|
|
|
|
|
const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc(
|
|
|
|
this.getFragment().getDocument()
|
|
|
|
);
|
|
|
|
this.editPanel.setDocumentReferences( docRefs );
|
|
|
|
|
|
|
|
this.actions.setAbilities( { insert: false } );
|
|
|
|
|
|
|
|
this.editPanel.setReferenceForEditing( newRef );
|
|
|
|
this.editPanel.setReadOnly( this.isReadOnly() );
|
|
|
|
|
|
|
|
this.trackedInputChange = false;
|
2024-09-23 09:49:21 +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.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-27 19:15:04 +00:00
|
|
|
this.editPanel.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-09-23 09:49:21 +00:00
|
|
|
this.reuseSearch = new ve.ui.MWReferenceSearchWidget( { $overlay: this.$overlay } );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Events
|
2024-09-23 09:49:21 +00:00
|
|
|
this.reuseSearch.connect( this, {
|
|
|
|
reuse: 'onReuseSearchResultsReuse',
|
|
|
|
extends: 'onReuseSearchResultsExtends'
|
|
|
|
} );
|
2024-07-27 19:15:04 +00:00
|
|
|
this.editPanel.connect( this, { change: 'onEditPanelInputChange' } );
|
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 );
|
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-08-01 15:08:33 +00:00
|
|
|
/**
|
|
|
|
* Insert a reference at the end of the selection, could also be a reuse of an exising reference
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {ve.dm.MWReferenceModel} ref
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceDialog.prototype.insertReference = function ( ref ) {
|
|
|
|
const surfaceModel = this.getFragment().getSurface();
|
|
|
|
|
|
|
|
if ( !ref.findInternalItem( surfaceModel ) ) {
|
|
|
|
ref.insertInternalItem( surfaceModel );
|
|
|
|
}
|
|
|
|
// Collapse returns a new fragment, so update this.fragment
|
|
|
|
this.fragment = this.getFragment().collapseToEnd();
|
|
|
|
ref.insertReferenceNode( this.getFragment() );
|
|
|
|
};
|
|
|
|
|
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( () => {
|
2024-08-13 17:37:50 +00:00
|
|
|
const ref = this.editPanel.getReferenceFromEditing();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
if ( !( this.selectedNode instanceof ve.dm.MWReferenceNode ) ) {
|
2024-08-13 17:37:50 +00:00
|
|
|
this.insertReference( ref );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 17:37:50 +00:00
|
|
|
ref.updateInternalItem( this.getFragment().getSurface() );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
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
|
2024-10-09 09:00:25 +00:00
|
|
|
* @param {ve.dm.MWReferenceModel} [data.createSubRef] Open the dialog to add additional details to a reuse
|
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( () => {
|
2024-07-02 09:34:21 +00:00
|
|
|
this.reuseReference = !!data.reuseReference;
|
|
|
|
if ( this.reuseReference ) {
|
2024-09-19 08:22:57 +00:00
|
|
|
this.reuseSearch.setInternalList( this.getFragment().getDocument().getInternalList() );
|
2024-07-02 09:34:21 +00:00
|
|
|
this.openReusePanel();
|
2024-10-09 09:00:25 +00:00
|
|
|
} else if ( data.createSubRef ) {
|
2024-10-10 11:24:54 +00:00
|
|
|
if ( this.selectedNode instanceof ve.dm.MWReferenceNode &&
|
|
|
|
this.selectedNode.getAttribute( 'placeholder' ) ) {
|
|
|
|
// remove the placeholder node from Citoid
|
2024-10-09 09:00:25 +00:00
|
|
|
this.getFragment().removeContent();
|
2024-10-10 11:24:54 +00:00
|
|
|
|
2024-10-09 09:00:25 +00:00
|
|
|
}
|
2024-10-10 11:24:54 +00:00
|
|
|
// we never want to edit an existing node here
|
|
|
|
this.selectedNode = null;
|
2024-10-09 09:00:25 +00:00
|
|
|
this.onReuseSearchResultsExtends( data.createSubRef );
|
2024-09-19 08:22:57 +00:00
|
|
|
} else {
|
|
|
|
this.panels.setItem( this.editPanel );
|
|
|
|
const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc(
|
|
|
|
this.getFragment().getDocument()
|
|
|
|
);
|
|
|
|
this.editPanel.setDocumentReferences( docRefs );
|
|
|
|
|
|
|
|
let ref;
|
|
|
|
if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) {
|
|
|
|
// edit an existing reference
|
|
|
|
ref = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.selectedNode );
|
|
|
|
if ( ref.extendsRef ) {
|
|
|
|
this.title.setLabel( ve.msg( 'cite-ve-dialog-reference-title-edit-details' ) );
|
|
|
|
}
|
|
|
|
this.actions.setAbilities( { done: false } );
|
|
|
|
} else {
|
|
|
|
// create a new reference
|
|
|
|
ref = new ve.dm.MWReferenceModel( this.getFragment().getDocument() );
|
|
|
|
this.actions.setAbilities( { insert: false } );
|
|
|
|
}
|
|
|
|
this.editPanel.setReferenceForEditing( ref );
|
|
|
|
this.editPanel.setReadOnly( this.isReadOnly() );
|
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-21 18:09:27 +00:00
|
|
|
this.editPanel.clear();
|
2024-07-16 14:06:07 +00:00
|
|
|
this.reuseSearch.clearSearch();
|
2024-05-31 14:27:11 +00:00
|
|
|
} );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWReferenceDialog );
|