Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor UserInterface MWReferenceEditPanel class.
5 *
6 * @copyright 2024 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * Creates a ve.ui.MWReferenceEditPanel object.
12 *
13 * @constructor
14 * @extends OO.ui.PanelLayout
15 * @param {Object} config
16 * @param {jQuery} config.$overlay Layer to render options dropdown outside of the parent dialog
17 */
18 ve.ui.MWReferenceEditPanel = function VeUiMWReferenceEditPanel( config ) {
19 // Configuration initialization
20 config = Object.assign( {
21 scrollable: true,
22 padded: true
23 }, config );
24
25 // Parent constructor
26 ve.ui.MWReferenceEditPanel.super.call( this, { scrollable: true, padded: true } );
27
28 // Initialization
29 this.$element.addClass( 've-ui-mwReferenceEditPanel' );
30
31 // Properties
32 /**
33 * @member {ve.dm.MWDocumentReferences|null}
34 */
35 this.docRefs = null;
36 /**
37 * @member {ve.dm.MWReferenceModel|null}
38 */
39 this.referenceModel = null;
40 /**
41 * @member {string|null}
42 */
43 this.originalGroup = null;
44 /**
45 * If the edit panel is used to insert a sub-reference
46 *
47 * @member {boolean}
48 * @private
49 */
50 this.isInsertingSubRef = false;
51
52 // Create content editor
53 this.referenceTarget = ve.init.target.createTargetWidget(
54 {
55 includeCommands: null,
56 excludeCommands: this.constructor.static.getExcludeCommands(),
57 importRules: this.constructor.static.getImportRules(),
58 inDialog: 'reference',
59 placeholder: ve.msg( 'cite-ve-dialog-reference-placeholder' )
60 }
61 );
62 this.contentFieldset = new OO.ui.FieldsetLayout();
63 this.contentFieldset.$element.append(
64 this.referenceTarget.$element
65 );
66
67 // Create group edit
68 this.optionsFieldset = new OO.ui.FieldsetLayout( {
69 label: ve.msg( 'cite-ve-dialog-reference-options-section' ),
70 icon: 'settings'
71 } );
72 this.referenceGroupInput = new ve.ui.MWReferenceGroupInputWidget( {
73 $overlay: config.$overlay,
74 emptyGroupName: ve.msg( 'cite-ve-dialog-reference-options-group-placeholder' )
75 } );
76 this.referenceGroupField = new OO.ui.FieldLayout( this.referenceGroupInput, {
77 align: 'top',
78 label: ve.msg( 'cite-ve-dialog-reference-options-group-label' )
79 } );
80 this.optionsFieldset.addItems( [ this.referenceGroupField ] );
81
82 // Create warning messages
83 this.reuseWarning = new OO.ui.MessageWidget( {
84 icon: 'alert',
85 inline: true,
86 classes: [ 've-ui-mwReferenceDialog-reuseWarning' ]
87 } );
88
89 this.extendsWarning = new OO.ui.MessageWidget( {
90 icon: 'alert',
91 inline: true,
92 classes: [ 've-ui-mwReferenceDialog-extendsWarning' ]
93 } );
94
95 this.referenceTarget.connect( this, { change: 'onInputChange' } );
96 this.referenceGroupInput.connect( this, { change: 'onInputChange' } );
97
98 // Append to panel element
99 this.$element.append(
100 this.reuseWarning.$element,
101 this.extendsWarning.$element,
102 this.contentFieldset.$element,
103 this.optionsFieldset.$element
104 );
105 };
106
107 /* Inheritance */
108
109 OO.inheritClass( ve.ui.MWReferenceEditPanel, OO.ui.PanelLayout );
110
111 /* Events */
112 /**
113 * A `change` event is emitted whenever the content or value of field is changed.
114 *
115 * @event ve.ui.MWReferenceEditPanel#change
116 * @param {Object} change
117 * @param {boolean} [change.isModified] If changes to the original content or values have been made
118 * @param {boolean} [change.hasContent] If there's non empty content set
119 */
120
121 /* Static Properties */
122 ve.ui.MWReferenceEditPanel.static.excludeCommands = [
123 // No formatting
124 'paragraph',
125 'heading1',
126 'heading2',
127 'heading3',
128 'heading4',
129 'heading5',
130 'heading6',
131 'preformatted',
132 'blockquote',
133 // No tables
134 'insertTable',
135 'deleteTable',
136 'mergeCells',
137 'tableCaption',
138 'tableCellHeader',
139 'tableCellData',
140 // No structure
141 'bullet',
142 'bulletWrapOnce',
143 'number',
144 'numberWrapOnce',
145 'indent',
146 'outdent',
147 // References
148 'reference',
149 'reference/existing',
150 'citoid',
151 'referencesList'
152 ];
153
154 /**
155 * Get the list of disallowed commands for the surface widget to edit the content. This includes
156 * all Cite related commands to disencourage nesting of references.
157 *
158 * @see ve.dm.ElementLinearData#sanitize
159 * @return {string[]} List of commands to exclude
160 */
161 ve.ui.MWReferenceEditPanel.static.getExcludeCommands = function () {
162 const citeCommands = Object.keys( ve.init.target.getSurface().commandRegistry.registry )
163 .filter( ( command ) => command.indexOf( 'cite-' ) !== -1 );
164
165 return ve.ui.MWReferenceEditPanel.static.excludeCommands.concat( citeCommands );
166 };
167
168 /**
169 * Get the import rules for the surface widget to edit the content
170 *
171 * @see ve.dm.ElementLinearData#sanitize
172 * @return {Object} Import rules
173 */
174 ve.ui.MWReferenceEditPanel.static.getImportRules = function () {
175 const rules = ve.copy( ve.init.target.constructor.static.importRules );
176 return ve.extendObject(
177 rules,
178 {
179 all: {
180 blacklist: ve.extendObject(
181 {
182 // Nested references are impossible
183 mwReference: true,
184 mwReferencesList: true,
185 // Lists and tables are actually possible in wikitext with a leading
186 // line break but we prevent creating these with the UI
187 list: true,
188 listItem: true,
189 definitionList: true,
190 definitionListItem: true,
191 table: true,
192 tableCaption: true,
193 tableSection: true,
194 tableRow: true,
195 tableCell: true,
196 mwTable: true,
197 mwTransclusionTableCell: true
198 },
199 ve.getProp( rules, 'all', 'blacklist' )
200 ),
201 // Headings are not possible in wikitext without HTML
202 conversions: ve.extendObject(
203 {
204 mwHeading: 'paragraph'
205 },
206 ve.getProp( rules, 'all', 'conversions' )
207 )
208 }
209 }
210 );
211 };
212
213 /**
214 * @param {ve.dm.MWDocumentReferences} docRefs
215 */
216 ve.ui.MWReferenceEditPanel.prototype.setDocumentReferences = function ( docRefs ) {
217 this.docRefs = docRefs;
218 this.referenceGroupInput.populateMenu( docRefs.getAllGroupNames() );
219 };
220
221 /**
222 * @param {ve.dm.MWReferenceModel} ref
223 */
224 ve.ui.MWReferenceEditPanel.prototype.setReferenceForEditing = function ( ref ) {
225 this.referenceModel = ref;
226 this.isInsertingSubRef = ref.extendsRef && !ref.getDocument().data.hasContent();
227
228 this.setFormFieldsFromRef( ref );
229 this.updateReuseWarningFromRef( ref );
230 this.updateExtendsWarningFromRef( ref );
231 };
232
233 /**
234 * @return {ve.dm.MWReferenceModel|null} Updated reference
235 */
236 ve.ui.MWReferenceEditPanel.prototype.getReferenceFromEditing = function () {
237 if ( this.referenceModel ) {
238 this.referenceModel.setGroup( this.referenceGroupInput.getValue() );
239 }
240
241 return this.referenceModel;
242 };
243
244 /**
245 * @private
246 * @param {ve.dm.MWReferenceModel} ref
247 */
248 ve.ui.MWReferenceEditPanel.prototype.setFormFieldsFromRef = function ( ref ) {
249 this.referenceTarget.setDocument( ref.getDocument() );
250
251 if ( this.isInsertingSubRef ) {
252 this.referenceTarget.getSurface().setPlaceholder(
253 ve.msg( 'cite-ve-dialog-reference-editing-add-details-placeholder' )
254 );
255 }
256
257 this.optionsFieldset.toggle( !ref.extendsRef );
258
259 this.originalGroup = ref.getGroup();
260
261 // Set the group input while it's disabled, so this doesn't pop up the group-picker menu
262 this.referenceGroupInput.setDisabled( true );
263 this.referenceGroupInput.setValue( this.originalGroup );
264 this.referenceGroupInput.setDisabled( false );
265 };
266
267 /**
268 * @private
269 * @param {ve.dm.MWReferenceModel} ref
270 */
271 ve.ui.MWReferenceEditPanel.prototype.updateReuseWarningFromRef = function ( ref ) {
272 const totalUsageCount = this.docRefs
273 .getGroupRefs( ref.getListGroup() )
274 .getTotalUsageCount( ref.getListKey() );
275 this.reuseWarning
276 .toggle( totalUsageCount > 1 )
277 .setLabel( ve.msg( 'cite-ve-dialog-reference-editing-reused-long', totalUsageCount ) );
278 };
279
280 /**
281 * @private
282 * @param {ve.dm.MWReferenceModel} ref
283 */
284 ve.ui.MWReferenceEditPanel.prototype.updateExtendsWarningFromRef = function ( ref ) {
285 if ( ref.extendsRef ) {
286 const parentNode = this.docRefs
287 .getGroupRefs( ref.getListGroup() )
288 .getInternalModelNode( ref.extendsRef );
289 this.extendsWarning.setLabel(
290 $( '<p>' )
291 .text( ve.msg( this.isInsertingSubRef ?
292 'cite-ve-dialog-reference-editing-add-details' :
293 'cite-ve-dialog-reference-editing-extends'
294 ) )
295 .append( parentNode ?
296 new ve.ui.MWPreviewElement( parentNode, { useView: true } ).$element :
297 $( '<div>' )
298 .addClass( 've-ui-mwReferenceContextItem-muted' )
299 .text( ve.msg( 'cite-ve-dialog-reference-missing-parent-ref' ) )
300 )
301 );
302 this.extendsWarning.setIcon( this.isInsertingSubRef ? null : 'alert' );
303 this.extendsWarning.toggle( true );
304 } else {
305 this.extendsWarning.toggle( false );
306 }
307 };
308
309 /**
310 * Handle reference change events
311 *
312 * @private
313 * @fires ve.ui.MWReferenceEditPanel#change
314 */
315 ve.ui.MWReferenceEditPanel.prototype.onInputChange = function () {
316 this.emit( 'change', {
317 isModified: this.isModified(),
318 hasContent: this.documentHasContent()
319 } );
320 };
321
322 /**
323 * Determine whether the reference document we're editing has any content.
324 *
325 * @private
326 * @return {boolean} Document has content
327 */
328 ve.ui.MWReferenceEditPanel.prototype.documentHasContent = function () {
329 // TODO: Check for other types of empty, e.g. only whitespace?
330 return this.referenceModel && this.referenceModel.getDocument().data.hasContent();
331 };
332
333 /**
334 * Determine whether any changes have been made (and haven't been undone).
335 *
336 * @private
337 * @return {boolean} Changes have been made
338 */
339 ve.ui.MWReferenceEditPanel.prototype.isModified = function () {
340 return this.documentHasContent() && ( this.referenceTarget.hasBeenModified() ||
341 this.referenceGroupInput.getValue() !== this.originalGroup );
342 };
343
344 /**
345 * Focus the edit panel
346 */
347 ve.ui.MWReferenceEditPanel.prototype.focus = function () {
348 this.referenceTarget.focus();
349 };
350
351 /**
352 * @param {boolean} [readOnly=false]
353 */
354 ve.ui.MWReferenceEditPanel.prototype.setReadOnly = function ( readOnly ) {
355 this.referenceTarget.setReadOnly( !!readOnly );
356 this.referenceGroupInput.setReadOnly( !!readOnly );
357 };
358
359 ve.ui.MWReferenceEditPanel.prototype.clear = function () {
360 this.referenceTarget.clear();
361 this.referenceModel = null;
362 };