2012-10-18 19:58:10 +02:00
|
|
|
var hotkeys = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
function do_narrow_action(action) {
|
|
|
|
if (current_msg_list.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
action(current_msg_list.selected_id(), {trigger: 'hotkey'});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-11 23:06:58 +02:00
|
|
|
var actions_dropdown_hotkeys = [
|
|
|
|
'down_arrow',
|
|
|
|
'up_arrow',
|
|
|
|
'vim_up',
|
|
|
|
'vim_down',
|
|
|
|
'enter'
|
|
|
|
];
|
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
function get_event_name(e) {
|
2013-07-17 17:37:27 +02:00
|
|
|
// Note that multiple keys can map to the same event_name, which
|
|
|
|
// we'll do in cases where they have the exact same semantics.
|
|
|
|
// DON'T FORGET: update keyboard_shortcuts.html
|
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
if ((e.which === 9) && e.shiftKey) {
|
|
|
|
return 'shift_tab';
|
|
|
|
}
|
2013-06-07 18:37:18 +02:00
|
|
|
|
2013-06-11 04:06:16 +02: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) {
|
|
|
|
return 'ignore';
|
|
|
|
}
|
2012-09-21 23:51:31 +02:00
|
|
|
|
2013-06-11 15:20:30 +02:00
|
|
|
if (!e.shiftKey) {
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case 33: // Page Up
|
|
|
|
return 'page_up';
|
|
|
|
case 34: // Page Down
|
|
|
|
return 'page_down';
|
|
|
|
case 35:
|
|
|
|
return 'end';
|
|
|
|
case 36:
|
|
|
|
return 'home';
|
|
|
|
case 38:
|
|
|
|
return 'up_arrow';
|
|
|
|
case 40:
|
|
|
|
return 'down_arrow';
|
|
|
|
}
|
2012-12-12 20:34:47 +01:00
|
|
|
}
|
2013-06-11 04:06:16 +02:00
|
|
|
|
|
|
|
switch (e.which) {
|
|
|
|
case 8:
|
|
|
|
return 'backspace';
|
2013-07-19 23:13:01 +02:00
|
|
|
case 9:
|
|
|
|
return 'tab';
|
2013-06-11 04:06:16 +02:00
|
|
|
case 13:
|
|
|
|
return 'enter';
|
|
|
|
case 27:
|
|
|
|
return 'escape';
|
|
|
|
case 32: // Spacebar
|
2013-06-11 15:20:30 +02:00
|
|
|
if (e.shiftKey) {
|
|
|
|
return 'page_up';
|
|
|
|
} else {
|
|
|
|
return 'page_down';
|
|
|
|
}
|
2013-06-11 04:06:16 +02:00
|
|
|
case 47: // '/': initiate search
|
|
|
|
return 'search';
|
|
|
|
case 63: // '?': Show keyboard shortcuts page
|
|
|
|
return 'show_shortcuts';
|
|
|
|
case 67: // 'C'
|
|
|
|
return 'compose_private_message';
|
2013-07-17 17:37:27 +02:00
|
|
|
case 74: // 'J'
|
|
|
|
return 'page_down';
|
|
|
|
case 75: // 'K'
|
|
|
|
return 'page_up';
|
2013-06-11 04:06:16 +02:00
|
|
|
case 82: // 'R': respond to author
|
|
|
|
return 'respond_to_author';
|
|
|
|
case 83: //'S'
|
|
|
|
return 'narrow_by_subject';
|
|
|
|
case 99: // 'c'
|
|
|
|
return 'compose';
|
2013-07-11 23:06:58 +02:00
|
|
|
case 105: // 'i'
|
|
|
|
return 'message_actions';
|
2013-06-11 04:06:16 +02:00
|
|
|
case 106: // 'j'
|
|
|
|
return 'vim_down';
|
|
|
|
case 107: // 'k'
|
|
|
|
return 'vim_up';
|
|
|
|
case 114: // 'r': respond to message
|
|
|
|
return 'reply_message';
|
|
|
|
case 115: // 's'
|
|
|
|
return 'narrow_by_recipient';
|
|
|
|
case 118: // 'v'
|
|
|
|
return 'narrow_private';
|
|
|
|
}
|
|
|
|
return 'ignore';
|
|
|
|
}
|
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) {
|
2012-11-30 18:28:23 +01:00
|
|
|
// Disable hotkeys on settings page etc., and when a modal pop-up
|
|
|
|
// is visible.
|
2013-08-01 17:47:48 +02:00
|
|
|
if (ui.home_tab_obscured()) {
|
2012-10-09 20:09:03 +02:00
|
|
|
return false;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-11-30 00:43:41 +01:00
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
var event_name = get_event_name(e);
|
|
|
|
|
|
|
|
if (event_name === 'ignore') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-11 23:06:58 +02:00
|
|
|
if (popovers.actions_popped() && actions_dropdown_hotkeys.indexOf(event_name) !== -1) {
|
|
|
|
popovers.actions_menu_handle_keyboard(event_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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')) {
|
2013-06-11 04:06:16 +02:00
|
|
|
if (event_name === 'backspace') {
|
2013-02-18 08:16:57 +01:00
|
|
|
// Ignore backspace; don't navigate back a page.
|
|
|
|
return true;
|
2013-06-11 04:06:16 +02:00
|
|
|
} else if (event_name === 'shift_tab') {
|
2013-02-18 08:16:57 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-07-19 23:13:01 +02:00
|
|
|
// In Safari and the desktop app, we can't tab to buttons. Intercept the
|
|
|
|
// tab from the message edit content box to Save and then Cancel.
|
|
|
|
if (event_name === "tab") {
|
|
|
|
if ($("#message_edit_content").is(":focus")) {
|
|
|
|
$(".message_edit_save").focus();
|
|
|
|
return true;
|
|
|
|
} else if ($(".message_edit_save").is(":focus")) {
|
|
|
|
$(".message_edit_cancel").focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-06-11 04:06:16 +02:00
|
|
|
if (event_name === 'escape') {
|
2012-11-29 19:55:08 +01:00
|
|
|
// 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-06-07 18:37:18 +02:00
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
if ((event_name === 'up_arrow' || event_name === 'down_arrow')
|
2013-06-07 18:37:18 +02:00
|
|
|
&& compose.composing()
|
2013-06-11 17:30:55 +02:00
|
|
|
&& compose.message_content() === ""
|
2013-07-18 23:11:12 +02:00
|
|
|
&& $('#new_message_content').is(':focus')) {
|
2013-06-07 18:37:18 +02:00
|
|
|
compose.cancel();
|
|
|
|
// don't return, as we still want it to be picked up by the code below
|
|
|
|
} else {
|
|
|
|
// 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.
|
2013-06-11 04:06:16 +02:00
|
|
|
if ($('a:focus,button:focus').length > 0 && event_name === 'enter') {
|
2013-03-06 23:19:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
switch (event_name) {
|
2013-07-11 23:06:58 +02:00
|
|
|
case 'message_actions':
|
|
|
|
var id = current_msg_list.selected_id();
|
|
|
|
popovers.show_actions_popover($(".selected_message .actions_hover")[0], id);
|
|
|
|
return true;
|
2013-06-19 17:10:42 +02:00
|
|
|
case 'narrow_by_recipient':
|
|
|
|
return do_narrow_action(narrow.by_recipient);
|
|
|
|
case 'narrow_by_subject':
|
|
|
|
return do_narrow_action(narrow.by_subject);
|
2013-06-11 04:06:16 +02:00
|
|
|
case 'narrow_private':
|
|
|
|
return do_narrow_action(function (target, opts) {
|
2013-07-10 03:22:34 +02:00
|
|
|
narrow.by('is', 'private', opts);
|
2013-06-11 04:06:16 +02:00
|
|
|
});
|
2013-06-19 17:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (event_name) {
|
2013-06-11 04:06:16 +02:00
|
|
|
case 'escape': // Esc: close actions popup, cancel compose, clear a find, or un-narrow
|
2013-06-28 22:38:40 +02:00
|
|
|
if (popovers.any_active()) {
|
|
|
|
popovers.hide_all();
|
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;
|
2013-06-11 04:06:16 +02:00
|
|
|
case 'compose': // 'c': compose
|
2013-07-12 23:16:07 +02:00
|
|
|
compose.start('stream');
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2013-06-11 04:06:16 +02:00
|
|
|
case 'compose_private_message':
|
2013-07-12 23:16:07 +02:00
|
|
|
compose.start('private');
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2013-06-19 17:10:42 +02:00
|
|
|
case 'enter': // Enter: respond to message (unless we need to do something else)
|
|
|
|
respond_to_cursor = true;
|
|
|
|
respond_to_message({trigger: 'hotkey enter'});
|
|
|
|
return true;
|
|
|
|
case 'reply_message': // 'r': respond to message
|
|
|
|
respond_to_cursor = true;
|
|
|
|
respond_to_message({trigger: 'hotkey'});
|
|
|
|
return true;
|
|
|
|
case 'respond_to_author': // 'R': respond to author
|
|
|
|
respond_to_message({reply_type: "personal", trigger: 'hotkey pm'});
|
|
|
|
return true;
|
2013-06-11 04:06:16 +02:00
|
|
|
case 'search':
|
2012-11-14 20:52:53 +01:00
|
|
|
search.initiate_search();
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2013-06-11 04:06:16 +02:00
|
|
|
case 'show_shortcuts': // Show keyboard shortcuts page
|
2012-10-25 00:15:36 +02:00
|
|
|
$('#keyboard-shortcuts').modal('show');
|
2012-11-29 19:55:08 +01:00
|
|
|
return true;
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:51:13 +02:00
|
|
|
if (current_msg_list.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event_name) {
|
|
|
|
case 'down_arrow':
|
|
|
|
case 'vim_down':
|
|
|
|
navigate.down();
|
|
|
|
return true;
|
|
|
|
case 'up_arrow':
|
|
|
|
case 'vim_up':
|
|
|
|
navigate.up();
|
|
|
|
return true;
|
|
|
|
case 'home':
|
|
|
|
navigate.to_home();
|
|
|
|
return true;
|
2013-07-24 23:45:19 +02:00
|
|
|
case 'end':
|
|
|
|
navigate.to_end();
|
|
|
|
return true;
|
|
|
|
case 'page_up':
|
|
|
|
navigate.page_up();
|
|
|
|
return true;
|
|
|
|
case 'page_down':
|
|
|
|
navigate.page_down();
|
|
|
|
return true;
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (process_hotkey(e)) {
|
2012-11-29 19:55:08 +01:00
|
|
|
e.preventDefault();
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-11-29 19:55:08 +01:00
|
|
|
}
|
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) {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (process_hotkey(e)) {
|
2012-11-29 19:55:08 +01:00
|
|
|
e.preventDefault();
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-11-29 19:55:08 +01:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
});
|
2012-10-18 19:58:10 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|