2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*
|
|
|
|
* VisualEditor user interface MWCitationDialog 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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dialog for inserting and editing MediaWiki citations.
|
|
|
|
*
|
|
|
|
* @constructor
|
2024-02-28 08:57:24 +00:00
|
|
|
* @extends ve.ui.MWTemplateDialog
|
2016-02-03 21:03:41 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ui.MWCitationDialog = function VeUiMWCitationDialog( config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWCitationDialog.super.call( this, config );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.referenceModel = null;
|
|
|
|
this.referenceNode = null;
|
|
|
|
this.inDialog = '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2021-09-20 09:00:00 +00:00
|
|
|
OO.inheritClass( ve.ui.MWCitationDialog, ve.ui.MWTransclusionDialog );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
2019-02-15 14:57:44 +00:00
|
|
|
ve.ui.MWCitationDialog.static.name = 'cite';
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the reference node to be edited.
|
|
|
|
*
|
|
|
|
* @return {ve.dm.MWReferenceNode|null} Reference node to be edited, null if none exists
|
|
|
|
*/
|
|
|
|
ve.ui.MWCitationDialog.prototype.getReferenceNode = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const selectedNode = this.getFragment().getSelectedNode();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
if ( selectedNode instanceof ve.dm.MWReferenceNode ) {
|
|
|
|
return selectedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCitationDialog.prototype.getSelectedNode = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const referenceNode = this.getReferenceNode();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let transclusionNode;
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( referenceNode ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const branches = referenceNode.getInternalItem().getChildren();
|
|
|
|
const leaves = branches &&
|
2016-02-03 21:03:41 +00:00
|
|
|
branches.length === 1 &&
|
|
|
|
branches[ 0 ].canContainContent() &&
|
|
|
|
branches[ 0 ].getChildren();
|
|
|
|
transclusionNode = leaves &&
|
|
|
|
leaves.length === 1 &&
|
|
|
|
leaves[ 0 ] instanceof ve.dm.MWTransclusionNode &&
|
|
|
|
leaves[ 0 ];
|
|
|
|
}
|
|
|
|
|
2019-01-09 22:31:21 +00:00
|
|
|
// Only use the selected node if it is the same template as this dialog expects
|
|
|
|
if ( transclusionNode && transclusionNode.isSingleTemplate( this.citationTemplate ) ) {
|
|
|
|
return transclusionNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
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.MWCitationDialog.prototype.initialize = function ( data ) {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWCitationDialog.super.prototype.initialize.call( this, data );
|
|
|
|
|
|
|
|
// HACK: Use the same styling as single-mode transclusion dialog - this should be generalized
|
|
|
|
this.$content.addClass( 've-ui-mwTransclusionDialog-single' );
|
2019-07-22 16:46:57 +00:00
|
|
|
|
|
|
|
this.$content.on( 'change', this.onInputChange.bind( this ) );
|
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.MWCitationDialog.prototype.getSetupProcess = function ( data ) {
|
|
|
|
return ve.ui.MWCitationDialog.super.prototype.getSetupProcess.call( this, data )
|
2019-01-09 22:31:21 +00:00
|
|
|
.first( function () {
|
2016-02-03 21:03:41 +00:00
|
|
|
data = data || {};
|
|
|
|
this.inDialog = data.inDialog;
|
2019-01-09 22:31:21 +00:00
|
|
|
this.citationTemplate = data.template;
|
2019-02-15 14:57:44 +00:00
|
|
|
this.citationTitle = data.title;
|
2019-07-22 16:46:57 +00:00
|
|
|
|
|
|
|
this.trackedCitationInputChange = false;
|
2019-01-09 22:31:21 +00:00
|
|
|
}, this )
|
|
|
|
.next( function () {
|
2019-02-15 14:57:44 +00:00
|
|
|
this.updateTitle();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Initialization
|
2019-01-09 22:31:21 +00:00
|
|
|
this.referenceNode = this.getReferenceNode();
|
|
|
|
if ( this.referenceNode ) {
|
|
|
|
this.referenceModel = ve.dm.MWReferenceModel.static.newFromReferenceNode(
|
|
|
|
this.referenceNode
|
|
|
|
);
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
2019-02-15 14:57:44 +00:00
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2019-02-15 14:57:44 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCitationDialog.prototype.updateTitle = function () {
|
|
|
|
if ( this.citationTitle ) {
|
|
|
|
this.title.setLabel( this.citationTitle );
|
|
|
|
} else {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWCitationDialog.super.prototype.updateTitle.call( this );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
2018-11-29 22:16:23 +00:00
|
|
|
ve.ui.MWCitationDialog.prototype.setApplicableStatus = function () {
|
|
|
|
ve.ui.MWCitationDialog.super.prototype.setApplicableStatus.call( this );
|
2019-02-21 21:56:51 +00:00
|
|
|
// Parent method disables 'done' if no changes were made (this is okay for us), and
|
2018-11-29 22:16:23 +00:00
|
|
|
// disables 'insert' if transclusion is empty (but it is never empty in our case).
|
|
|
|
// Instead, disable 'insert' if no parameters were added.
|
2021-09-27 09:17:38 +00:00
|
|
|
this.actions.setAbilities( { insert: this.transclusionModel.containsValuableData() } );
|
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.MWCitationDialog.prototype.getActionProcess = function ( action ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const dialog = this;
|
2016-02-03 21:03:41 +00:00
|
|
|
if (
|
|
|
|
this.inDialog !== 'reference' &&
|
2019-02-21 21:56:51 +00:00
|
|
|
( action === 'done' || action === 'insert' )
|
2016-02-03 21:03:41 +00:00
|
|
|
) {
|
|
|
|
return new OO.ui.Process( function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const deferred = $.Deferred();
|
2016-02-03 21:03:41 +00:00
|
|
|
dialog.checkRequiredParameters().done( function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const surfaceModel = dialog.getFragment().getSurface();
|
|
|
|
const doc = surfaceModel.getDocument();
|
|
|
|
const internalList = doc.getInternalList();
|
|
|
|
const obj = dialog.transclusionModel.getPlainObject();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2019-01-09 22:31:21 +00:00
|
|
|
// We had a reference, but no template node (or wrong kind of template node)
|
|
|
|
if ( dialog.referenceModel && !dialog.selectedNode ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refDoc = dialog.referenceModel.getDocument();
|
2024-01-30 11:18:52 +00:00
|
|
|
// Empty the existing reference, whatever it contained. This allows
|
|
|
|
// the dialog to be used for arbitrary references (to replace their
|
|
|
|
// contents with a citation).
|
2019-01-09 22:31:21 +00:00
|
|
|
refDoc.commit(
|
2024-01-30 11:18:52 +00:00
|
|
|
ve.dm.TransactionBuilder.static
|
|
|
|
.newFromRemoval( refDoc, refDoc.getDocumentRange(), true )
|
2019-01-09 22:31:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( !dialog.referenceModel ) {
|
|
|
|
// Collapse returns a new fragment, so update dialog.fragment
|
|
|
|
dialog.fragment = dialog.getFragment().collapseToEnd();
|
|
|
|
dialog.referenceModel = new ve.dm.MWReferenceModel( doc );
|
|
|
|
dialog.referenceModel.insertInternalItem( surfaceModel );
|
|
|
|
dialog.referenceModel.insertReferenceNode( dialog.getFragment() );
|
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const item = dialog.referenceModel.findInternalItem( surfaceModel );
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( item ) {
|
|
|
|
if ( dialog.selectedNode ) {
|
|
|
|
dialog.transclusionModel.updateTransclusionNode(
|
|
|
|
surfaceModel, dialog.selectedNode
|
|
|
|
);
|
|
|
|
} else if ( obj !== null ) {
|
|
|
|
dialog.transclusionModel.insertTransclusionNode(
|
2024-01-30 11:18:52 +00:00
|
|
|
// HACK: This is trying to place the cursor inside the first
|
|
|
|
// content branch node but this theoretically not a safe
|
|
|
|
// assumption - in practice, the citation dialog will only reach
|
|
|
|
// this code if we are inserting (not updating) a transclusion, so
|
|
|
|
// the referenceModel will have already initialized the internal
|
|
|
|
// node with a paragraph - getting the range of the item covers
|
|
|
|
// the entire paragraph so we have to get the range of it's first
|
|
|
|
// (and empty) child
|
2016-02-03 21:03:41 +00:00
|
|
|
dialog.getFragment().clone(
|
2018-11-07 15:51:51 +00:00
|
|
|
new ve.dm.LinearSelection( item.getChildren()[ 0 ].getRange() )
|
2016-05-15 11:16:51 +00:00
|
|
|
),
|
|
|
|
'inline'
|
2016-02-03 21:03:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:18:52 +00:00
|
|
|
// HACK: Scorch the earth - this is only needed because without it, the
|
|
|
|
// references list won't re-render properly, and can be removed once
|
|
|
|
// someone fixes that
|
2016-02-03 21:03:41 +00:00
|
|
|
dialog.referenceModel.setDocument(
|
|
|
|
doc.cloneFromRange(
|
|
|
|
internalList.getItemNode( dialog.referenceModel.getListIndex() ).getRange()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
dialog.referenceModel.updateInternalItem( surfaceModel );
|
|
|
|
|
|
|
|
dialog.close( { action: action } );
|
|
|
|
} ).always( deferred.resolve );
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
return ve.ui.MWCitationDialog.super.prototype.getActionProcess.call( this, action );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCitationDialog.prototype.getTeardownProcess = function ( data ) {
|
|
|
|
return ve.ui.MWCitationDialog.super.prototype.getTeardownProcess.call( this, data )
|
|
|
|
.first( function () {
|
|
|
|
// Cleanup
|
|
|
|
this.referenceModel = null;
|
|
|
|
this.referenceNode = null;
|
|
|
|
}, this );
|
|
|
|
};
|
2019-02-15 14:57:44 +00:00
|
|
|
|
2019-07-22 16:46:57 +00:00
|
|
|
/**
|
|
|
|
* Handle change events on the transclusion inputs
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event} ev The browser event
|
|
|
|
*/
|
|
|
|
ve.ui.MWCitationDialog.prototype.onInputChange = function () {
|
|
|
|
if ( !this.trackedCitationInputChange ) {
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'manual-template-input' } );
|
|
|
|
this.trackedCitationInputChange = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 14:57:44 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWCitationDialog );
|