2 * Utility functions for jazzing up HTMLForm elements.
4 * @class jQuery.plugin.htmlform
11 * Helper function for hide-if to find the nearby form field.
13 * Find the closest match for the given name, "closest" being the minimum
14 * level of parents to go to find a form field matching the given name or
15 * ending in array keys matching the given name (e.g. "baz" matches
18 * @param {jQuery} element
19 * @param {string} name
20 * @return {jQuery|null}
22 function hideIfGetField( $el
, name
) {
25 sel
= '[name="' + name
+ '"],' +
26 '[name="wp' + name
+ '"],' +
27 '[name$="' + name
.replace( /^([^\[]+)/, '[$1]' ) + '"]';
28 for ( $p
= $el
.parent(); $p
.length
> 0; $p
= $p
.parent() ) {
29 $found
= $p
.find( sel
);
30 if ( $found
.length
> 0 ) {
38 * Helper function for hide-if to return a test function and list of
39 * dependent fields for a hide-if specification.
41 * @param {jQuery} element
42 * @param {Array} hide-if spec
43 * @return {Array} 2 elements: jQuery of dependent fields, and test function
45 function hideIfParse( $el
, spec
) {
46 var op
, i
, l
, v
, $field
, $fields
, func
, funcs
, getVal
;
57 for ( i
= 1; i
< l
; i
++ ) {
58 if ( !$.isArray( spec
[i
] ) ) {
59 throw new Error( op
+ ' parameters must be arrays' );
61 v
= hideIfParse( $el
, spec
[i
] );
62 $fields
= $fields
.add( v
[0] );
71 for ( i
= 0; i
< l
; i
++ ) {
83 for ( i
= 0; i
< l
; i
++ ) {
95 for ( i
= 0; i
< l
; i
++ ) {
107 for ( i
= 0; i
< l
; i
++ ) {
117 return [ $fields
, func
];
121 throw new Error( 'NOT takes exactly one parameter' );
123 if ( !$.isArray( spec
[1] ) ) {
124 throw new Error( 'NOT parameters must be arrays' );
126 v
= hideIfParse( $el
, spec
[1] );
129 return [ $fields
, function () {
136 throw new Error( op
+ ' takes exactly two parameters' );
138 $field
= hideIfGetField( $el
, spec
[1] );
140 return [ $(), function () {
146 if ( $field
.first().attr( 'type' ) === 'radio' ||
147 $field
.first().attr( 'type' ) === 'checkbox'
149 getVal = function () {
150 var $selected
= $field
.filter( ':checked' );
151 return $selected
.length
> 0 ? $selected
.val() : '';
154 getVal = function () {
162 return getVal() === v
;
167 return getVal() !== v
;
172 return [ $field
, func
];
175 throw new Error( 'Unrecognized operation \'' + op
+ '\'' );
180 * jQuery plugin to fade or snap to visible state.
182 * @param {boolean} [instantToggle=false]
186 $.fn
.goIn = function ( instantToggle
) {
187 if ( instantToggle
=== true ) {
188 return $( this ).show();
190 return $( this ).stop( true, true ).fadeIn();
194 * jQuery plugin to fade or snap to hiding state.
196 * @param {boolean} [instantToggle=false]
200 $.fn
.goOut = function ( instantToggle
) {
201 if ( instantToggle
=== true ) {
202 return $( this ).hide();
204 return $( this ).stop( true, true ).fadeOut();
208 * Bind a function to the jQuery object via live(), and also immediately trigger
209 * the function on the objects with an 'instant' parameter set to true.
210 * @param {Function} callback
211 * @param {boolean|jQuery.Event} callback.immediate True when the event is called immediately,
212 * an event object when triggered from an event.
214 $.fn
.liveAndTestAtStart = function ( callback
) {
216 .live( 'change', callback
)
218 callback
.call( this, true );
222 function enhance( $root
) {
224 // Animate the SelectOrOther fields, to only show the text field when
225 // 'other' is selected.
226 $root
.find( '.mw-htmlform-select-or-other' ).liveAndTestAtStart( function ( instant
) {
227 var $other
= $root
.find( '#' + $( this ).attr( 'id' ) + '-other' );
228 $other
= $other
.add( $other
.siblings( 'br' ) );
229 if ( $( this ).val() === 'other' ) {
230 $other
.goIn( instant
);
232 $other
.goOut( instant
);
236 // Set up hide-if elements
237 $root
.find( '.mw-htmlform-hide-if' ).each( function () {
239 spec
= $el
.data( 'hideIf' ),
240 v
, $fields
, test
, func
;
246 v
= hideIfParse( $el
, spec
);
256 $fields
.change( func
);
260 function addMulti( $oldContainer
, $container
) {
261 var name
= $oldContainer
.find( 'input:first-child' ).attr( 'name' ),
262 oldClass
= ( ' ' + $oldContainer
.attr( 'class' ) + ' ' ).replace( /(mw-htmlform-field-HTMLMultiSelectField|mw-chosen)/g, '' ),
263 $select
= $( '<select>' ),
264 dataPlaceholder
= mw
.message( 'htmlform-chosen-placeholder' );
265 oldClass
= $.trim( oldClass
);
268 multiple
: 'multiple',
269 'data-placeholder': dataPlaceholder
.plain(),
270 'class': 'htmlform-chzn-select mw-input ' + oldClass
272 $oldContainer
.find( 'input' ).each( function () {
273 var $oldInput
= $( this ),
274 checked
= $oldInput
.prop( 'checked' ),
275 $option
= $( '<option>' );
276 $option
.prop( 'value', $oldInput
.prop( 'value' ) );
278 $option
.prop( 'selected', true );
280 $option
.text( $oldInput
.prop( 'value' ) );
281 $select
.append( $option
);
283 $container
.append( $select
);
286 function convertCheckboxesToMulti( $oldContainer
, type
) {
287 var $fieldLabel
= $( '<td>' ),
289 $fieldLabelText
= $( '<label>' ),
291 if ( type
=== 'tr' ) {
292 addMulti( $oldContainer
, $td
);
293 $container
= $( '<tr>' );
294 $container
.append( $td
);
295 } else if ( type
=== 'div' ) {
296 $fieldLabel
= $( '<div>' );
297 $container
= $( '<div>' );
298 addMulti( $oldContainer
, $container
);
300 $fieldLabel
.attr( 'class', 'mw-label' );
301 $fieldLabelText
.text( $oldContainer
.find( '.mw-label label' ).text() );
302 $fieldLabel
.append( $fieldLabelText
);
303 $container
.prepend( $fieldLabel
);
304 $oldContainer
.replaceWith( $container
);
308 if ( $root
.find( '.mw-chosen' ).length
) {
309 mw
.loader
.using( 'jquery.chosen', function () {
310 $root
.find( '.mw-chosen' ).each( function () {
311 var type
= this.nodeName
.toLowerCase(),
312 $converted
= convertCheckboxesToMulti( $( this ), type
);
313 $converted
.find( '.htmlform-chzn-select' ).chosen( { width
: 'auto' } );
318 var $matrixTooltips
= $root
.find( '.mw-htmlform-matrix .mw-htmlform-tooltip' );
319 if ( $matrixTooltips
.length
) {
320 mw
.loader
.using( 'jquery.tipsy', function () {
321 $matrixTooltips
.tipsy( { gravity
: 's' } );
325 // Add/remove cloner clones without having to resubmit the form
326 $root
.find( '.mw-htmlform-cloner-delete-button' ).click( function ( ev
) {
328 $( this ).closest( 'li.mw-htmlform-cloner-li' ).remove();
331 $root
.find( '.mw-htmlform-cloner-create-button' ).click( function ( ev
) {
336 $ul
= $( this ).prev( 'ul.mw-htmlform-cloner-ul' );
338 html
= $ul
.data( 'template' ).replace(
339 $ul
.data( 'uniqueId' ), 'clone' + ( ++cloneCounter
), 'g'
343 .addClass( 'mw-htmlform-cloner-li' )
350 mw
.hook( 'htmlform.enhance' ).fire( $root
);
355 enhance( $( document
) );
360 * @mixins jQuery.plugin.htmlform
362 }( mediaWiki
, jQuery
) );