mirror of
https://github.com/zulip/zulip.git
synced 2025-11-13 18:36:36 +00:00
Reduce code duplication in viewport.js
This also gets rid of an inconsistency in the use of jwindow vs. $(window). (imported from commit a42e47ef0dd9eaf9aaa4d4e2eedb466d77ec3385)
This commit is contained in:
committed by
Luke Faraone
parent
7b75dd9cc4
commit
338a0fb4ce
@@ -2,8 +2,7 @@ var viewport = (function () {
|
||||
var exports = {};
|
||||
|
||||
var jwindow;
|
||||
var height;
|
||||
var width;
|
||||
var dimensions = {};
|
||||
var in_stoppable_autoscroll = false;
|
||||
|
||||
exports.at_top = function () {
|
||||
@@ -94,28 +93,21 @@ exports.scrollTop = function viewport_scrollTop () {
|
||||
return jwindow.scrollTop.apply(jwindow, arguments);
|
||||
};
|
||||
|
||||
exports.height = function viewport_height() {
|
||||
function make_dimen_wrapper(dimen_name, dimen_func) {
|
||||
return function viewport_dimension_wrapper() {
|
||||
if (arguments.length !== 0) {
|
||||
height = undefined;
|
||||
return jwindow.height.apply(jwindow, arguments);
|
||||
delete dimensions[dimen_name];
|
||||
return dimen_func.apply(jwindow, arguments);
|
||||
}
|
||||
if (height === undefined) {
|
||||
height = $(window).height();
|
||||
if (! dimensions.hasOwnProperty(dimen_name)) {
|
||||
dimensions[dimen_name] = dimen_func.call(jwindow);
|
||||
}
|
||||
return height;
|
||||
};
|
||||
|
||||
exports.width = function viewport_width() {
|
||||
if (arguments.length !== 0) {
|
||||
width = undefined;
|
||||
return jwindow.width.apply(jwindow, arguments);
|
||||
}
|
||||
if (width === undefined) {
|
||||
width = jwindow.width();
|
||||
}
|
||||
return width;
|
||||
};
|
||||
return dimensions[dimen_name];
|
||||
};
|
||||
}
|
||||
|
||||
exports.height = make_dimen_wrapper('height', $(window).height);
|
||||
exports.width = make_dimen_wrapper('width', $(window).width);
|
||||
|
||||
exports.stop_auto_scrolling = function() {
|
||||
if (in_stoppable_autoscroll) {
|
||||
@@ -152,8 +144,7 @@ $(function () {
|
||||
jwindow = $(window);
|
||||
// This handler must be placed before all resize handlers in our application
|
||||
jwindow.resize(function () {
|
||||
height = undefined;
|
||||
width = undefined;
|
||||
dimensions = {};
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user