diff --git a/static/js/ui.js b/static/js/ui.js index 6f963037ad..fdcc6ed493 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -1401,10 +1401,12 @@ function scroll_finish() { scroll_timer = setTimeout(scroll_finished, 100); } -$(window).scroll($.throttle(50, function (e) { - process_visible_unread_messages(); - scroll_finish(); -})); +exports.register_scroll_handler = function () { + $(window).scroll($.throttle(50, function (e) { + process_visible_unread_messages(); + scroll_finish(); + })); +}; var presence_descriptions = { active: 'is active', diff --git a/static/js/zulip.js b/static/js/zulip.js index ffaf5567d6..75073662fb 100644 --- a/static/js/zulip.js +++ b/static/js/zulip.js @@ -39,6 +39,8 @@ var pointer_update_in_flight = false; var events_stored_during_tutorial = []; +var waiting_on_browser_scroll = true; + function add_person(person) { page_params.people_list.push(person); people_dict[person.email] = person; @@ -1051,6 +1053,13 @@ function process_result(messages, opts) { } function get_old_messages_success(data, opts) { + if (waiting_on_browser_scroll) { + setTimeout(function () { + get_old_messages_success(data, opts); + }, 25); + return; + } + if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) { // We unnarrowed before receiving new messages so // don't bother processing the newly arrived messages. @@ -1315,6 +1324,35 @@ function main() { } } +function install_main_scroll_handler() { + waiting_on_browser_scroll = false; + // Unregister this special un-throttled scroll handler. + $(window).unbind("scroll"); + // Register the normal throttled scroll handler. + ui.register_scroll_handler(); +} + $(function () { + // On a page load or reload, the browser will, at an idle time, scroll to + // the top of the page. We can't intercept this browser-induced scroll, so + // to make sure it doesn't in interfere with our scrolling to the correct + // place in your message feed, we let the browser scroll happen first. + + // After some time, if we haven't seen the browser scroll, give up + // and call main. + var browser_scroll_timer = setTimeout(function () { + install_main_scroll_handler(); + }, 500); + + (window).scroll(function () { + if (viewport.scrollTop() < viewport.height()) { + // This is the browser-induced scroll to the top of the + // page that we were waiting for. It only happens once, + // so stop waiting and call main. + install_main_scroll_handler(); + clearTimeout(browser_scroll_timer); + } + }); + main(); });