Speed up key handling by adding modals.is_active().

The function modals.is_active() can see if modals are open
without having to look at the DOM.  This should make it snappier
to type in the compose box.  Even if the speedup is pretty minor,
not having to worry about jQuery slowness should make it easier
to diagnose future compose box issues.

The new function gets used in other places, too, where performance
isn't so much an issue.
This commit is contained in:
Steve Howell 2017-05-05 17:40:32 -07:00 committed by Tim Abbott
parent 3c0ef6295f
commit 742c55f514
11 changed files with 17 additions and 21 deletions

View File

@ -177,11 +177,11 @@ function stubbing(func_name_to_stub, test_function) {
} }
_.each([return_true, return_false], function (is_settings_page) { _.each([return_true, return_false], function (is_settings_page) {
_.each([return_true, return_false], function (home_tab_obscured) { _.each([return_true, return_false], function (is_active) {
_.each([return_true, return_false], function (is_info_overlay) { _.each([return_true, return_false], function (is_info_overlay) {
hotkey.is_settings_page = is_settings_page; hotkey.is_settings_page = is_settings_page;
set_global('ui_state', {home_tab_obscured: home_tab_obscured, set_global('modals', {is_active: is_active});
is_info_overlay: is_info_overlay}); set_global('ui_state', {is_info_overlay: is_info_overlay});
test_normal_typing(); test_normal_typing();
}); });

View File

@ -314,7 +314,7 @@ $(function () {
}); });
$(".brand").on('click', function (e) { $(".brand").on('click', function (e) {
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
ui_util.change_tab_to('#home'); ui_util.change_tab_to('#home');
} else { } else {
narrow.restore_home_state(); narrow.restore_home_state();

View File

@ -456,7 +456,7 @@ exports.process_hotkey = function (e, hotkey) {
return false; return false;
} }
if (hotkey.message_view_only && ui_state.home_tab_obscured()) { if (hotkey.message_view_only && modals.is_active()) {
return false; return false;
} }

View File

@ -12,6 +12,10 @@ function reset_state() {
open_modal_name = undefined; open_modal_name = undefined;
} }
exports.is_active = function () {
return !!open_modal_name;
};
exports.open_overlay = function (opts) { exports.open_overlay = function (opts) {
if (!opts.name || !opts.overlay || !opts.on_close) { if (!opts.name || !opts.overlay || !opts.on_close) {
blueslip.error('Programming error in open_modal'); blueslip.error('Programming error in open_modal');

View File

@ -461,7 +461,7 @@ exports.restore_home_state = function () {
// If we click on the Home link from another nav pane, just go // If we click on the Home link from another nav pane, just go
// back to the state you were in (possibly still narrowed) before // back to the state you were in (possibly still narrowed) before
// you left the Home pane. // you left the Home pane.
if (!ui_state.home_tab_obscured()) { if (!modals.is_active()) {
exports.deactivate(); exports.deactivate();
} }
navigate.maybe_scroll_to_selected(); navigate.maybe_scroll_to_selected();

View File

@ -427,7 +427,7 @@ $(function () {
if (e.metaKey || e.ctrlKey) { if (e.metaKey || e.ctrlKey) {
return; return;
} }
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
ui_util.change_tab_to('#home'); ui_util.change_tab_to('#home');
} }
var stream = $(e.target).parents('li').attr('data-name'); var stream = $(e.target).parents('li').attr('data-name');
@ -500,7 +500,7 @@ function maybe_select_stream(e) {
var topStream = $('#stream_filters li.narrow-filter').first().data('name'); var topStream = $('#stream_filters li.narrow-filter').first().data('name');
if (topStream !== undefined) { if (topStream !== undefined) {
// undefined if there are no results // undefined if there are no results
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
ui_util.change_tab_to('#home'); ui_util.change_tab_to('#home');
} }
exports.clear_and_hide_search(); exports.clear_and_hide_search();

View File

@ -11,7 +11,7 @@ exports.update_in_home_view = function (sub, value) {
var msg_offset; var msg_offset;
var saved_ypos; var saved_ypos;
// Save our current scroll position // Save our current scroll position
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
saved_ypos = message_viewport.scrollTop(); saved_ypos = message_viewport.scrollTop();
} else if (home_msg_list === current_msg_list && } else if (home_msg_list === current_msg_list &&
current_msg_list.selected_row().offset() !== null) { current_msg_list.selected_row().offset() !== null) {
@ -24,7 +24,7 @@ exports.update_in_home_view = function (sub, value) {
message_util.add_messages(message_list.all.all_messages(), home_msg_list); message_util.add_messages(message_list.all.all_messages(), home_msg_list);
// Ensure we're still at the same scroll position // Ensure we're still at the same scroll position
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
message_viewport.scrollTop(saved_ypos); message_viewport.scrollTop(saved_ypos);
} else if (home_msg_list === current_msg_list) { } else if (home_msg_list === current_msg_list) {
// We pass use_closest to handle the case where the // We pass use_closest to handle the case where the

View File

@ -212,7 +212,7 @@ exports.set_click_handlers = function (callbacks) {
// In a more componentized world, we would delegate some // In a more componentized world, we would delegate some
// of this stuff back up to our parents. // of this stuff back up to our parents.
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
ui_util.change_tab_to('#home'); ui_util.change_tab_to('#home');
} }

View File

@ -504,7 +504,7 @@ function welcome() {
} }
exports.start = function () { exports.start = function () {
if (ui_state.home_tab_obscured()) { if (modals.is_active()) {
ui_util.change_tab_to('#home'); ui_util.change_tab_to('#home');
} }
narrow.deactivate(); narrow.deactivate();

View File

@ -68,7 +68,7 @@ $(function () {
}); });
message_viewport.message_pane.mousewheel(function (e, delta) { message_viewport.message_pane.mousewheel(function (e, delta) {
if (!ui_state.home_tab_obscured()) { if (!modals.is_active()) {
// In the message view, we use a throttled mousewheel handler. // In the message view, we use a throttled mousewheel handler.
throttled_mousewheelhandler(e, delta); throttled_mousewheelhandler(e, delta);
} }

View File

@ -2,14 +2,6 @@ var ui_state = (function () {
var exports = {}; var exports = {};
exports.home_tab_obscured = function () {
if ($('.overlay.show').length > 0) {
return 'modal';
}
return false;
};
exports.is_info_overlay = function () { exports.is_info_overlay = function () {
return ($(".informational-overlays").hasClass("show")); return ($(".informational-overlays").hasClass("show"));
}; };