2 * Utility functions for jazzing up HTMLForm elements.
7 * jQuery plugin to fade or snap to visible state.
9 * @param {boolean} instantToggle [optional]
12 $.fn
.goIn = function ( instantToggle
) {
13 if ( instantToggle
=== true ) {
14 return $(this).show();
16 return $(this).stop( true, true ).fadeIn();
20 * jQuery plugin to fade or snap to hiding state.
22 * @param {boolean} instantToggle [optional]
25 $.fn
.goOut = function ( instantToggle
) {
26 if ( instantToggle
=== true ) {
27 return $(this).hide();
29 return $(this).stop( true, true ).fadeOut();
33 * Bind a function to the jQuery object via live(), and also immediately trigger
34 * the function on the objects with an 'instant' parameter set to true.
35 * @param {Function} callback Takes one parameter, which is {true} when the
36 * event is called immediately, and {jQuery.Event} when triggered from an event.
38 $.fn
.liveAndTestAtStart = function ( callback
){
40 .live( 'change', callback
)
42 callback
.call( this, true );
48 // Animate the SelectOrOther fields, to only show the text field when
49 // 'other' is selected.
50 $( '.mw-htmlform-select-or-other' ).liveAndTestAtStart( function ( instant
) {
51 var $other
= $( '#' + $(this).attr( 'id' ) + '-other' );
52 $other
= $other
.add( $other
.siblings( 'br' ) );
53 if ( $(this).val() === 'other' ) {
54 $other
.goIn( instant
);
56 $other
.goOut( instant
);
62 function addMulti( $oldContainer
, $container
) {
63 var name
= $oldContainer
.find( 'input:first-child' ).attr( 'name' ),
64 oldClass
= ( ' ' + $oldContainer
.attr( 'class' ) + ' ' ).replace( /(mw-htmlform-field-HTMLMultiSelectField|mw-chosen)/g, '' ),
65 $select
= $( '<select>' ),
66 dataPlaceholder
= mw
.message( 'htmlform-chosen-placeholder' );
67 oldClass
= $.trim( oldClass
);
71 'data-placeholder': dataPlaceholder
.plain(),
72 'class': 'htmlform-chzn-select mw-input ' + oldClass
74 $oldContainer
.find( 'input' ).each( function () {
75 var $oldInput
= $(this),
76 checked
= $oldInput
.prop( 'checked' ),
77 $option
= $( '<option>' );
78 $option
.prop( 'value', $oldInput
.prop( 'value' ) );
80 $option
.prop( 'selected', true );
82 $option
.text( $oldInput
.prop( 'value' ) );
83 $select
.append( $option
);
85 $container
.append( $select
);
88 function convertCheckboxesToMulti( $oldContainer
, type
) {
89 var $fieldLabel
= $( '<td>' ),
91 $fieldLabelText
= $( '<label>' ),
93 if ( type
=== 'tr' ) {
94 addMulti( $oldContainer
, $td
);
95 $container
= $( '<tr>' );
96 $container
.append( $td
);
97 } else if ( type
=== 'div' ) {
98 $fieldLabel
= $( '<div>' );
99 $container
= $( '<div>' );
100 addMulti( $oldContainer
, $container
);
102 $fieldLabel
.attr( 'class', 'mw-label' );
103 $fieldLabelText
.text( $oldContainer
.find( '.mw-label label' ).text() );
104 $fieldLabel
.append( $fieldLabelText
);
105 $container
.prepend( $fieldLabel
);
106 $oldContainer
.replaceWith( $container
);
110 if ( $( '.mw-chosen' ).length
) {
111 mw
.loader
.using( 'jquery.chosen', function () {
112 $( '.mw-chosen' ).each( function () {
113 var type
= this.nodeName
.toLowerCase(),
114 $converted
= convertCheckboxesToMulti( $( this ), type
);
115 $converted
.find( '.htmlform-chzn-select' ).chosen( { width
: 'auto' } );
121 var $matrixTooltips
= $( '.mw-htmlform-matrix .mw-htmlform-tooltip' );
122 if ( $matrixTooltips
.length
) {
123 mw
.loader
.using( 'jquery.tipsy', function () {
124 $matrixTooltips
.tipsy( { gravity
: 's' } );
128 }( mediaWiki
, jQuery
) );