Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.ui.MWCitationNeededContextItem.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor MWCitationNeededContextItem class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * Context item for a citation needed template.
12 *
13 * @constructor
14 * @extends ve.ui.MWDefinedTransclusionContextItem
15 * @param {ve.ui.LinearContext} context Context the item is in
16 * @param {ve.dm.Model} model Model the item is related to
17 * @param {Object} [config]
18 */
19 ve.ui.MWCitationNeededContextItem = function VeUiMWCitationNeededContextItem() {
20 // Parent constructor
21 ve.ui.MWCitationNeededContextItem.super.apply( this, arguments );
22
23 this.addButton = new OO.ui.ButtonWidget( {
24 label: ve.msg( 'cite-ve-citationneeded-button' ),
25 flags: [ 'progressive' ]
26 } ).on( 'click', this.onAddClick.bind( this ) );
27
28 // Remove progressive flag from edit, as addButton is now the
29 // main progressive action in the context.
30 this.editButton.setFlags( { progressive: false } );
31
32 // Initialization
33 this.$element.addClass( 've-ui-mwCitationNeededContextItem' );
34 };
35
36 /* Inheritance */
37
38 OO.inheritClass( ve.ui.MWCitationNeededContextItem, ve.ui.MWDefinedTransclusionContextItem );
39
40 /* Static Properties */
41
42 ve.ui.MWCitationNeededContextItem.static.name = 'citationNeeded';
43
44 ve.ui.MWCitationNeededContextItem.static.icon = 'quotes';
45
46 ve.ui.MWCitationNeededContextItem.static.label = OO.ui.deferMsg( 'cite-ve-citationneeded-title' );
47
48 /* Methods */
49
50 ve.ui.MWCitationNeededContextItem.prototype.onAddClick = function () {
51 const surface = this.context.getSurface();
52 const encapsulatedWikitext = this.getCanonicalParam( 'encapsulate' );
53
54 let promise;
55 if ( encapsulatedWikitext ) {
56 this.addButton.setDisabled( true );
57 promise = ve.init.target
58 .parseWikitextFragment( encapsulatedWikitext, false, this.model.getDocument() )
59 .then( ( response ) => {
60
61 if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
62 return ve.createDeferred().reject().promise();
63 }
64
65 const dmDoc = ve.ui.MWWikitextStringTransferHandler.static
66 .createDocumentFromParsoidHtml(
67 response.visualeditor.content,
68 surface.getModel().getDocument()
69 );
70 const nodes = dmDoc.getDocumentNode().children.filter( ( node ) => !node.isInternal() );
71 let range;
72
73 // Unwrap single content branch nodes to match internal copy/paste behaviour
74 // (which wouldn't put the open and close tags in the clipboard to begin with).
75 if (
76 nodes.length === 1 &&
77 nodes[ 0 ].canContainContent()
78 ) {
79 range = nodes[ 0 ].getRange();
80 }
81
82 surface.getModel().pushStaging();
83 surface.getModel().getFragment()
84 .insertDocument( dmDoc, range ).collapseToEnd().select();
85 return true;
86 } );
87 promise.always( () => {
88 this.addButton.setDisabled( false );
89 } );
90 } else {
91 promise = ve.createDeferred().resolve( false ).promise();
92 }
93
94 // TODO: This assumes Citoid is installed...
95 const action = ve.ui.actionFactory.create( 'citoid', surface );
96 promise.then( ( inStaging ) => {
97 action.open( true, undefined, inStaging );
98 } );
99 ve.track( 'activity.' + this.constructor.static.name, { action: 'context-add-citation' } );
100 };
101
102 /**
103 * @override
104 */
105 ve.ui.MWCitationNeededContextItem.prototype.renderBody = function () {
106 const date = this.getCanonicalParam( 'date' );
107 let description = ve.msg( 'cite-ve-citationneeded-description' );
108
109 if ( date ) {
110 description += ve.msg( 'word-separator' ) + ve.msg( 'parentheses', date );
111 }
112
113 this.$body.empty();
114 this.$body.append( $( '<p>' ).addClass( 've-ui-mwCitationNeededContextItem-description' ).text( description ) );
115
116 const reason = this.getCanonicalParam( 'reason' );
117 if ( reason ) {
118 this.$body.append(
119 $( '<p>' ).addClass( 've-ui-mwCitationNeededContextItem-reason' ).append(
120 document.createTextNode( ve.msg( 'cite-ve-citationneeded-reason' ) + ve.msg( 'word-separator' ) ),
121 // TODO: reason could have HTML entities, but this is rare
122 $( '<em>' ).text( reason )
123 )
124 );
125 }
126 this.$body.append( this.addButton.$element );
127 };
128
129 /* Registration */
130
131 ve.ui.contextItemFactory.register( ve.ui.MWCitationNeededContextItem );