summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki/htmlform/hide-if.js
blob: 6d3c9fd18960a8434eea84647531cf542cc9b97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * HTMLForm enhancements:
 * Set up 'hide-if' behaviors for form fields that have them.
 */
( function ( mw, $ ) {

	/**
	 * Helper function for hide-if to find the nearby form field.
	 *
	 * Find the closest match for the given name, "closest" being the minimum
	 * level of parents to go to find a form field matching the given name or
	 * ending in array keys matching the given name (e.g. "baz" matches
	 * "foo[bar][baz]").
	 *
	 * @ignore
	 * @private
	 * @param {jQuery} $el
	 * @param {string} name
	 * @return {jQuery|OO.ui.Widget|null}
	 */
	function hideIfGetField( $el, name ) {
		var $found, $p, $widget,
			suffix = name.replace( /^([^[]+)/, '[$1]' );

		function nameFilter() {
			return this.name === name ||
				( this.name === ( 'wp' + name ) ) ||
				this.name.slice( -suffix.length ) === suffix;
		}

		for ( $p = $el.parent(); $p.length > 0; $p = $p.parent() ) {
			$found = $p.find( '[name]' ).filter( nameFilter );
			if ( $found.length ) {
				$widget = $found.closest( '.oo-ui-widget[data-ooui]' );
				if ( $widget.length ) {
					return OO.ui.Widget.static.infuse( $widget );
				}
				return $found;
			}
		}
		return null;
	}

	/**
	 * Helper function for hide-if to return a test function and list of
	 * dependent fields for a hide-if specification.
	 *
	 * @ignore
	 * @private
	 * @param {jQuery} $el
	 * @param {Array} spec
	 * @return {Array}
	 * @return {Array} return.0 Dependent fields, array of jQuery objects or OO.ui.Widgets
	 * @return {Function} return.1 Test function
	 */
	function hideIfParse( $el, spec ) {
		var op, i, l, v, field, $field, fields, func, funcs, getVal;

		op = spec[ 0 ];
		l = spec.length;
		switch ( op ) {
			case 'AND':
			case 'OR':
			case 'NAND':
			case 'NOR':
				funcs = [];
				fields = [];
				for ( i = 1; i < l; i++ ) {
					if ( !Array.isArray( spec[ i ] ) ) {
						throw new Error( op + ' parameters must be arrays' );
					}
					v = hideIfParse( $el, spec[ i ] );
					fields = fields.concat( v[ 0 ] );
					funcs.push( v[ 1 ] );
				}

				l = funcs.length;
				switch ( op ) {
					case 'AND':
						func = function () {
							var i;
							for ( i = 0; i < l; i++ ) {
								if ( !funcs[ i ]() ) {
									return false;
								}
							}
							return true;
						};
						break;

					case 'OR':
						func = function () {
							var i;
							for ( i = 0; i < l; i++ ) {
								if ( funcs[ i ]() ) {
									return true;
								}
							}
							return false;
						};
						break;

					case 'NAND':
						func = function () {
							var i;
							for ( i = 0; i < l; i++ ) {
								if ( !funcs[ i ]() ) {
									return true;
								}
							}
							return false;
						};
						break;

					case 'NOR':
						func = function () {
							var i;
							for ( i = 0; i < l; i++ ) {
								if ( funcs[ i ]() ) {
									return false;
								}
							}
							return true;
						};
						break;
				}

				return [ fields, func ];

			case 'NOT':
				if ( l !== 2 ) {
					throw new Error( 'NOT takes exactly one parameter' );
				}
				if ( !Array.isArray( spec[ 1 ] ) ) {
					throw new Error( 'NOT parameters must be arrays' );
				}
				v = hideIfParse( $el, spec[ 1 ] );
				fields = v[ 0 ];
				func = v[ 1 ];
				return [ fields, function () {
					return !func();
				} ];

			case '===':
			case '!==':
				if ( l !== 3 ) {
					throw new Error( op + ' takes exactly two parameters' );
				}
				field = hideIfGetField( $el, spec[ 1 ] );
				if ( !field ) {
					return [ [], function () {
						return false;
					} ];
				}
				v = spec[ 2 ];

				if ( !( field instanceof jQuery ) ) {
					// field is a OO.ui.Widget
					if ( field.supports( 'isSelected' ) ) {
						getVal = function () {
							var selected = field.isSelected();
							return selected ? field.getValue() : '';
						};
					} else {
						getVal = function () {
							return field.getValue();
						};
					}
				} else {
					$field = $( field );
					if ( $field.prop( 'type' ) === 'radio' || $field.prop( 'type' ) === 'checkbox' ) {
						getVal = function () {
							var $selected = $field.filter( ':checked' );
							return $selected.length ? $selected.val() : '';
						};
					} else {
						getVal = function () {
							return $field.val();
						};
					}
				}

				switch ( op ) {
					case '===':
						func = function () {
							return getVal() === v;
						};
						break;
					case '!==':
						func = function () {
							return getVal() !== v;
						};
						break;
				}

				return [ [ field ], func ];

			default:
				throw new Error( 'Unrecognized operation \'' + op + '\'' );
		}
	}

	mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
		var
			$fields = $root.find( '.mw-htmlform-hide-if' ),
			$oouiFields = $fields.filter( '[data-ooui]' ),
			modules = [];

		if ( $oouiFields.length ) {
			modules.push( 'mediawiki.htmlform.ooui' );
			$oouiFields.each( function () {
				var data, extraModules,
					$el = $( this );

				data = $el.data( 'mw-modules' );
				if ( data ) {
					// We can trust this value, 'data-mw-*' attributes are banned from user content in Sanitizer
					extraModules = data.split( ',' );
					modules.push.apply( modules, extraModules );
				}
			} );
		}

		mw.loader.using( modules ).done( function () {
			$fields.each( function () {
				var v, i, fields, test, func, spec, self,
					$el = $( this );

				if ( $el.is( '[data-ooui]' ) ) {
					// self should be a FieldLayout that mixes in mw.htmlform.Element
					self = OO.ui.FieldLayout.static.infuse( $el );
					spec = self.hideIf;
					// The original element has been replaced with infused one
					$el = self.$element;
				} else {
					self = $el;
					spec = $el.data( 'hideIf' );
				}

				if ( !spec ) {
					return;
				}

				v = hideIfParse( $el, spec );
				fields = v[ 0 ];
				test = v[ 1 ];
				// The .toggle() method works mostly the same for jQuery objects and OO.ui.Widget
				func = function () {
					var shouldHide = test();
					self.toggle( !shouldHide );

					// It is impossible to submit a form with hidden fields failing validation, e.g. one that
					// is required. However, validity is not checked for disabled fields, as these are not
					// submitted with the form. So we should also disable fields when hiding them.
					if ( self instanceof jQuery ) {
						// This also finds elements inside any nested fields (in case of HTMLFormFieldCloner),
						// which is problematic. But it works because:
						// * HTMLFormFieldCloner::createFieldsForKey() copies 'hide-if' rules to nested fields
						// * jQuery collections like $fields are in document order, so we register event
						//   handlers for parents first
						// * Event handlers are fired in the order they were registered, so even if the handler
						//   for parent messed up the child, the handle for child will run next and fix it
						self.find( 'input, textarea, select' ).each( function () {
							var $this = $( this );
							if ( shouldHide ) {
								if ( $this.data( 'was-disabled' ) === undefined ) {
									$this.data( 'was-disabled', $this.prop( 'disabled' ) );
								}
								$this.prop( 'disabled', true );
							} else {
								$this.prop( 'disabled', $this.data( 'was-disabled' ) );
							}
						} );
					} else {
						// self is a OO.ui.FieldLayout
						if ( shouldHide ) {
							if ( self.wasDisabled === undefined ) {
								self.wasDisabled = self.fieldWidget.isDisabled();
							}
							self.fieldWidget.setDisabled( true );
						} else if ( self.wasDisabled !== undefined ) {
							self.fieldWidget.setDisabled( self.wasDisabled );
						}
					}
				};
				for ( i = 0; i < fields.length; i++ ) {
					// The .on() method works mostly the same for jQuery objects and OO.ui.Widget
					fields[ i ].on( 'change', func );
				}
				func();
			} );
		} );
	} );

}( mediaWiki, jQuery ) );