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 exports = {};
|
||||||
|
|
||||||
var jwindow;
|
var jwindow;
|
||||||
var height;
|
var dimensions = {};
|
||||||
var width;
|
|
||||||
var in_stoppable_autoscroll = false;
|
var in_stoppable_autoscroll = false;
|
||||||
|
|
||||||
exports.at_top = function () {
|
exports.at_top = function () {
|
||||||
@@ -94,28 +93,21 @@ exports.scrollTop = function viewport_scrollTop () {
|
|||||||
return jwindow.scrollTop.apply(jwindow, arguments);
|
return jwindow.scrollTop.apply(jwindow, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.height = function viewport_height() {
|
function make_dimen_wrapper(dimen_name, dimen_func) {
|
||||||
if (arguments.length !== 0) {
|
return function viewport_dimension_wrapper() {
|
||||||
height = undefined;
|
if (arguments.length !== 0) {
|
||||||
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;
|
}
|
||||||
};
|
return dimensions[dimen_name];
|
||||||
|
};
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
exports.height = make_dimen_wrapper('height', $(window).height);
|
||||||
|
exports.width = make_dimen_wrapper('width', $(window).width);
|
||||||
|
|
||||||
exports.stop_auto_scrolling = function() {
|
exports.stop_auto_scrolling = function() {
|
||||||
if (in_stoppable_autoscroll) {
|
if (in_stoppable_autoscroll) {
|
||||||
@@ -152,8 +144,7 @@ $(function () {
|
|||||||
jwindow = $(window);
|
jwindow = $(window);
|
||||||
// This handler must be placed before all resize handlers in our application
|
// This handler must be placed before all resize handlers in our application
|
||||||
jwindow.resize(function () {
|
jwindow.resize(function () {
|
||||||
height = undefined;
|
dimensions = {};
|
||||||
width = undefined;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user