Cache the window selector as 'viewport' in an attempt to improve performance.

(imported from commit 3e01382260938737fbee663f6a9e94ad495ef21e)
This commit is contained in:
Waseem Daher 2012-10-15 22:07:52 -04:00
parent 45629a362d
commit b76422d8d0
4 changed files with 3 additions and 12 deletions

View File

@ -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'
;

View File

@ -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;

View File

@ -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]);

View File

@ -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())) {