2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-05 21:35:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MWCitationNeededContextItem class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context item for a citation needed template.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.MWDefinedTransclusionContextItem
|
|
|
|
*
|
|
|
|
* @constructor
|
2023-07-10 13:33:24 +00:00
|
|
|
* @param {ve.ui.LinearContext} context Context the item is in
|
|
|
|
* @param {ve.dm.Model} model Model the item is related to
|
2021-12-29 17:22:08 +00:00
|
|
|
* @param {Object} [config]
|
2018-12-05 21:35:04 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCitationNeededContextItem = function VeUiMWCitationNeededContextItem() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWCitationNeededContextItem.super.apply( this, arguments );
|
|
|
|
|
|
|
|
this.addButton = new OO.ui.ButtonWidget( {
|
|
|
|
label: ve.msg( 'cite-ve-citationneeded-button' ),
|
|
|
|
flags: [ 'progressive' ]
|
2020-10-19 13:48:07 +00:00
|
|
|
} ).on( 'click', this.onAddClick.bind( this ) );
|
2018-12-05 21:35:04 +00:00
|
|
|
|
|
|
|
// Remove progressive flag from edit, as addButton is now the
|
|
|
|
// main progressive action in the context.
|
|
|
|
this.editButton.setFlags( { progressive: false } );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$element.addClass( 've-ui-mwCitationNeededContextItem' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWCitationNeededContextItem, ve.ui.MWDefinedTransclusionContextItem );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.MWCitationNeededContextItem.static.name = 'citationNeeded';
|
|
|
|
|
|
|
|
ve.ui.MWCitationNeededContextItem.static.icon = 'quotes';
|
|
|
|
|
|
|
|
ve.ui.MWCitationNeededContextItem.static.label = OO.ui.deferMsg( 'cite-ve-citationneeded-title' );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2020-10-19 13:48:07 +00:00
|
|
|
ve.ui.MWCitationNeededContextItem.prototype.onAddClick = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const contextItem = this;
|
|
|
|
const surface = this.context.getSurface();
|
|
|
|
const encapsulatedWikitext = this.getCanonicalParam( 'encapsulate' );
|
2020-10-19 13:48:07 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let promise;
|
2020-10-19 13:48:07 +00:00
|
|
|
if ( encapsulatedWikitext ) {
|
|
|
|
this.addButton.setDisabled( true );
|
|
|
|
promise = ve.init.target.parseWikitextFragment( encapsulatedWikitext, false, this.model.getDocument() ).then( function ( response ) {
|
|
|
|
|
|
|
|
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
|
|
|
|
return ve.createDeferred().reject().promise();
|
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const dmDoc = ve.ui.MWWikitextStringTransferHandler.static.createDocumentFromParsoidHtml(
|
2020-10-19 13:48:07 +00:00
|
|
|
response.visualeditor.content,
|
|
|
|
surface.getModel().getDocument()
|
|
|
|
);
|
2023-08-21 08:08:23 +00:00
|
|
|
const nodes = dmDoc.getDocumentNode().children.filter( function ( node ) {
|
2020-10-19 13:48:07 +00:00
|
|
|
return !node.isInternal();
|
|
|
|
} );
|
2023-08-21 08:08:23 +00:00
|
|
|
let range;
|
2020-10-19 13:48:07 +00:00
|
|
|
|
|
|
|
// Unwrap single content branch nodes to match internal copy/paste behaviour
|
|
|
|
// (which wouldn't put the open and close tags in the clipboard to begin with).
|
|
|
|
if (
|
|
|
|
nodes.length === 1 &&
|
|
|
|
nodes[ 0 ].canContainContent()
|
|
|
|
) {
|
|
|
|
range = nodes[ 0 ].getRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
surface.getModel().pushStaging();
|
|
|
|
surface.getModel().getFragment().insertDocument( dmDoc, range ).collapseToEnd().select();
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
promise.always( function () {
|
|
|
|
contextItem.addButton.setDisabled( false );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
promise = ve.createDeferred().resolve( false ).promise();
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:28:17 +00:00
|
|
|
// TODO: This assumes Citoid is installed...
|
2023-08-21 08:08:23 +00:00
|
|
|
const action = ve.ui.actionFactory.create( 'citoid', surface );
|
2020-10-19 13:48:07 +00:00
|
|
|
promise.then( function ( inStaging ) {
|
|
|
|
action.open( true, undefined, inStaging );
|
|
|
|
} );
|
|
|
|
ve.track( 'activity.' + this.constructor.static.name, { action: 'context-add-citation' } );
|
|
|
|
};
|
|
|
|
|
2018-12-05 21:35:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWCitationNeededContextItem.prototype.renderBody = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const date = this.getCanonicalParam( 'date' );
|
|
|
|
let description = ve.msg( 'cite-ve-citationneeded-description' );
|
2020-12-14 14:45:25 +00:00
|
|
|
|
|
|
|
if ( date ) {
|
|
|
|
description += ve.msg( 'word-separator' ) + ve.msg( 'parentheses', date );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$body.empty();
|
|
|
|
this.$body.append( $( '<p>' ).addClass( 've-ui-mwCitationNeededContextItem-description' ).text( description ) );
|
2021-11-03 12:28:17 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const reason = this.getCanonicalParam( 'reason' );
|
2020-12-14 14:45:25 +00:00
|
|
|
if ( reason ) {
|
|
|
|
this.$body.append(
|
|
|
|
$( '<p>' ).addClass( 've-ui-mwCitationNeededContextItem-reason' ).append(
|
|
|
|
document.createTextNode( ve.msg( 'cite-ve-citationneeded-reason' ) + ve.msg( 'word-separator' ) ),
|
|
|
|
// TODO: reason could have HTML entities, but this is rare
|
|
|
|
$( '<em>' ).text( reason )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.$body.append( this.addButton.$element );
|
2018-12-05 21:35:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.contextItemFactory.register( ve.ui.MWCitationNeededContextItem );
|