Add HTMLAutoCompleteSelectField
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.htmlform.js
index 8be1321..594800e 100644 (file)
@@ -15,6 +15,7 @@
         * 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}
         * 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, fields, func, funcs, getVal;
@@ -63,7 +67,7 @@
                                                throw new Error( op + ' parameters must be arrays' );
                                        }
                                        v = hideIfParse( $el, spec[i] );
-                                       fields.push( v[0] );
+                                       fields = fields.concat( v[0].toArray() );
                                        funcs.push( v[1] );
                                }
                                $fields = $( fields );
        } );
 
        function enhance( $root ) {
+               var $matrixTooltips, $autocomplete;
 
                /**
                 * @ignore
                        } );
                }
 
-               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();
                        $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 = $( '<li>' )