2013-05-07 17:15:20 +02:00
|
|
|
var viewport = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var jwindow;
|
2013-06-19 18:12:49 +02:00
|
|
|
var dimensions = {};
|
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 () {
|
2013-06-19 20:20:56 +02:00
|
|
|
return (exports.scrollTop() <= 0);
|
2013-05-28 22:36:48 +02:00
|
|
|
};
|
|
|
|
|
2013-06-06 16:10:12 +02:00
|
|
|
exports.message_viewport_info = function () {
|
|
|
|
// Return a structure that tells us details of the viewport
|
|
|
|
// accounting for fixed elements like the top navbar.
|
|
|
|
//
|
|
|
|
// The message_header is NOT considered to be part of the visible
|
|
|
|
// message pane, which should make sense for callers, who will
|
|
|
|
// generally be concerned about whether actual message content is
|
|
|
|
// visible.
|
|
|
|
|
|
|
|
var res = {};
|
|
|
|
|
|
|
|
var element_just_above_us = $("#tab_bar_underpadding");
|
|
|
|
|
|
|
|
res.visible_top =
|
|
|
|
element_just_above_us.offset().top
|
|
|
|
+ element_just_above_us.height()
|
|
|
|
+ $(".message_header").height();
|
|
|
|
|
|
|
|
var element_just_below_us = $("#compose");
|
|
|
|
|
|
|
|
res.visible_height =
|
|
|
|
element_just_below_us.offset().top
|
|
|
|
- res.visible_top;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2013-05-28 22:36:48 +02:00
|
|
|
exports.at_bottom = function () {
|
|
|
|
// outerHeight(true): Include margin
|
2013-06-19 20:20:56 +02:00
|
|
|
var bottom = exports.scrollTop() + exports.height();
|
2013-05-28 22:36:48 +02:00
|
|
|
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-07-26 19:14:33 +02:00
|
|
|
// This differs from at_bottom in that it only requires the bottom message to
|
|
|
|
// be visible, but you may be able to scroll down further.
|
|
|
|
exports.bottom_message_visible = function () {
|
|
|
|
var last_row = rows.last_visible();
|
|
|
|
if (last_row.length) {
|
|
|
|
var message_bottom = last_row[0].getBoundingClientRect().bottom;
|
|
|
|
var bottom_of_feed = $("#compose")[0].getBoundingClientRect().top;
|
|
|
|
return bottom_of_feed > message_bottom;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-06 23:52:02 +02:00
|
|
|
exports.is_below_visible_bottom = function (offset) {
|
2013-06-19 20:20:56 +02:00
|
|
|
return offset > exports.scrollTop() + exports.height() - $("#compose").height();
|
2013-06-06 23:52:02 +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-06-06 16:10:12 +02:00
|
|
|
// viewport_info = result of calling viewport.message_viewport_info
|
2013-05-30 20:39:28 +02:00
|
|
|
// 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
|
2013-06-19 20:20:56 +02:00
|
|
|
- exports.scrollTop();
|
2013-05-30 20:39:28 +02:00
|
|
|
|
|
|
|
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-06-19 20:20:56 +02:00
|
|
|
exports.scrollTop(new_scroll_top);
|
2013-05-30 20:39:28 +02:00
|
|
|
};
|
|
|
|
|
2013-10-30 19:53:17 +01:00
|
|
|
function in_viewport_or_tall(rect, top_of_feed, bottom_of_feed,
|
|
|
|
require_fully_visible) {
|
|
|
|
if (require_fully_visible) {
|
|
|
|
return ((rect.top > top_of_feed) && // Message top is in view and
|
|
|
|
((rect.bottom < bottom_of_feed) || // message is fully in view or
|
|
|
|
((rect.height > bottom_of_feed - top_of_feed) &&
|
|
|
|
(rect.top < bottom_of_feed)))); // message is tall.
|
|
|
|
} else {
|
|
|
|
return (rect.bottom > top_of_feed && rect.top < bottom_of_feed);
|
|
|
|
}
|
2013-07-12 20:22:27 +02:00
|
|
|
}
|
2013-07-05 17:03:14 +02:00
|
|
|
|
2013-07-12 20:22:27 +02:00
|
|
|
function add_to_visible_messages(candidates, visible_messages,
|
2013-10-30 19:53:17 +01:00
|
|
|
top_of_feed, bottom_of_feed,
|
|
|
|
require_fully_visible) {
|
2013-08-01 00:16:40 +02:00
|
|
|
_.every(candidates, function (row) {
|
2013-07-12 20:22:27 +02:00
|
|
|
var row_rect = row.getBoundingClientRect();
|
|
|
|
// Mark very tall messages as read once we've gotten past them
|
2013-10-30 19:53:17 +01:00
|
|
|
if (in_viewport_or_tall(row_rect, top_of_feed, bottom_of_feed,
|
|
|
|
require_fully_visible)) {
|
2013-07-12 20:22:27 +02:00
|
|
|
visible_messages.push(current_msg_list.get(rows.id($(row))));
|
2013-08-01 00:16:40 +02:00
|
|
|
return true;
|
2013-07-12 20:22:27 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-07-03 21:51:59 +02:00
|
|
|
|
2013-07-31 23:28:42 +02:00
|
|
|
var top_of_feed = new util.CachedValue({
|
|
|
|
compute_value: function () {
|
|
|
|
return $("#tab_bar_underpadding")[0].getBoundingClientRect().bottom;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var bottom_of_feed = new util.CachedValue({
|
|
|
|
compute_value: function () {
|
|
|
|
return $("#compose")[0].getBoundingClientRect().top;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-10-30 19:53:17 +01:00
|
|
|
exports.visible_messages = function (require_fully_visible) {
|
2013-07-12 20:22:27 +02:00
|
|
|
// Note that when using getBoundingClientRect() we are getting offsets
|
|
|
|
// relative to the visible window, but when using jQuery's offset() we are
|
|
|
|
// getting offsets relative to the full scrollable window. You can't try to
|
|
|
|
// compare heights from these two methods.
|
|
|
|
|
2013-07-11 17:14:11 +02:00
|
|
|
var selected = current_msg_list.selected_message();
|
2013-07-31 23:28:42 +02:00
|
|
|
var height = bottom_of_feed.get() - top_of_feed.get();
|
2013-07-11 17:14:11 +02:00
|
|
|
|
2013-07-12 21:00:02 +02:00
|
|
|
// Being simplistic about this, the smallest message is 25 px high.
|
2013-08-14 22:00:32 +02:00
|
|
|
var selected_row = current_msg_list.selected_row();
|
2013-07-12 21:00:02 +02:00
|
|
|
var num_neighbors = Math.floor(height / 25);
|
2013-07-11 17:14:11 +02:00
|
|
|
|
2013-07-12 20:22:27 +02:00
|
|
|
// We do this explicitly without merges and without recalculating
|
|
|
|
// the feed bounds to keep this computation as cheap as possible.
|
2013-07-11 17:14:11 +02:00
|
|
|
var visible_messages = [];
|
2013-12-09 20:15:33 +01:00
|
|
|
var messages_above_pointer = selected_row.prevAll("div.message_row[zid]:lt(" + num_neighbors + ")");
|
|
|
|
var messages_below_pointer = selected_row.nextAll("div.message_row[zid]:lt(" + num_neighbors + ")");
|
2013-07-12 20:22:27 +02:00
|
|
|
add_to_visible_messages(selected_row, visible_messages,
|
2013-10-30 19:53:17 +01:00
|
|
|
top_of_feed.get(), bottom_of_feed.get(), require_fully_visible);
|
2013-07-12 20:22:27 +02:00
|
|
|
add_to_visible_messages(messages_above_pointer, visible_messages,
|
2013-10-30 19:53:17 +01:00
|
|
|
top_of_feed.get(), bottom_of_feed.get(), require_fully_visible);
|
2013-07-12 20:22:27 +02:00
|
|
|
add_to_visible_messages(messages_below_pointer, visible_messages,
|
2013-10-30 19:53:17 +01:00
|
|
|
top_of_feed.get(), bottom_of_feed.get(), require_fully_visible);
|
2013-07-11 17:14:11 +02:00
|
|
|
|
|
|
|
return visible_messages;
|
|
|
|
};
|
|
|
|
|
2013-11-27 16:52:21 +01:00
|
|
|
exports.scrollTop = function viewport_scrollTop (target_scrollTop) {
|
2013-11-26 20:47:59 +01:00
|
|
|
var orig_scrollTop = jwindow.scrollTop();
|
2013-11-27 16:52:21 +01:00
|
|
|
if (target_scrollTop === undefined) {
|
2013-11-26 20:47:59 +01:00
|
|
|
return orig_scrollTop;
|
|
|
|
}
|
|
|
|
var ret = jwindow.scrollTop(target_scrollTop);
|
|
|
|
var new_scrollTop = jwindow.scrollTop();
|
|
|
|
var space_to_scroll = $("#bottom_whitespace").offset().top - viewport.height();
|
|
|
|
|
|
|
|
// Check whether our scrollTop didn't move even though one could have scrolled down
|
2013-11-27 16:17:55 +01:00
|
|
|
if (space_to_scroll > 0 && target_scrollTop > 0 &&
|
|
|
|
orig_scrollTop === 0 && new_scrollTop === 0) {
|
2013-11-26 20:47:59 +01:00
|
|
|
// Chrome has a bug where sometimes calling
|
|
|
|
// window.scrollTop(x) has no effect, resulting in the browser
|
|
|
|
// staying at 0 -- and afterwards if you call
|
|
|
|
// window.scrollTop(x) again, it will still do nothing. To
|
|
|
|
// fix this, we need to first scroll to some other place.
|
|
|
|
blueslip.info("ScrollTop did nothing when scrolling to " + target_scrollTop + ", fixing...");
|
|
|
|
// First scroll to 1 in order to clear the stuck state
|
|
|
|
jwindow.scrollTop(1);
|
|
|
|
// And then scroll where we intended to scroll to
|
|
|
|
ret = jwindow.scrollTop(target_scrollTop);
|
|
|
|
if (jwindow.scrollTop() === 0) {
|
2013-12-03 20:30:43 +01:00
|
|
|
blueslip.info("ScrollTop fix did not work when scrolling to " + target_scrollTop +
|
|
|
|
"! space_to_scroll was " + space_to_scroll);
|
2013-11-26 20:47:59 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-27 16:52:21 +01:00
|
|
|
return ret;
|
2013-05-07 17:15:20 +02:00
|
|
|
};
|
|
|
|
|
2013-06-19 18:12:49 +02:00
|
|
|
function make_dimen_wrapper(dimen_name, dimen_func) {
|
2013-08-06 21:53:41 +02:00
|
|
|
dimensions[dimen_name] = new util.CachedValue({
|
|
|
|
compute_value: function () {
|
|
|
|
return dimen_func.call(jwindow);
|
|
|
|
}
|
|
|
|
});
|
2013-06-19 18:12:49 +02:00
|
|
|
return function viewport_dimension_wrapper() {
|
|
|
|
if (arguments.length !== 0) {
|
2013-08-06 21:53:41 +02:00
|
|
|
dimensions[dimen_name].reset();
|
2013-06-19 18:12:49 +02:00
|
|
|
return dimen_func.apply(jwindow, arguments);
|
|
|
|
}
|
2013-08-06 21:53:41 +02:00
|
|
|
return dimensions[dimen_name].get();
|
2013-06-19 18:12:49 +02:00
|
|
|
};
|
|
|
|
}
|
2013-05-07 17:15:20 +02:00
|
|
|
|
2013-06-19 18:12:49 +02:00
|
|
|
exports.height = make_dimen_wrapper('height', $(window).height);
|
|
|
|
exports.width = make_dimen_wrapper('width', $(window).width);
|
2013-05-24 19:53:25 +02:00
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.stop_auto_scrolling = function () {
|
2013-05-24 19:53:25 +02:00
|
|
|
if (in_stoppable_autoscroll) {
|
|
|
|
$("html, body").stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-12 19:13:37 +01:00
|
|
|
exports.is_narrow = function () {
|
|
|
|
// This basically returns true when we hide the right sidebar for
|
|
|
|
// skinny mode. It would be nice to have a less brittle
|
|
|
|
// test for this. See the "@media (max-width: 975px)" section in
|
|
|
|
// zulip.css.
|
|
|
|
return window.innerWidth <= 975;
|
|
|
|
};
|
|
|
|
|
2013-05-24 19:53:25 +02:00
|
|
|
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 () {
|
2013-08-06 21:53:41 +02:00
|
|
|
dimensions.height.reset();
|
|
|
|
dimensions.width.reset();
|
2013-07-31 23:28:42 +02:00
|
|
|
top_of_feed.reset();
|
|
|
|
bottom_of_feed.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('compose_started compose_canceled compose_finished', function () {
|
|
|
|
bottom_of_feed.reset();
|
2013-05-07 17:15:20 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|