2013-05-07 17:15:20 +02:00
|
|
|
var viewport = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var jwindow;
|
|
|
|
var height;
|
|
|
|
var width;
|
2013-05-24 19:53:25 +02:00
|
|
|
var in_stoppable_autoscroll = false;
|
2013-05-07 17:15:20 +02:00
|
|
|
|
2013-05-28 22:36:48 +02:00
|
|
|
exports.at_top = function () {
|
|
|
|
return (jwindow.scrollTop() <= 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.at_bottom = function () {
|
|
|
|
// outerHeight(true): Include margin
|
|
|
|
var bottom = jwindow.scrollTop() + jwindow.height();
|
|
|
|
var window_size = $(document).height();
|
|
|
|
|
2013-05-30 00:42:38 +02:00
|
|
|
// We only know within a pixel or two if we're
|
|
|
|
// exactly at the bottom, due to browser quirkiness,
|
|
|
|
// and we err on the side of saying that we are at
|
|
|
|
// the bottom.
|
|
|
|
return bottom + 2 >= window_size;
|
2013-05-28 22:36:48 +02:00
|
|
|
};
|
|
|
|
|
2013-06-05 17:28:10 +02:00
|
|
|
exports.set_message_position = function (message_top, message_height, viewport_info, ratio) {
|
2013-05-30 20:39:28 +02:00
|
|
|
// message_top = offset of the top of a message that you are positioning
|
2013-06-05 17:28:10 +02:00
|
|
|
// message_height = height of the message that you are positioning
|
2013-05-30 20:39:28 +02:00
|
|
|
// viewport_info = result of calling ui.message_viewport_info
|
|
|
|
// ratio = fraction indicating how far down the screen the msg should be
|
|
|
|
|
2013-06-05 17:28:10 +02:00
|
|
|
var how_far_down_in_visible_page = viewport_info.visible_height * ratio;
|
|
|
|
|
|
|
|
// special case: keep large messages fully on the screen
|
|
|
|
if (how_far_down_in_visible_page + message_height > viewport_info.visible_height) {
|
|
|
|
how_far_down_in_visible_page = viewport_info.visible_height - message_height;
|
|
|
|
|
|
|
|
// Next handle truly gigantic messages. We just say that the top of the
|
|
|
|
// message goes to the top of the viewing area. Realistically, gigantic
|
|
|
|
// messages should either be condensed, socially frowned upon, or scrolled
|
|
|
|
// with the mouse.
|
|
|
|
if (how_far_down_in_visible_page < 0) {
|
|
|
|
how_far_down_in_visible_page = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 20:39:28 +02:00
|
|
|
var hidden_top =
|
|
|
|
viewport_info.visible_top
|
|
|
|
- jwindow.scrollTop();
|
|
|
|
|
|
|
|
var message_offset =
|
2013-06-05 17:28:10 +02:00
|
|
|
how_far_down_in_visible_page
|
2013-05-30 20:39:28 +02:00
|
|
|
+ hidden_top;
|
|
|
|
|
|
|
|
var new_scroll_top =
|
|
|
|
message_top
|
|
|
|
- message_offset;
|
|
|
|
|
2013-05-31 23:45:40 +02:00
|
|
|
suppress_scroll_pointer_update = true; // Gets set to false in the scroll handler.
|
2013-05-30 20:39:28 +02:00
|
|
|
jwindow.scrollTop(new_scroll_top);
|
|
|
|
};
|
|
|
|
|
2013-05-07 17:15:20 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2013-05-24 19:53:25 +02:00
|
|
|
|
|
|
|
exports.stop_auto_scrolling = function() {
|
|
|
|
if (in_stoppable_autoscroll) {
|
|
|
|
$("html, body").stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.system_initiated_animate_scroll = function (scroll_amount) {
|
|
|
|
suppress_scroll_pointer_update = true; // Gets set to false in the scroll handler.
|
|
|
|
var viewport_offset = exports.scrollTop();
|
2013-05-31 19:59:24 +02:00
|
|
|
in_stoppable_autoscroll = true;
|
2013-05-24 19:53:25 +02:00
|
|
|
$("html, body").animate({
|
|
|
|
scrollTop: viewport_offset + scroll_amount,
|
|
|
|
always: function () {
|
|
|
|
in_stoppable_autoscroll = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.user_initiated_animate_scroll = function (scroll_amount) {
|
2013-05-28 20:06:07 +02:00
|
|
|
suppress_scroll_pointer_update = true; // Gets set to false in the scroll handler.
|
2013-05-24 19:53:25 +02:00
|
|
|
in_stoppable_autoscroll = false; // defensive
|
|
|
|
|
2013-05-28 20:06:07 +02:00
|
|
|
var viewport_offset = exports.scrollTop();
|
|
|
|
|
2013-05-24 19:53:25 +02:00
|
|
|
// We use $('html, body') because you can't animate window.scrollTop
|
|
|
|
// on Chrome (http://bugs.jquery.com/ticket/10419).
|
2013-05-28 20:06:07 +02:00
|
|
|
$("html, body").animate({
|
|
|
|
scrollTop: viewport_offset + scroll_amount
|
2013-05-24 19:53:25 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-07 17:15:20 +02:00
|
|
|
$(function () {
|
|
|
|
jwindow = $(window);
|
|
|
|
// This handler must be placed before all resize handlers in our application
|
|
|
|
jwindow.resize(function () {
|
|
|
|
height = undefined;
|
|
|
|
width = undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|