From a5c8b5dc7eabffe33e02af572f445f1baba00151 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 9 Oct 2012 13:50:39 -0400 Subject: [PATCH] Detect whether the user is currently in an input area correctly. Rather than trying to keep track of whether the last thing that happened was an input area being focused (which had all kinds of bugs), just detect whether we're in an input area using the appropriate jquery selector. Hopefully this has OK performance. (imported from commit 6150692ffcb0ab9b04244c3d053b5527847ded2d) --- tools/jslint/check-all.js | 1 - zephyr/static/js/hotkey.js | 46 +++++++++++++++++--------------------- zephyr/static/js/ui.js | 8 ------- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index f7db3bc038..9401251ee4 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -18,7 +18,6 @@ var globals = + ' get_id get_zephyr_row' // hotkey.js - + ' set_keydown_in_input' // narrow.js + ' narrowed show_all_messages' diff --git a/zephyr/static/js/hotkey.js b/zephyr/static/js/hotkey.js index 40d0703e2e..fde6305483 100644 --- a/zephyr/static/js/hotkey.js +++ b/zephyr/static/js/hotkey.js @@ -33,6 +33,12 @@ function simulate_keydown(keycode) { function process_hotkey(code) { var next_zephyr; + + // Disable hotkeys when in an input, textarea, or button + if ($('input:focus,textarea:focus,button:focus').length > 0) { + return process_key_in_input(code); + } + if (directional_hotkeys.hasOwnProperty(code)) { next_zephyr = directional_hotkeys[code](selected_zephyr); if (next_zephyr.length !== 0) { @@ -80,10 +86,10 @@ function process_hotkey(code) { return process_compose_hotkey; case 114: // 'r': respond to zephyr respond_to_zephyr(); - return process_key_in_input; + return process_hotkey; case 82: // 'R': respond to author respond_to_zephyr("personal"); - return process_key_in_input; + return process_hotkey; case 103: // 'g': start of "go to" command return process_goto_hotkey; } @@ -91,6 +97,11 @@ function process_hotkey(code) { return false; } +/* The current handler function for keydown events. + It should return a new handler, or 'false' to + decline to handle the event. */ +var keydown_handler = process_hotkey; + var goto_hotkeys = { 99: narrow_by_recipient, // 'c' 105: narrow_instance, // 'i' @@ -104,27 +115,27 @@ function process_goto_hotkey(code) { goto_hotkeys[code](); /* Always return to the initial hotkey mode, even - with an unrecognized "go to" command. */ + after an unrecognized "go to" command. */ return process_hotkey; } function process_key_in_input(code) { if (code === 27) { - // User hit Escape key + // If the user hit the escape key, hide the compose window hide_compose(); - return process_hotkey; } + // Otherwise, let the browser handle the key normally return false; } function process_compose_hotkey(code) { - if (code === 9) { // Tab: toggle between class and huddle compose tabs. + if (code === 9) { // Tab: toggles between class and huddle compose tabs. toggle_compose(); return process_compose_hotkey; - } else { - set_keydown_in_input(true); - simulate_keydown(code); } + // After we hit a non-tab character, tab should be processed normally + keydown_handler = process_hotkey; + return false; } $(document).keydown(function (e) { @@ -135,23 +146,6 @@ $(document).keyup(function (e) { pressed_keys = {}; }); -/* The current handler function for keydown events. - It should return a new handler, or 'false' to - decline to handle the event. */ -var keydown_handler = process_hotkey; - -function set_keydown_in_input(flag) { - // No argument should behave like 'true'. - if (flag === undefined) - flag = true; - - if (flag) { - keydown_handler = process_key_in_input; - } else { - keydown_handler = process_hotkey; - } -} - /* We register both a keydown and a keypress function because we want to intercept pgup/pgdn, escape, etc, and process them as they happen on the keyboard. However, if we processed diff --git a/zephyr/static/js/ui.js b/zephyr/static/js/ui.js index ea4a74af68..ac5dbdcc87 100644 --- a/zephyr/static/js/ui.js +++ b/zephyr/static/js/ui.js @@ -110,14 +110,6 @@ $(function () { // NB: This just binds to current elements, and won't bind to elements // created after ready() is called. - // Enable our hotkeys when we're not focused on a data entry box - $('input, textarea, button').focus(function () { - set_keydown_in_input(true); - }); - $('input, textarea, button').blur(function () { - set_keydown_in_input(false); - }); - $('#zephyr-type-tabs a[href="#class-message"]').on('shown', function (e) { $('#personal-message').hide(); $('#class-message').show();