Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWTransclusionOutlineTemplateWidget.js

raw symbols SCIP
1 /**
2 * Container for a template as rendered in the template dialog sidebar.
3 * Contains search and visibility inputs, and a list of parameters when available.
4 *
5 * @class
6 * @extends ve.ui.MWTransclusionOutlinePartWidget
7 *
8 * @constructor
9 * @param {ve.dm.MWTemplateModel} template
10 * @param {boolean} [replacesPlaceholder=false]
11 * @property {ve.dm.MWTemplateModel} templateModel
12 * @property {ve.ui.MWTransclusionOutlineParameterSelectWidget} parameterList
13 */
14 ve.ui.MWTransclusionOutlineTemplateWidget = function VeUiMWTransclusionOutlineTemplateWidget( template, replacesPlaceholder ) {
15 const spec = template.getSpec();
16
17 // Parent constructor
18 ve.ui.MWTransclusionOutlineTemplateWidget.super.call( this, template, {
19 icon: 'puzzle',
20 label: spec.getLabel(),
21 ariaDescriptionUnselected: ve.msg( 'visualeditor-dialog-transclusion-template-widget-aria' ),
22 ariaDescriptionSelected: ve.msg( 'visualeditor-dialog-transclusion-template-widget-aria-selected' ),
23 ariaDescriptionSelectedSingle: ve.msg( 'visualeditor-dialog-transclusion-template-widget-aria-selected-single' )
24 } );
25
26 this.$element.addClass( 've-ui-mwTransclusionOutlineTemplateWidget' );
27
28 // Initialization
29 this.templateModel = template.connect( this, {
30 add: 'onParameterAddedToTemplateModel',
31 remove: 'onParameterRemovedFromTemplateModel'
32 } );
33
34 const canFilter = this.shouldFiltersBeShown(),
35 initiallyHideUnused = canFilter && !replacesPlaceholder && !this.transclusionModel.isSingleTemplate();
36
37 const parameterNames = this.getRelevantTemplateParameters( initiallyHideUnused ? 'used' : 'all' );
38 if ( parameterNames.length ) {
39 this.initializeParameterList();
40 this.parameterList.addItems( parameterNames.map( this.createCheckbox.bind( this ) ) );
41 } else if ( !canFilter ) {
42 this.$noParametersNote = $( '<div>' )
43 .text( ve.msg( 'visualeditor-dialog-transclusion-no-template-parameters' ) )
44 .addClass( 've-ui-mwTransclusionOutlineTemplateWidget-no-template-parameters' );
45 this.$element.append( this.$noParametersNote );
46 }
47
48 this.toggleFilters();
49 if ( initiallyHideUnused ) {
50 // This is only to update the label of the "Hide unused" button
51 this.toggleUnusedWidget.toggleUnusedParameters( false );
52 }
53 };
54
55 /* Inheritance */
56
57 OO.inheritClass( ve.ui.MWTransclusionOutlineTemplateWidget, ve.ui.MWTransclusionOutlinePartWidget );
58
59 /* Events */
60
61 /**
62 * Triggered when the user uses the search widget at the top to filter the list of parameters.
63 *
64 * @event ve.ui.MWTransclusionOutlineTemplateWidget#filterParametersById
65 * @param {Object.<string,boolean>} visibility Keyed by unique id of the parameter, e.g. something
66 * like "part_1/param1". Note this lists only parameters that are currently shown as a checkbox.
67 * The spec might contain more parameters (e.g. deprecated).
68 */
69
70 /**
71 * @event ve.ui.MWTransclusionOutlineTemplateWidget#transclusionOutlineItemSelected
72 * @param {string} id Item ID
73 * @param {boolean} soft If true, focus should stay in the sidebar.
74 */
75
76 /* Static Properties */
77
78 /**
79 * Minimum number of parameters required before search and filter options appear.
80 *
81 * @static
82 * @property {number}
83 */
84 ve.ui.MWTransclusionOutlineTemplateWidget.static.searchableParameterCount = 4;
85
86 /* Methods */
87
88 /**
89 * @private
90 */
91 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.initializeParameterList = function () {
92 if ( this.parameterList ) {
93 return;
94 }
95
96 const $parametersAriaDescription = $( '<span>' )
97 .text( ve.msg( 'visualeditor-dialog-transclusion-param-selection-aria-description' ) )
98 .addClass( 've-ui-mwTransclusionOutline-ariaHidden' );
99
100 this.parameterList = new ve.ui.MWTransclusionOutlineParameterSelectWidget( {
101 ariaLabel: ve.msg( 'visualeditor-dialog-transclusion-param-selection-aria-label', this.templateModel.getSpec().getLabel() ),
102 $ariaDescribedBy: $parametersAriaDescription
103 } ).connect( this, {
104 choose: 'onTemplateParameterChoose',
105 templateParameterSpaceDown: 'onTemplateParameterSpaceDown'
106 } );
107
108 this.$element.append(
109 $parametersAriaDescription,
110 this.parameterList.$element
111 );
112 };
113
114 /**
115 * @private
116 */
117 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.createAllParameterCheckboxes = function () {
118 if ( !this.parameterListComplete ) {
119 this.initializeParameterList();
120 this.getRelevantTemplateParameters().forEach( ( paramName ) => {
121 if ( !this.parameterList.findItemFromData( paramName ) ) {
122 this.parameterList.addItems(
123 [ this.createCheckbox( paramName ) ],
124 this.findCanonicalPosition( paramName )
125 );
126 }
127 } );
128 this.parameterListComplete = true;
129 }
130 };
131
132 /**
133 * @private
134 * @param {string} [filter='all'] Either "used", "unused", or "all"
135 * @return {string[]}
136 */
137 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.getRelevantTemplateParameters = function ( filter ) {
138 const template = this.templateModel;
139
140 let parameterNames;
141 switch ( filter ) {
142 case 'used':
143 parameterNames = template.getOrderedParameterNames();
144 break;
145 case 'unused':
146 parameterNames = template.getAllParametersOrdered().filter( ( name ) => !( name in template.getParameters() ) );
147 break;
148 default:
149 parameterNames = template.getAllParametersOrdered();
150 }
151
152 return parameterNames.filter( ( name ) => {
153 // Don't offer deprecated parameters, unless they are already used
154 if ( template.getSpec().isParameterDeprecated( name ) && !template.hasParameter( name ) ) {
155 return false;
156 }
157 // Never create a checkbox for a not yet named parameter placeholder
158 return !!name;
159 } );
160 };
161
162 /**
163 * @private
164 * @param {string} paramName Parameter name or alias as used in the model
165 * @return {OO.ui.OptionWidget}
166 */
167 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.createCheckbox = function ( paramName ) {
168 const spec = this.templateModel.getSpec(),
169 parameterModel = this.templateModel.getParameter( paramName );
170
171 return ve.ui.MWTransclusionOutlineParameterSelectWidget.static.createItem( {
172 required: spec.isParameterRequired( paramName ),
173 label: spec.getParameterLabel( paramName ),
174 data: paramName,
175 selected: !!parameterModel,
176 hasValue: !!parameterModel && !!parameterModel.getValue()
177 } );
178 };
179
180 /**
181 * @private
182 * @param {string} paramName
183 * @return {number}
184 */
185 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.findCanonicalPosition = function ( paramName ) {
186 // Note this might include parameters that don't have a checkbox, e.g. deprecated
187 const allParamNames = this.templateModel.getAllParametersOrdered();
188 let insertAt = 0;
189 for ( let i = 0; i < allParamNames.length; i++ ) {
190 if ( allParamNames[ i ] === paramName || !this.parameterList.items[ insertAt ] ) {
191 break;
192 } else if ( this.parameterList.items[ insertAt ].getData() === allParamNames[ i ] ) {
193 insertAt++;
194 }
195 }
196 return insertAt;
197 };
198
199 /**
200 * @param {string} [paramName] Parameter name to set, e.g. "param1". Omit to remove setting.
201 */
202 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.setParameter = function ( paramName ) {
203 if ( this.parameterList ) {
204 this.parameterList.setActiveParameter( paramName );
205 }
206 };
207
208 /**
209 * @param {string} paramName
210 * @param {boolean} hasValue
211 */
212 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.toggleHasValue = function ( paramName, hasValue ) {
213 if ( this.parameterList ) {
214 const item = this.parameterList.findItemFromData( paramName );
215 if ( item ) {
216 item.toggleHasValue( hasValue );
217 }
218 }
219 };
220
221 /**
222 * @inheritDoc
223 */
224 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.setSelected = function ( state ) {
225 // FIXME: This is a super-specific hack; should be replaced with a more generic solution
226 if ( !state && this.isSelected() && this.parameterList ) {
227 this.parameterList.highlightItem();
228 }
229 ve.ui.MWTransclusionOutlineTemplateWidget.super.prototype.setSelected.call( this, state );
230 };
231
232 /**
233 * @private
234 * @param {ve.dm.MWParameterModel} param
235 */
236 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onParameterAddedToTemplateModel = function ( param ) {
237 const paramName = param.getName();
238 // The placeholder (currently) doesn't get a corresponding item in the sidebar
239 if ( !paramName ) {
240 return;
241 }
242
243 if ( this.$noParametersNote ) {
244 this.$noParametersNote.remove();
245 delete this.$noParametersNote;
246 }
247
248 this.initializeParameterList();
249
250 // All parameters known via the spec already have a checkbox
251 let item = this.parameterList.findItemFromData( paramName );
252 if ( item ) {
253 // Reset the "hide unused" filter for this field, it's going to be used
254 item.toggle( true );
255 } else {
256 item = this.createCheckbox( paramName );
257 this.parameterList.addItems( [ item ], this.findCanonicalPosition( paramName ) );
258
259 this.toggleFilters();
260
261 // Make sure an active filter is applied to the new checkbox as well
262 const filter = this.searchWidget && this.searchWidget.getValue();
263 if ( filter ) {
264 this.filterParameters( filter );
265 }
266 }
267
268 item.setSelected( true, true );
269
270 // Reset filter, but only if it hides the relevant checkbox
271 if ( !item.isVisible() ) {
272 this.searchWidget.setValue( '' );
273 }
274 };
275
276 /**
277 * @private
278 * @param {ve.dm.MWParameterModel} param
279 */
280 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onParameterRemovedFromTemplateModel = function ( param ) {
281 this.parameterList.markParameterAsUnused( param.getName() );
282 };
283
284 /**
285 * @private
286 * @param {OO.ui.OptionWidget} item
287 * @param {boolean} selected
288 */
289 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onTemplateParameterChoose = function ( item, selected ) {
290 this.toggleParameter( item, selected, false );
291 };
292
293 /**
294 * @private
295 * @param {OO.ui.OptionWidget} item
296 * @param {boolean} selected
297 */
298 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onTemplateParameterSpaceDown = function ( item, selected ) {
299 this.toggleParameter( item, selected, true );
300 };
301
302 /**
303 * @private
304 * @param {OO.ui.OptionWidget} item
305 * @param {boolean} selected
306 * @param {boolean} soft If true, focus should stay in the sidebar.
307 * @fires ve.ui.MWTransclusionOutlineTemplateWidget#transclusionOutlineItemSelected
308 */
309 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.toggleParameter = function ( item, selected, soft ) {
310 const paramName = item.getData();
311 let param = this.templateModel.getParameter( paramName );
312 if ( !selected ) {
313 this.templateModel.removeParameter( param );
314 } else if ( !param ) {
315 param = new ve.dm.MWParameterModel( this.templateModel, paramName );
316 this.templateModel.addParameter( param );
317 }
318
319 this.updateUnusedParameterToggleState();
320
321 if ( param && selected ) {
322 this.emit( 'transclusionOutlineItemSelected', param.getId(), soft );
323 }
324 };
325
326 /**
327 * @private
328 */
329 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.updateUnusedParameterToggleState = function () {
330 if ( this.toggleUnusedWidget ) {
331 this.toggleUnusedWidget.setDisabled( !this.getRelevantTemplateParameters( 'unused' ).length );
332 }
333 };
334
335 /**
336 * @private
337 * @return {boolean}
338 */
339 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.shouldFiltersBeShown = function () {
340 const min = this.constructor.static.searchableParameterCount,
341 existingParameterWidgets = this.parameterList && this.parameterList.getItemCount();
342 // Avoid expensive calls when there are already enough items in the parameter list
343 return existingParameterWidgets >= min || this.getRelevantTemplateParameters().length >= min;
344 };
345
346 /**
347 * @private
348 */
349 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.toggleFilters = function () {
350 const visible = this.shouldFiltersBeShown();
351 if ( this.searchWidget ) {
352 this.searchWidget.toggle( visible );
353 this.toggleUnusedWidget.toggle( visible );
354 } else if ( visible ) {
355 this.initializeFilters();
356 this.updateUnusedParameterToggleState();
357 }
358
359 this.recalculateStickyHeaderHeight();
360 };
361
362 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.recalculateStickyHeaderHeight = function () {
363 // A template with no used parameters might have a sticky header, but no paramater list yet
364 if ( this.$stickyHeader && this.parameterList ) {
365 this.parameterList.stickyHeaderHeight = Math.floor( this.$stickyHeader.outerHeight() );
366 }
367 };
368
369 /**
370 * @private
371 */
372 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.initializeFilters = function () {
373 this.searchWidget = new OO.ui.SearchInputWidget( {
374 title: ve.msg( 'visualeditor-dialog-transclusion-filter-title', this.templateModel.getSpec().getLabel() ),
375 placeholder: ve.msg( 'visualeditor-dialog-transclusion-filter-placeholder' ),
376 classes: [ 've-ui-mwTransclusionOutlineTemplateWidget-searchWidget' ]
377 } ).connect( this, {
378 change: 'filterParameters'
379 } );
380 this.searchWidget.$element.attr( 'role', 'search' );
381
382 this.toggleUnusedWidget = new ve.ui.MWTransclusionOutlineToggleUnusedWidget();
383 this.toggleUnusedWidget.connect( this, {
384 toggleUnusedFields: 'onToggleUnusedFields'
385 } );
386
387 this.infoWidget = new OO.ui.LabelWidget( {
388 label: ve.msg( 'visualeditor-dialog-transclusion-filter-no-match' ),
389 classes: [ 've-ui-mwTransclusionOutlineTemplateWidget-no-match' ]
390 } ).toggle( false );
391
392 this.$stickyHeader = $( '<div>' )
393 .addClass( 've-ui-mwTransclusionOutlineTemplateWidget-sticky' )
394 .append(
395 this.header.$element,
396 this.searchWidget.$element,
397 this.toggleUnusedWidget.$element
398 );
399
400 this.$element.prepend(
401 this.$stickyHeader,
402 this.infoWidget.$element
403 );
404 };
405
406 /**
407 * Narrows the list of checkboxes down to parameters that match the user's input. We search the
408 * parameter's primary name, aliases, label, and description. But not e.g. the example value.
409 *
410 * @private
411 * @param {string} query user input
412 * @fires ve.ui.MWTransclusionOutlineTemplateWidget#filterParametersById
413 */
414 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.filterParameters = function ( query ) {
415 const template = this.templateModel,
416 spec = this.templateModel.getSpec(),
417 visibility = {};
418 let nothingFound = true;
419
420 query = query.trim().toLowerCase();
421 this.createAllParameterCheckboxes();
422
423 // Note: We can't really cache this because the list of know parameters can change any time
424 this.parameterList.items.forEach( ( item ) => {
425 const paramName = item.getData(),
426 placesToSearch = [
427 spec.getPrimaryParameterName( paramName ),
428 spec.getParameterLabel( paramName ),
429 spec.getParameterDescription( paramName )
430 ].concat( spec.getParameterAliases( paramName ) );
431
432 const foundSomeMatch = placesToSearch.some(
433 // Aliases missed validation for a long time and aren't guaranteed to be strings
434 ( term ) => term && typeof term === 'string' && term.toLowerCase().indexOf( query ) !== -1
435 );
436
437 item.toggle( foundSomeMatch );
438
439 nothingFound = nothingFound && !foundSomeMatch;
440
441 const param = template.getParameter( paramName );
442 if ( param ) {
443 visibility[ param.getId() ] = foundSomeMatch;
444 }
445 } );
446
447 this.toggleUnusedWidget.toggle( !query );
448 this.infoWidget.toggle( nothingFound );
449 this.parameterList.setTabIndex( nothingFound ? -1 : 0 );
450 // The "hide unused" button might be hidden now, which changes the height of the sticky header
451 this.recalculateStickyHeaderHeight();
452 this.emit( 'filterParametersById', visibility );
453 };
454
455 /**
456 * @private
457 * @param {boolean} visibility
458 * @param {boolean} [fromClick]
459 */
460 ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onToggleUnusedFields = function ( visibility, fromClick ) {
461 if ( visibility ) {
462 this.createAllParameterCheckboxes();
463 }
464
465 if ( this.parameterList ) {
466 this.parameterList.items.forEach( ( item ) => {
467 item.toggle( visibility || item.isSelected() );
468 } );
469 }
470
471 if ( !visibility && fromClick ) {
472 this.header.scrollElementIntoView().then( () => {
473 if ( this.parameterList ) {
474 this.parameterList.ensureVisibilityOfFirstCheckedParameter();
475 }
476 } );
477 }
478 };