2012-12-05 23:54:49 +01:00
|
|
|
/*jslint nomen: true */
|
2013-04-25 21:13:06 +02:00
|
|
|
function MessageList(table_name, filter, opts) {
|
2013-02-28 23:00:03 +01:00
|
|
|
$.extend(this, {collapse_messages: true}, opts);
|
2012-12-05 23:54:49 +01:00
|
|
|
this._items = [];
|
|
|
|
this._hash = {};
|
|
|
|
this.table_name = table_name;
|
2013-04-25 21:13:06 +02:00
|
|
|
this.filter = filter;
|
2013-02-20 18:26:50 +01:00
|
|
|
this._selected_id = -1;
|
2013-02-26 23:30:13 +01:00
|
|
|
this._message_groups = [];
|
2013-03-07 21:16:20 +01:00
|
|
|
// Half-open interval of the indices that define the current render window
|
|
|
|
this._render_win_start = 0;
|
|
|
|
this._render_win_end = 0;
|
2013-02-27 20:47:04 +01:00
|
|
|
|
|
|
|
if (this.table_name) {
|
|
|
|
this._clear_table();
|
|
|
|
}
|
2013-04-25 21:13:06 +02:00
|
|
|
if (this.filter === undefined) {
|
|
|
|
this.filter = new narrow.Filter();
|
|
|
|
}
|
2013-04-10 17:42:14 +02:00
|
|
|
this.narrowed = false;
|
|
|
|
if (this.table_name === "zfilt") {
|
|
|
|
this.narrowed = true;
|
|
|
|
}
|
2012-12-05 23:54:49 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-26 23:30:13 +01:00
|
|
|
(function () {
|
|
|
|
|
|
|
|
function add_display_time(message, prev) {
|
|
|
|
var time = new XDate(message.timestamp * 1000);
|
2013-06-24 23:16:50 +02:00
|
|
|
var include_date = false;
|
2013-02-26 23:30:13 +01:00
|
|
|
|
|
|
|
if (prev !== undefined) {
|
|
|
|
var prev_time = new XDate(prev.timestamp * 1000);
|
|
|
|
if (time.toDateString() !== prev_time.toDateString()) {
|
|
|
|
include_date = true;
|
|
|
|
}
|
2013-06-24 23:16:50 +02:00
|
|
|
} else {
|
|
|
|
include_date = true;
|
2013-02-26 23:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (include_date) {
|
2013-06-24 23:16:50 +02:00
|
|
|
// NB: show_date is HTML, inserted into the document without escaping.
|
2013-07-01 20:51:39 +02:00
|
|
|
message.show_date = (timerender.render_date(time))[0].outerHTML;
|
2013-06-28 19:32:55 +02:00
|
|
|
} else {
|
|
|
|
// This is run on re-render, and must remove the date if a message
|
|
|
|
// from the same day is added above this one when scrolling up.
|
|
|
|
message.show_date = undefined;
|
2013-02-26 23:30:13 +01:00
|
|
|
}
|
2013-06-24 23:16:50 +02:00
|
|
|
|
2013-06-28 19:32:55 +02:00
|
|
|
if (message.timestr === undefined){
|
|
|
|
message.timestr = time.toString("HH:mm");
|
|
|
|
}
|
2013-02-26 23:30:13 +01:00
|
|
|
}
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
MessageList.prototype = {
|
|
|
|
get: function MessageList_get(id) {
|
|
|
|
id = parseInt(id, 10);
|
|
|
|
if (isNaN(id)) {
|
2013-02-22 16:26:13 +01:00
|
|
|
return undefined;
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
|
|
|
return this._hash[id];
|
|
|
|
},
|
|
|
|
|
|
|
|
empty: function MessageList_empty() {
|
|
|
|
return this._items.length === 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
first: function MessageList_first() {
|
|
|
|
return this._items[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
last: function MessageList_last() {
|
|
|
|
return this._items[this._items.length - 1];
|
|
|
|
},
|
|
|
|
|
2013-02-27 20:47:04 +01:00
|
|
|
_clear_table: function MessageList__clear_table() {
|
|
|
|
// 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(this.table_name).children().detach();
|
|
|
|
},
|
|
|
|
|
2013-07-09 00:33:38 +02:00
|
|
|
_clear_rendering_state: function MessageList__clear_rendering_state(clear_table) {
|
2013-04-10 18:30:36 +02:00
|
|
|
this._message_groups = [];
|
2013-07-09 00:33:38 +02:00
|
|
|
if (clear_table) {
|
|
|
|
this._clear_table();
|
|
|
|
}
|
2013-04-10 23:38:30 +02:00
|
|
|
this.last_message_historical = false;
|
2013-07-03 22:31:35 +02:00
|
|
|
|
|
|
|
this._render_win_start = 0;
|
|
|
|
this._render_win_end = 0;
|
2013-04-10 18:30:36 +02:00
|
|
|
},
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
clear: function MessageList_clear(opts) {
|
|
|
|
opts = $.extend({}, {clear_selected_id: true}, opts);
|
|
|
|
|
|
|
|
this._items = [];
|
|
|
|
this._hash = {};
|
2013-07-09 00:33:38 +02:00
|
|
|
this._clear_rendering_state(true);
|
2013-02-22 20:48:31 +01:00
|
|
|
|
|
|
|
if (opts.clear_selected_id) {
|
|
|
|
this._selected_id = -1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-02-20 18:26:50 +01:00
|
|
|
selected_id: function MessageList_selected_id() {
|
|
|
|
return this._selected_id;
|
|
|
|
},
|
|
|
|
|
2013-02-20 18:33:04 +01:00
|
|
|
select_id: function MessageList_select_id(id, opts) {
|
2013-07-03 22:14:32 +02:00
|
|
|
opts = $.extend({
|
|
|
|
then_scroll: false,
|
|
|
|
use_closest: false,
|
|
|
|
mark_read: true
|
|
|
|
}, opts, {
|
|
|
|
id: id,
|
|
|
|
msg_list: this,
|
|
|
|
previously_selected: this._selected_id
|
|
|
|
});
|
2013-07-03 19:15:07 +02:00
|
|
|
|
2013-02-20 18:33:04 +01:00
|
|
|
id = parseInt(id, 10);
|
|
|
|
if (isNaN(id)) {
|
2013-03-11 17:32:28 +01:00
|
|
|
blueslip.fatal("Bad message id");
|
2013-02-20 18:33:04 +01:00
|
|
|
}
|
|
|
|
if (this.get(id) === undefined) {
|
2013-02-22 16:26:44 +01:00
|
|
|
if (!opts.use_closest) {
|
2013-06-04 21:58:33 +02:00
|
|
|
blueslip.error("Selected message id not in MessageList",
|
|
|
|
{table_name: this.table_name, id: id});
|
2013-02-22 16:26:44 +01:00
|
|
|
}
|
2013-03-11 17:32:28 +01:00
|
|
|
id = this.closest_id(id);
|
|
|
|
opts.id = id;
|
2013-02-20 18:33:04 +01:00
|
|
|
}
|
2013-03-13 18:48:02 +01:00
|
|
|
|
2013-02-20 18:33:04 +01:00
|
|
|
this._selected_id = id;
|
2013-07-03 19:53:27 +02:00
|
|
|
if (!opts.from_rendering) {
|
|
|
|
this._maybe_rerender();
|
|
|
|
}
|
2013-03-04 20:22:09 +01:00
|
|
|
|
2013-02-20 20:49:49 +01:00
|
|
|
$(document).trigger($.Event('message_selected.zephyr', opts));
|
2013-02-20 18:33:04 +01:00
|
|
|
},
|
|
|
|
|
2013-02-14 23:48:37 +01:00
|
|
|
selected_message: function MessageList_selected_message() {
|
2013-02-20 18:26:50 +01:00
|
|
|
return this.get(this._selected_id);
|
2013-02-14 23:48:37 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
selected_row: function MessageList_selected_row() {
|
2013-02-20 18:26:50 +01:00
|
|
|
return rows.get(this._selected_id, this.table_name);
|
2013-02-14 23:48:37 +01:00
|
|
|
},
|
|
|
|
|
2013-02-20 00:49:21 +01:00
|
|
|
closest_id: function MessageList_closest_id(id) {
|
|
|
|
if (this._items.length === 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
var closest = util.lower_bound(this._items, id,
|
|
|
|
function (a, b) {
|
|
|
|
return a.id < b;
|
|
|
|
});
|
|
|
|
if (closest === this._items.length
|
|
|
|
|| (closest !== 0
|
|
|
|
&& (id - this._items[closest - 1].id <
|
|
|
|
this._items[closest].id - id)))
|
|
|
|
{
|
|
|
|
closest = closest - 1;
|
|
|
|
}
|
|
|
|
return this._items[closest].id;
|
|
|
|
},
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
_add_to_hash: function MessageList__add_to_hash(messages) {
|
|
|
|
var self = this;
|
|
|
|
messages.forEach(function (elem) {
|
|
|
|
var id = parseInt(elem.id, 10);
|
|
|
|
if (isNaN(id)) {
|
2013-03-11 17:32:28 +01:00
|
|
|
blueslip.fatal("Bad message id");
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
|
|
|
if (self._hash[id] !== undefined) {
|
2013-03-11 17:32:28 +01:00
|
|
|
blueslip.error("Duplicate message added to MessageList");
|
|
|
|
return;
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
|
|
|
self._hash[id] = elem;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-02-28 22:38:08 +01:00
|
|
|
// Number of messages to render at a time
|
|
|
|
_RENDER_WINDOW_SIZE: 400,
|
|
|
|
// Number of messages away from edge of render window at which we
|
|
|
|
// trigger a re-render
|
|
|
|
_RENDER_THRESHOLD: 50,
|
|
|
|
|
2013-04-10 18:30:36 +02:00
|
|
|
_update_render_window: function MessageList__update_render_window(selected_idx, check_for_changed) {
|
|
|
|
var new_start = Math.max(selected_idx - this._RENDER_WINDOW_SIZE / 2, 0);
|
|
|
|
if (check_for_changed && new_start === this._render_win_start) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._render_win_start = new_start;
|
|
|
|
this._render_win_end = Math.min(this._render_win_start + this._RENDER_WINDOW_SIZE,
|
|
|
|
this._items.length);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_selected_idx: function MessageList__selected_idx() {
|
|
|
|
return util.lower_bound(this._items, this._selected_id,
|
|
|
|
function (a, b) { return a.id < b; });
|
|
|
|
},
|
|
|
|
|
2013-02-28 22:38:08 +01:00
|
|
|
_maybe_rerender: function MessageList__maybe_rerender() {
|
|
|
|
if (this.table_name === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:30:36 +02:00
|
|
|
var selected_idx = this._selected_idx();
|
2013-02-28 22:38:08 +01:00
|
|
|
|
|
|
|
// We rerender under the following conditions:
|
|
|
|
// * The selected message is within this._RENDER_THRESHOLD messages
|
|
|
|
// of the top of the currently rendered window and the top
|
|
|
|
// of the window does not abut the beginning of the message
|
|
|
|
// list
|
|
|
|
// * The selected message is within this._RENDER_THRESHOLD messages
|
|
|
|
// of the bottom of the currently rendered window and the
|
|
|
|
// bottom of the window does not abut the end of the
|
|
|
|
// message list
|
2013-03-07 21:16:20 +01:00
|
|
|
if (! (((selected_idx - this._render_win_start < this._RENDER_THRESHOLD)
|
|
|
|
&& (this._render_win_start !== 0)) ||
|
|
|
|
((this._render_win_end - selected_idx <= this._RENDER_THRESHOLD)
|
|
|
|
&& (this._render_win_end !== this._items.length))))
|
2013-02-28 22:38:08 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:30:36 +02:00
|
|
|
if (!this._update_render_window(selected_idx, true)) {
|
2013-02-28 22:38:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
this._rerender_preserving_scrolltop();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_rerender_preserving_scrolltop: function MessageList__rerender_preserving_scrolltop() {
|
2013-03-13 18:48:02 +01:00
|
|
|
// scrolltop_offset is the number of pixels between the top of the
|
|
|
|
// viewable window and the newly selected message
|
|
|
|
var scrolltop_offset;
|
|
|
|
var selected_row = rows.get(this._selected_id, this.table_name);
|
|
|
|
var selected_in_view = (selected_row.length > 0);
|
|
|
|
if (selected_in_view) {
|
|
|
|
scrolltop_offset = viewport.scrollTop() - selected_row.offset().top;
|
|
|
|
}
|
|
|
|
|
2013-02-28 22:38:08 +01:00
|
|
|
this._clear_table();
|
2013-03-07 21:16:20 +01:00
|
|
|
this._render(this._items.slice(this._render_win_start,
|
2013-04-10 18:35:23 +02:00
|
|
|
this._render_win_end), 'bottom');
|
2013-03-13 18:48:02 +01:00
|
|
|
|
|
|
|
// If we could see the newly selected message, scroll the
|
|
|
|
// window such that the newly selected message is at the
|
|
|
|
// same location as it would have been before we
|
|
|
|
// re-rendered.
|
|
|
|
if (selected_in_view) {
|
|
|
|
viewport.scrollTop(rows.get(this._selected_id, this.table_name).offset().top + scrolltop_offset);
|
|
|
|
}
|
2013-02-28 22:38:08 +01:00
|
|
|
},
|
|
|
|
|
2013-07-02 18:14:13 +02:00
|
|
|
_render: function MessageList__render(messages, where, messages_are_new) {
|
2013-02-28 22:38:08 +01:00
|
|
|
if (messages.length === 0 || this.table_name === undefined)
|
2013-02-26 23:30:13 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var table_name = this.table_name;
|
|
|
|
var table = rows.get_table(table_name);
|
|
|
|
var messages_to_render = [];
|
|
|
|
var ids_where_next_is_same_sender = {};
|
|
|
|
var prev;
|
2013-07-03 19:53:27 +02:00
|
|
|
var orig_scrolltop_offset, last_message_id;
|
2013-02-26 23:30:13 +01:00
|
|
|
|
|
|
|
var current_group = [];
|
|
|
|
var new_message_groups = [];
|
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
if (where === "bottom") {
|
|
|
|
// Remove the trailing bookend; it'll be re-added after we do our rendering
|
|
|
|
this.clear_trailing_bookend();
|
2013-07-09 00:06:04 +02:00
|
|
|
} else if (this.selected_row().length > 0) {
|
2013-07-03 19:53:27 +02:00
|
|
|
orig_scrolltop_offset = this.selected_row().offset().top - viewport.scrollTop();
|
2013-04-10 23:38:30 +02:00
|
|
|
}
|
|
|
|
|
2013-02-28 23:00:03 +01:00
|
|
|
if (where === 'top' && this.collapse_messages && this._message_groups.length > 0) {
|
2013-02-26 23:30:13 +01: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
|
|
|
|
// subject, which could be a problem down the line. For now we hope
|
|
|
|
// that subject views will not be very big.
|
|
|
|
|
|
|
|
var top_group = this._message_groups[0];
|
|
|
|
var top_messages = [];
|
|
|
|
$.each(top_group, function (index, id) {
|
|
|
|
rows.get(id, table_name).remove();
|
2013-07-03 21:14:54 +02:00
|
|
|
// Remove any date row headers for these messages
|
|
|
|
$('.date_row[data-zid=' + id + ']').remove();
|
2013-02-26 23:30:13 +01:00
|
|
|
top_messages.push(self.get(id));
|
|
|
|
});
|
|
|
|
messages = messages.concat(top_messages);
|
|
|
|
|
|
|
|
// Delete the leftover recipient label.
|
|
|
|
table.find('.recipient_row:first').remove();
|
|
|
|
} else {
|
|
|
|
last_message_id = rows.id(table.find('tr[zid]:last'));
|
|
|
|
prev = this.get(last_message_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(messages, function (index, message) {
|
|
|
|
message.include_recipient = false;
|
|
|
|
message.include_bookend = false;
|
2013-06-21 08:14:22 +02:00
|
|
|
message.include_footer = false;
|
2013-06-24 23:16:50 +02:00
|
|
|
|
|
|
|
add_display_time(message, prev);
|
|
|
|
|
2013-04-03 23:30:06 +02:00
|
|
|
if (util.same_recipient(prev, message) && self.collapse_messages &&
|
2013-06-24 23:16:50 +02:00
|
|
|
prev.historical === message.historical && !message.show_date) {
|
2013-02-26 23:30:13 +01:00
|
|
|
current_group.push(message.id);
|
2013-06-28 18:05:33 +02:00
|
|
|
// prev is no longer the last element in this block
|
|
|
|
prev.include_footer = false;
|
2013-02-26 23:30:13 +01:00
|
|
|
} else {
|
|
|
|
if (current_group.length > 0)
|
|
|
|
new_message_groups.push(current_group);
|
|
|
|
current_group = [message.id];
|
|
|
|
|
|
|
|
// Add a space to the table, but not for the first element.
|
|
|
|
message.include_recipient = true;
|
|
|
|
message.include_bookend = (prev !== undefined);
|
2013-06-28 18:05:33 +02:00
|
|
|
if (prev !== undefined) {
|
|
|
|
prev.include_footer = true;
|
2013-06-21 08:14:22 +02:00
|
|
|
}
|
2013-04-03 23:30:06 +02:00
|
|
|
message.subscribed = false;
|
|
|
|
message.unsubscribed = false;
|
|
|
|
if (message.include_bookend && message.historical !== prev.historical) {
|
|
|
|
if (message.historical) {
|
2013-04-18 17:13:43 +02:00
|
|
|
message.unsubscribed = message.stream;
|
2013-04-03 23:30:06 +02:00
|
|
|
} else {
|
2013-04-18 17:13:43 +02:00
|
|
|
message.subscribed = message.stream;
|
2013-04-03 23:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-26 23:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message.include_sender = true;
|
|
|
|
if (!message.include_recipient &&
|
2013-06-24 23:22:24 +02:00
|
|
|
util.same_sender(prev, message)) {
|
2013-02-26 23:30:13 +01:00
|
|
|
message.include_sender = false;
|
|
|
|
ids_where_next_is_same_sender[prev.id] = true;
|
|
|
|
}
|
|
|
|
|
2013-07-01 20:35:06 +02:00
|
|
|
if (message.last_edit_timestamp !== undefined) {
|
2013-05-21 17:48:46 +02:00
|
|
|
// Add or update the last_edit_timestr
|
|
|
|
var last_edit_time = new XDate(message.last_edit_timestamp * 1000);
|
2013-07-01 20:32:23 +02:00
|
|
|
message.last_edit_timestr =
|
2013-07-01 20:51:39 +02:00
|
|
|
(timerender.render_date(last_edit_time))[0].outerHTML
|
2013-07-01 20:32:23 +02:00
|
|
|
+ " " + last_edit_time.toString("HH:mm");
|
2013-05-21 17:48:46 +02:00
|
|
|
}
|
2013-02-26 23:30:13 +01:00
|
|
|
|
|
|
|
message.dom_id = table_name + message.id;
|
|
|
|
|
2013-06-13 23:48:23 +02:00
|
|
|
message.small_avatar_url = ui.small_avatar_url(message);
|
2013-02-26 23:30:13 +01:00
|
|
|
|
|
|
|
if (message.is_stream) {
|
2013-04-18 17:13:43 +02:00
|
|
|
message.background_color = subs.get_color(message.stream);
|
2013-02-26 23:30:13 +01:00
|
|
|
message.color_class = subs.get_color_class(message.background_color);
|
2013-04-18 17:13:43 +02:00
|
|
|
message.invite_only = subs.get_invite_only(message.stream);
|
2013-02-26 23:30:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-12 22:36:13 +01:00
|
|
|
message.contains_mention = notifications.speaking_at_me(message);
|
2013-07-01 23:28:27 +02:00
|
|
|
message.unread = unread.message_unread(message);
|
2013-03-12 22:36:13 +01:00
|
|
|
|
2013-02-26 23:30:13 +01:00
|
|
|
messages_to_render.push(message);
|
|
|
|
prev = message;
|
2013-04-10 23:38:30 +02:00
|
|
|
self.last_message_historical = message.historical;
|
2013-02-26 23:30:13 +01:00
|
|
|
});
|
|
|
|
|
2013-06-21 08:14:22 +02:00
|
|
|
if (prev) {
|
|
|
|
prev.include_footer = true;
|
|
|
|
}
|
2013-02-26 23:30:13 +01:00
|
|
|
if (messages_to_render.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_group.length > 0)
|
|
|
|
new_message_groups.push(current_group);
|
|
|
|
|
|
|
|
if (where === 'top') {
|
|
|
|
this._message_groups = new_message_groups.concat(this._message_groups);
|
|
|
|
} else {
|
|
|
|
this._message_groups = this._message_groups.concat(new_message_groups);
|
|
|
|
}
|
|
|
|
|
2013-02-16 10:45:32 +01:00
|
|
|
var rendered_elems = $(templates.render('message', {
|
2013-02-26 23:30:13 +01:00
|
|
|
messages: messages_to_render,
|
2013-04-29 22:56:50 +02:00
|
|
|
include_layout_row: (table.find('tr:first').length === 0),
|
|
|
|
use_match_properties: this.filter.is_search()
|
2013-02-26 23:30:13 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
$.each(rendered_elems, function (index, elem) {
|
|
|
|
var row = $(elem);
|
|
|
|
if (! row.hasClass('message_row')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var id = rows.id(row);
|
|
|
|
if (ids_where_next_is_same_sender[id]) {
|
|
|
|
row.find('.messagebox').addClass("next_is_same_sender");
|
|
|
|
}
|
2013-06-28 16:02:58 +02:00
|
|
|
|
|
|
|
if (row.hasClass('mention')) {
|
|
|
|
row.find('.user-mention').each(function () {
|
|
|
|
var email = $(this).attr('data-user-email');
|
|
|
|
if (email === '*' || email === page_params.email) {
|
|
|
|
$(this).addClass('user-mention-me');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-02-26 23:30:13 +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
|
2013-06-21 08:14:22 +02:00
|
|
|
// If the previous message was part of the same block but
|
|
|
|
// had a footer, we need to remove it.
|
|
|
|
if (last_message_id !== undefined) {
|
2013-02-26 23:30:13 +01:00
|
|
|
var row = rows.get(last_message_id, table_name);
|
2013-06-21 08:14:22 +02:00
|
|
|
if (ids_where_next_is_same_sender[last_message_id]) {
|
|
|
|
row.find('.messagebox').addClass("next_is_same_sender");
|
2013-06-28 18:05:33 +02:00
|
|
|
}
|
|
|
|
// We didn't actually rerender the original last message,
|
|
|
|
// but we might have set .include_footer=false for it in
|
|
|
|
// the above loop since it was the previous message for
|
|
|
|
// messages[0]. If so, we need to update the DOM.
|
|
|
|
if (this.get(last_message_id) && ! this.get(last_message_id).include_footer) {
|
|
|
|
row.removeClass('last_message');
|
2013-06-21 08:14:22 +02:00
|
|
|
}
|
2013-02-26 23:30:13 +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.
|
|
|
|
table.find('.ztable_layout_row').after(rendered_elems);
|
2013-07-03 19:53:27 +02:00
|
|
|
|
2013-07-09 00:06:04 +02:00
|
|
|
if (this === current_msg_list && orig_scrolltop_offset !== undefined) {
|
2013-07-03 19:53:27 +02:00
|
|
|
// Restore the selected row to its original position in
|
|
|
|
// relation to the top of the window
|
|
|
|
viewport.scrollTop(this.selected_row().offset().top - orig_scrolltop_offset);
|
|
|
|
this.select_id(this._selected_id, {from_rendering: true});
|
|
|
|
}
|
2013-02-26 23:30:13 +01:00
|
|
|
} else {
|
|
|
|
table.append(rendered_elems);
|
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
this.update_trailing_bookend();
|
|
|
|
|
2013-02-26 23:30:13 +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();
|
|
|
|
}});
|
|
|
|
}
|
2013-03-06 17:41:47 +01:00
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
$.each(rendered_elems, function (idx, elem) {
|
2013-05-15 00:22:16 +02:00
|
|
|
var row = $(elem);
|
|
|
|
if (! row.hasClass('message_row')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var id = rows.id(row);
|
|
|
|
message_edit.maybe_show_edit(row, id);
|
|
|
|
});
|
2013-05-01 23:21:45 +02:00
|
|
|
$.each(rendered_elems, ui.process_condensing);
|
2013-03-13 22:47:38 +01:00
|
|
|
|
2013-03-06 17:41:47 +01:00
|
|
|
// Re-add the fading of messages that is lost when we re-render.
|
|
|
|
compose.update_faded_messages();
|
2013-03-23 05:09:41 +01:00
|
|
|
|
2013-07-02 18:14:13 +02:00
|
|
|
if (this === current_msg_list && messages_are_new) {
|
2013-05-21 23:04:47 +02:00
|
|
|
this._maybe_autoscroll(rendered_elems);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_maybe_autoscroll: function MessageList__maybe_autoscroll(rendered_elems) {
|
|
|
|
|
2013-03-29 19:14:30 +01:00
|
|
|
// If we are near the bottom of our feed (the bottom is visible) and can
|
|
|
|
// scroll up without moving the pointer out of the viewport, do so, by
|
|
|
|
// up to the amount taken up by the new message.
|
|
|
|
|
2013-05-21 23:04:47 +02:00
|
|
|
var selected_row = current_msg_list.selected_row();
|
|
|
|
var last_visible = rows.last_visible();
|
|
|
|
|
|
|
|
// Make sure we have a selected row and last visible row. (defensive)
|
|
|
|
if (!(selected_row && (selected_row.length > 0) && last_visible)) {
|
2013-03-23 05:09:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-21 23:04:47 +02:00
|
|
|
var selected_row_offset = selected_row.offset().top;
|
2013-06-06 16:10:12 +02:00
|
|
|
var info = viewport.message_viewport_info();
|
2013-05-21 23:04:47 +02:00
|
|
|
var available_space_for_scroll = selected_row_offset - info.visible_top;
|
|
|
|
|
|
|
|
// Don't scroll if we can't move the pointer up.
|
|
|
|
if (available_space_for_scroll <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-23 05:09:41 +01:00
|
|
|
|
2013-05-21 23:04:47 +02:00
|
|
|
var new_messages_height = 0;
|
2013-07-05 17:43:56 +02:00
|
|
|
$.each(rendered_elems, function () {
|
2013-05-21 23:04:47 +02:00
|
|
|
// Sometimes there are non-DOM elements in rendered_elems; only
|
|
|
|
// try to get the heights of actual trs.
|
|
|
|
if ($(this).is("tr")) {
|
|
|
|
new_messages_height += $(this).height();
|
2013-05-20 18:02:44 +02:00
|
|
|
}
|
2013-05-21 23:04:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (new_messages_height <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This next decision is fairly debatable. For a big message that
|
|
|
|
// would push the pointer off the screen, we do a partial autoscroll,
|
|
|
|
// which has the following implications:
|
|
|
|
// a) user sees scrolling (good)
|
|
|
|
// b) user's pointer stays on screen (good)
|
|
|
|
// c) scroll amount isn't really tied to size of new messages (bad)
|
|
|
|
// d) all the bad things about scrolling for users who want messages
|
|
|
|
// to stay on the screen
|
|
|
|
var scroll_amount = new_messages_height;
|
|
|
|
|
|
|
|
if (scroll_amount > available_space_for_scroll) {
|
|
|
|
scroll_amount = available_space_for_scroll;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's work our way back to whether the user was already dealing
|
|
|
|
// with messages off the screen, in which case we shouldn't autoscroll.
|
|
|
|
var bottom_last_visible = last_visible.offset().top + last_visible.height();
|
|
|
|
var bottom_old_last_visible = bottom_last_visible - new_messages_height;
|
|
|
|
var bottom_viewport = info.visible_top + info.visible_height;
|
|
|
|
|
|
|
|
// Exit if the user was already past the bottom.
|
|
|
|
if (bottom_old_last_visible > bottom_viewport) {
|
|
|
|
return;
|
2013-03-23 05:09:41 +01:00
|
|
|
}
|
2013-05-21 23:04:47 +02:00
|
|
|
|
|
|
|
// Ok, we are finally ready to actually scroll.
|
2013-05-24 19:53:25 +02:00
|
|
|
viewport.system_initiated_animate_scroll(scroll_amount);
|
2013-02-26 23:30:13 +01:00
|
|
|
},
|
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
clear_trailing_bookend: function MessageList_clear_trailing_bookend() {
|
|
|
|
var trailing_bookend = rows.get_table(this.table_name).find('#trailing_bookend');
|
|
|
|
trailing_bookend.remove();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Maintains a trailing bookend element explaining any changes in
|
|
|
|
// your subscribed/unsubscribed status at the bottom of the
|
|
|
|
// message list.
|
|
|
|
update_trailing_bookend: function MessageList_update_trailing_bookend() {
|
|
|
|
this.clear_trailing_bookend();
|
|
|
|
if (!this.narrowed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stream = narrow.stream();
|
|
|
|
if (stream === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-12 21:13:19 +02:00
|
|
|
var trailing_bookend_content, subscribed = subs.is_subscribed(stream);
|
2013-04-10 23:38:30 +02:00
|
|
|
if (subscribed) {
|
|
|
|
if (this.last_message_historical) {
|
|
|
|
trailing_bookend_content = "--- Subscribed to stream " + stream + " ---";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!this.last_message_historical) {
|
|
|
|
trailing_bookend_content = "--- Unsubscribed from stream " + stream + " ---";
|
|
|
|
} else {
|
|
|
|
trailing_bookend_content = "--- Not subscribed to stream " + stream + " ---";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (trailing_bookend_content !== undefined) {
|
|
|
|
var rendered_trailing_bookend = $(templates.render('trailing_bookend', {
|
|
|
|
trailing_bookend: trailing_bookend_content
|
|
|
|
}));
|
|
|
|
rows.get_table(this.table_name).append(rendered_trailing_bookend);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-07-02 18:14:13 +02:00
|
|
|
append: function MessageList_append(messages, messages_are_new) {
|
2012-12-05 23:54:49 +01:00
|
|
|
this._items = this._items.concat(messages);
|
|
|
|
this._add_to_hash(messages);
|
2013-02-28 22:38:08 +01:00
|
|
|
|
2013-03-07 21:16:20 +01:00
|
|
|
var cur_window_size = this._render_win_end - this._render_win_start;
|
2013-02-28 22:38:08 +01:00
|
|
|
if (cur_window_size < this._RENDER_WINDOW_SIZE) {
|
2013-03-06 19:20:15 +01:00
|
|
|
var slice_to_render = messages.slice(0, this._RENDER_WINDOW_SIZE - cur_window_size);
|
2013-07-02 18:14:13 +02:00
|
|
|
this._render(slice_to_render, 'bottom', messages_are_new);
|
2013-03-07 21:16:20 +01:00
|
|
|
this._render_win_end += slice_to_render.length;
|
2013-02-26 23:30:13 +01:00
|
|
|
}
|
2013-03-12 18:25:37 +01:00
|
|
|
|
|
|
|
// If the pointer is high on the page such that there is a
|
|
|
|
// lot of empty space below and the render window is full, a
|
|
|
|
// newly recieved message should trigger a rerender so that
|
|
|
|
// the new message, which will appear in the viewable area,
|
|
|
|
// is rendered.
|
|
|
|
this._maybe_rerender();
|
2012-12-05 23:54:49 +01:00
|
|
|
},
|
|
|
|
|
2013-02-28 23:00:03 +01:00
|
|
|
prepend: function MessageList_prepend(messages) {
|
2012-12-05 23:54:49 +01:00
|
|
|
this._items = messages.concat(this._items);
|
|
|
|
this._add_to_hash(messages);
|
2013-02-28 22:38:08 +01:00
|
|
|
|
2013-03-07 21:16:20 +01:00
|
|
|
this._render_win_start += messages.length;
|
|
|
|
this._render_win_end += messages.length;
|
2013-07-03 19:53:27 +02:00
|
|
|
|
|
|
|
var cur_window_size = this._render_win_end - this._render_win_start;
|
|
|
|
if (cur_window_size < this._RENDER_WINDOW_SIZE) {
|
|
|
|
var slice_to_render = messages.slice(0, this._RENDER_WINDOW_SIZE - cur_window_size);
|
|
|
|
this._render(slice_to_render, 'top', false);
|
|
|
|
this._render_win_start -= slice_to_render.length;
|
|
|
|
}
|
2012-12-05 23:54:49 +01:00
|
|
|
},
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
add_and_rerender: function MessageList_add_and_rerender(messages) {
|
2013-04-10 18:30:36 +02:00
|
|
|
// To add messages that might be in the interior of our
|
|
|
|
// existing messages list, we just add the new messages and
|
|
|
|
// then rerender the whole thing.
|
|
|
|
this._items = messages.concat(this._items);
|
2013-07-05 17:43:56 +02:00
|
|
|
this._items.sort(function (a, b) {return a.id - b.id;});
|
2013-04-10 18:30:36 +02:00
|
|
|
this._add_to_hash(messages);
|
|
|
|
|
2013-07-09 00:33:38 +02:00
|
|
|
this._clear_rendering_state(true);
|
2013-04-10 18:30:36 +02:00
|
|
|
|
|
|
|
this._update_render_window(this._selected_idx(), false);
|
|
|
|
|
|
|
|
this._render(this._items.slice(this._render_win_start,
|
|
|
|
this._render_win_end), 'bottom');
|
|
|
|
},
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
show_edit_message: function MessageList_show_edit_message(row, edit_obj) {
|
|
|
|
row.find(".message_edit_form").empty().append(edit_obj.form);
|
|
|
|
row.find(".message_content").hide();
|
|
|
|
row.find(".message_edit").show();
|
|
|
|
},
|
|
|
|
|
|
|
|
hide_edit_message: function MessageList_hide_edit_message(row) {
|
|
|
|
row.find(".message_content").show();
|
|
|
|
row.find(".message_edit").hide();
|
|
|
|
},
|
|
|
|
|
2013-07-01 23:28:27 +02:00
|
|
|
show_message_as_read: function (message) {
|
|
|
|
rows.get(message.id, this.table_name).removeClass('unread');
|
|
|
|
},
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
rerender: function MessageList_rerender() {
|
|
|
|
// We need to clear the rendering state, rather than just
|
|
|
|
// doing _clear_table, since we want to potentially recollapse
|
|
|
|
// things.
|
2013-07-09 00:33:38 +02:00
|
|
|
this._clear_rendering_state(false);
|
|
|
|
this._update_render_window(this._selected_idx(), false);
|
2013-05-14 21:18:11 +02:00
|
|
|
this._rerender_preserving_scrolltop();
|
2013-06-04 22:07:44 +02:00
|
|
|
if (this._selected_id !== -1) {
|
|
|
|
this.select_id(this._selected_id);
|
|
|
|
}
|
2013-05-14 21:18:11 +02:00
|
|
|
},
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
all: function MessageList_all() {
|
|
|
|
return this._items;
|
|
|
|
}
|
|
|
|
};
|
2013-04-09 19:59:15 +02:00
|
|
|
|
2013-05-30 16:11:06 +02:00
|
|
|
// We stop autoscrolling when the user is clearly in the middle of
|
|
|
|
// doing something. Be careful, though, if you try to capture
|
|
|
|
// mousemove, then you will have to contend with the autoscroll
|
|
|
|
// itself generating mousemove events.
|
|
|
|
$(document).on('message_selected.zephyr hashchange.zephyr mousewheel', function (event) {
|
2013-05-24 19:53:25 +02:00
|
|
|
viewport.stop_auto_scrolling();
|
2013-04-09 19:59:15 +02:00
|
|
|
});
|
2013-02-26 23:30:13 +01:00
|
|
|
}());
|
2012-12-05 23:54:49 +01:00
|
|
|
/*jslint nomen: false */
|