Merge "Floor slave lag before display"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.suggestions.js
index df6fff3..884ecb6 100644 (file)
  *
  *             $( '#textbox' ).suggestions();
  *
- * Uses jQuery.suggestions singleteon internally.
+ * Uses jQuery.suggestions singleton internally.
  *
  * @class jQuery.plugin.suggestions
  */
 
+ // jscs:disable checkParamNames
 /**
  * @method suggestions
  * @chainable
@@ -93,6 +94,7 @@
  * @param {boolean} [options.highlightInput=false] Whether to highlight matched portions of the
  *  input or not.
  */
+ // jscs:enable checkParamNames
 
 ( function ( $ ) {
 
                                                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 );
+                                                               context.config.update.after.call( context.data.$textbox, cache[ val ].metadata );
                                                        }
                                                        cacheHit = true;
                                                } else {
                                                context.config.fetch.call(
                                                        context.data.$textbox,
                                                        val,
-                                                       function ( suggestions ) {
+                                                       function ( suggestions, metadata ) {
                                                                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 );
+                                                                       context.config.update.after.call( context.data.$textbox, metadata );
                                                                }
                                                                if ( context.config.cache ) {
                                                                        cache[ val ] = {
                                                                                suggestions: suggestions,
+                                                                               metadata: metadata,
                                                                                timestamp: +new Date()
                                                                        };
                                                                }
                                        } else if ( selected.is( '.suggestions-special' ) ) {
                                                if ( typeof context.config.special.select === 'function' ) {
                                                        // Allow the callback to decide whether to prevent default or not
-                                                       if ( context.config.special.select.call( selected, context.data.$textbox ) === true ) {
+                                                       if ( context.config.special.select.call( selected, context.data.$textbox, 'keyboard' ) === true ) {
                                                                preventDefault = false;
                                                        }
                                                }
                                        } else {
                                                if ( typeof context.config.result.select === 'function' ) {
                                                        // Allow the callback to decide whether to prevent default or not
-                                                       if ( context.config.result.select.call( selected, context.data.$textbox ) === true ) {
+                                                       if ( context.config.result.select.call( selected, context.data.$textbox, 'keyboard' ) === true ) {
                                                                preventDefault = false;
                                                        }
                                                }
                                                                if ( $result.get( 0 ) !== $other.get( 0 ) ) {
                                                                        return;
                                                                }
-                                                               // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
+                                                               $.suggestions.highlight( context, $result, true );
+                                                               if ( typeof context.config.result.select === 'function' ) {
+                                                                       context.config.result.select.call( $result, context.data.$textbox, 'mouse' );
+                                                               }
+                                                               // Don't interfere with special clicks (e.g. to open in new tab)
                                                                if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
-                                                                       $.suggestions.highlight( context, $result, true );
-                                                                       if ( typeof context.config.result.select === 'function' ) {
-                                                                               context.config.result.select.call( $result, context.data.$textbox );
-                                                                       }
                                                                        // This will hide the link we're just clicking on, which causes problems
                                                                        // when done synchronously in at least Firefox 3.6 (bug 62858).
                                                                        setTimeout( function () {
                                                                if ( $special.get( 0 ) !== $other.get( 0 ) ) {
                                                                        return;
                                                                }
-                                                               // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
+                                                               if ( typeof context.config.special.select === 'function' ) {
+                                                                       context.config.special.select.call( $special, context.data.$textbox, 'mouse' );
+                                                               }
+                                                               // Don't interfere with special clicks (e.g. to open in new tab)
                                                                if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
-                                                                       if ( typeof context.config.special.select === 'function' ) {
-                                                                               context.config.special.select.call( $special, context.data.$textbox );
-                                                                       }
                                                                        // This will hide the link we're just clicking on, which causes problems
                                                                        // when done synchronously in at least Firefox 3.6 (bug 62858).
                                                                        setTimeout( function () {
                                                $.suggestions.keypress( e, context, context.data.keypressed );
                                        } )
                                        .keyup( function ( e ) {
-                                               // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
-                                               // keypress in between, solve it
-                                               if ( context.data.keypressedCount === 0 ) {
+                                               // The keypress event is fired when a key is pressed down and that key normally
+                                               // produces a character value. We also want to handle some keys that don't
+                                               // produce a character value so we also attach to the keydown/keyup events.
+                                               // List of codes sourced from
+                                               // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
+                                               var allowed = [
+                                                       40, // up arrow
+                                                       38, // down arrow
+                                                       27, // escape
+                                                       13, // enter
+                                                       46, // delete
+                                                       8   // backspace
+                                               ];
+                                               if ( context.data.keypressedCount === 0
+                                                       && e.which === context.data.keypressed
+                                                       && $.inArray( e.which, allowed ) !== -1
+                                               ) {
                                                        $.suggestions.keypress( e, context, context.data.keypressed );
                                                }
                                        } )