Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 /*
4 * VisualEditor user interface MWCitationDialog class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * Dialog for inserting and editing MediaWiki citations that use the templates that are set up for
12 * the VisualEditor citation tool.
13 *
14 * @constructor
15 * @extends ve.ui.MWTemplateDialog
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.MWCitationDialog = function VeUiMWCitationDialog( config ) {
19 // Parent constructor
20 ve.ui.MWCitationDialog.super.call( this, config );
21
22 // Properties
23 this.referenceModel = null;
24 this.referenceNode = null;
25 this.inDialog = '';
26 };
27
28 /* Inheritance */
29
30 OO.inheritClass( ve.ui.MWCitationDialog, ve.ui.MWTransclusionDialog );
31
32 /* Static Properties */
33
34 ve.ui.MWCitationDialog.static.name = 'cite';
35
36 /* Methods */
37
38 /**
39 * Get the reference node to be edited.
40 *
41 * @return {ve.dm.MWReferenceNode|null} Reference node to be edited, null if none exists
42 */
43 ve.ui.MWCitationDialog.prototype.getReferenceNode = function () {
44 const selectedNode = this.getFragment().getSelectedNode();
45
46 if ( selectedNode instanceof ve.dm.MWReferenceNode ) {
47 return selectedNode;
48 }
49
50 return null;
51 };
52
53 /**
54 * @override
55 */
56 ve.ui.MWCitationDialog.prototype.getSelectedNode = function () {
57 const referenceNode = this.getReferenceNode();
58
59 if ( referenceNode ) {
60 const branches = referenceNode.getInternalItem().getChildren();
61 const leaves = branches &&
62 branches.length === 1 &&
63 branches[ 0 ].canContainContent() &&
64 branches[ 0 ].getChildren();
65 const transclusionNode = leaves &&
66 leaves.length === 1 &&
67 leaves[ 0 ] instanceof ve.dm.MWTransclusionNode &&
68 leaves[ 0 ];
69
70 // Only use the selected node if it is the same template as this dialog expects
71 if ( transclusionNode && transclusionNode.isSingleTemplate( this.citationTemplate ) ) {
72 return transclusionNode;
73 }
74 }
75
76 return null;
77 };
78
79 /**
80 * @override
81 */
82 ve.ui.MWCitationDialog.prototype.initialize = function ( data ) {
83 // Parent method
84 ve.ui.MWCitationDialog.super.prototype.initialize.call( this, data );
85
86 // HACK: Use the same styling as single-mode transclusion dialog - this should be generalized
87 this.$content.addClass( 've-ui-mwTransclusionDialog-single' );
88
89 this.$content.on( 'change', this.onInputChange.bind( this ) );
90 };
91
92 /**
93 * @override
94 */
95 ve.ui.MWCitationDialog.prototype.getSetupProcess = function ( data ) {
96 return ve.ui.MWCitationDialog.super.prototype.getSetupProcess.call( this, data )
97 .first( () => {
98 data = data || {};
99 this.inDialog = data.inDialog;
100 this.citationTemplate = data.template;
101 this.citationTitle = data.title;
102
103 this.trackedCitationInputChange = false;
104 } )
105 .next( () => {
106 this.updateTitle();
107
108 // Initialization
109 this.referenceNode = this.getReferenceNode();
110 if ( this.referenceNode ) {
111 this.referenceModel = ve.dm.MWReferenceModel.static.newFromReferenceNode(
112 this.referenceNode
113 );
114 }
115 } );
116 };
117
118 /**
119 * @override
120 */
121 ve.ui.MWCitationDialog.prototype.updateTitle = function () {
122 if ( this.citationTitle ) {
123 this.title.setLabel( this.citationTitle );
124 } else {
125 // Parent method
126 ve.ui.MWCitationDialog.super.prototype.updateTitle.call( this );
127 }
128 };
129
130 /**
131 * @override
132 */
133 ve.ui.MWCitationDialog.prototype.setApplicableStatus = function () {
134 ve.ui.MWCitationDialog.super.prototype.setApplicableStatus.call( this );
135 // Parent method disables 'done' if no changes were made (this is okay for us), and
136 // disables 'insert' if transclusion is empty (but it is never empty in our case).
137 // Instead, disable 'insert' if no parameters were added.
138 this.actions.setAbilities( { insert: this.transclusionModel.containsValuableData() } );
139 };
140
141 /**
142 * @override
143 */
144 ve.ui.MWCitationDialog.prototype.getActionProcess = function ( action ) {
145 if (
146 this.inDialog !== 'reference' &&
147 ( action === 'done' || action === 'insert' )
148 ) {
149 return new OO.ui.Process( () => {
150 const deferred = $.Deferred();
151 this.checkRequiredParameters().done( () => {
152 const surfaceModel = this.getFragment().getSurface();
153 const doc = surfaceModel.getDocument();
154
155 // We had a reference, but no template node (or wrong kind of template node)
156 if ( this.referenceModel && !this.selectedNode ) {
157 const refDoc = this.referenceModel.getDocument();
158 // Empty the existing reference, whatever it contained. This allows
159 // the dialog to be used for arbitrary references (to replace their
160 // contents with a citation).
161 refDoc.commit(
162 ve.dm.TransactionBuilder.static
163 .newFromRemoval( refDoc, refDoc.getDocumentRange(), true )
164 );
165 }
166
167 if ( !this.referenceModel ) {
168 // Collapse returns a new fragment, so update this.fragment
169 this.fragment = this.getFragment().collapseToEnd();
170 this.referenceModel = new ve.dm.MWReferenceModel( doc );
171 this.referenceModel.insertInternalItem( surfaceModel );
172 this.referenceModel.insertReferenceNode( this.getFragment() );
173 }
174
175 const item = this.referenceModel.findInternalItem( surfaceModel );
176 if ( item ) {
177 if ( this.selectedNode ) {
178 this.transclusionModel.updateTransclusionNode(
179 surfaceModel, this.selectedNode
180 );
181 } else if ( this.transclusionModel.getPlainObject() !== null ) {
182 this.transclusionModel.insertTransclusionNode(
183 // HACK: This is trying to place the cursor inside the first
184 // content branch node but this theoretically not a safe
185 // assumption - in practice, the citation dialog will only reach
186 // this code if we are inserting (not updating) a transclusion, so
187 // the referenceModel will have already initialized the internal
188 // node with a paragraph - getting the range of the item covers
189 // the entire paragraph so we have to get the range of it's first
190 // (and empty) child
191 this.getFragment().clone(
192 new ve.dm.LinearSelection( item.getChildren()[ 0 ].getRange() )
193 ),
194 'inline'
195 );
196 }
197 }
198
199 // HACK: Scorch the earth - this is only needed because without it, the
200 // references list won't re-render properly, and can be removed once
201 // someone fixes that
202 this.referenceModel.setDocument(
203 doc.cloneFromRange(
204 doc.getInternalList().getItemNode( this.referenceModel.getListIndex() ).getRange()
205 )
206 );
207 this.referenceModel.updateInternalItem( surfaceModel );
208
209 this.close( { action: action } );
210 } ).always( deferred.resolve );
211
212 return deferred;
213 } );
214 }
215
216 // Parent method
217 return ve.ui.MWCitationDialog.super.prototype.getActionProcess.call( this, action );
218 };
219
220 /**
221 * @override
222 */
223 ve.ui.MWCitationDialog.prototype.getTeardownProcess = function ( data ) {
224 return ve.ui.MWCitationDialog.super.prototype.getTeardownProcess.call( this, data )
225 .first( () => {
226 // Cleanup
227 this.referenceModel = null;
228 this.referenceNode = null;
229 } );
230 };
231
232 /**
233 * Handle change events on the transclusion inputs
234 *
235 * @param {jQuery.Event} ev The browser event
236 */
237 ve.ui.MWCitationDialog.prototype.onInputChange = function () {
238 if ( !this.trackedCitationInputChange ) {
239 ve.track( 'activity.' + this.constructor.static.name, { action: 'manual-template-input' } );
240 this.trackedCitationInputChange = true;
241 }
242 };
243
244 /* Registration */
245
246 ve.ui.windowFactory.register( ve.ui.MWCitationDialog );