From b76422d8d0b0a12e252e409b36a6d5a8ad5dcb4c Mon Sep 17 00:00:00 2001 From: Waseem Daher Date: Mon, 15 Oct 2012 22:07:52 -0400 Subject: [PATCH] Cache the window selector as 'viewport' in an attempt to improve performance. (imported from commit 3e01382260938737fbee663f6a9e94ad495ef21e) --- tools/jslint/check-all.js | 2 +- zephyr/static/js/hotkey.js | 1 - zephyr/static/js/ui.js | 3 --- zephyr/static/js/zephyr.js | 9 ++------- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index 668743bffc..4c8113fbf4 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -45,7 +45,7 @@ var globals = + ' scroll_to_selected select_and_show_by_id' + ' selected_message selected_message_id' + ' at_top_of_viewport at_bottom_of_viewport' - + + ' viewport' ; diff --git a/zephyr/static/js/hotkey.js b/zephyr/static/js/hotkey.js index 3c3d7ff58e..347a3d1f7d 100644 --- a/zephyr/static/js/hotkey.js +++ b/zephyr/static/js/hotkey.js @@ -46,7 +46,6 @@ function process_hotkey(code) { // // FIXME: this doesn't work for End because get_last_visible() // always returns a message. - var viewport = $(window); viewport.scrollTop($("#main_div").outerHeight(true)); } return process_hotkey; diff --git a/zephyr/static/js/ui.js b/zephyr/static/js/ui.js index e84263973e..380fd6369c 100644 --- a/zephyr/static/js/ui.js +++ b/zephyr/static/js/ui.js @@ -72,7 +72,6 @@ function mousemove() { } function resizehandler(e) { - var viewport = $(window); var sidebar = $("#sidebar"); var sidebar_nav = $(".sidebar-nav"); var composebox = $("#compose"); @@ -274,13 +273,11 @@ $(function () { $('#sidebar a[data-toggle="pill"]').on('show', function (e) { // Save the position of our old tab away, before we switch - var viewport = $(window); var old_tab = $(e.relatedTarget).attr('href'); scroll_positions[old_tab] = viewport.scrollTop(); }); $('#sidebar a[data-toggle="pill"]').on('shown', function (e) { // Right after we show the new tab, restore its old scroll position - var viewport = $(window); var target_tab = $(e.target).attr('href'); if (scroll_positions.hasOwnProperty(target_tab)) { viewport.scrollTop(scroll_positions[target_tab]); diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index ff1da3a982..b6d08b3bfc 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -4,6 +4,7 @@ var subject_dict = {}; var people_hash = {}; var selected_message_class = 'selected_message'; +var viewport = $(window); $(function () { var i; @@ -69,14 +70,12 @@ var message_groups = { function above_view_threshold(message) { // Barnowl-style thresholds: the pointer is never above the // 1/5-mark. - var viewport = $(window); return message.offset().top < viewport.scrollTop() + viewport.height() / 5; } function below_view_threshold(message) { // Barnowl-style thresholds: the pointer is never below the // 4/5-mark. - var viewport = $(window); return message.offset().top + message.outerHeight(true) > viewport.scrollTop() + viewport.height() * 4 / 5; } @@ -88,7 +87,6 @@ function recenter_view(message) { // If this logic changes, above_view_threshold andd // below_view_threshold must also change. - var viewport = $(window); if (above_view_threshold(message)) { viewport.scrollTop(selected_message.offset().top - viewport.height() / 2); } else if (below_view_threshold(message)) { @@ -189,7 +187,6 @@ function update_selected_message(message) { } function select_message(next_message, scroll_to) { - var viewport = $(window); /* If the message exists but is hidden, try to find the next visible one. */ if (next_message.length !== 0 && next_message.is(':hidden')) { @@ -539,17 +536,15 @@ setInterval(function() { }, 5000); function at_top_of_viewport() { - return ($(window).scrollTop() === 0); + return (viewport.scrollTop() === 0); } function at_bottom_of_viewport() { - var viewport = $(window); return (viewport.scrollTop() + viewport.height() >= $("#main_div").outerHeight(true)); } function keep_pointer_in_view() { var candidate; - var viewport = $(window); var next_message = get_message_row(selected_message_id); if (above_view_threshold(next_message) && (!at_top_of_viewport())) {