From a70fb98038554adeca5fac640f8b375cf39a17c9 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Sat, 24 Nov 2012 02:03:17 -0500 Subject: [PATCH] Reduce code duplication in keep_pointer_in_view Fixes #269. (imported from commit 5d382c0e1493a0798fc9bd1f5a8f37e15f2ea5ef) --- zephyr/static/js/zephyr.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index c9b7405895..118e79be7d 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -756,25 +756,21 @@ function keep_pointer_in_view() { if (next_message.length === 0) return; - if (above_view_threshold(next_message) && (!at_top_of_viewport())) { - while (above_view_threshold(next_message)) { - candidate = rows.next_visible(next_message); - if (candidate.length === 0) { + function adjust(past_threshold, at_end, advance) { + if (!past_threshold(next_message) || at_end()) + return false; // try other side + while (past_threshold(next_message)) { + candidate = advance(next_message); + if (candidate.length === 0) break; - } else { - next_message = candidate; - } - } - } else if (below_view_threshold(next_message) && (!at_bottom_of_viewport())) { - while (below_view_threshold(next_message)) { - candidate = rows.prev_visible(next_message); - if (candidate.length === 0) { - break; - } else { - next_message = candidate; - } + next_message = candidate; } + return true; } + + if (! adjust(above_view_threshold, at_top_of_viewport, rows.next_visible)) + adjust(below_view_threshold, at_bottom_of_viewport, rows.prev_visible); + update_selected_message(next_message, {update_server: true}); }