Module pattern for hotkey.js

(imported from commit 56616bc8eeaa594ca70bb92bf17a88f97027fd35)
This commit is contained in:
Keegan McAllister 2012-10-18 13:58:10 -04:00
parent 461da3f742
commit 1398641aa3
3 changed files with 21 additions and 11 deletions

View File

@ -19,8 +19,7 @@ var globals =
+ ' get_id get_message_row' + ' get_id get_message_row'
// hotkey.js // hotkey.js
+ ' process_goto_hotkey process_compose_hotkey process_key_in_input' + ' hotkeys'
+ ' set_compose_hotkey'
// narrow.js // narrow.js
+ ' narrow_target_message_id narrowed show_all_messages' + ' narrow_target_message_id narrowed show_all_messages'

View File

@ -26,7 +26,7 @@ function compose_button(tabname) {
clear_compose_box(); clear_compose_box();
$('#sidebar a[href="#home"]').tab('show'); $('#sidebar a[href="#home"]').tab('show');
show_compose(tabname, $("#" + tabname)); show_compose(tabname, $("#" + tabname));
set_compose_hotkey(); hotkeys.set_compose();
} }
function toggle_compose() { function toggle_compose() {

View File

@ -1,3 +1,7 @@
var hotkeys = (function () {
var exports = {};
var pressed_keys = {}; var pressed_keys = {};
function num_pressed_keys() { function num_pressed_keys() {
@ -18,6 +22,9 @@ var directional_hotkeys = {
35: get_last_visible // End 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) { function simulate_keydown(keycode) {
$(document).trigger($.Event('keydown', {keyCode: keycode})); $(document).trigger($.Event('keydown', {keyCode: keycode}));
} }
@ -111,7 +118,7 @@ var goto_hotkeys = {
27: hide_compose // Esc 27: hide_compose // Esc
}; };
function process_goto_hotkey(code) { process_goto_hotkey = function (code) {
narrow_target_message_id = selected_message_id; narrow_target_message_id = selected_message_id;
if (goto_hotkeys.hasOwnProperty(code)) if (goto_hotkeys.hasOwnProperty(code))
@ -120,18 +127,18 @@ function process_goto_hotkey(code) {
/* Always return to the initial hotkey mode, even /* Always return to the initial hotkey mode, even
after an unrecognized "go to" command. */ after an unrecognized "go to" command. */
return process_hotkey; return process_hotkey;
} };
function process_key_in_input(code) { process_key_in_input = function (code) {
if (code === 27) { if (code === 27) {
// If the user hit the escape key, hide the compose window // If the user hit the escape key, hide the compose window
hide_compose(); hide_compose();
} }
// Otherwise, let the browser handle the key normally // Otherwise, let the browser handle the key normally
return false; return false;
} };
function process_compose_hotkey(code) { process_compose_hotkey = function (code) {
if (code === 9) { // Tab: toggles between stream and huddle compose tabs. if (code === 9) { // Tab: toggles between stream and huddle compose tabs.
toggle_compose(); toggle_compose();
return process_compose_hotkey; return process_compose_hotkey;
@ -140,11 +147,11 @@ function process_compose_hotkey(code) {
// like any other keys typed in the input box // like any other keys typed in the input box
keydown_handler = process_hotkey; keydown_handler = process_hotkey;
return process_hotkey(code); return process_hotkey(code);
} };
function set_compose_hotkey() { exports.set_compose = function () {
keydown_handler = process_compose_hotkey; keydown_handler = process_compose_hotkey;
} };
$(document).keydown(function (e) { $(document).keydown(function (e) {
pressed_keys[e.which] = true; pressed_keys[e.which] = true;
@ -189,3 +196,7 @@ $(document).keypress(function (event) {
} }
} }
}); });
return exports;
}());