build: Enable jscs rules 'requireSpacesInside*Brackets' and make pass
[lhc/web/wiklou.git] / resources / src / jquery / jquery.suggestions.js
index 813c37c..c43e0ff 100644 (file)
  * @param {Function} options.result.select Called in context of the suggestions-result-current element.
  * @param {jQuery} options.result.select.$textbox
  *
+ * @param {Object} [options.update] Set of callbacks for listening to a change in the text input.
+ *
+ * @param {Function} options.update.before Called right after the user changes the textbox text.
+ * @param {Function} options.update.after Called after results are updated either from the cache or
+ * the API as a result of the user input.
+ *
  * @param {jQuery} [options.$region=this] The element to place the suggestions below and match width of.
  *
  * @param {string[]} [options.suggestions] Array of suggestions to display.
@@ -83,7 +89,7 @@
  * @param {boolean} [options.positionFromLeft] Sets `expandFrom=left`, for backwards
  *  compatibility.
  *
- * @param {boolean} [options.highlightInput=false] Whether to hightlight matched portions of the
+ * @param {boolean} [options.highlightInput=false] Whether to highlight matched portions of the
  *  input or not.
  */
 ( function ( $ ) {
@@ -144,6 +150,10 @@ $.suggestions = {
                                cache = context.data.cache,
                                cacheHit;
 
+                       if ( typeof context.config.update.before === 'function' ) {
+                               context.config.update.before.call( context.data.$textbox );
+                       }
+
                        // Only fetch if the value in the textbox changed and is not empty, or if the results were hidden
                        // if the textbox is empty then clear the result div, but leave other settings intouched
                        if ( val.length === 0 ) {
@@ -158,6 +168,9 @@ $.suggestions = {
                                if ( context.config.cache && hasOwn.call( cache, val ) ) {
                                        if ( +new Date() - cache[ val ].timestamp < context.config.cacheMaxAge ) {
                                                context.data.$textbox.suggestions( 'suggestions', cache[ val ].suggestions );
+                                               if ( typeof context.config.update.after === 'function' ) {
+                                                       context.config.update.after.call( context.data.$textbox );
+                                               }
                                                cacheHit = true;
                                        } else {
                                                // Cache expired
@@ -171,6 +184,9 @@ $.suggestions = {
                                                function ( suggestions ) {
                                                        suggestions = suggestions.slice( 0, context.config.maxRows );
                                                        context.data.$textbox.suggestions( 'suggestions', suggestions );
+                                                       if ( typeof context.config.update.after === 'function' ) {
+                                                               context.config.update.after.call( context.data.$textbox );
+                                                       }
                                                        if ( context.config.cache ) {
                                                                cache[ val ] = {
                                                                        suggestions: suggestions,
@@ -227,12 +243,13 @@ $.suggestions = {
                        case 'cancel':
                        case 'special':
                        case 'result':
+                       case 'update':
                        case '$region':
                        case 'expandFrom':
-                               context.config[property] = value;
+                               context.config[ property ] = value;
                                break;
                        case 'suggestions':
-                               context.config[property] = value;
+                               context.config[ property ] = value;
                                // Update suggestions
                                if ( context.data !== undefined ) {
                                        if ( context.data.$textbox.val().length === 0 ) {
@@ -260,7 +277,7 @@ $.suggestions = {
                                                                expandFrom = 'left';
 
                                                        // Catch invalid values, default to 'auto'
-                                                       } else if ( $.inArray( expandFrom, ['left', 'right', 'start', 'end', 'auto'] ) === -1 ) {
+                                                       } else if ( $.inArray( expandFrom, [ 'left', 'right', 'start', 'end', 'auto' ] ) === -1 ) {
                                                                expandFrom = 'auto';
                                                        }
 
@@ -319,11 +336,11 @@ $.suggestions = {
                                                expWidth = -1;
                                                for ( i = 0; i < context.config.suggestions.length; i++ ) {
                                                        /*jshint loopfunc:true */
-                                                       text = context.config.suggestions[i];
+                                                       text = context.config.suggestions[ i ];
                                                        $result = $( '<div>' )
                                                                .addClass( 'suggestions-result' )
                                                                .attr( 'rel', i )
-                                                               .data( 'text', context.config.suggestions[i] )
+                                                               .data( 'text', context.config.suggestions[ i ] )
                                                                .mousemove( function () {
                                                                        context.data.selectedWithMouse = true;
                                                                        $.suggestions.highlight(
@@ -335,7 +352,7 @@ $.suggestions = {
                                                                .appendTo( $results );
                                                        // Allow custom rendering
                                                        if ( typeof context.config.result.render === 'function' ) {
-                                                               context.config.result.render.call( $result, context.config.suggestions[i], context );
+                                                               context.config.result.render.call( $result, context.config.suggestions[ i ], context );
                                                        } else {
                                                                $result.text( text );
                                                        }
@@ -376,22 +393,22 @@ $.suggestions = {
                                }
                                break;
                        case 'maxRows':
-                               context.config[property] = Math.max( 1, Math.min( 100, value ) );
+                               context.config[ property ] = Math.max( 1, Math.min( 100, value ) );
                                break;
                        case 'delay':
-                               context.config[property] = Math.max( 0, Math.min( 1200, value ) );
+                               context.config[ property ] = Math.max( 0, Math.min( 1200, value ) );
                                break;
                        case 'cacheMaxAge':
-                               context.config[property] = Math.max( 1, value );
+                               context.config[ property ] = Math.max( 1, value );
                                break;
                        case 'maxExpandFactor':
-                               context.config[property] = Math.max( 1, value );
+                               context.config[ property ] = Math.max( 1, value );
                                break;
                        case 'cache':
                        case 'submitOnClick':
                        case 'positionFromLeft':
                        case 'highlightInput':
-                               context.config[property] = !!value;
+                               context.config[ property ] = !!value;
                                break;
                }
        },
@@ -559,6 +576,7 @@ $.fn.suggestions = function () {
                                        cancel: function () {},
                                        special: {},
                                        result: {},
+                                       update: {},
                                        $region: $( this ),
                                        suggestions: [],
                                        maxRows: 10,
@@ -577,18 +595,18 @@ $.fn.suggestions = function () {
 
                // Handle various calling styles
                if ( args.length > 0 ) {
-                       if ( typeof args[0] === 'object' ) {
+                       if ( typeof args[ 0 ] === 'object' ) {
                                // Apply set of properties
-                               for ( key in args[0] ) {
-                                       $.suggestions.configure( context, key, args[0][key] );
+                               for ( key in args[ 0 ] ) {
+                                       $.suggestions.configure( context, key, args[ 0 ][ key ] );
                                }
-                       } else if ( typeof args[0] === 'string' ) {
+                       } else if ( typeof args[ 0 ] === 'string' ) {
                                if ( args.length > 1 ) {
                                        // Set property values
-                                       $.suggestions.configure( context, args[0], args[1] );
+                                       $.suggestions.configure( context, args[ 0 ], args[ 1 ] );
                                } else if ( returnValue === null || returnValue === undefined ) {
                                        // Get property values, but don't give access to internal data - returns only the first
-                                       returnValue = ( args[0] in context.config ? undefined : context.config[args[0]] );
+                                       returnValue = ( args[ 0 ] in context.config ? undefined : context.config[ args[ 0 ] ] );
                                }
                        }
                }