2013-09-22 21:11:43 +02:00
|
|
|
var all_msg_list = new MessageList(
|
|
|
|
undefined, undefined,
|
|
|
|
{muting_enabled: false}
|
|
|
|
);
|
2013-07-25 22:08:16 +02:00
|
|
|
var home_msg_list = new MessageList('zhome',
|
2014-02-03 22:48:25 +01:00
|
|
|
new Filter([{operator: "in", operand: "home"}]), {muting_enabled: true}
|
2013-07-25 22:08:16 +02:00
|
|
|
);
|
2013-02-12 20:01:24 +01:00
|
|
|
var narrowed_msg_list;
|
2013-02-22 20:48:31 +01:00
|
|
|
var current_msg_list = home_msg_list;
|
2013-09-06 00:19:15 +02:00
|
|
|
|
2013-08-19 21:17:10 +02:00
|
|
|
var recent_subjects = new Dict({fold_case: true});
|
2012-09-22 01:11:54 +02:00
|
|
|
|
2013-03-04 23:44:07 +01:00
|
|
|
var queued_mark_as_read = [];
|
|
|
|
var queued_flag_timer;
|
|
|
|
|
2012-11-27 23:17:30 +01:00
|
|
|
|
2013-02-12 22:32:14 +01:00
|
|
|
// Toggles re-centering the pointer in the window
|
|
|
|
// when Home is next clicked by the user
|
|
|
|
var recenter_pointer_on_display = false;
|
|
|
|
var suppress_scroll_pointer_update = false;
|
2013-03-28 19:16:48 +01:00
|
|
|
// Includes both scroll and arrow events. Negative means scroll up,
|
|
|
|
// positive means scroll down.
|
|
|
|
var last_viewport_movement_direction = 1;
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-02-20 20:49:49 +01:00
|
|
|
var furthest_read = -1;
|
|
|
|
var server_furthest_read = -1;
|
2013-03-13 21:53:36 +01:00
|
|
|
var pointer_update_in_flight = false;
|
2013-02-20 20:49:49 +01:00
|
|
|
|
2013-06-05 17:56:43 +02:00
|
|
|
function keep_pointer_in_view() {
|
|
|
|
// See recenter_view() for related logic to keep the pointer onscreen.
|
|
|
|
// This function mostly comes into place for mouse scrollers, and it
|
|
|
|
// keeps the pointer in view. For people who purely scroll with the
|
|
|
|
// mouse, the pointer is kind of meaningless to them, but keyboard
|
|
|
|
// users will occasionally do big mouse scrolls, so this gives them
|
|
|
|
// a pointer reasonably close to the middle of the screen.
|
|
|
|
var candidate;
|
|
|
|
var next_row = current_msg_list.selected_row();
|
|
|
|
|
2013-08-01 17:47:48 +02:00
|
|
|
if (next_row.length === 0) {
|
2013-06-05 17:56:43 +02:00
|
|
|
return;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-05 17:56:43 +02:00
|
|
|
|
2013-06-06 16:10:12 +02:00
|
|
|
var info = viewport.message_viewport_info();
|
2013-06-05 17:56:43 +02:00
|
|
|
var top_threshold = info.visible_top + (1/10 * info.visible_height);
|
|
|
|
var bottom_threshold = info.visible_top + (9/10 * info.visible_height);
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
function message_is_far_enough_down() {
|
|
|
|
if (viewport.at_top()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var message_top = next_row.offset().top;
|
|
|
|
|
2013-08-05 21:42:59 +02:00
|
|
|
// If the message starts after the very top of the screen, we just
|
|
|
|
// leave it alone. This avoids bugs like #1608, where overzealousness
|
|
|
|
// about repositioning the pointer can cause users to miss messages.
|
|
|
|
if (message_top >= info.visible_top) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
|
2013-08-05 21:42:59 +02:00
|
|
|
// If at least part of the message is below top_threshold (10% from
|
|
|
|
// the top), then we also leave it alone.
|
2013-08-05 21:32:57 +02:00
|
|
|
var bottom_offset = message_top + next_row.outerHeight(true);
|
|
|
|
if (bottom_offset >= top_threshold) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, the message is not "in view."
|
|
|
|
return false;
|
2013-06-05 17:56:43 +02:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
function message_is_far_enough_up() {
|
|
|
|
return viewport.at_bottom() ||
|
|
|
|
(next_row.offset().top <= bottom_threshold);
|
2013-06-05 17:56:43 +02:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
function adjust(in_view, get_next_row) {
|
|
|
|
// return true only if we make an actual adjustment, so
|
|
|
|
// that we know to short circuit the other direction
|
|
|
|
if (in_view(next_row)) {
|
2013-06-05 17:56:43 +02:00
|
|
|
return false; // try other side
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-08-05 21:32:57 +02:00
|
|
|
while (!in_view(next_row)) {
|
|
|
|
candidate = get_next_row(next_row);
|
2013-08-01 17:47:48 +02:00
|
|
|
if (candidate.length === 0) {
|
2013-06-05 17:56:43 +02:00
|
|
|
break;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-05 17:56:43 +02:00
|
|
|
next_row = candidate;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
if (!adjust(message_is_far_enough_down, rows.next_visible)) {
|
|
|
|
adjust(message_is_far_enough_up, rows.prev_visible);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-05 17:56:43 +02:00
|
|
|
|
|
|
|
current_msg_list.select_id(rows.id(next_row), {from_scroll: true});
|
|
|
|
}
|
|
|
|
|
2013-07-03 19:32:16 +02:00
|
|
|
function recenter_view(message, opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = opts || {};
|
|
|
|
|
2013-06-03 17:04:01 +02:00
|
|
|
// Barnowl-style recentering: if the pointer is too high, move it to
|
2013-06-04 22:08:52 +02:00
|
|
|
// the 1/2 marks. If the pointer is too low, move it to the 1/7 mark.
|
2013-06-03 17:04:01 +02:00
|
|
|
// See keep_pointer_in_view() for related logic to keep the pointer onscreen.
|
2012-10-05 16:31:10 +02:00
|
|
|
|
2013-06-06 16:10:12 +02:00
|
|
|
var viewport_info = viewport.message_viewport_info();
|
2013-06-04 22:08:52 +02:00
|
|
|
var top_threshold = viewport_info.visible_top;
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2013-06-04 22:08:52 +02:00
|
|
|
var bottom_threshold = viewport_info.visible_top + viewport_info.visible_height;
|
2013-03-28 19:16:48 +01:00
|
|
|
|
2013-05-30 20:39:28 +02:00
|
|
|
var message_top = message.offset().top;
|
2013-06-05 17:28:10 +02:00
|
|
|
var message_height = message.outerHeight(true);
|
|
|
|
var message_bottom = message_top + message_height;
|
|
|
|
|
2013-05-30 20:39:28 +02:00
|
|
|
var is_above = message_top < top_threshold;
|
2013-06-04 22:08:52 +02:00
|
|
|
var is_below = message_bottom > bottom_threshold;
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2013-07-03 19:32:16 +02:00
|
|
|
if (opts.from_scroll) {
|
2013-03-28 19:16:48 +01:00
|
|
|
// If the message you're trying to center on is already in view AND
|
|
|
|
// you're already trying to move in the direction of that message,
|
|
|
|
// don't try to recenter. This avoids disorienting jumps when the
|
|
|
|
// pointer has gotten itself outside the threshold (e.g. by
|
|
|
|
// autoscrolling).
|
2013-05-30 20:39:28 +02:00
|
|
|
if (is_above && last_viewport_movement_direction >= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (is_below && last_viewport_movement_direction <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 19:16:48 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 19:32:16 +02:00
|
|
|
if (is_above || opts.force_center) {
|
2013-06-05 17:28:10 +02:00
|
|
|
viewport.set_message_position(message_top, message_height, viewport_info, 1/2);
|
2013-05-30 20:39:28 +02:00
|
|
|
} else if (is_below) {
|
2013-06-05 17:28:10 +02:00
|
|
|
viewport.set_message_position(message_top, message_height, viewport_info, 1/7);
|
2012-10-05 16:31:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function scroll_to_selected() {
|
2013-02-14 23:48:37 +01:00
|
|
|
var selected_row = current_msg_list.selected_row();
|
2013-08-01 17:47:48 +02:00
|
|
|
if (selected_row && (selected_row.length !== 0)) {
|
2013-02-14 23:48:37 +01:00
|
|
|
recenter_view(selected_row);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-09-07 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:32:14 +01:00
|
|
|
function maybe_scroll_to_selected() {
|
|
|
|
// If we have been previously instructed to re-center to the
|
|
|
|
// selected message, then do so
|
|
|
|
if (recenter_pointer_on_display) {
|
|
|
|
scroll_to_selected();
|
|
|
|
recenter_pointer_on_display = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 20:46:42 +01:00
|
|
|
function get_private_message_recipient(message, attr, fallback_attr) {
|
2012-09-13 22:00:11 +02:00
|
|
|
var recipient, i;
|
2013-07-29 22:49:42 +02:00
|
|
|
var other_recipients = _.filter(message.display_recipient,
|
|
|
|
function (element) {
|
2013-03-25 23:26:14 +01:00
|
|
|
return element.email !== page_params.email;
|
2012-10-15 21:57:41 +02:00
|
|
|
});
|
2012-10-17 23:07:46 +02:00
|
|
|
if (other_recipients.length === 0) {
|
2012-12-03 19:49:12 +01:00
|
|
|
// private message with oneself
|
2012-10-27 02:31:46 +02:00
|
|
|
return message.display_recipient[0][attr];
|
2012-10-17 23:07:46 +02:00
|
|
|
}
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-10-27 02:31:46 +02:00
|
|
|
recipient = other_recipients[0][attr];
|
2014-01-13 20:46:42 +01:00
|
|
|
if (recipient === undefined && fallback_attr !== undefined) {
|
|
|
|
recipient = other_recipients[0][fallback_attr];
|
|
|
|
}
|
2012-10-15 21:57:41 +02:00
|
|
|
for (i = 1; i < other_recipients.length; i++) {
|
2014-01-13 20:46:42 +01:00
|
|
|
var attr_value = other_recipients[i][attr];
|
|
|
|
if (attr_value === undefined && fallback_attr !== undefined) {
|
|
|
|
attr_value = other_recipients[i][fallback_attr];
|
|
|
|
}
|
|
|
|
recipient += ', ' + attr_value;
|
2012-09-26 21:25:49 +02:00
|
|
|
}
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2013-07-08 19:53:03 +02:00
|
|
|
function respond_to_message(opts) {
|
|
|
|
var message, msg_type;
|
|
|
|
// Before initiating a reply to a message, if there's an
|
|
|
|
// in-progress composition, snapshot it.
|
|
|
|
compose.snapshot_message();
|
|
|
|
|
|
|
|
message = current_msg_list.selected_message();
|
|
|
|
|
|
|
|
if (message === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-31 17:44:21 +01:00
|
|
|
unread.mark_message_as_read(message);
|
2013-07-08 19:53:03 +02:00
|
|
|
|
|
|
|
var stream = '';
|
|
|
|
var subject = '';
|
|
|
|
if (message.type === "stream") {
|
|
|
|
stream = message.stream;
|
|
|
|
subject = message.subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pm_recipient = message.reply_to;
|
|
|
|
if (opts.reply_type === "personal" && message.type === "private") {
|
|
|
|
// reply_to for private messages is everyone involved, so for
|
|
|
|
// personals replies we need to set the the private message
|
|
|
|
// recipient to just the sender
|
|
|
|
pm_recipient = message.sender_email;
|
|
|
|
}
|
|
|
|
if (opts.reply_type === 'personal' || message.type === 'private') {
|
|
|
|
msg_type = 'private';
|
|
|
|
} else {
|
|
|
|
msg_type = message.type;
|
|
|
|
}
|
|
|
|
compose.start(msg_type, {'stream': stream, 'subject': subject,
|
|
|
|
'private_message_recipient': pm_recipient,
|
|
|
|
'replying_to_message': message,
|
|
|
|
'trigger': opts.trigger});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-19 20:05:44 +02:00
|
|
|
function update_pointer() {
|
|
|
|
if (!pointer_update_in_flight) {
|
2013-03-13 21:53:36 +01:00
|
|
|
pointer_update_in_flight = true;
|
2016-04-02 20:24:19 +02:00
|
|
|
return channel.put({
|
|
|
|
url: '/json/users/me/pointer',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-03-13 21:53:36 +01:00
|
|
|
data: {pointer: furthest_read},
|
|
|
|
success: function () {
|
|
|
|
server_furthest_read = furthest_read;
|
|
|
|
pointer_update_in_flight = false;
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
pointer_update_in_flight = false;
|
|
|
|
}
|
|
|
|
});
|
2013-06-19 20:05:44 +02:00
|
|
|
} else {
|
|
|
|
// Return an empty, resolved Deferred.
|
|
|
|
return $.when();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function send_pointer_update() {
|
|
|
|
// Only bother if you've read new messages.
|
|
|
|
if (furthest_read > server_furthest_read) {
|
|
|
|
update_pointer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unconditionally_send_pointer_update() {
|
|
|
|
if (pointer_update_in_flight) {
|
|
|
|
// Keep trying.
|
2013-07-26 23:28:21 +02:00
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
deferred.resolve(unconditionally_send_pointer_update());
|
|
|
|
}, 100);
|
|
|
|
return deferred;
|
2013-06-19 20:05:44 +02:00
|
|
|
} else {
|
|
|
|
return update_pointer();
|
2013-02-20 04:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 17:36:48 +02:00
|
|
|
function fast_forward_pointer() {
|
2016-04-02 20:07:28 +02:00
|
|
|
channel.get({
|
|
|
|
url: '/users/me',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-03-25 23:26:14 +01:00
|
|
|
data: {email: page_params.email},
|
2013-02-20 04:19:18 +01:00
|
|
|
success: function (data) {
|
2014-01-31 17:44:21 +01:00
|
|
|
unread.mark_all_as_read(function () {
|
2013-03-15 20:07:38 +01:00
|
|
|
furthest_read = data.max_message_id;
|
2013-06-19 20:05:44 +02:00
|
|
|
unconditionally_send_pointer_update().then(function () {
|
|
|
|
ui.change_tab_to('#home');
|
2015-11-29 03:00:00 +01:00
|
|
|
reload.initiate({immediate: true,
|
|
|
|
save_pointer: false,
|
|
|
|
save_narrow: false,
|
2015-11-30 17:49:37 +01:00
|
|
|
save_compose: true});
|
2013-06-19 20:05:44 +02:00
|
|
|
});
|
2013-03-15 20:07:38 +01:00
|
|
|
});
|
2013-02-20 04:19:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-08-02 23:38:26 +02:00
|
|
|
|
2013-08-08 20:24:25 +02:00
|
|
|
function consider_bankruptcy() {
|
2013-08-19 19:02:52 +02:00
|
|
|
// Until we've handled possibly declaring bankruptcy, don't show
|
|
|
|
// unread counts since they only consider messages that are loaded
|
|
|
|
// client side and may be different from the numbers reported by
|
|
|
|
// the server.
|
|
|
|
|
2013-08-08 20:24:25 +02:00
|
|
|
if (!page_params.furthest_read_time) {
|
|
|
|
// We've never read a message.
|
2014-01-31 17:44:21 +01:00
|
|
|
unread.enable();
|
2013-08-08 20:24:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var now = new XDate(true).getTime() / 1000;
|
|
|
|
if ((page_params.unread_count > 500) &&
|
|
|
|
(now - page_params.furthest_read_time > 60 * 60 * 24 * 2)) { // 2 days.
|
|
|
|
var unread_info = templates.render('bankruptcy_modal',
|
|
|
|
{"unread_count": page_params.unread_count});
|
|
|
|
$('#bankruptcy-unread-count').html(unread_info);
|
|
|
|
$('#bankruptcy').modal('show');
|
2013-08-19 19:02:52 +02:00
|
|
|
} else {
|
2014-01-31 17:44:21 +01:00
|
|
|
unread.enable();
|
2013-08-08 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 23:13:22 +01:00
|
|
|
// This is annoying to move to unread.js because the natural name
|
|
|
|
// would be unread.process_loaded_messages, which this calls
|
|
|
|
function process_loaded_for_unread(messages) {
|
|
|
|
activity.process_loaded_messages(messages);
|
|
|
|
activity.update_huddles();
|
|
|
|
unread.process_loaded_messages(messages);
|
|
|
|
unread.update_unread_counts();
|
2014-03-13 19:03:31 +01:00
|
|
|
resize.resize_page_components();
|
2014-01-31 23:13:22 +01:00
|
|
|
}
|
|
|
|
|
2013-09-06 00:18:29 +02:00
|
|
|
function main() {
|
2013-08-02 23:38:26 +02:00
|
|
|
activity.set_user_statuses(page_params.initial_presences,
|
|
|
|
page_params.initial_servertime);
|
|
|
|
|
|
|
|
server_furthest_read = page_params.initial_pointer;
|
2013-08-13 22:52:56 +02:00
|
|
|
if (page_params.orig_initial_pointer !== undefined &&
|
|
|
|
page_params.orig_initial_pointer > server_furthest_read) {
|
|
|
|
server_furthest_read = page_params.orig_initial_pointer;
|
|
|
|
}
|
|
|
|
furthest_read = server_furthest_read;
|
2013-08-02 23:38:26 +02:00
|
|
|
|
2013-08-08 20:24:25 +02:00
|
|
|
// Before trying to load messages: is this user way behind?
|
|
|
|
consider_bankruptcy();
|
|
|
|
|
2013-08-02 23:38:26 +02:00
|
|
|
// We only send pointer updates when the user has been idle for a
|
|
|
|
// short while to avoid hammering the server
|
|
|
|
$(document).idle({idle: 1000,
|
|
|
|
onIdle: send_pointer_update,
|
|
|
|
keepTracking: true});
|
|
|
|
|
|
|
|
$(document).on('message_selected.zulip', function (event) {
|
2013-12-13 20:20:28 +01:00
|
|
|
// Only advance the pointer when not narrowed
|
2013-12-18 19:06:55 +01:00
|
|
|
if (event.id === -1) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
// Additionally, don't advance the pointer server-side
|
|
|
|
// if the selected message is local-only
|
2013-12-13 20:20:28 +01:00
|
|
|
if (event.msg_list === home_msg_list && page_params.narrow_stream === undefined) {
|
2013-12-19 17:03:08 +01:00
|
|
|
if (event.id > furthest_read &&
|
|
|
|
home_msg_list.get(event.id).local_id === undefined) {
|
2013-11-26 19:06:21 +01:00
|
|
|
furthest_read = event.id;
|
|
|
|
}
|
2013-08-02 23:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.mark_read && event.previously_selected !== -1) {
|
|
|
|
// Mark messages between old pointer and new pointer as read
|
|
|
|
var messages;
|
|
|
|
if (event.id < event.previously_selected) {
|
2014-01-31 22:06:07 +01:00
|
|
|
messages = event.msg_list.message_range(event.id, event.previously_selected);
|
2013-08-02 23:38:26 +02:00
|
|
|
} else {
|
2014-01-31 22:06:07 +01:00
|
|
|
messages = event.msg_list.message_range(event.previously_selected, event.id);
|
2013-08-02 23:38:26 +02:00
|
|
|
}
|
2014-01-31 17:44:21 +01:00
|
|
|
unread.mark_messages_as_read(messages, {from: 'pointer'});
|
2013-08-02 23:38:26 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
main();
|
|
|
|
});
|