Merge "js suggest: better keyup/keydown handling"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 29 Jun 2016 08:42:53 +0000 (08:42 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 29 Jun 2016 08:42:53 +0000 (08:42 +0000)
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 );
                                                }
                                        } )