2012-10-04 07:11:36 +02:00
|
|
|
/*global
|
|
|
|
process_goto_hotkey: false,
|
|
|
|
process_compose_hotkey: false,
|
|
|
|
process_key_in_input: false */
|
|
|
|
|
|
|
|
// We don't generally treat these as global.
|
|
|
|
// Tell JSLint they are, to break the mutual recursion.
|
|
|
|
|
2012-10-03 23:19:07 +02:00
|
|
|
|
2012-10-04 00:04:58 +02:00
|
|
|
var pressed_keys = {};
|
|
|
|
|
|
|
|
function num_pressed_keys() {
|
|
|
|
var size = 0, key;
|
|
|
|
for (key in pressed_keys) {
|
|
|
|
if (pressed_keys.hasOwnProperty(key))
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-09-21 23:51:31 +02:00
|
|
|
var directional_hotkeys = {
|
|
|
|
40: get_next_visible, // down arrow
|
2012-09-27 18:30:21 +02:00
|
|
|
74: get_next_visible, // 'j'
|
2012-09-21 23:51:31 +02:00
|
|
|
38: get_prev_visible, // up arrow
|
2012-09-27 18:30:21 +02:00
|
|
|
75: get_prev_visible, // 'k'
|
2012-09-21 23:51:31 +02:00
|
|
|
36: get_first_visible, // Home
|
|
|
|
35: get_last_visible // End
|
|
|
|
};
|
|
|
|
|
2012-10-02 21:41:40 +02:00
|
|
|
function simulate_keydown(keycode) {
|
2012-09-24 21:02:13 +02:00
|
|
|
$(document).trigger($.Event('keydown', {keyCode: keycode}));
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
function process_hotkey(code) {
|
2012-10-03 23:47:15 +02:00
|
|
|
var next_zephyr;
|
2012-10-04 07:27:53 +02:00
|
|
|
if (directional_hotkeys.hasOwnProperty(code)) {
|
2012-09-21 23:51:31 +02:00
|
|
|
next_zephyr = directional_hotkeys[code](selected_zephyr);
|
2012-09-21 22:35:32 +02:00
|
|
|
if (next_zephyr.length !== 0) {
|
|
|
|
select_zephyr(next_zephyr, true);
|
|
|
|
}
|
|
|
|
return process_hotkey;
|
2012-09-21 23:51:31 +02:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-09-26 23:37:21 +02:00
|
|
|
if (num_pressed_keys() > 1) {
|
|
|
|
// If you are already holding down another key, none of these
|
|
|
|
// actions apply.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-21 23:51:31 +02:00
|
|
|
switch (code) {
|
2012-09-24 16:52:48 +02:00
|
|
|
case 33: // Page Up
|
2012-09-24 19:50:09 +02:00
|
|
|
keep_pointer_in_view();
|
2012-10-03 23:47:15 +02:00
|
|
|
return false; // We want the browser to actually page up and down
|
2012-09-24 16:52:48 +02:00
|
|
|
case 34: // Page Down
|
2012-09-24 19:50:09 +02:00
|
|
|
keep_pointer_in_view();
|
2012-10-03 23:47:15 +02:00
|
|
|
return false;
|
2012-09-21 22:35:32 +02:00
|
|
|
case 27: // Esc: hide compose pane
|
|
|
|
hide_compose();
|
|
|
|
return process_hotkey;
|
2012-09-25 16:04:27 +02:00
|
|
|
case 67: // 'c': compose
|
|
|
|
compose_button();
|
|
|
|
return process_compose_hotkey;
|
2012-09-21 22:35:32 +02:00
|
|
|
case 82: // 'r': respond to zephyr
|
|
|
|
respond_to_zephyr();
|
|
|
|
return process_key_in_input;
|
|
|
|
|
|
|
|
case 71: // 'g': start of "go to" command
|
|
|
|
return process_goto_hotkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-22 00:34:26 +02:00
|
|
|
var goto_hotkeys = {
|
|
|
|
67: narrow_by_recipient, // 'c'
|
|
|
|
73: narrow_instance, // 'i'
|
|
|
|
80: narrow_all_personals, // 'p'
|
|
|
|
65: show_all_messages, // 'a'
|
|
|
|
27: hide_compose // Esc
|
|
|
|
};
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-09-22 00:34:26 +02:00
|
|
|
function process_goto_hotkey(code) {
|
2012-10-04 07:27:53 +02:00
|
|
|
if (goto_hotkeys.hasOwnProperty(code))
|
2012-09-22 00:34:26 +02:00
|
|
|
goto_hotkeys[code]();
|
2012-09-21 22:35:32 +02:00
|
|
|
|
|
|
|
/* Always return to the initial hotkey mode, even
|
|
|
|
with an unrecognized "go to" command. */
|
|
|
|
return process_hotkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_key_in_input(code) {
|
|
|
|
if (code === 27) {
|
|
|
|
// User hit Escape key
|
|
|
|
hide_compose();
|
|
|
|
return process_hotkey;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:04:27 +02:00
|
|
|
function process_compose_hotkey(code) {
|
|
|
|
if (code === 9) { // Tab: toggle between class and huddle compose tabs.
|
|
|
|
toggle_compose();
|
|
|
|
return process_compose_hotkey;
|
|
|
|
} else {
|
2012-10-04 07:25:43 +02:00
|
|
|
set_keydown_in_input(true);
|
2012-10-02 21:41:40 +02:00
|
|
|
simulate_keydown(code);
|
2012-09-25 16:04:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:37:21 +02:00
|
|
|
$(document).keydown(function (e) {
|
|
|
|
pressed_keys[e.which] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).keyup(function (e) {
|
|
|
|
pressed_keys = {};
|
|
|
|
});
|
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
/* 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;
|
|
|
|
|
2012-10-04 07:25:43 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
$(document).keydown(function (event) {
|
|
|
|
var result = keydown_handler(event.keyCode);
|
|
|
|
if (typeof result === 'function') {
|
|
|
|
keydown_handler = result;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|