js suggest: better keyup/keydown handling
authorErik Bernhardson <ebernhardson@wikimedia.org>
Mon, 13 Jun 2016 18:08:44 +0000 (11:08 -0700)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Mon, 20 Jun 2016 20:38:08 +0000 (13:38 -0700)
some actions, like ctrl-click, trigger the keyup event even though it
has no bearing on autocomplete. This leads to extra impression-results
tracking events being fired. The existing keypress event, per spec,
handles input that normally produces a character value. The keyup /
keydown events should only be handling special non-character producing
keys that directly effect autocomplete. As such put together a list of
relevant keys and only fire on those.

Change-Id: I8d2a4fade84a5b94a81fa7ee0192912d2407ba92

resources/src/jquery/jquery.suggestions.js

index 38e5a1f..212c8f4 100644 (file)
                                                $.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 );
                                                }
                                        } )