2016-04-20 22:37:26 +02:00
|
|
|
var home_msg_list = new message_list.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;
|
|
|
|
|
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-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.
|
2016-04-16 14:22:03 +02:00
|
|
|
// See keep_pointer_in_view() in pointer.js 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
|
2016-04-12 17:38:47 +02:00
|
|
|
if (pointer.recenter_pointer_on_display) {
|
2013-02-12 22:32:14 +01:00
|
|
|
scroll_to_selected();
|
2016-04-12 17:38:47 +02:00
|
|
|
pointer.recenter_pointer_on_display = false;
|
2013-02-12 22:32:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2013-07-26 23:28:21 +02: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);
|
|
|
|
|
2016-04-12 17:38:47 +02:00
|
|
|
pointer.server_furthest_read = page_params.initial_pointer;
|
2013-08-13 22:52:56 +02:00
|
|
|
if (page_params.orig_initial_pointer !== undefined &&
|
2016-04-12 17:38:47 +02:00
|
|
|
page_params.orig_initial_pointer > pointer.server_furthest_read) {
|
|
|
|
pointer.server_furthest_read = page_params.orig_initial_pointer;
|
2013-08-13 22:52:56 +02:00
|
|
|
}
|
2016-04-12 17:38:47 +02:00
|
|
|
pointer.furthest_read = pointer.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,
|
2016-04-12 17:38:47 +02:00
|
|
|
onIdle: pointer.send_pointer_update,
|
2013-08-02 23:38:26 +02:00
|
|
|
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) {
|
2016-04-12 17:38:47 +02:00
|
|
|
if (event.id > pointer.furthest_read &&
|
2013-12-19 17:03:08 +01:00
|
|
|
home_msg_list.get(event.id).local_id === undefined) {
|
2016-04-12 17:38:47 +02:00
|
|
|
pointer.furthest_read = event.id;
|
2013-11-26 19:06:21 +01:00
|
|
|
}
|
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();
|
|
|
|
});
|