2012-10-09 23:48:41 +02:00
|
|
|
var message_array = [];
|
|
|
|
var message_dict = {};
|
2012-11-20 19:50:28 +01:00
|
|
|
var message_in_table = {zhome: {}, zfilt: {}};
|
2012-10-12 19:14:59 +02:00
|
|
|
var subject_dict = {};
|
2013-01-14 22:18:30 +01:00
|
|
|
var people_dict = {};
|
2012-09-22 01:11:54 +02:00
|
|
|
|
2012-10-16 04:07:52 +02:00
|
|
|
var viewport = $(window);
|
2012-10-15 19:24:34 +02:00
|
|
|
|
2013-02-08 19:24:39 +01:00
|
|
|
// For tracking where you are in the home view
|
|
|
|
var persistent_message_id = -1;
|
|
|
|
|
2012-10-17 23:45:03 +02:00
|
|
|
var selected_message_id = -1; /* to be filled in on document.ready */
|
2012-11-28 23:06:05 +01:00
|
|
|
var selected_message = $(); /* = rows.get(selected_message_id) */
|
2012-10-16 22:06:03 +02:00
|
|
|
var get_updates_params = {
|
2012-10-17 23:10:34 +02:00
|
|
|
pointer: -1,
|
2012-11-27 21:26:48 +01:00
|
|
|
server_generation: -1 /* to be filled in on document.ready */
|
2012-10-17 23:45:03 +02:00
|
|
|
};
|
2012-11-27 21:06:17 +01:00
|
|
|
var get_updates_failures = 0;
|
2012-10-17 23:45:03 +02:00
|
|
|
|
2012-11-27 23:17:30 +01:00
|
|
|
var load_more_enabled = true;
|
|
|
|
// If the browser hasn't scrolled away from the top of the page
|
|
|
|
// since the last time that we ran load_more_messages(), we do
|
|
|
|
// not load_more_messages().
|
|
|
|
var have_scrolled_away_from_top = true;
|
|
|
|
|
2012-10-01 22:41:53 +02:00
|
|
|
// The "message groups", i.e. blocks of messages collapsed by recipient.
|
|
|
|
// Each message table has a list of lists.
|
|
|
|
var message_groups = {
|
|
|
|
zhome: [],
|
|
|
|
zfilt: []
|
|
|
|
};
|
|
|
|
|
2012-11-30 03:49:18 +01:00
|
|
|
var disable_pointer_movement = false;
|
|
|
|
|
2013-01-14 22:18:30 +01:00
|
|
|
function add_person(person) {
|
|
|
|
people_list.push(person);
|
|
|
|
people_dict[person.email] = person;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
$.each(people_list, function (idx, person) {
|
|
|
|
people_dict[person.email] = person;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-10-16 16:45:33 +02:00
|
|
|
// Why do we look at the 'bottom' in above_view_threshold and the top
|
|
|
|
// in below_view_threshold as opposed to vice versa? Mostly to handle
|
|
|
|
// the case of gigantic messages. Imagine the case of a selected
|
|
|
|
// message that's so big that it takes up an two entire screens. The
|
|
|
|
// selector shouldn't move away from it until after the *bottom* of
|
|
|
|
// the message has gone too high up on the screen. (Otherwise we'd
|
|
|
|
// move the pointer right after part of the first screenful.)
|
|
|
|
function above_view_threshold(message, useTop) {
|
|
|
|
// Barnowl-style thresholds: the bottom of the pointer is never
|
|
|
|
// above the 1/5 mark.
|
|
|
|
// (if useTop = true, we look at the top of the pointer instead)
|
|
|
|
var position = message.offset().top;
|
|
|
|
if (!useTop) {
|
2012-11-01 19:03:48 +01:00
|
|
|
// outerHeight(true): Include margin
|
2012-10-16 16:45:33 +02:00
|
|
|
position += message.outerHeight(true);
|
|
|
|
}
|
|
|
|
return position < viewport.scrollTop() + viewport.height() / 5;
|
2012-10-05 16:31:10 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
function below_view_threshold(message) {
|
2012-10-16 16:45:33 +02:00
|
|
|
// Barnowl-style thresholds: the top of the pointer is never below
|
2012-10-16 17:30:07 +02:00
|
|
|
// the 2/3-mark.
|
|
|
|
return message.offset().top > viewport.scrollTop() + viewport.height() * 2 / 3;
|
2012-10-05 16:31:10 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
function recenter_view(message) {
|
2012-10-05 16:31:10 +02:00
|
|
|
// Barnowl-style recentering: if the pointer is too high, center
|
|
|
|
// in the middle of the screen. If the pointer is too low, center
|
|
|
|
// on the 1/5-mark.
|
|
|
|
|
|
|
|
// If this logic changes, above_view_threshold andd
|
|
|
|
// below_view_threshold must also change.
|
2012-10-16 16:45:33 +02:00
|
|
|
if (above_view_threshold(message, true)) {
|
|
|
|
// We specifically say useTop=true here, because suppose you're using
|
|
|
|
// the arrow keys to arrow up and you've moved up to a huge message.
|
|
|
|
// The message is so big that the bottom part of makes it not
|
|
|
|
// "above the view threshold". But since we're using the arrow keys
|
|
|
|
// to get here, the reason we selected this message is because
|
|
|
|
// we want to read it; so here we care about the top part.
|
2012-10-10 16:17:58 +02:00
|
|
|
viewport.scrollTop(selected_message.offset().top - viewport.height() / 2);
|
2012-10-10 16:35:48 +02:00
|
|
|
} else if (below_view_threshold(message)) {
|
2012-10-10 16:17:58 +02:00
|
|
|
viewport.scrollTop(selected_message.offset().top - viewport.height() / 5);
|
2012-10-05 16:31:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function scroll_to_selected() {
|
2012-10-23 22:50:22 +02:00
|
|
|
if (selected_message && (selected_message.length !== 0))
|
|
|
|
recenter_view(selected_message);
|
2012-09-07 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
function get_private_message_recipient(message, attr) {
|
2012-09-13 22:00:11 +02:00
|
|
|
var recipient, i;
|
2012-10-15 21:57:41 +02:00
|
|
|
var other_recipients = $.grep(message.display_recipient,
|
|
|
|
function (element, index) {
|
|
|
|
return element.email !== email;
|
|
|
|
});
|
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];
|
2012-10-15 21:57:41 +02:00
|
|
|
for (i = 1; i < other_recipients.length; i++) {
|
2012-10-27 02:31:46 +02:00
|
|
|
recipient += ', ' + other_recipients[i][attr];
|
2012-09-26 21:25:49 +02:00
|
|
|
}
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2012-10-10 16:18:51 +02:00
|
|
|
function respond_to_message(reply_type) {
|
2012-10-18 20:17:55 +02:00
|
|
|
var message, msg_type;
|
2012-10-10 16:35:48 +02:00
|
|
|
message = message_dict[selected_message_id];
|
2012-10-18 20:17:55 +02:00
|
|
|
|
|
|
|
var stream = '';
|
|
|
|
var subject = '';
|
2012-10-10 23:09:16 +02:00
|
|
|
if (message.type === "stream") {
|
2012-10-18 20:17:55 +02:00
|
|
|
stream = message.display_recipient;
|
|
|
|
subject = message.subject;
|
2012-10-09 20:18:55 +02:00
|
|
|
}
|
2012-10-18 20:17:55 +02:00
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
var pm_recipient = message.reply_to;
|
2012-12-03 19:49:12 +01:00
|
|
|
if (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
|
2012-11-08 00:38:21 +01:00
|
|
|
pm_recipient = message.sender_email;
|
2012-10-09 15:51:12 +02:00
|
|
|
}
|
2012-12-03 19:49:12 +01:00
|
|
|
if (reply_type === 'personal' || message.type === 'private') {
|
2012-11-08 00:38:21 +01:00
|
|
|
msg_type = 'private';
|
|
|
|
} else {
|
2012-10-18 20:17:55 +02:00
|
|
|
msg_type = message.type;
|
2012-09-12 16:23:55 +02:00
|
|
|
}
|
2012-10-19 21:59:03 +02:00
|
|
|
compose.start(msg_type, {'stream': stream, 'subject': subject,
|
2012-11-08 00:38:21 +01:00
|
|
|
'private_message_recipient': pm_recipient});
|
2012-09-12 16:23:55 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 23:29:30 +02:00
|
|
|
// Called by mouseover etc.
|
2012-10-22 19:05:06 +02:00
|
|
|
function select_message_by_id(message_id, opts) {
|
2012-10-23 22:48:20 +02:00
|
|
|
return select_message(rows.get(message_id), opts);
|
2012-09-13 16:58:29 +02:00
|
|
|
}
|
|
|
|
|
2012-10-29 22:02:10 +01:00
|
|
|
var furthest_read = -1;
|
|
|
|
var server_furthest_read = -1;
|
2012-10-30 20:47:30 +01:00
|
|
|
// We only send pointer updates when the user has been idle for a
|
|
|
|
// short while to avoid hammering the server
|
2012-10-22 18:28:40 +02:00
|
|
|
function send_pointer_update() {
|
2012-10-29 22:02:10 +01:00
|
|
|
if (furthest_read > server_furthest_read) {
|
|
|
|
$.post("/json/update_pointer",
|
|
|
|
{pointer: furthest_read},
|
|
|
|
function () {
|
|
|
|
server_furthest_read = furthest_read;
|
|
|
|
});
|
2012-10-22 18:28:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 04:48:21 +02:00
|
|
|
$(function () {
|
2013-02-08 19:24:39 +01:00
|
|
|
persistent_message_id = initial_pointer;
|
2012-10-29 22:02:10 +01:00
|
|
|
furthest_read = initial_pointer;
|
|
|
|
server_furthest_read = initial_pointer;
|
2012-10-30 20:47:30 +01:00
|
|
|
$(document).idle({idle: 1000,
|
|
|
|
onIdle: send_pointer_update,
|
|
|
|
keepTracking: true});
|
2012-10-27 04:48:21 +02:00
|
|
|
});
|
2012-10-22 18:28:40 +02:00
|
|
|
|
2012-10-22 19:10:09 +02:00
|
|
|
function update_selected_message(message, opts) {
|
2013-01-08 23:34:20 +01:00
|
|
|
var cls = 'selected_message';
|
2012-10-27 02:58:21 +02:00
|
|
|
$('.' + cls).removeClass(cls);
|
|
|
|
message.addClass(cls);
|
2012-09-24 19:50:09 +02:00
|
|
|
|
2012-10-18 20:55:41 +02:00
|
|
|
var new_selected_id = rows.id(message);
|
2013-02-08 19:24:39 +01:00
|
|
|
// Narrowing is a temporary view on top of the home view and
|
|
|
|
// doesn't affect your pointer in the home view.
|
|
|
|
// Similarly, lurk mode does not affect your pointer.
|
|
|
|
if (! narrow.active() && lurk_stream === undefined) {
|
|
|
|
persistent_message_id = new_selected_id;
|
|
|
|
if (new_selected_id > furthest_read)
|
|
|
|
{
|
|
|
|
furthest_read = new_selected_id;
|
|
|
|
}
|
2012-09-24 19:50:09 +02:00
|
|
|
}
|
2013-02-08 19:24:39 +01:00
|
|
|
|
2012-10-10 00:09:25 +02:00
|
|
|
selected_message_id = new_selected_id;
|
2012-10-10 16:35:48 +02:00
|
|
|
selected_message = message;
|
2012-09-24 19:50:09 +02:00
|
|
|
}
|
|
|
|
|
2012-10-22 19:05:06 +02:00
|
|
|
function select_message(next_message, opts) {
|
2012-11-27 21:15:29 +01:00
|
|
|
opts = $.extend({}, {then_scroll: false}, opts);
|
2012-09-06 19:32:02 +02:00
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
/* If the message exists but is hidden, try to find the next visible one. */
|
2012-10-23 22:17:57 +02:00
|
|
|
if (next_message.is(':hidden')) {
|
2012-10-18 20:55:41 +02:00
|
|
|
next_message = rows.next_visible(next_message);
|
2012-09-06 19:32:02 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 22:19:28 +02:00
|
|
|
/* Fall back to the last visible message. */
|
2012-10-09 23:45:41 +02:00
|
|
|
if (next_message.length === 0) {
|
2012-10-23 22:19:28 +02:00
|
|
|
next_message = rows.last_visible();
|
2012-10-23 22:17:57 +02:00
|
|
|
if (next_message.length === 0) {
|
|
|
|
// There are no messages!
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-17 16:42:45 +02:00
|
|
|
}
|
2012-09-06 19:32:02 +02:00
|
|
|
|
2012-10-26 17:44:22 +02:00
|
|
|
if (next_message.id !== selected_message_id) {
|
|
|
|
update_selected_message(next_message, opts);
|
|
|
|
}
|
2012-09-05 16:56:10 +02:00
|
|
|
|
2012-10-22 19:05:06 +02:00
|
|
|
if (opts.then_scroll) {
|
2012-10-09 23:45:41 +02:00
|
|
|
recenter_view(next_message);
|
2012-08-31 17:10:19 +02:00
|
|
|
}
|
2012-10-23 22:48:20 +02:00
|
|
|
|
|
|
|
return true;
|
2012-08-31 17:10:19 +02:00
|
|
|
}
|
|
|
|
|
2012-11-07 19:33:40 +01:00
|
|
|
function same_stream_and_subject(a, b) {
|
|
|
|
// Streams and subjects are case-insensitive. Streams have
|
|
|
|
// already been forced to the canonical case.
|
|
|
|
return ((a.recipient_id === b.recipient_id) &&
|
|
|
|
(a.subject.toLowerCase() === b.subject.toLowerCase()));
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:29:35 +02:00
|
|
|
function same_recipient(a, b) {
|
|
|
|
if ((a === undefined) || (b === undefined))
|
|
|
|
return false;
|
|
|
|
if (a.type !== b.type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (a.type) {
|
2012-12-03 19:49:12 +01:00
|
|
|
case 'private':
|
2012-09-24 20:29:35 +02:00
|
|
|
return a.reply_to === b.reply_to;
|
2012-10-10 22:57:21 +02:00
|
|
|
case 'stream':
|
2012-11-07 19:33:40 +01:00
|
|
|
return same_stream_and_subject(a, b);
|
2012-09-24 20:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// should never get here
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:26:50 +02:00
|
|
|
function same_sender(a, b) {
|
|
|
|
return ((a !== undefined) && (b !== undefined) &&
|
|
|
|
(a.sender_email === b.sender_email));
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:41:53 +02:00
|
|
|
function clear_table(table_name) {
|
2012-11-21 23:25:27 +01:00
|
|
|
// We do not want to call .empty() because that also clears
|
|
|
|
// jQuery data. This does mean, however, that we need to be
|
|
|
|
// mindful of memory leaks.
|
|
|
|
rows.get_table(table_name).children().detach();
|
2012-10-01 22:41:53 +02:00
|
|
|
message_groups[table_name] = [];
|
2012-11-20 19:50:28 +01:00
|
|
|
message_in_table[table_name] = {};
|
2012-10-01 22:41:53 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
function add_display_time(message, prev) {
|
2012-10-05 20:44:29 +02:00
|
|
|
var two_digits = function (x) { return ('0' + x).slice(-2); };
|
2012-10-10 16:35:48 +02:00
|
|
|
var time = new XDate(message.timestamp * 1000);
|
|
|
|
var include_date = message.include_recipient;
|
2012-10-05 20:44:29 +02:00
|
|
|
|
|
|
|
if (prev !== undefined) {
|
|
|
|
var prev_time = new XDate(prev.timestamp * 1000);
|
|
|
|
if (time.toDateString() !== prev_time.toDateString()) {
|
|
|
|
include_date = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 19:24:51 +01:00
|
|
|
// NB: timestr is HTML, inserted into the document without escaping.
|
2012-10-05 20:44:29 +02:00
|
|
|
if (include_date) {
|
2012-10-10 16:35:48 +02:00
|
|
|
message.timestr = time.toString("MMM dd") + " " +
|
2012-10-05 20:44:29 +02:00
|
|
|
time.toString("HH:mm");
|
|
|
|
} else {
|
2012-10-10 16:35:48 +02:00
|
|
|
message.timestr = time.toString("HH:mm");
|
2012-10-05 20:44:29 +02:00
|
|
|
}
|
2012-11-01 19:07:03 +01:00
|
|
|
// Rather than using time.toLocaleString(), which varies by
|
|
|
|
// browser, just do our own hardcoded formatting.
|
|
|
|
message.full_date_str = time.toDateString() + " " + time.toTimeString();
|
2012-10-05 20:44:29 +02:00
|
|
|
}
|
|
|
|
|
2012-11-18 22:53:50 +01:00
|
|
|
function add_to_table(messages, table_name, filter_function, where, allow_collapse) {
|
2012-10-10 16:29:48 +02:00
|
|
|
if (messages.length === 0)
|
2012-10-02 22:43:36 +02:00
|
|
|
return;
|
|
|
|
|
2012-10-27 03:03:06 +02:00
|
|
|
var table = rows.get_table(table_name);
|
2012-10-10 16:29:48 +02:00
|
|
|
var messages_to_render = [];
|
2012-11-16 19:38:29 +01:00
|
|
|
var ids_where_next_is_same_sender = {};
|
2012-09-28 19:48:04 +02:00
|
|
|
var prev;
|
2012-11-16 19:38:29 +01:00
|
|
|
var last_message_id;
|
2012-09-28 19:48:04 +02:00
|
|
|
|
2012-10-01 22:41:53 +02:00
|
|
|
var current_group = [];
|
|
|
|
var new_message_groups = [];
|
|
|
|
|
2012-11-21 04:32:39 +01:00
|
|
|
if (where === 'top' && narrow.allow_collapse() && message_groups[table_name].length > 0) {
|
2012-10-03 00:17:24 +02:00
|
|
|
// Delete the current top message group, and add it back in with these
|
|
|
|
// messages, in order to collapse properly.
|
|
|
|
//
|
|
|
|
// This means we redraw the entire view on each update when narrowed by
|
2012-10-11 00:01:39 +02:00
|
|
|
// subject, which could be a problem down the line. For now we hope
|
|
|
|
// that subject views will not be very big.
|
2012-10-03 00:17:24 +02:00
|
|
|
|
|
|
|
var top_group = message_groups[table_name][0];
|
|
|
|
var top_messages = [];
|
|
|
|
$.each(top_group, function (index, id) {
|
2012-10-18 20:55:41 +02:00
|
|
|
rows.get(id, table_name).remove();
|
2012-10-09 23:48:41 +02:00
|
|
|
top_messages.push(message_dict[id]);
|
2012-10-03 00:17:24 +02:00
|
|
|
});
|
2012-10-10 16:29:48 +02:00
|
|
|
messages = messages.concat(top_messages);
|
2012-10-03 00:17:24 +02:00
|
|
|
|
|
|
|
// Delete the leftover recipient label.
|
|
|
|
table.find('.recipient_row:first').remove();
|
|
|
|
} else {
|
2012-11-27 23:31:48 +01:00
|
|
|
last_message_id = rows.id(table.find('tr[zid]:last'));
|
2012-11-16 19:38:29 +01:00
|
|
|
prev = message_dict[last_message_id];
|
2012-10-03 00:17:24 +02:00
|
|
|
}
|
2012-09-20 20:33:57 +02:00
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
$.each(messages, function (index, message) {
|
|
|
|
if (! filter_function(message))
|
2012-09-24 21:21:23 +02:00
|
|
|
return;
|
|
|
|
|
2012-11-20 19:50:28 +01:00
|
|
|
message_in_table[table_name][message.id] = true;
|
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
message.include_recipient = false;
|
|
|
|
message.include_bookend = false;
|
2012-11-18 22:53:50 +01:00
|
|
|
if (same_recipient(prev, message) && allow_collapse) {
|
2012-10-10 16:35:48 +02:00
|
|
|
current_group.push(message.id);
|
2012-10-01 22:41:53 +02:00
|
|
|
} else {
|
|
|
|
if (current_group.length > 0)
|
|
|
|
new_message_groups.push(current_group);
|
2012-10-10 16:35:48 +02:00
|
|
|
current_group = [message.id];
|
2012-10-01 22:41:53 +02:00
|
|
|
|
2012-09-24 22:36:09 +02:00
|
|
|
// Add a space to the table, but not for the first element.
|
2012-10-10 16:35:48 +02:00
|
|
|
message.include_recipient = true;
|
|
|
|
message.include_bookend = (prev !== undefined);
|
2012-09-19 19:15:12 +02:00
|
|
|
}
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
message.include_sender = true;
|
|
|
|
if (!message.include_recipient &&
|
|
|
|
same_sender(prev, message) &&
|
|
|
|
(Math.abs(message.timestamp - prev.timestamp) < 60*10)) {
|
|
|
|
message.include_sender = false;
|
2012-11-16 19:38:29 +01:00
|
|
|
ids_where_next_is_same_sender[prev.id] = true;
|
2012-09-24 21:21:23 +02:00
|
|
|
}
|
2012-09-19 19:15:12 +02:00
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
add_display_time(message, prev);
|
2012-10-05 20:44:29 +02:00
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
message.dom_id = table_name + message.id;
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-11-06 23:06:47 +01:00
|
|
|
if (message.sender_email === email) {
|
2012-11-16 16:45:39 +01:00
|
|
|
message.stamp = ui.get_gravatar_stamp();
|
2012-11-06 23:06:47 +01:00
|
|
|
}
|
|
|
|
|
2013-01-07 23:33:36 +01:00
|
|
|
if (message.is_stream) {
|
|
|
|
message.background_color = subs.get_color(message.display_recipient);
|
2013-02-05 20:51:11 +01:00
|
|
|
message.invite_only = subs.get_invite_only(message.display_recipient);
|
2013-01-07 23:33:36 +01:00
|
|
|
}
|
2012-12-01 04:37:52 +01:00
|
|
|
|
2012-10-10 16:35:48 +02:00
|
|
|
messages_to_render.push(message);
|
|
|
|
prev = message;
|
2012-09-24 22:36:09 +02:00
|
|
|
});
|
|
|
|
|
2012-11-20 20:06:36 +01:00
|
|
|
if (messages_to_render.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:41:53 +02:00
|
|
|
if (current_group.length > 0)
|
|
|
|
new_message_groups.push(current_group);
|
|
|
|
|
|
|
|
if (where === 'top') {
|
|
|
|
message_groups[table_name] = new_message_groups.concat(message_groups[table_name]);
|
|
|
|
} else {
|
|
|
|
message_groups[table_name] = message_groups[table_name].concat(new_message_groups);
|
|
|
|
}
|
|
|
|
|
2012-11-16 19:38:29 +01:00
|
|
|
var rendered_elems = $(templates.message({
|
2012-10-10 16:29:48 +02:00
|
|
|
messages: messages_to_render,
|
2012-10-03 23:22:51 +02:00
|
|
|
include_layout_row: (table.find('tr:first').length === 0)
|
2012-11-16 19:38:29 +01:00
|
|
|
}));
|
2012-09-20 20:33:57 +02:00
|
|
|
|
2012-11-16 19:38:29 +01:00
|
|
|
$.each(rendered_elems, function (index, elem) {
|
|
|
|
var row = $(elem);
|
|
|
|
if (! row.hasClass('message_row')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var id = row.attr('zid');
|
|
|
|
if (ids_where_next_is_same_sender[id]) {
|
|
|
|
row.find('.messagebox').addClass("next_is_same_sender");
|
|
|
|
}
|
2012-09-24 22:36:09 +02:00
|
|
|
});
|
|
|
|
|
2012-11-16 19:38:29 +01:00
|
|
|
// The message that was last before this batch came in has to be
|
|
|
|
// handled specially because we didn't just render it and
|
|
|
|
// therefore have to lookup its associated element
|
|
|
|
if (last_message_id !== undefined
|
|
|
|
&& ids_where_next_is_same_sender[last_message_id])
|
|
|
|
{
|
|
|
|
var row = rows.get(last_message_id, table_name);
|
|
|
|
row.find('.messagebox').addClass("next_is_same_sender");
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:32:39 +01:00
|
|
|
if (where === 'top' && table.find('.ztable_layout_row').length > 0) {
|
|
|
|
// If we have a totally empty narrow, there may not
|
|
|
|
// be a .ztable_layout_row.
|
2012-11-16 19:38:29 +01:00
|
|
|
table.find('.ztable_layout_row').after(rendered_elems);
|
|
|
|
} else {
|
|
|
|
table.append(rendered_elems);
|
2012-11-21 18:15:07 +01:00
|
|
|
|
|
|
|
// XXX: This is absolutely awful. There is a firefox bug
|
|
|
|
// where when table rows as DOM elements are appended (as
|
|
|
|
// opposed to as a string) a border is sometimes added to the
|
|
|
|
// row. This border goes away if we add a dummy row to the
|
|
|
|
// top of the table (it doesn't go away on any reflow,
|
|
|
|
// though, as resizing the window doesn't make them go away).
|
|
|
|
// So, we add an empty row and then garbage collect them
|
|
|
|
// later when the user is idle.
|
|
|
|
var dummy = $("<tr></tr>");
|
|
|
|
table.find('.ztable_layout_row').after(dummy);
|
|
|
|
$(document).idle({'idle': 1000*10,
|
|
|
|
'onIdle': function () {
|
|
|
|
dummy.remove();
|
|
|
|
}});
|
2012-11-16 19:38:29 +01:00
|
|
|
}
|
2012-09-13 22:00:11 +02:00
|
|
|
}
|
|
|
|
|
2012-11-07 20:31:45 +01:00
|
|
|
function case_insensitive_find(term, array) {
|
|
|
|
var lowered_term = term.toLowerCase();
|
|
|
|
return $.grep(array, function (elt) {
|
|
|
|
return elt.toLowerCase() === lowered_term;
|
|
|
|
}).length !== 0;
|
|
|
|
}
|
|
|
|
|
2012-11-20 19:50:28 +01:00
|
|
|
function add_message_metadata(message, dummy) {
|
|
|
|
if (message_dict[message.id]) {
|
|
|
|
return message_dict[message.id];
|
|
|
|
}
|
2012-12-14 18:12:54 +01:00
|
|
|
get_updates_params.last = Math.max(get_updates_params.last || 0, message.id);
|
2012-10-25 23:32:20 +02:00
|
|
|
|
2012-10-12 17:26:04 +02:00
|
|
|
var involved_people;
|
|
|
|
|
2012-10-10 16:34:42 +02:00
|
|
|
switch (message.type) {
|
2012-10-10 22:57:21 +02:00
|
|
|
case 'stream':
|
2012-10-10 23:31:26 +02:00
|
|
|
message.is_stream = true;
|
2012-10-12 19:14:59 +02:00
|
|
|
if (! subject_dict.hasOwnProperty(message.display_recipient)) {
|
|
|
|
subject_dict[message.display_recipient] = [];
|
|
|
|
}
|
2012-11-07 20:31:45 +01:00
|
|
|
if (! case_insensitive_find(message.subject, subject_dict[message.display_recipient])) {
|
2012-11-01 17:03:45 +01:00
|
|
|
subject_dict[message.display_recipient].push(message.subject);
|
2012-10-12 19:14:59 +02:00
|
|
|
subject_dict[message.display_recipient].sort();
|
|
|
|
// We don't need to update the autocomplete after this because
|
|
|
|
// the subject box's source is a function
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
2012-10-10 16:34:42 +02:00
|
|
|
message.reply_to = message.sender_email;
|
2012-10-12 17:26:04 +02:00
|
|
|
|
2012-10-13 00:32:59 +02:00
|
|
|
involved_people = [{'full_name': message.sender_full_name,
|
2012-10-12 17:26:04 +02:00
|
|
|
'email': message.sender_email}];
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
case 'private':
|
|
|
|
message.is_private = true;
|
|
|
|
message.reply_to = get_private_message_recipient(message, 'email');
|
|
|
|
message.display_reply_to = get_private_message_recipient(message, 'full_name');
|
2012-10-12 17:26:04 +02:00
|
|
|
|
|
|
|
involved_people = message.display_recipient;
|
2012-11-19 18:36:53 +01:00
|
|
|
|
|
|
|
if (message.sender_email === email) {
|
2012-11-26 19:24:34 +01:00
|
|
|
typeahead_helper.update_your_recipients(involved_people);
|
2012-11-19 18:36:53 +01:00
|
|
|
} else {
|
2012-11-26 19:24:34 +01:00
|
|
|
typeahead_helper.update_all_recipients(involved_people);
|
2012-11-19 18:36:53 +01:00
|
|
|
}
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
2012-10-12 17:26:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-13 00:17:48 +02:00
|
|
|
// Add new people involved in this message to the people list
|
|
|
|
$.each(involved_people, function (idx, person) {
|
|
|
|
// Do the hasOwnProperty() call via the prototype to avoid problems
|
|
|
|
// with keys like "hasOwnProperty"
|
2012-11-26 19:24:34 +01:00
|
|
|
if (! typeahead_helper.known_to_typeahead(person)) {
|
2013-01-14 22:18:30 +01:00
|
|
|
add_person(person);
|
2012-11-19 01:09:40 +01:00
|
|
|
typeahead_helper.autocomplete_needs_update(true);
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
2012-10-13 00:17:48 +02:00
|
|
|
});
|
2012-08-30 20:24:29 +02:00
|
|
|
|
2012-10-10 16:34:42 +02:00
|
|
|
message_dict[message.id] = message;
|
2012-11-20 19:50:28 +01:00
|
|
|
return message;
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-20 17:35:30 +01:00
|
|
|
function add_messages(messages, add_to_home) {
|
2012-11-20 19:50:28 +01:00
|
|
|
var prepended = false;
|
2012-10-25 00:42:45 +02:00
|
|
|
if (!messages)
|
2012-09-28 19:27:52 +02:00
|
|
|
return;
|
|
|
|
|
2013-01-16 19:50:18 +01:00
|
|
|
util.destroy_loading_indicator($('#page_loading_indicator'));
|
2012-11-20 19:50:28 +01:00
|
|
|
messages = $.map(messages, add_message_metadata);
|
2012-11-20 17:35:30 +01:00
|
|
|
|
2012-11-20 16:51:08 +01:00
|
|
|
if (add_to_home) {
|
2013-02-08 19:24:39 +01:00
|
|
|
// persistent_message_id is guaranteed to be between the top and bottom
|
2012-11-20 19:50:28 +01:00
|
|
|
var top_messages_home = $.grep(messages, function (elem, idx) {
|
2013-02-08 19:24:39 +01:00
|
|
|
return (elem.id < persistent_message_id && ! message_in_table.zhome[elem.id]);
|
2012-11-20 19:50:28 +01:00
|
|
|
});
|
|
|
|
var bottom_messages_home = $.grep(messages, function (elem, idx) {
|
2013-02-08 19:24:39 +01:00
|
|
|
return (elem.id >= persistent_message_id && ! message_in_table.zhome[elem.id]);
|
2012-11-20 19:50:28 +01:00
|
|
|
});
|
|
|
|
message_array = top_messages_home.concat(message_array).concat(bottom_messages_home);
|
2013-01-30 21:10:55 +01:00
|
|
|
add_to_table(top_messages_home, 'zhome', narrow.in_home, "top", true);
|
|
|
|
add_to_table(bottom_messages_home, 'zhome', narrow.in_home, "bottom", true);
|
2012-11-20 19:50:28 +01:00
|
|
|
if ((top_messages_home.length > 0) && !narrow.active()) {
|
|
|
|
prepended = true;
|
|
|
|
}
|
2012-10-01 21:02:39 +02:00
|
|
|
}
|
|
|
|
|
2012-11-20 17:35:30 +01:00
|
|
|
if (narrow.active()) {
|
2013-02-08 19:24:39 +01:00
|
|
|
// selected_message_id is guaranteed to be between the top and bottom
|
2012-11-20 19:50:28 +01:00
|
|
|
var top_messages_narrow = $.grep(messages, function (elem, idx) {
|
|
|
|
return (elem.id < selected_message_id && ! message_in_table.zfilt[elem.id]);
|
|
|
|
});
|
|
|
|
var bottom_messages_narrow = $.grep(messages, function (elem, idx) {
|
2013-01-14 04:14:46 +01:00
|
|
|
return (elem.id >= selected_message_id && ! message_in_table.zfilt[elem.id]);
|
2012-11-20 19:50:28 +01:00
|
|
|
});
|
|
|
|
add_to_table(top_messages_narrow, 'zfilt', narrow.predicate(), "top", narrow.allow_collapse());
|
|
|
|
add_to_table(bottom_messages_narrow, 'zfilt', narrow.predicate(), "bottom", narrow.allow_collapse());
|
|
|
|
if (top_messages_narrow.length > 0) {
|
|
|
|
prepended = true;
|
|
|
|
}
|
2012-11-20 17:35:30 +01:00
|
|
|
}
|
2012-09-25 00:42:04 +02:00
|
|
|
|
2012-10-03 00:23:03 +02:00
|
|
|
// If we received the initially selected message, select it on the client side,
|
|
|
|
// but not if the user has already selected another one during load.
|
2012-10-10 00:09:25 +02:00
|
|
|
if ((selected_message_id === -1) && (message_dict.hasOwnProperty(initial_pointer))) {
|
2012-11-27 21:15:29 +01:00
|
|
|
select_message_by_id(initial_pointer, {then_scroll: true});
|
2012-10-03 00:23:03 +02:00
|
|
|
}
|
2012-09-26 20:28:22 +02:00
|
|
|
|
2012-11-06 00:31:51 +01:00
|
|
|
if ((selected_message_id === -1) && ! have_initial_messages) {
|
2012-11-27 21:15:29 +01:00
|
|
|
select_message_by_id(message_array[0].id, {then_scroll: false});
|
2012-11-06 00:31:51 +01:00
|
|
|
}
|
|
|
|
|
2012-09-29 01:38:03 +02:00
|
|
|
// If we prepended messages, then we need to scroll back to the pointer.
|
|
|
|
// This will mess with the user's scrollwheel use; possibly we should be
|
|
|
|
// more clever here. However (for now) we only prepend on page load,
|
|
|
|
// so maybe it's okay.
|
2012-10-03 00:39:21 +02:00
|
|
|
//
|
|
|
|
// We also need to re-select the message by ID, because we might have
|
|
|
|
// removed and re-added the row as part of prepend collapsing.
|
2012-11-20 19:50:28 +01:00
|
|
|
if (prepended && (selected_message_id >= 0)) {
|
2012-11-27 21:15:29 +01:00
|
|
|
select_message_by_id(selected_message_id, {then_scroll: true});
|
2012-10-03 00:39:21 +02:00
|
|
|
}
|
2012-09-29 01:38:03 +02:00
|
|
|
|
2012-11-19 01:09:40 +01:00
|
|
|
if (typeahead_helper.autocomplete_needs_update()) {
|
|
|
|
typeahead_helper.update_autocomplete();
|
2012-11-01 18:01:33 +01:00
|
|
|
}
|
2013-01-30 21:49:56 +01:00
|
|
|
|
|
|
|
// If the new messages are off the screen, show a notification
|
|
|
|
notifications_bar.update();
|
2012-09-24 21:21:23 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 20:21:22 +02:00
|
|
|
var get_updates_xhr;
|
|
|
|
var get_updates_timeout;
|
2012-11-20 23:15:50 +01:00
|
|
|
function get_updates(options) {
|
|
|
|
var defaults = {dont_block: false};
|
|
|
|
options = $.extend({}, defaults, options);
|
|
|
|
|
2012-10-29 22:02:10 +01:00
|
|
|
get_updates_params.pointer = furthest_read;
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_params.dont_block = options.dont_block || get_updates_failures > 0;
|
2013-01-16 00:52:57 +01:00
|
|
|
if (lurk_stream !== undefined)
|
|
|
|
get_updates_params.stream_name = lurk_stream;
|
2012-11-27 21:26:48 +01:00
|
|
|
if (reload.is_pending()) {
|
|
|
|
// We only send a server_generation to the server if we're
|
|
|
|
// interested in an immediate reply to tell us if we need to
|
|
|
|
// reload. Once we're already reloading, we need to not
|
|
|
|
// submit the parameter, so that the server will process our
|
|
|
|
// future requests in longpolling mode.
|
|
|
|
delete get_updates_params.server_generation;
|
|
|
|
}
|
2012-10-17 23:10:34 +02:00
|
|
|
|
2012-10-10 20:21:22 +02:00
|
|
|
get_updates_xhr = $.ajax({
|
2012-08-31 21:33:04 +02:00
|
|
|
type: 'POST',
|
2012-10-16 21:42:40 +02:00
|
|
|
url: '/json/get_updates',
|
2012-10-16 22:06:03 +02:00
|
|
|
data: get_updates_params,
|
2012-08-31 21:33:04 +02:00
|
|
|
dataType: 'json',
|
2012-12-11 18:08:18 +01:00
|
|
|
timeout: poll_timeout,
|
2012-08-31 21:33:04 +02:00
|
|
|
success: function (data) {
|
2012-10-23 17:00:00 +02:00
|
|
|
if (! data) {
|
|
|
|
// The server occationally returns no data during a
|
|
|
|
// restart. Ignore those responses so the page keeps
|
|
|
|
// working
|
|
|
|
get_updates_timeout = setTimeout(get_updates, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
|
|
|
|
2012-10-17 00:24:29 +02:00
|
|
|
if (get_updates_params.server_generation === -1) {
|
|
|
|
get_updates_params.server_generation = data.server_generation;
|
|
|
|
} else if (data.server_generation !== get_updates_params.server_generation) {
|
2012-10-29 21:02:46 +01:00
|
|
|
reload.initiate();
|
Make the client reload the page when it detects a server restart
If the client is not composing a message, we can just force a page
reload. However, if he is composing a message, we must preserve that
message while still reloading as soon as possible.
We take the following approach: if the client has not completed the
composition after 5 minutes, do a compose-preserving reload
(described below). If he sends the message before the timeout
expires, reload the page after a successful send. If the send fails
(not due to server timeout), however, we do a compose-perserving
reload in case the error was due to the data format changing. If the
send failed due to server timeout, we don't reload because the reload
will probably also fail.
In a compose-preserving reload, we redirect to an URI that has a
fragment indicating we are doing a reload and containing all the
necessary information for restoring the compose window to its
previous state. On page load, we check the fragment to see if we
just did a compose-preserving reload, and, if we did, we restore the
compose window (or just try the send again in the case of send
failure). The URI fragment looks like:
(imported from commit af4eeb3930c24118e088057d4da456748fbd2229)
2012-10-16 21:16:06 +02:00
|
|
|
}
|
|
|
|
|
2012-10-25 21:48:35 +02:00
|
|
|
if (data.messages.length !== 0) {
|
2012-11-20 17:35:30 +01:00
|
|
|
add_messages(data.messages, true);
|
2012-11-23 23:53:38 +01:00
|
|
|
notifications.received_messages(data.messages);
|
2012-10-17 23:10:34 +02:00
|
|
|
}
|
|
|
|
|
2012-11-20 22:13:06 +01:00
|
|
|
if (data.zephyr_mirror_active === false) {
|
|
|
|
$('#zephyr-mirror-error').show();
|
|
|
|
} else {
|
|
|
|
$('#zephyr-mirror-error').hide();
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:02:10 +01:00
|
|
|
if (data.new_pointer !== undefined
|
2012-10-29 22:02:10 +01:00
|
|
|
&& data.new_pointer > furthest_read)
|
2012-10-29 22:02:10 +01:00
|
|
|
{
|
2012-10-29 22:02:10 +01:00
|
|
|
furthest_read = data.new_pointer;
|
|
|
|
server_furthest_read = data.new_pointer;
|
2012-11-27 21:15:29 +01:00
|
|
|
select_message_by_id(data.new_pointer, {then_scroll: true});
|
2012-10-29 22:02:10 +01:00
|
|
|
}
|
2012-10-17 23:10:34 +02:00
|
|
|
|
2012-12-11 18:17:56 +01:00
|
|
|
get_updates_timeout = setTimeout(get_updates, 0);
|
2012-08-31 21:33:04 +02:00
|
|
|
},
|
2012-09-05 22:17:14 +02:00
|
|
|
error: function (xhr, error_type, exn) {
|
2013-01-23 17:33:07 +01:00
|
|
|
// If we are old enough to have messages outside of the Tornado
|
|
|
|
// cache, immediately reload.
|
|
|
|
if ((xhr.status === 400) &&
|
|
|
|
($.parseJSON(xhr.responseText).msg.indexOf("too old") !== -1)) {
|
|
|
|
reload.initiate({immediate: true});
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:35:15 +02:00
|
|
|
if (error_type === 'timeout') {
|
2012-09-05 22:17:14 +02:00
|
|
|
// Retry indefinitely on timeout.
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
2012-09-05 22:17:14 +02:00
|
|
|
} else {
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures += 1;
|
2012-09-05 22:17:14 +02:00
|
|
|
}
|
|
|
|
|
2012-11-27 21:06:17 +01:00
|
|
|
if (get_updates_failures >= 5) {
|
2012-08-31 21:33:04 +02:00
|
|
|
$('#connection-error').show();
|
|
|
|
} else {
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
2012-08-31 21:33:04 +02:00
|
|
|
}
|
2012-09-05 22:33:04 +02:00
|
|
|
|
2012-11-27 21:06:17 +01:00
|
|
|
var retry_sec = Math.min(90, Math.exp(get_updates_failures/2));
|
2012-10-10 20:21:22 +02:00
|
|
|
get_updates_timeout = setTimeout(get_updates, retry_sec*1000);
|
2012-08-31 21:33:04 +02:00
|
|
|
}
|
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
2012-09-04 20:31:23 +02:00
|
|
|
|
2012-11-28 23:48:09 +01:00
|
|
|
function load_old_messages(anchor, num_before, num_after, cont, for_narrow,
|
2012-11-27 22:30:47 +01:00
|
|
|
cont_will_add_messages) {
|
2012-11-28 23:48:09 +01:00
|
|
|
if (for_narrow === undefined) {
|
|
|
|
for_narrow = false;
|
2012-11-09 01:06:07 +01:00
|
|
|
}
|
2012-11-27 22:30:47 +01:00
|
|
|
if (cont_will_add_messages === undefined) {
|
|
|
|
cont_will_add_messages = false;
|
|
|
|
}
|
2012-11-09 01:06:07 +01:00
|
|
|
|
2012-12-19 23:58:02 +01:00
|
|
|
var data = {anchor: anchor, num_before: num_before, num_after: num_after};
|
|
|
|
|
2013-01-16 00:52:57 +01:00
|
|
|
if (lurk_stream !== undefined)
|
|
|
|
data.stream = lurk_stream;
|
|
|
|
|
2012-12-19 23:58:02 +01:00
|
|
|
if (for_narrow && narrow.active())
|
2013-02-05 19:25:30 +01:00
|
|
|
data.narrow = JSON.stringify(narrow.public_operators());
|
2012-12-19 23:58:02 +01:00
|
|
|
|
2013-01-07 23:35:00 +01:00
|
|
|
function process_result(messages) {
|
|
|
|
$('#connection-error').hide();
|
|
|
|
|
|
|
|
if (messages.length !== 0 && !cont_will_add_messages) {
|
|
|
|
add_messages(messages, !for_narrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cont !== undefined) {
|
|
|
|
cont(messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:42:45 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/get_old_messages',
|
2012-12-19 23:58:02 +01:00
|
|
|
data: data,
|
2012-10-25 00:42:45 +02:00
|
|
|
dataType: 'json',
|
|
|
|
success: function (data) {
|
|
|
|
if (! data) {
|
|
|
|
// The server occationally returns no data during a
|
|
|
|
// restart. Ignore those responses and try again
|
|
|
|
setTimeout(function () {
|
2012-11-29 22:07:19 +01:00
|
|
|
load_old_messages(anchor, num_before, num_after, cont, for_narrow,
|
|
|
|
cont_will_add_messages);
|
2012-10-25 00:42:45 +02:00
|
|
|
}, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-07 23:35:00 +01:00
|
|
|
process_result(data.messages);
|
2012-10-25 00:42:45 +02:00
|
|
|
},
|
|
|
|
error: function (xhr, error_type, exn) {
|
2013-01-07 23:35:00 +01:00
|
|
|
if (xhr.status === 400) {
|
|
|
|
// Bad request: We probably specified a narrow operator
|
|
|
|
// for a nonexistent stream or something. We shouldn't
|
|
|
|
// retry or display a connection error.
|
|
|
|
//
|
|
|
|
// FIXME: Warn the user when this has happened?
|
|
|
|
process_result([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:42:45 +02:00
|
|
|
// We might want to be more clever here
|
|
|
|
$('#connection-error').show();
|
|
|
|
setTimeout(function () {
|
2012-11-29 22:07:19 +01:00
|
|
|
load_old_messages(anchor, num_before, num_after, cont, for_narrow,
|
|
|
|
cont_will_add_messages);
|
2012-10-25 00:42:45 +02:00
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the initial message list
|
|
|
|
$(function () {
|
2012-10-26 19:30:42 +02:00
|
|
|
function load_more(messages) {
|
2012-10-25 00:42:45 +02:00
|
|
|
// catch the user up
|
2012-11-01 21:31:31 +01:00
|
|
|
if (messages.length !== 1) {
|
2012-10-25 00:42:45 +02:00
|
|
|
var latest_id = messages[messages.length-1].id;
|
2012-11-01 21:31:31 +01:00
|
|
|
load_old_messages(latest_id, 0, 400, load_more);
|
2012-10-26 00:06:40 +02:00
|
|
|
return;
|
2012-10-25 00:42:45 +02:00
|
|
|
}
|
2012-10-26 00:06:40 +02:00
|
|
|
// now start subscribing to updates
|
|
|
|
get_updates();
|
2012-10-25 00:42:45 +02:00
|
|
|
|
2012-10-26 19:30:42 +02:00
|
|
|
// backfill more messages after the user is idle
|
2012-11-06 19:16:45 +01:00
|
|
|
var backfill_batch_size = 1000;
|
2012-10-26 19:30:42 +02:00
|
|
|
$(document).idle({'idle': 1000*10,
|
|
|
|
'onIdle': function () {
|
|
|
|
var first_id = message_array[0].id;
|
2012-11-01 21:31:31 +01:00
|
|
|
load_old_messages(first_id, backfill_batch_size, 0);
|
2012-10-26 19:30:42 +02:00
|
|
|
}});
|
2012-10-25 00:42:45 +02:00
|
|
|
}
|
|
|
|
|
2012-11-05 23:56:27 +01:00
|
|
|
if (have_initial_messages) {
|
2012-11-27 23:17:30 +01:00
|
|
|
load_old_messages(initial_pointer, 200, 200, load_more);
|
2012-11-05 23:56:27 +01:00
|
|
|
} else {
|
|
|
|
get_updates();
|
|
|
|
}
|
2012-10-25 00:42:45 +02:00
|
|
|
});
|
|
|
|
|
2012-11-20 23:15:50 +01:00
|
|
|
function restart_get_updates(options) {
|
2012-10-19 21:51:36 +02:00
|
|
|
if (get_updates_xhr !== undefined)
|
|
|
|
get_updates_xhr.abort();
|
|
|
|
|
|
|
|
if (get_updates_timeout !== undefined)
|
|
|
|
clearTimeout(get_updates_timeout);
|
|
|
|
|
2012-11-20 23:15:50 +01:00
|
|
|
get_updates(options);
|
2012-10-19 21:51:36 +02:00
|
|
|
}
|
|
|
|
|
2012-11-27 23:17:30 +01:00
|
|
|
function reset_load_more_status() {
|
|
|
|
load_more_enabled = true;
|
|
|
|
have_scrolled_away_from_top = true;
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:28:57 +02:00
|
|
|
function load_more_messages() {
|
2012-11-06 19:17:38 +01:00
|
|
|
var batch_size = 400;
|
2012-12-03 19:46:51 +01:00
|
|
|
var table, oldest_message_id;
|
2012-11-27 23:17:30 +01:00
|
|
|
if (!load_more_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-16 19:50:18 +01:00
|
|
|
ui.show_loading_more_messages_indicator();
|
2012-11-27 23:17:30 +01:00
|
|
|
load_more_enabled = false;
|
2012-12-03 19:46:51 +01:00
|
|
|
table = narrow.active() ? "zfilt" : "zhome";
|
|
|
|
oldest_message_id = rows.id(rows.get_table(table).find("tr[zid]:first"));
|
2012-12-04 20:04:36 +01:00
|
|
|
if (isNaN(oldest_message_id)) {
|
2012-12-21 00:46:55 +01:00
|
|
|
if (selected_message_id === -1) {
|
|
|
|
// If we arrived on the page via a #narrow URL, selected_message_id
|
|
|
|
// will still be -1, so use the initial_pointer as our anchor
|
|
|
|
oldest_message_id = initial_pointer;
|
|
|
|
} else {
|
|
|
|
oldest_message_id = selected_message_id;
|
|
|
|
}
|
2012-12-04 20:04:36 +01:00
|
|
|
}
|
2012-12-03 19:46:51 +01:00
|
|
|
load_old_messages(oldest_message_id, batch_size, 0,
|
2012-10-25 21:45:06 +02:00
|
|
|
function (messages) {
|
2013-01-16 19:50:18 +01:00
|
|
|
ui.hide_loading_more_messages_indicator();
|
2012-11-27 23:17:30 +01:00
|
|
|
if (messages.length === batch_size + 1) {
|
|
|
|
load_more_enabled = true;
|
2012-10-25 21:45:06 +02:00
|
|
|
}
|
2012-11-28 23:48:09 +01:00
|
|
|
}, narrow.active());
|
2012-10-19 22:28:57 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 20:21:22 +02:00
|
|
|
var watchdog_time = $.now();
|
2012-11-20 21:37:31 +01:00
|
|
|
setInterval(function () {
|
2012-10-10 20:21:22 +02:00
|
|
|
var new_time = $.now();
|
|
|
|
if ((new_time - watchdog_time) > 20000) { // 20 seconds.
|
|
|
|
// Our app's JS wasn't running (the machine was probably
|
|
|
|
// asleep). Now that we're running again, immediately poll for
|
|
|
|
// new updates.
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures = 0;
|
2012-11-27 21:13:32 +01:00
|
|
|
restart_get_updates({dont_block: true});
|
2012-10-10 20:21:22 +02:00
|
|
|
}
|
|
|
|
watchdog_time = new_time;
|
|
|
|
}, 5000);
|
|
|
|
|
2012-10-05 19:28:10 +02:00
|
|
|
function at_top_of_viewport() {
|
2012-10-16 04:07:52 +02:00
|
|
|
return (viewport.scrollTop() === 0);
|
2012-10-05 19:28:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function at_bottom_of_viewport() {
|
2012-11-01 19:03:48 +01:00
|
|
|
// outerHeight(true): Include margin
|
2012-10-05 19:28:10 +02:00
|
|
|
return (viewport.scrollTop() + viewport.height() >= $("#main_div").outerHeight(true));
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:44:41 +02:00
|
|
|
function keep_pointer_in_view() {
|
2012-10-05 19:28:10 +02:00
|
|
|
var candidate;
|
2012-11-25 21:21:55 +01:00
|
|
|
var next_message = selected_message;
|
2012-11-30 03:49:18 +01:00
|
|
|
|
|
|
|
if (disable_pointer_movement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:50:22 +02:00
|
|
|
if (next_message.length === 0)
|
|
|
|
return;
|
2012-09-26 20:44:41 +02:00
|
|
|
|
2012-11-24 08:03:17 +01:00
|
|
|
function adjust(past_threshold, at_end, advance) {
|
|
|
|
if (!past_threshold(next_message) || at_end())
|
|
|
|
return false; // try other side
|
|
|
|
while (past_threshold(next_message)) {
|
|
|
|
candidate = advance(next_message);
|
|
|
|
if (candidate.length === 0)
|
2012-10-05 19:28:10 +02:00
|
|
|
break;
|
2012-11-24 08:03:17 +01:00
|
|
|
next_message = candidate;
|
2012-09-26 20:44:41 +02:00
|
|
|
}
|
2012-11-24 08:03:17 +01:00
|
|
|
return true;
|
2012-09-26 20:44:41 +02:00
|
|
|
}
|
2012-11-24 08:03:17 +01:00
|
|
|
|
|
|
|
if (! adjust(above_view_threshold, at_top_of_viewport, rows.next_visible))
|
|
|
|
adjust(below_view_threshold, at_bottom_of_viewport, rows.prev_visible);
|
|
|
|
|
2012-11-27 21:15:29 +01:00
|
|
|
update_selected_message(next_message);
|
2012-10-10 20:20:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The idea here is when you've scrolled to the very
|
|
|
|
// bottom of the page, e.g., the scroll handler isn't
|
|
|
|
// going to fire anymore. But if I continue to use
|
|
|
|
// the scrollwheel, the selection should advance until
|
|
|
|
// I'm at the very top or the very bottom of the page.
|
2012-10-16 18:16:26 +02:00
|
|
|
function move_pointer_at_page_top_and_bottom(delta) {
|
|
|
|
if (delta !== 0 && (at_top_of_viewport() || at_bottom_of_viewport())) {
|
2012-11-25 21:21:55 +01:00
|
|
|
var next_message = selected_message;
|
2012-10-16 18:16:26 +02:00
|
|
|
if (delta > 0) {
|
|
|
|
// Scrolling up (want older messages)
|
2012-10-18 20:55:41 +02:00
|
|
|
next_message = rows.prev_visible(next_message);
|
2012-10-16 18:16:26 +02:00
|
|
|
} else {
|
|
|
|
// We're scrolling down (we want more recent messages)
|
2012-10-18 20:55:41 +02:00
|
|
|
next_message = rows.next_visible(next_message);
|
2012-10-16 18:16:26 +02:00
|
|
|
}
|
|
|
|
if (next_message.length !== 0) {
|
|
|
|
update_selected_message(next_message);
|
|
|
|
}
|
2012-09-26 20:44:41 +02:00
|
|
|
}
|
|
|
|
}
|