From 1398641aa39b98e46af53973334e120b4775c75b Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Thu, 18 Oct 2012 13:58:10 -0400 Subject: [PATCH] Module pattern for hotkey.js (imported from commit 56616bc8eeaa594ca70bb92bf17a88f97027fd35) --- tools/jslint/check-all.js | 3 +-- zephyr/static/js/compose.js | 2 +- zephyr/static/js/hotkey.js | 27 +++++++++++++++++++-------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index 5a2fef53ab..7aa58f2cbe 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -19,8 +19,7 @@ var globals = + ' get_id get_message_row' // hotkey.js - + ' process_goto_hotkey process_compose_hotkey process_key_in_input' - + ' set_compose_hotkey' + + ' hotkeys' // narrow.js + ' narrow_target_message_id narrowed show_all_messages' diff --git a/zephyr/static/js/compose.js b/zephyr/static/js/compose.js index b42bb47611..1df0558e5e 100644 --- a/zephyr/static/js/compose.js +++ b/zephyr/static/js/compose.js @@ -26,7 +26,7 @@ function compose_button(tabname) { clear_compose_box(); $('#sidebar a[href="#home"]').tab('show'); show_compose(tabname, $("#" + tabname)); - set_compose_hotkey(); + hotkeys.set_compose(); } function toggle_compose() { diff --git a/zephyr/static/js/hotkey.js b/zephyr/static/js/hotkey.js index 6c113028cb..1aaeec21dd 100644 --- a/zephyr/static/js/hotkey.js +++ b/zephyr/static/js/hotkey.js @@ -1,3 +1,7 @@ +var hotkeys = (function () { + +var exports = {}; + var pressed_keys = {}; function num_pressed_keys() { @@ -18,6 +22,9 @@ var directional_hotkeys = { 35: get_last_visible // End }; +// These are not exported, but we declare them here to make JSLint happy. +var process_key_in_input, process_compose_hotkey, process_goto_hotkey; + function simulate_keydown(keycode) { $(document).trigger($.Event('keydown', {keyCode: keycode})); } @@ -111,7 +118,7 @@ var goto_hotkeys = { 27: hide_compose // Esc }; -function process_goto_hotkey(code) { +process_goto_hotkey = function (code) { narrow_target_message_id = selected_message_id; if (goto_hotkeys.hasOwnProperty(code)) @@ -120,18 +127,18 @@ function process_goto_hotkey(code) { /* Always return to the initial hotkey mode, even after an unrecognized "go to" command. */ return process_hotkey; -} +}; -function process_key_in_input(code) { +process_key_in_input = function (code) { if (code === 27) { // If the user hit the escape key, hide the compose window hide_compose(); } // Otherwise, let the browser handle the key normally return false; -} +}; -function process_compose_hotkey(code) { +process_compose_hotkey = function (code) { if (code === 9) { // Tab: toggles between stream and huddle compose tabs. toggle_compose(); return process_compose_hotkey; @@ -140,11 +147,11 @@ function process_compose_hotkey(code) { // like any other keys typed in the input box keydown_handler = process_hotkey; return process_hotkey(code); -} +}; -function set_compose_hotkey() { +exports.set_compose = function () { keydown_handler = process_compose_hotkey; -} +}; $(document).keydown(function (e) { pressed_keys[e.which] = true; @@ -189,3 +196,7 @@ $(document).keypress(function (event) { } } }); + +return exports; + +}());