X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki%2Fmediawiki.htmlform.js;h=594800e15db8260d1e130e3109ba230a27981600;hb=f637ad308f73a1686f8faee4b1123480f8417cda;hp=f7aa7f8bd011d0d28ecd065af8a80a9e5d913733;hpb=0ff2c62197fdaf79f82d8657afd70621725c9762;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki/mediawiki.htmlform.js b/resources/src/mediawiki/mediawiki.htmlform.js index f7aa7f8bd0..594800e15d 100644 --- a/resources/src/mediawiki/mediawiki.htmlform.js +++ b/resources/src/mediawiki/mediawiki.htmlform.js @@ -15,19 +15,24 @@ * ending in array keys matching the given name (e.g. "baz" matches * "foo[bar][baz]"). * + * @private * @param {jQuery} element * @param {string} name * @return {jQuery|null} */ function hideIfGetField( $el, name ) { - var sel, $found, $p; + var $found, $p, + suffix = name.replace( /^([^\[]+)/, '[$1]' ); + + function nameFilter() { + return this.name === name || + ( this.name === ( 'wp' + name ) ) || + this.name.slice( -suffix.length ) === suffix; + } - sel = '[name="' + name + '"],' + - '[name="wp' + name + '"],' + - '[name$="' + name.replace( /^([^\[]+)/, '[$1]' ) + '"]'; for ( $p = $el.parent(); $p.length > 0; $p = $p.parent() ) { - $found = $p.find( sel ); - if ( $found.length > 0 ) { + $found = $p.find( '[name]' ).filter( nameFilter ); + if ( $found.length ) { return $found; } } @@ -38,12 +43,15 @@ * Helper function for hide-if to return a test function and list of * dependent fields for a hide-if specification. * + * @private * @param {jQuery} element * @param {Array} hide-if spec - * @return {Array} 2 elements: jQuery of dependent fields, and test function + * @return {Array} + * @return {jQuery} return.0 Dependent fields + * @return {Function} return.1 Test function */ function hideIfParse( $el, spec ) { - var op, i, l, v, $field, $fields, func, funcs, getVal; + var op, i, l, v, $field, $fields, fields, func, funcs, getVal; op = spec[0]; l = spec.length; @@ -53,15 +61,16 @@ case 'NAND': case 'NOR': funcs = []; - $fields = $(); + fields = []; for ( i = 1; i < l; i++ ) { if ( !$.isArray( spec[i] ) ) { throw new Error( op + ' parameters must be arrays' ); } v = hideIfParse( $el, spec[i] ); - $fields = $fields.add( v[0] ); + fields = fields.concat( v[0].toArray() ); funcs.push( v[1] ); } + $fields = $( fields ); l = funcs.length; switch ( op ) { @@ -143,12 +152,12 @@ } v = spec[2]; - if ( $field.first().attr( 'type' ) === 'radio' || - $field.first().attr( 'type' ) === 'checkbox' + if ( $field.first().prop( 'type' ) === 'radio' || + $field.first().prop( 'type' ) === 'checkbox' ) { getVal = function () { var $selected = $field.filter( ':checked' ); - return $selected.length > 0 ? $selected.val() : ''; + return $selected.length ? $selected.val() : ''; }; } else { getVal = function () { @@ -185,9 +194,9 @@ */ $.fn.goIn = function ( instantToggle ) { if ( instantToggle === true ) { - return $( this ).show(); + return this.show(); } - return $( this ).stop( true, true ).fadeIn(); + return this.stop( true, true ).fadeIn(); }; /** @@ -199,31 +208,42 @@ */ $.fn.goOut = function ( instantToggle ) { if ( instantToggle === true ) { - return $( this ).hide(); + return this.hide(); } - return $( this ).stop( true, true ).fadeOut(); + return this.stop( true, true ).fadeOut(); }; /** * Bind a function to the jQuery object via live(), and also immediately trigger * the function on the objects with an 'instant' parameter set to true. + * + * @method liveAndTestAtStart + * @deprecated since 1.24 Use .on() and .each() directly. * @param {Function} callback * @param {boolean|jQuery.Event} callback.immediate True when the event is called immediately, * an event object when triggered from an event. + * @return jQuery + * @chainable */ - $.fn.liveAndTestAtStart = function ( callback ) { - $( this ) + mw.log.deprecate( $.fn, 'liveAndTestAtStart', function ( callback ) { + this + // Can't really migrate to .on() generically, needs knowledge of + // calling code to know the correct selector. Fix callers and + // get rid of this .liveAndTestAtStart() hack. .live( 'change', callback ) .each( function () { callback.call( this, true ); } ); - }; + } ); function enhance( $root ) { + var $matrixTooltips, $autocomplete; - // Animate the SelectOrOther fields, to only show the text field when - // 'other' is selected. - $root.find( '.mw-htmlform-select-or-other' ).liveAndTestAtStart( function ( instant ) { + /** + * @ignore + * @param {boolean|jQuery.Event} instant + */ + function handleSelectOrOther( instant ) { var $other = $root.find( '#' + $( this ).attr( 'id' ) + '-other' ); $other = $other.add( $other.siblings( 'br' ) ); if ( $( this ).val() === 'other' ) { @@ -231,13 +251,21 @@ } else { $other.goOut( instant ); } - } ); + } + + // Animate the SelectOrOther fields, to only show the text field when + // 'other' is selected. + $root + .on( 'change', '.mw-htmlform-select-or-other', handleSelectOrOther ) + .each( function () { + handleSelectOrOther.call( this, true ); + } ); // Set up hide-if elements $root.find( '.mw-htmlform-hide-if' ).each( function () { - var $el = $( this ), - spec = $el.data( 'hideIf' ), - v, $fields, test, func; + var v, $fields, test, func, + $el = $( this ), + spec = $el.data( 'hideIf' ); if ( !spec ) { return; @@ -253,7 +281,7 @@ $el.show(); } }; - $fields.change( func ); + $fields.on( 'change', func ); func(); } ); @@ -315,13 +343,30 @@ } ); } - var $matrixTooltips = $root.find( '.mw-htmlform-matrix .mw-htmlform-tooltip' ); + $matrixTooltips = $root.find( '.mw-htmlform-matrix .mw-htmlform-tooltip' ); if ( $matrixTooltips.length ) { mw.loader.using( 'jquery.tipsy', function () { $matrixTooltips.tipsy( { gravity: 's' } ); } ); } + // Set up autocomplete fields + $autocomplete = $root.find( '.mw-htmlform-autocomplete' ); + if ( $autocomplete.length ) { + mw.loader.using( 'jquery.suggestions', function () { + $autocomplete.suggestions( { + fetch: function ( val ) { + var $el = $( this ); + $el.suggestions( 'suggestions', + $.grep( $el.data( 'autocomplete' ), function ( v ) { + return v.indexOf( val ) === 0; + } ) + ); + } + } ); + } ); + } + // Add/remove cloner clones without having to resubmit the form $root.find( '.mw-htmlform-cloner-delete-button' ).click( function ( ev ) { ev.preventDefault(); @@ -336,7 +381,8 @@ $ul = $( this ).prev( 'ul.mw-htmlform-cloner-ul' ); html = $ul.data( 'template' ).replace( - $ul.data( 'uniqueId' ), 'clone' + ( ++cloneCounter ), 'g' + new RegExp( $.escapeRE( $ul.data( 'uniqueId' ) ), 'g' ), + 'clone' + ( ++cloneCounter ) ); $li = $( '
  • ' )