From 7d7d89e4867bad59ac902883c584eaaf59a17482 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 31 May 2018 22:50:17 -0700 Subject: [PATCH] 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. --- static/js/narrow.js | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/static/js/narrow.js b/static/js/narrow.js index 73e7b5eb39..fe87a4ee5c 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -106,17 +106,11 @@ exports.activate = function (raw_operators, opts) { }); var id_info = { - previous_id: undefined, target_id: undefined, local_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 // selected and should be the center of the narrow. 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); } - var offset_of_prev_selection = (function () { - if (opts.then_select_offset !== undefined) { - // If the caller passes in an explicit offset, - // we don't need the offset of then_select_id; - return; + if (opts.then_select_id > 0) { + // We override target_id in this case, since the user could be + // having a near: narrow auto-reloaded. + id_info.target_id = opts.then_select_id; + 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) { unread.messages_read_in_narrow = false; @@ -200,16 +193,10 @@ exports.activate = function (raw_operators, opts) { message_list.narrowed = msg_list; current_msg_list = message_list.narrowed; - var then_select_offset = (function () { - if (opts.then_select_offset !== undefined) { - // We get passed in a offset for reloads. - return opts.then_select_offset; - } - - if (id_info.previous_id === id_info.final_select_id) { - return offset_of_prev_selection; - } - }()); + var then_select_offset; + if (id_info.target_id === id_info.final_select_id) { + then_select_offset = opts.then_select_offset; + } var select_immediately = (id_info.local_select_id !== undefined);