2012-10-18 19:58:10 +02:00
|
|
|
var hotkeys = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
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-10-03 17:04:43 +02:00
|
|
|
106: get_next_visible, // 'j'
|
2012-09-21 23:51:31 +02:00
|
|
|
38: get_prev_visible, // up arrow
|
2012-10-03 17:04:43 +02:00
|
|
|
107: get_prev_visible, // 'k'
|
2012-09-21 23:51:31 +02:00
|
|
|
36: get_first_visible, // Home
|
|
|
|
35: get_last_visible // End
|
|
|
|
};
|
|
|
|
|
2012-10-18 19:58:10 +02:00
|
|
|
// These are not exported, but we declare them here to make JSLint happy.
|
|
|
|
var process_key_in_input, process_compose_hotkey, process_goto_hotkey;
|
|
|
|
|
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-09 23:45:41 +02:00
|
|
|
var next_message;
|
2012-10-09 19:50:39 +02:00
|
|
|
|
2012-10-09 20:09:03 +02:00
|
|
|
// Disable hotkeys on settings page etc.
|
|
|
|
if (!$('#home').hasClass('active')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-09 19:50:39 +02:00
|
|
|
// Disable hotkeys when in an input, textarea, or button
|
|
|
|
if ($('input:focus,textarea:focus,button:focus').length > 0) {
|
|
|
|
return process_key_in_input(code);
|
|
|
|
}
|
|
|
|
|
2012-10-04 07:27:53 +02:00
|
|
|
if (directional_hotkeys.hasOwnProperty(code)) {
|
2012-10-10 16:17:58 +02:00
|
|
|
next_message = directional_hotkeys[code](selected_message);
|
2012-10-09 23:45:41 +02:00
|
|
|
if (next_message.length !== 0) {
|
2012-10-09 23:51:45 +02:00
|
|
|
select_message(next_message, true);
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
2012-10-09 23:45:41 +02:00
|
|
|
if ((next_message.length === 0) && (code === 40 || code === 106)) {
|
2012-10-10 16:22:10 +02:00
|
|
|
// At the last message, scroll to the bottom so we have
|
|
|
|
// lots of nice whitespace for new messages coming in.
|
2012-10-05 22:13:51 +02:00
|
|
|
//
|
|
|
|
// FIXME: this doesn't work for End because get_last_visible()
|
2012-10-10 16:22:10 +02:00
|
|
|
// always returns a message.
|
2012-10-05 22:13:51 +02:00
|
|
|
viewport.scrollTop($("#main_div").outerHeight(true));
|
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
return process_hotkey;
|
2012-09-21 23:51:31 +02:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-10-03 17:30:48 +02:00
|
|
|
if (num_pressed_keys() > 1 &&
|
|
|
|
// "shift" "caps lock"
|
|
|
|
!((pressed_keys[16] === true || pressed_keys[20]) &&
|
|
|
|
num_pressed_keys() === 2)) {
|
2012-09-26 23:37:21 +02:00
|
|
|
// 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-10-05 21:51:47 +02:00
|
|
|
if (at_top_of_viewport()) {
|
2012-10-09 23:51:45 +02:00
|
|
|
select_message(get_first_visible(), false);
|
2012-10-05 21:51:47 +02:00
|
|
|
}
|
2012-10-03 23:47:15 +02:00
|
|
|
return false; // We want the browser to actually page up and down
|
2012-10-17 17:23:50 +02:00
|
|
|
case 32: // Spacebar
|
2012-09-24 16:52:48 +02:00
|
|
|
case 34: // Page Down
|
2012-10-05 21:51:47 +02:00
|
|
|
if (at_bottom_of_viewport()) {
|
2012-10-09 23:51:45 +02:00
|
|
|
select_message(get_last_visible(), false);
|
2012-10-05 21:51:47 +02:00
|
|
|
}
|
2012-10-03 23:47:15 +02:00
|
|
|
return false;
|
2012-10-11 20:30:34 +02:00
|
|
|
case 27: // Esc: hide compose pane or un-narrow
|
|
|
|
if (composing_message()) {
|
|
|
|
hide_compose();
|
|
|
|
} else {
|
2012-10-18 20:12:04 +02:00
|
|
|
narrow.show_all_messages();
|
2012-10-11 20:30:34 +02:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
return process_hotkey;
|
2012-10-03 17:04:43 +02:00
|
|
|
case 99: // 'c': compose
|
2012-10-17 17:55:11 +02:00
|
|
|
compose_button('stream');
|
|
|
|
return process_compose_hotkey;
|
|
|
|
case 67: // 'C': compose huddle
|
|
|
|
compose_button('personal');
|
2012-09-25 16:04:27 +02:00
|
|
|
return process_compose_hotkey;
|
2012-10-10 16:22:10 +02:00
|
|
|
case 114: // 'r': respond to message
|
2012-10-10 16:18:51 +02:00
|
|
|
respond_to_message();
|
2012-10-09 19:50:39 +02:00
|
|
|
return process_hotkey;
|
2012-10-03 17:30:48 +02:00
|
|
|
case 82: // 'R': respond to author
|
2012-10-10 16:18:51 +02:00
|
|
|
respond_to_message("personal");
|
2012-10-09 19:50:39 +02:00
|
|
|
return process_hotkey;
|
2012-10-03 17:04:43 +02:00
|
|
|
case 103: // 'g': start of "go to" command
|
2012-09-21 22:35:32 +02:00
|
|
|
return process_goto_hotkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-09 19:50:39 +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-09-22 00:34:26 +02:00
|
|
|
var goto_hotkeys = {
|
2012-10-18 20:12:04 +02:00
|
|
|
99: narrow.by_recipient, // 'c'
|
|
|
|
105: narrow.by_subject, // 'i'
|
|
|
|
112: narrow.all_personals, // 'p'
|
|
|
|
97: narrow.show_all_messages, // 'a'
|
|
|
|
27: hide_compose // Esc
|
2012-09-22 00:34:26 +02:00
|
|
|
};
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-10-18 19:58:10 +02:00
|
|
|
process_goto_hotkey = function (code) {
|
2012-10-18 20:12:04 +02:00
|
|
|
narrow.target(selected_message_id);
|
2012-10-15 18:57:24 +02:00
|
|
|
|
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
|
2012-10-09 19:50:39 +02:00
|
|
|
after an unrecognized "go to" command. */
|
2012-09-21 22:35:32 +02:00
|
|
|
return process_hotkey;
|
2012-10-18 19:58:10 +02:00
|
|
|
};
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-10-18 19:58:10 +02:00
|
|
|
process_key_in_input = function (code) {
|
2012-09-21 22:35:32 +02:00
|
|
|
if (code === 27) {
|
2012-10-09 19:50:39 +02:00
|
|
|
// If the user hit the escape key, hide the compose window
|
2012-09-21 22:35:32 +02:00
|
|
|
hide_compose();
|
|
|
|
}
|
2012-10-09 19:50:39 +02:00
|
|
|
// Otherwise, let the browser handle the key normally
|
2012-09-21 22:35:32 +02:00
|
|
|
return false;
|
2012-10-18 19:58:10 +02:00
|
|
|
};
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-10-18 19:58:10 +02:00
|
|
|
process_compose_hotkey = function (code) {
|
2012-10-10 23:37:14 +02:00
|
|
|
if (code === 9) { // Tab: toggles between stream and huddle compose tabs.
|
2012-09-25 16:04:27 +02:00
|
|
|
toggle_compose();
|
|
|
|
return process_compose_hotkey;
|
|
|
|
}
|
2012-10-09 20:10:41 +02:00
|
|
|
// Process the first non-tab character and everything after it
|
|
|
|
// like any other keys typed in the input box
|
2012-10-09 19:50:39 +02:00
|
|
|
keydown_handler = process_hotkey;
|
2012-10-09 20:10:41 +02:00
|
|
|
return process_hotkey(code);
|
2012-10-18 19:58:10 +02:00
|
|
|
};
|
2012-09-25 16:04:27 +02:00
|
|
|
|
2012-10-18 19:58:10 +02:00
|
|
|
exports.set_compose = function () {
|
2012-10-17 18:05:43 +02:00
|
|
|
keydown_handler = process_compose_hotkey;
|
2012-10-18 19:58:10 +02:00
|
|
|
};
|
2012-10-17 18:05:43 +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-10-03 17:04:43 +02:00
|
|
|
/* 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
|
|
|
|
letters/numbers in keydown, we wouldn't know what the case of
|
|
|
|
the letters were.
|
|
|
|
|
|
|
|
We want case-sensitive hotkeys (such as in the case of r vs R)
|
|
|
|
so we bail in .keydown if the event is a letter or number and
|
|
|
|
instead just let keypress go for it. */
|
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
$(document).keydown(function (event) {
|
2012-10-03 17:04:43 +02:00
|
|
|
if (48 > event.which ||90 < event.which) { // outside the alphanumeric range
|
|
|
|
var result = keydown_handler(event.which);
|
|
|
|
if (typeof result === 'function') {
|
|
|
|
keydown_handler = result;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).keypress(function (event) {
|
|
|
|
// What exactly triggers .keypress may vary by browser.
|
|
|
|
// Welcome to compatability hell.
|
2012-10-15 16:45:31 +02:00
|
|
|
//
|
|
|
|
// In particular, when you press tab in Firefox, it fires a
|
|
|
|
// keypress event with keycode 0 after processing the original
|
|
|
|
// event.
|
|
|
|
if (event.which !== 0 && event.charCode !== 0) {
|
|
|
|
var result = keydown_handler(event.which);
|
|
|
|
if (typeof result === 'function') {
|
|
|
|
keydown_handler = result;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
|
|
|
});
|
2012-10-18 19:58:10 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|