Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/pages/ve.ui.MWParameterPage.js

raw symbols SCIP
1 /*!
2 * VisualEditor user interface MWParameterPage class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Container for editing the value of a parameter in the template dialog
10 * content pane. Includes a dynamic value input depending on the parameter's
11 * type documented in TemplateData.
12 *
13 * @class
14 * @extends OO.ui.PageLayout
15 *
16 * @constructor
17 * @param {ve.dm.MWParameterModel} parameter Template parameter
18 * @param {Object} [config] Configuration options
19 * @param {jQuery} [config.$overlay] Overlay to render dropdowns in
20 * @param {boolean} [config.readOnly] Parameter is read-only
21 */
22 ve.ui.MWParameterPage = function VeUiMWParameterPage( parameter, config ) {
23 const paramName = parameter.getName();
24
25 // Configuration initialization
26 config = ve.extendObject( {
27 scrollable: false
28 }, config );
29
30 // Parent constructor
31 ve.ui.MWParameterPage.super.call( this, parameter.getId(), config );
32
33 // Properties
34 this.edited = false;
35 this.parameter = parameter;
36 this.spec = parameter.getTemplate().getSpec();
37 this.defaultValue = parameter.getDefaultValue();
38 this.exampleValue = parameter.getExampleValue();
39 this.hasValue = null;
40
41 this.$info = $( '<div>' );
42 this.$field = $( '<div>' );
43
44 // Construct the field docs for the template description
45 const $doc = $( '<div>' )
46 .attr( 'id', OO.ui.generateElementId() )
47 .addClass( 've-ui-mwParameterPage-doc' );
48 const description = this.spec.getParameterDescription( paramName );
49 if ( description ) {
50 $( '<p>' ).text( description ).appendTo( $doc );
51 }
52
53 // Note: Calling createValueInput() sets some properties we rely on later in this function
54 this.valueInput = this.createValueInput()
55 .setValue( this.parameter.getValue() )
56 .connect( this, { change: 'onValueInputChange' } );
57
58 this.valueInput.$input.attr( 'aria-describedby', $doc.attr( 'id' ) );
59
60 if ( config.readOnly && this.valueInput.setReadOnly ) {
61 this.valueInput.setReadOnly( true );
62 }
63
64 const labelElement = new OO.ui.LabelWidget( {
65 input: this.valueInput,
66 label: this.spec.getParameterLabel( paramName ),
67 classes: [ 've-ui-mwParameterPage-label' ]
68 } );
69
70 let statusIndicator;
71 if ( this.parameter.isRequired() ) {
72 $( '<p>' )
73 .addClass( 've-ui-mwParameterPage-doc-required' )
74 .text( ve.msg( 'visualeditor-dialog-transclusion-required-parameter-description' ) )
75 .appendTo( $doc );
76 } else if ( this.parameter.isDeprecated() ) {
77 statusIndicator = new OO.ui.IndicatorWidget( {
78 classes: [ 've-ui-mwParameterPage-statusIndicator' ],
79 indicator: 'alert',
80 title: ve.msg( 'visualeditor-dialog-transclusion-deprecated-parameter' )
81 } );
82 $( '<p>' )
83 .addClass( 've-ui-mwParameterPage-doc-deprecated' )
84 .text( ve.msg(
85 'visualeditor-dialog-transclusion-deprecated-parameter-description',
86 this.spec.getParameterDeprecationDescription( paramName )
87 ) )
88 .appendTo( $doc );
89 }
90
91 if ( this.defaultValue ) {
92 $( '<p>' )
93 .addClass( 've-ui-mwParameterPage-doc-default' )
94 .text( ve.msg( 'visualeditor-dialog-transclusion-param-default', this.defaultValue ) )
95 .appendTo( $doc );
96 }
97
98 if ( this.exampleValue ) {
99 $( '<p>' )
100 .addClass( 've-ui-mwParameterPage-doc-example' )
101 .text( ve.msg(
102 'visualeditor-dialog-transclusion-param-example-long',
103 this.exampleValue
104 ) )
105 .appendTo( $doc );
106 }
107
108 // Initialization
109 this.$info
110 .addClass( 've-ui-mwParameterPage-info' )
111 .append( labelElement.$element );
112 if ( statusIndicator ) {
113 this.$info.append( ' ', statusIndicator.$element );
114 }
115 this.$field
116 .addClass( 've-ui-mwParameterPage-field' )
117 .append(
118 this.valueInput.$element
119 );
120
121 if ( !this.parameter.isDocumented() ) {
122 $( '<span>' )
123 .addClass( 've-ui-mwParameterPage-undocumentedLabel' )
124 .text( ve.msg( 'visualeditor-dialog-transclusion-param-undocumented' ) )
125 .appendTo( labelElement.$element );
126 }
127
128 this.$element
129 .addClass( 've-ui-mwParameterPage' )
130 .append( this.$info, this.$field );
131
132 if ( $doc.children().length ) {
133 this.$field.addClass( 've-ui-mwParameterPage-inlineDescription' );
134 this.collapsibleDoc = new ve.ui.MWExpandableContentElement( {
135 classes: [ 've-ui-mwParameterPage-inlineDescription' ],
136 $content: $doc
137 } );
138 this.$info.after( this.collapsibleDoc.$element );
139 }
140 };
141
142 /* Inheritance */
143
144 OO.inheritClass( ve.ui.MWParameterPage, OO.ui.PageLayout );
145
146 /* Events */
147
148 /**
149 * Triggered when the parameter value changes between empty and not empty.
150 *
151 * @event ve.ui.MWParameterPage#hasValueChange
152 * @param string parameterId Keyed by unique id of the parameter, e.g. something
153 * like "part_1/param1".
154 * @param boolean hasValue
155 */
156
157 /* Methods */
158
159 /**
160 * Get default configuration for an input widget.
161 *
162 * @private
163 * @return {Object}
164 */
165 ve.ui.MWParameterPage.prototype.getDefaultInputConfig = function () {
166 const valueInputConfig = {
167 autosize: true,
168 required: this.parameter.isRequired()
169 };
170
171 if ( this.defaultValue ) {
172 valueInputConfig.placeholder = ve.msg(
173 'visualeditor-dialog-transclusion-param-default',
174 this.defaultValue
175 );
176 }
177
178 return valueInputConfig;
179 };
180
181 /**
182 * Create a value input widget based on the parameter type and whether it is
183 * required or not.
184 *
185 * @private
186 * @return {OO.ui.InputWidget}
187 */
188 ve.ui.MWParameterPage.prototype.createValueInput = function () {
189 const type = this.parameter.getType(),
190 value = this.parameter.getValue(),
191 valueInputConfig = this.getDefaultInputConfig();
192
193 // TODO:
194 // * date - T100206
195 // * number - T124850
196 // * unbalanced-wikitext/content - T106242
197 // * string? - T124917
198 if (
199 type === 'wiki-page-name' &&
200 ( value === '' || mw.Title.newFromText( value ) )
201 ) {
202 return new mw.widgets.TitleInputWidget( ve.extendObject( {
203 api: ve.init.target.getContentApi()
204 }, valueInputConfig ) );
205 } else if (
206 type === 'wiki-file-name' &&
207 ( value === '' || mw.Title.newFromText( value ) )
208 ) {
209 return new mw.widgets.TitleInputWidget( ve.extendObject( {}, valueInputConfig, {
210 api: ve.init.target.getContentApi(),
211 namespace: 6,
212 showImages: true
213 } ) );
214 } else if (
215 type === 'wiki-user-name' &&
216 ( value === '' || mw.Title.newFromText( value ) )
217 ) {
218 valueInputConfig.validate = function ( val ) {
219 // TODO: Check against wgMaxNameChars
220 // TODO: Check against unicode validation regex from MW core's User::isValidUserName
221 return !!mw.Title.newFromText( val );
222 };
223 return new mw.widgets.UserInputWidget( ve.extendObject( {
224 api: ve.init.target.getContentApi()
225 }, valueInputConfig ) );
226 } else if (
227 type === 'wiki-template-name' &&
228 ( value === '' || mw.Title.newFromText( value ) )
229 ) {
230 return new mw.widgets.TitleInputWidget( ve.extendObject( {
231 api: ve.init.target.getContentApi()
232 }, valueInputConfig, {
233 namespace: mw.config.get( 'wgNamespaceIds' ).template
234 } ) );
235 } else if ( type === 'boolean' && ( value === '1' || value === '0' ) ) {
236 return new ve.ui.MWParameterCheckboxInputWidget( valueInputConfig );
237 } else if (
238 type === 'url' &&
239 (
240 value === '' ||
241 ve.init.platform.getExternalLinkUrlProtocolsRegExp().exec( value.trim() )
242 )
243 ) {
244 return ve.ui.MWExternalLinkAnnotationWidget.static.createExternalLinkInputWidget( valueInputConfig );
245 } else if (
246 this.parameter.getSuggestedValues().length &&
247 this.isSuggestedValueType( type )
248 ) {
249 valueInputConfig.menu = { filterFromInput: true, highlightOnFilter: true };
250 valueInputConfig.options =
251 this.parameter.getSuggestedValues().filter(
252 // This wasn't validated for a while, existing templates can do anything here
253 ( suggestedValue ) => typeof suggestedValue === 'string'
254 ).map( ( suggestedValue ) => ( { data: suggestedValue, label: suggestedValue || '\xA0' } ) );
255 return new OO.ui.ComboBoxInputWidget( valueInputConfig );
256 } else if ( type !== 'line' || value.indexOf( '\n' ) !== -1 ) {
257 // If the type is line, but there are already newlines in the provided
258 // value, don't break the existing content by only providing a single-
259 // line field. (This implies that the TemplateData for the field isn't
260 // complying with its use in practice...)
261 return new ve.ui.MWLazyMultilineTextInputWidget( valueInputConfig );
262 }
263
264 // Wrapping single line input (T348482)
265 return new ve.ui.MWLazyMultilineTextInputWidget( ve.extendObject( {
266 rows: 1,
267 autosize: true,
268 allowLinebreaks: false
269 }, valueInputConfig ) );
270 };
271
272 /**
273 * Whether or not to show suggested values for a given parameter type
274 *
275 * @private
276 * @param {string} type Parameter type
277 * @return {boolean} True if suggested values should be shown
278 */
279 ve.ui.MWParameterPage.prototype.isSuggestedValueType = function ( type ) {
280 return [ 'unknown', 'content', 'line', 'string', 'number', 'unbalanced-wikitext' ].indexOf( type ) > -1;
281 };
282
283 /**
284 * @private
285 * @return {boolean} True if there is either user-provided input or a default value
286 */
287 ve.ui.MWParameterPage.prototype.containsSomeValue = function () {
288 // Note: For templates that allow overriding a default value with nothing, the empty string is
289 // meaningful user input. For templates that don't, the parameter can never be truly empty.
290 return !!( this.valueInput.getValue() || this.defaultValue );
291 };
292
293 /**
294 * Handle change events from the value input
295 *
296 * @private
297 * @param {string} value
298 */
299 ve.ui.MWParameterPage.prototype.onValueInputChange = function () {
300 const value = this.valueInput.getValue();
301
302 if ( !this.edited ) {
303 ve.track( 'activity.transclusion', { action: 'edit-parameter-value' } );
304 }
305 this.edited = true;
306 this.parameter.setValue( value );
307
308 if ( !!value !== this.hasValue ) {
309 this.hasValue = !!value;
310 this.emit( 'hasValueChange', this.parameter.getId(), this.hasValue );
311 }
312 };
313
314 /**
315 * @inheritdoc
316 */
317 ve.ui.MWParameterPage.prototype.focus = function () {
318 this.valueInput.focus();
319 };
320
321 /**
322 * Refresh collapsible children.
323 */
324 ve.ui.MWParameterPage.prototype.updateSize = function () {
325 if ( this.collapsibleDoc ) {
326 this.collapsibleDoc.updateSize();
327 }
328 };