narrow: Clean up computation of then_select_offset.

We had a significant amount of code for handling what seemed to be 2
cases, but which were really just a single case (if we are trying to
narrow to a specific message ID, and we end up landing on it, restore
the previous offset; with the special case that the previous offset
might be passed in from the previous call).

This cleanup also fixes a very minor bug, where our background
auto-reload (`reload.initiate({immediate: true});` in the JS console)
would incorrectly reset the pointer position to match the a near:
message ID if that was present in the narrow.
This commit is contained in:
Tim Abbott 2018-05-31 22:50:17 -07:00
parent baa691db7d
commit 7d7d89e486
1 changed files with 14 additions and 27 deletions

View File

@ -106,17 +106,11 @@ exports.activate = function (raw_operators, opts) {
}); });
var id_info = { var id_info = {
previous_id: undefined,
target_id: undefined, target_id: undefined,
local_select_id: undefined, local_select_id: undefined,
final_select_id: undefined, final_select_id: undefined,
}; };
if (opts.then_select_id > 0) {
id_info.previous_id = opts.then_select_id;
id_info.target_id = id_info.previous_id;
}
// These two narrowing operators specify what message should be // These two narrowing operators specify what message should be
// selected and should be the center of the narrow. // selected and should be the center of the narrow.
if (filter.has_operator("near")) { if (filter.has_operator("near")) {
@ -126,18 +120,17 @@ exports.activate = function (raw_operators, opts) {
id_info.target_id = parseInt(filter.operands("id")[0], 10); id_info.target_id = parseInt(filter.operands("id")[0], 10);
} }
var offset_of_prev_selection = (function () { if (opts.then_select_id > 0) {
if (opts.then_select_offset !== undefined) { // We override target_id in this case, since the user could be
// If the caller passes in an explicit offset, // having a near: narrow auto-reloaded.
// we don't need the offset of then_select_id; id_info.target_id = opts.then_select_id;
return; if (opts.then_select_offset === undefined) {
var row = current_msg_list.get_row(opts.then_select_id);
if (row.length > 0) {
opts.then_select_offset = row.offset().top;
}
} }
}
var row = current_msg_list.get_row(opts.previous_id);
if (row.length > 0) {
return row.offset().top;
}
}());
if (!was_narrowed_already) { if (!was_narrowed_already) {
unread.messages_read_in_narrow = false; unread.messages_read_in_narrow = false;
@ -200,16 +193,10 @@ exports.activate = function (raw_operators, opts) {
message_list.narrowed = msg_list; message_list.narrowed = msg_list;
current_msg_list = message_list.narrowed; current_msg_list = message_list.narrowed;
var then_select_offset = (function () { var then_select_offset;
if (opts.then_select_offset !== undefined) { if (id_info.target_id === id_info.final_select_id) {
// We get passed in a offset for reloads. then_select_offset = opts.then_select_offset;
return opts.then_select_offset; }
}
if (id_info.previous_id === id_info.final_select_id) {
return offset_of_prev_selection;
}
}());
var select_immediately = (id_info.local_select_id !== undefined); var select_immediately = (id_info.local_select_id !== undefined);