2012-10-18 19:58:10 +02:00
|
|
|
var hotkeys = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2012-09-21 23:51:31 +02:00
|
|
|
var directional_hotkeys = {
|
2013-04-02 20:56:38 +02:00
|
|
|
40: {getrow: rows.next_visible, direction: 1}, // down arrow
|
|
|
|
106: {getrow: rows.next_visible, direction: 1}, // 'j'
|
|
|
|
38: {getrow: rows.prev_visible, direction: -1}, // up arrow
|
|
|
|
107: {getrow: rows.prev_visible, direction: -1}, // 'k'
|
|
|
|
36: {getrow: rows.first_visible, direction: -1} // Home
|
2013-02-28 22:38:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var directional_hotkeys_id = {
|
2013-04-02 20:56:38 +02:00
|
|
|
35: {getid: function () {return current_msg_list.last().id;},
|
|
|
|
direction: 1} // End
|
2012-09-21 23:51:31 +02:00
|
|
|
};
|
|
|
|
|
2012-10-23 20:55:24 +02:00
|
|
|
var narrow_hotkeys = {
|
2012-12-03 19:49:12 +01:00
|
|
|
115: narrow.by_recipient, // 's'
|
|
|
|
83: narrow.by_subject, // 'S'
|
2012-12-12 20:34:47 +01:00
|
|
|
118: function () { // 'v'
|
2013-01-02 19:21:39 +01:00
|
|
|
narrow.by('is', 'private-message');
|
2012-12-12 20:34:47 +01:00
|
|
|
}
|
2012-10-23 20:55:24 +02:00
|
|
|
};
|
|
|
|
|
2012-11-29 19:55:08 +01:00
|
|
|
// Process a keydown or keypress event.
|
|
|
|
//
|
|
|
|
// Returns true if we handled it, false if the browser should.
|
2012-11-11 22:08:52 +01:00
|
|
|
function process_hotkey(e) {
|
|
|
|
var code = e.which;
|
2013-04-02 20:56:38 +02:00
|
|
|
var next_row, dirkey;
|
2012-10-09 19:50:39 +02:00
|
|
|
|
2012-11-30 18:28:23 +01:00
|
|
|
// Disable hotkeys on settings page etc., and when a modal pop-up
|
|
|
|
// is visible.
|
2012-12-03 20:54:29 +01:00
|
|
|
if (ui.home_tab_obscured())
|
2012-10-09 20:09:03 +02:00
|
|
|
return false;
|
2012-11-30 00:43:41 +01:00
|
|
|
|
2013-02-18 08:16:57 +01:00
|
|
|
// Handle a few keys specially when the send button is focused.
|
|
|
|
if ($('#compose-send-button').is(':focus')) {
|
|
|
|
if (code === 8) {
|
|
|
|
// Ignore backspace; don't navigate back a page.
|
|
|
|
return true;
|
|
|
|
} else if ((code === 9) && e.shiftKey) {
|
|
|
|
// Shift-Tab: go back to content textarea and restore
|
|
|
|
// cursor position.
|
|
|
|
ui.restore_compose_cursor();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-08 17:25:17 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 19:55:08 +01:00
|
|
|
// Process hotkeys specially when in an input, textarea, or send button
|
2012-11-02 20:39:33 +01:00
|
|
|
if ($('input:focus,textarea:focus,#compose-send-button:focus').length > 0) {
|
2012-11-29 19:55:08 +01:00
|
|
|
if (code === 27) {
|
|
|
|
// If one of our typeaheads is open, do nothing so that the Esc
|
|
|
|
// will go to close it
|
|
|
|
if ($("#subject").data().typeahead.shown ||
|
|
|
|
$("#stream").data().typeahead.shown ||
|
2013-01-10 21:17:21 +01:00
|
|
|
$("#private_message_recipient").data().typeahead.shown ||
|
2013-03-11 16:03:47 +01:00
|
|
|
$("#new_message_content").data().typeahead.shown ||
|
|
|
|
$("#search_query").data().typeahead.shown) {
|
|
|
|
// For some reason this code is only needed in Firefox;
|
|
|
|
// in Chrome our typeahead is able to intercept the Esc
|
|
|
|
// event before we even get it.
|
|
|
|
// Regardless, we do nothing in this case.
|
|
|
|
return true;
|
|
|
|
} else if (compose.composing()) {
|
2012-11-29 19:55:08 +01:00
|
|
|
// If the user hit the escape key, cancel the current compose
|
|
|
|
compose.cancel();
|
2013-03-11 16:03:47 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// We pressed Esc and something was focused, and the composebox
|
|
|
|
// wasn't open. In that case, we should blur the input.
|
|
|
|
// (this is almost certainly the searchbar)
|
|
|
|
$("input:focus,textarea:focus").blur();
|
|
|
|
return true;
|
2012-11-29 19:55:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-12 21:53:54 +01:00
|
|
|
// If we just typed a character to change the recipient in the
|
|
|
|
// compose box, this means that we're no longer replying to
|
|
|
|
// whatever the original message was, and we should unfade.
|
|
|
|
if (compose.composing() &&
|
|
|
|
$("#stream:focus,#subject:focus,#private_message_recipient:focus").length > 0) {
|
|
|
|
compose.unfade_messages(true);
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-29 19:55:08 +01:00
|
|
|
// Let the browser handle the key normally.
|
|
|
|
return false;
|
2012-10-09 19:50:39 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 23:19:01 +01:00
|
|
|
// If we're on a button or a link and have pressed enter, let the
|
|
|
|
// browser handle the keypress
|
|
|
|
//
|
|
|
|
// This is subtle and here's why: Suppose you have the focus on a
|
|
|
|
// stream name in your left sidebar. j and k will still move your
|
|
|
|
// cursor up and down, but Enter won't reply -- it'll just trigger
|
|
|
|
// the link on the sidebar! So you keep pressing enter over and
|
|
|
|
// over again. Until you click somewhere or press r.
|
|
|
|
if ($('a:focus,button:focus').length > 0 && code === 13) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-28 22:38:08 +01:00
|
|
|
if (directional_hotkeys_id.hasOwnProperty(code)) {
|
2013-04-09 22:37:37 +02:00
|
|
|
if (current_msg_list.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-02 20:56:38 +02:00
|
|
|
dirkey = directional_hotkeys_id[code];
|
|
|
|
var next_id = dirkey.getid();
|
2013-04-03 19:33:10 +02:00
|
|
|
last_viewport_movement_direction = dirkey.direction;
|
|
|
|
current_msg_list.select_id(next_id, {then_scroll: true,
|
|
|
|
from_scroll: true});
|
2013-02-28 22:38:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-04 07:27:53 +02:00
|
|
|
if (directional_hotkeys.hasOwnProperty(code)) {
|
2013-04-09 22:37:37 +02:00
|
|
|
if (current_msg_list.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-02 20:56:38 +02:00
|
|
|
dirkey = directional_hotkeys[code];
|
|
|
|
last_viewport_movement_direction = dirkey.direction;
|
|
|
|
next_row = dirkey.getrow(current_msg_list.selected_row());
|
2013-02-14 23:48:37 +01:00
|
|
|
if (next_row.length !== 0) {
|
2013-04-03 19:33:10 +02:00
|
|
|
current_msg_list.select_id(rows.id(next_row),
|
|
|
|
{then_scroll: true,
|
|
|
|
from_scroll: true});
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
2013-02-14 23:48:37 +01:00
|
|
|
if ((next_row.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
|
|
|
//
|
2012-10-18 20:55:41 +02:00
|
|
|
// FIXME: this doesn't work for End because rows.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-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-09-21 23:51:31 +02:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-10-23 20:55:24 +02:00
|
|
|
if (narrow_hotkeys.hasOwnProperty(code)) {
|
2013-04-09 22:37:37 +02:00
|
|
|
if (current_msg_list.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-20 18:26:50 +01:00
|
|
|
narrow_hotkeys[code](current_msg_list.selected_id());
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-10-23 20:55:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-11 22:46:23 +01:00
|
|
|
// We're in the middle of a combo; stop processing because
|
|
|
|
// we want the browser to handle it (to avoid breaking
|
|
|
|
// things like Ctrl-C or Command-C for copy).
|
|
|
|
if (e.metaKey || e.ctrlKey) {
|
2012-09-26 23:37:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-21 23:51:31 +02:00
|
|
|
switch (code) {
|
2012-09-24 16:52:48 +02:00
|
|
|
case 33: // Page Up
|
2013-04-03 17:23:02 +02:00
|
|
|
if (at_top_of_viewport() && !current_msg_list.empty()) {
|
2013-02-20 20:49:49 +01:00
|
|
|
current_msg_list.select_id(current_msg_list.first().id, {then_scroll: 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
|
2013-04-03 17:23:02 +02:00
|
|
|
if (at_bottom_of_viewport() && !current_msg_list.empty()) {
|
2013-02-20 20:49:49 +01:00
|
|
|
current_msg_list.select_id(current_msg_list.last().id, {then_scroll: false});
|
2012-10-05 21:51:47 +02:00
|
|
|
}
|
2012-10-03 23:47:15 +02:00
|
|
|
return false;
|
2013-02-08 23:03:33 +01:00
|
|
|
case 27: // Esc: close actions popup, cancel compose, clear a find, or un-narrow
|
|
|
|
if (ui.actions_currently_popped()) {
|
|
|
|
ui.hide_actions_popover();
|
2012-10-29 20:48:22 +01:00
|
|
|
} else if (compose.composing()) {
|
2012-10-18 20:17:55 +02:00
|
|
|
compose.cancel();
|
2012-10-11 20:30:34 +02:00
|
|
|
} else {
|
2012-12-18 21:45:48 +01:00
|
|
|
search.clear_search();
|
2012-10-11 20:30:34 +02:00
|
|
|
}
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-10-03 17:04:43 +02:00
|
|
|
case 99: // 'c': compose
|
2013-02-01 23:16:00 +01:00
|
|
|
compose.set_mode('stream');
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-12-03 19:49:12 +01:00
|
|
|
case 67: // 'C': compose private message
|
2013-02-01 23:16:00 +01:00
|
|
|
compose.set_mode('private');
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-12-07 20:18:01 +01:00
|
|
|
case 13: // Enter: respond to message (unless we need to do something else)
|
2013-02-28 21:11:53 +01:00
|
|
|
respond_to_message();
|
|
|
|
return true;
|
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-11-29 19:55:08 +01:00
|
|
|
return true;
|
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-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-11-01 17:05:17 +01:00
|
|
|
case 47: // '/': initiate search
|
2012-11-14 20:52:53 +01:00
|
|
|
search.initiate_search();
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-10-25 00:15:36 +02:00
|
|
|
case 63: // '?': Show keyboard shortcuts page
|
|
|
|
$('#keyboard-shortcuts').modal('show');
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-11-11 22:48:51 +01:00
|
|
|
$(document).keydown(function (e) {
|
2012-10-31 23:10:15 +01:00
|
|
|
// Restrict to non-alphanumeric keys
|
2012-11-29 19:55:08 +01:00
|
|
|
if (48 > e.which || 90 < e.which) {
|
|
|
|
if (process_hotkey(e))
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2012-10-03 17:04:43 +02:00
|
|
|
});
|
|
|
|
|
2012-11-11 22:48:51 +01:00
|
|
|
$(document).keypress(function (e) {
|
2012-10-03 17:04:43 +02:00
|
|
|
// 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.
|
2012-11-29 19:55:08 +01:00
|
|
|
if (e.which !== 0 && e.charCode !== 0) {
|
|
|
|
if (process_hotkey(e))
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
});
|
2012-10-18 19:58:10 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|