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:
Zev Benjamin
2013-06-19 12:12:49 -04:00
committed by Luke Faraone
parent 7b75dd9cc4
commit 338a0fb4ce

View File

@@ -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() {
if (arguments.length !== 0) {
height = undefined;
return jwindow.height.apply(jwindow, arguments);
}
if (height === undefined) {
height = $(window).height();
}
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;
};
function make_dimen_wrapper(dimen_name, dimen_func) {
return function viewport_dimension_wrapper() {
if (arguments.length !== 0) {
delete dimensions[dimen_name];
return dimen_func.apply(jwindow, arguments);
}
if (! dimensions.hasOwnProperty(dimen_name)) {
dimensions[dimen_name] = dimen_func.call(jwindow);
}
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 = {};
});
});