Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor UserInterface MediaWiki MWReferenceDialog 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, editing and re-using MediaWiki references.
12 *
13 * @constructor
14 * @extends ve.ui.NodeDialog
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWReferenceDialog = function VeUiMWReferenceDialog( config ) {
18 // Parent constructor
19 ve.ui.MWReferenceDialog.super.call( this, config );
20
21 // Properties
22 this.reuseReference = false;
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.ui.MWReferenceDialog, ve.ui.NodeDialog );
28
29 /* Static Properties */
30
31 ve.ui.MWReferenceDialog.static.name = 'reference';
32
33 ve.ui.MWReferenceDialog.static.title =
34 OO.ui.deferMsg( 'cite-ve-dialog-reference-title' );
35
36 ve.ui.MWReferenceDialog.static.actions = [
37 {
38 action: 'done',
39 label: OO.ui.deferMsg( 'visualeditor-dialog-action-apply' ),
40 flags: [ 'progressive', 'primary' ],
41 modes: 'edit'
42 },
43 {
44 action: 'insert',
45 label: OO.ui.deferMsg( 'visualeditor-dialog-action-insert' ),
46 flags: [ 'progressive', 'primary' ],
47 modes: 'insert'
48 },
49 {
50 label: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ),
51 flags: [ 'safe', 'close' ],
52 modes: [ 'readonly', 'insert', 'edit', 'insert-select' ]
53 }
54 ];
55
56 ve.ui.MWReferenceDialog.static.modelClasses = [ ve.dm.MWReferenceNode ];
57
58 /* Methods */
59 /**
60 * Handle ve.ui.MWReferenceEditPanel#change events
61 *
62 * @param {Object} change
63 * @param {boolean} [change.isModified] If changes to the original content or values have been made
64 * @param {boolean} [change.hasContent] If there's non empty content set
65 */
66 ve.ui.MWReferenceDialog.prototype.onEditPanelInputChange = function ( change ) {
67 this.actions.setAbilities( {
68 done: change.isModified,
69 insert: change.hasContent
70 } );
71
72 if ( !this.trackedInputChange ) {
73 ve.track( 'activity.' + this.constructor.static.name, { action: 'input' } );
74 this.trackedInputChange = true;
75 }
76 };
77
78 /**
79 * Handle search results ref reuse events.
80 *
81 * @param {ve.dm.MWReferenceModel} ref
82 */
83 ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsReuse = function ( ref ) {
84 if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) {
85 this.getFragment().removeContent();
86 this.selectedNode = null;
87 }
88
89 this.insertReference( ref );
90
91 ve.track( 'activity.' + this.constructor.static.name, { action: 'reuse-choose' } );
92
93 this.close( { action: 'insert' } );
94 };
95
96 /**
97 * Handle search results popup menu extends events.
98 *
99 * @param {ve.dm.MWReferenceModel} originalRef
100 */
101 ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsExtends = function ( originalRef ) {
102 const newRef = new ve.dm.MWReferenceModel( this.getFragment().getDocument() );
103 newRef.extendsRef = originalRef.getListKey();
104 newRef.group = originalRef.getGroup();
105
106 this.actions.setMode( 'insert' );
107 this.panels.setItem( this.editPanel );
108 this.title.setLabel( ve.msg( 'cite-ve-dialog-reference-title-add-details' ) );
109
110 const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc(
111 this.getFragment().getDocument()
112 );
113 this.editPanel.setDocumentReferences( docRefs );
114
115 this.actions.setAbilities( { insert: false } );
116
117 this.editPanel.setReferenceForEditing( newRef );
118 this.editPanel.setReadOnly( this.isReadOnly() );
119
120 this.trackedInputChange = false;
121 };
122
123 /**
124 * @override
125 */
126 ve.ui.MWReferenceDialog.prototype.getReadyProcess = function ( data ) {
127 return ve.ui.MWReferenceDialog.super.prototype.getReadyProcess.call( this, data )
128 .next( () => {
129 if ( this.reuseReference ) {
130 this.reuseSearch.getQuery().focus().select();
131 } else {
132 this.editPanel.focus();
133 }
134 } );
135 };
136
137 /**
138 * @override
139 */
140 ve.ui.MWReferenceDialog.prototype.getBodyHeight = function () {
141 // Clamp value to between 300 and 400px height, preferring the actual height if available
142 return Math.min(
143 400,
144 Math.max(
145 300,
146 Math.ceil( this.panels.getCurrentItem().$element[ 0 ].scrollHeight )
147 )
148 );
149 };
150
151 /**
152 * @override
153 */
154 ve.ui.MWReferenceDialog.prototype.initialize = function () {
155 // Parent method
156 ve.ui.MWReferenceDialog.super.prototype.initialize.call( this );
157
158 // Properties
159 this.panels = new OO.ui.StackLayout();
160 this.editPanel = new ve.ui.MWReferenceEditPanel( { $overlay: this.$overlay } );
161 this.reuseSearchPanel = new OO.ui.PanelLayout();
162
163 this.reuseSearch = new ve.ui.MWReferenceSearchWidget( { $overlay: this.$overlay } );
164
165 // Events
166 this.reuseSearch.connect( this, {
167 reuse: 'onReuseSearchResultsReuse',
168 extends: 'onReuseSearchResultsExtends'
169 } );
170 this.editPanel.connect( this, { change: 'onEditPanelInputChange' } );
171
172 // Initialization
173 this.$content.addClass( 've-ui-mwReferenceDialog' );
174
175 this.panels.addItems( [ this.editPanel, this.reuseSearchPanel ] );
176 this.reuseSearchPanel.$element.append( this.reuseSearch.$element );
177 this.$body.append( this.panels.$element );
178 };
179
180 /**
181 * Switches dialog to use existing reference mode.
182 */
183 ve.ui.MWReferenceDialog.prototype.openReusePanel = function () {
184 this.actions.setMode( 'insert-select' );
185 this.reuseSearch.buildIndex();
186 this.panels.setItem( this.reuseSearchPanel );
187
188 // https://phabricator.wikimedia.org/T362347
189 ve.track( 'activity.' + this.constructor.static.name, { action: 'dialog-open-reuse' } );
190 };
191
192 /**
193 * Insert a reference at the end of the selection, could also be a reuse of an exising reference
194 *
195 * @private
196 * @param {ve.dm.MWReferenceModel} ref
197 */
198 ve.ui.MWReferenceDialog.prototype.insertReference = function ( ref ) {
199 const surfaceModel = this.getFragment().getSurface();
200
201 if ( !ref.findInternalItem( surfaceModel ) ) {
202 ref.insertInternalItem( surfaceModel );
203 }
204 // Collapse returns a new fragment, so update this.fragment
205 this.fragment = this.getFragment().collapseToEnd();
206 ref.insertReferenceNode( this.getFragment() );
207 };
208
209 /**
210 * @override
211 */
212 ve.ui.MWReferenceDialog.prototype.getActionProcess = function ( action ) {
213 if ( action === 'insert' || action === 'done' ) {
214 return new OO.ui.Process( () => {
215 const ref = this.editPanel.getReferenceFromEditing();
216
217 if ( !( this.selectedNode instanceof ve.dm.MWReferenceNode ) ) {
218 this.insertReference( ref );
219 }
220
221 ref.updateInternalItem( this.getFragment().getSurface() );
222
223 this.close( { action: action } );
224 } );
225 }
226 return ve.ui.MWReferenceDialog.super.prototype.getActionProcess.call( this, action );
227 };
228
229 /**
230 * @override
231 * @param {Object} [data] Setup data
232 * @param {boolean} [data.reuseReference=false] Open the dialog in "use existing reference" mode
233 * @param {ve.dm.MWReferenceModel} [data.createSubRef] Open the dialog to add additional details to a reuse
234 */
235 ve.ui.MWReferenceDialog.prototype.getSetupProcess = function ( data ) {
236 data = data || {};
237 return ve.ui.MWReferenceDialog.super.prototype.getSetupProcess.call( this, data )
238 .next( () => {
239 this.reuseReference = !!data.reuseReference;
240 if ( this.reuseReference ) {
241 this.reuseSearch.setInternalList( this.getFragment().getDocument().getInternalList() );
242 this.openReusePanel();
243 } else if ( data.createSubRef ) {
244 if ( this.selectedNode instanceof ve.dm.MWReferenceNode &&
245 this.selectedNode.getAttribute( 'placeholder' ) ) {
246 // remove the placeholder node from Citoid
247 this.getFragment().removeContent();
248
249 }
250 // we never want to edit an existing node here
251 this.selectedNode = null;
252 this.onReuseSearchResultsExtends( data.createSubRef );
253 } else {
254 this.panels.setItem( this.editPanel );
255 const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc(
256 this.getFragment().getDocument()
257 );
258 this.editPanel.setDocumentReferences( docRefs );
259
260 let ref;
261 if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) {
262 // edit an existing reference
263 ref = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.selectedNode );
264 if ( ref.extendsRef ) {
265 this.title.setLabel( ve.msg( 'cite-ve-dialog-reference-title-edit-details' ) );
266 }
267 this.actions.setAbilities( { done: false } );
268 } else {
269 // create a new reference
270 ref = new ve.dm.MWReferenceModel( this.getFragment().getDocument() );
271 this.actions.setAbilities( { insert: false } );
272 }
273 this.editPanel.setReferenceForEditing( ref );
274 this.editPanel.setReadOnly( this.isReadOnly() );
275 }
276
277 this.trackedInputChange = false;
278 } );
279 };
280
281 /**
282 * @override
283 */
284 ve.ui.MWReferenceDialog.prototype.getTeardownProcess = function ( data ) {
285 return ve.ui.MWReferenceDialog.super.prototype.getTeardownProcess.call( this, data )
286 .first( () => {
287 this.editPanel.clear();
288 this.reuseSearch.clearSearch();
289 } );
290 };
291
292 /* Registration */
293
294 ve.ui.windowFactory.register( ve.ui.MWReferenceDialog );