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-07-25 22:08:16 +02:00
|
|
|
_.extend(this, {
|
|
|
|
collapse_messages: true,
|
|
|
|
summarize_read: false
|
|
|
|
}, opts);
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view = new MessageListView(this, table_name, this.collapse_messages);
|
2013-07-25 22:08:16 +02:00
|
|
|
|
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-27 20:47:04 +01:00
|
|
|
|
2013-04-25 21:13:06 +02:00
|
|
|
if (this.filter === undefined) {
|
2013-08-10 01:31:31 +02:00
|
|
|
this.filter = new Filter();
|
2013-04-25 21:13:06 +02:00
|
|
|
}
|
2013-04-10 17:42:14 +02:00
|
|
|
this.narrowed = false;
|
|
|
|
if (this.table_name === "zfilt") {
|
|
|
|
this.narrowed = true;
|
|
|
|
}
|
2013-08-14 23:48:28 +02:00
|
|
|
|
|
|
|
this.num_appends = 0;
|
|
|
|
this.min_id_exempted_from_summaries = -1;
|
2012-12-05 23:54:49 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-26 23:30:13 +01:00
|
|
|
(function () {
|
|
|
|
|
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];
|
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
get_messages: function MessageList_get_mesages() {
|
|
|
|
return this._items;
|
|
|
|
},
|
|
|
|
|
|
|
|
num_items: function MessageList_num_items() {
|
|
|
|
return this._items.length;
|
|
|
|
},
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
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-08-14 23:04:24 +02:00
|
|
|
nth_most_recent_id: function MessageList_nth_most_recent_id(n) {
|
|
|
|
var i = this._items.length - n;
|
|
|
|
if (i < 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return this._items[i].id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
clear: function MessageList_clear(opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.extend({clear_selected_id: true}, opts);
|
2013-02-22 20:48:31 +01:00
|
|
|
|
|
|
|
this._items = [];
|
|
|
|
this._hash = {};
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.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-30 05:11:50 +02:00
|
|
|
opts = _.extend({
|
2013-07-03 22:14:32 +02:00
|
|
|
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
|
|
|
}
|
2013-08-07 20:28:50 +02: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) {
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.maybe_rerender();
|
2013-07-03 19:53:27 +02:00
|
|
|
}
|
2013-03-04 20:22:09 +01:00
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('message_selected.zulip', opts));
|
2013-02-20 18:33:04 +01:00
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
reselect_selected_id: function MessageList_select_closest_id() {
|
|
|
|
this.select_id(this._selected_id, {from_rendering: true});
|
|
|
|
},
|
|
|
|
|
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-08-14 22:00:32 +02:00
|
|
|
return this.get_row(this._selected_id);
|
2013-02-14 23:48:37 +01:00
|
|
|
},
|
|
|
|
|
2013-08-23 20:28:43 +02:00
|
|
|
on_expandable_row: function MessageList_on_expandable_row() {
|
|
|
|
return this.view.is_expandable_row(this.selected_row());
|
2013-08-08 00:12:27 +02:00
|
|
|
},
|
|
|
|
|
2013-02-20 00:49:21 +01:00
|
|
|
closest_id: function MessageList_closest_id(id) {
|
2013-07-25 22:08:16 +02:00
|
|
|
var items = this._items;
|
|
|
|
|
|
|
|
if (items.length === 0) {
|
2013-02-20 00:49:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
|
|
|
|
var closest = util.lower_bound(items, id,
|
2013-02-20 00:49:21 +01:00
|
|
|
function (a, b) {
|
|
|
|
return a.id < b;
|
|
|
|
});
|
2013-07-25 22:08:16 +02:00
|
|
|
|
|
|
|
if (closest === items.length
|
2013-02-20 00:49:21 +01:00
|
|
|
|| (closest !== 0
|
2013-07-25 22:08:16 +02:00
|
|
|
&& (id - items[closest - 1].id <
|
|
|
|
items[closest].id - id)))
|
2013-02-20 00:49:21 +01:00
|
|
|
{
|
|
|
|
closest = closest - 1;
|
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
return items[closest].id;
|
2013-02-20 00:49:21 +01:00
|
|
|
},
|
|
|
|
|
2013-07-24 22:33:06 +02:00
|
|
|
advance_past_messages: function MessageList_advance_past_messages(msg_ids) {
|
|
|
|
// Start with the current pointer, but then keep advancing the
|
|
|
|
// pointer while the next message's id is in msg_ids. See trac #1555
|
|
|
|
// for more context, but basically we are skipping over contiguous
|
|
|
|
// messages that we have recently visited.
|
|
|
|
var next_msg_id = 0;
|
|
|
|
|
|
|
|
var id_set = {};
|
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(msg_ids, function (msg_id) {
|
2013-07-24 22:33:06 +02:00
|
|
|
id_set[msg_id] = true;
|
|
|
|
});
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
var idx = this.selected_idx() + 1;
|
2013-07-24 22:33:06 +02:00
|
|
|
while (idx < this._items.length) {
|
|
|
|
var msg_id = this._items[idx].id;
|
|
|
|
if (!id_set[msg_id]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next_msg_id = msg_id;
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_msg_id > 0) {
|
|
|
|
this._selected_id = next_msg_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-08-16 17:10:22 +02:00
|
|
|
is_summarized_message: function (message) {
|
2013-08-08 18:33:53 +02:00
|
|
|
if (!feature_flags.summarize_read_while_narrowed ||
|
|
|
|
message === undefined || message.flags === undefined) {
|
2013-07-25 22:08:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-14 23:48:28 +02:00
|
|
|
if (message.id >= this.min_id_exempted_from_summaries) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
if (this.summarize_read === 'home') {
|
|
|
|
return message.flags.indexOf('summarize_in_home') !== -1;
|
|
|
|
} else if (this.summarize_read === 'stream' ) {
|
|
|
|
return message.flags.indexOf('summarize_in_stream') !== -1;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
selected_idx: function MessageList_selected_idx() {
|
2013-04-10 18:30:36 +02:00
|
|
|
return util.lower_bound(this._items, this._selected_id,
|
|
|
|
function (a, b) { return a.id < b; });
|
|
|
|
},
|
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
// 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() {
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.clear_trailing_bookend();
|
2013-04-10 23:38:30 +02:00
|
|
|
if (!this.narrowed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stream = narrow.stream();
|
|
|
|
if (stream === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-15 21:11:07 +02:00
|
|
|
var trailing_bookend_content, subscribed = stream_data.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) {
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.render_trailing_bookend(trailing_bookend_content);
|
2013-04-10 23:38:30 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-14 23:48:28 +02:00
|
|
|
start_summary_exemption: function MessageList_start_summary_exemption() {
|
2013-08-15 16:52:04 +02:00
|
|
|
var num_exempt = 8;
|
2013-08-14 23:48:28 +02:00
|
|
|
this.min_id_exempted_from_summaries = this.nth_most_recent_id(num_exempt);
|
|
|
|
},
|
|
|
|
|
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);
|
2013-08-14 23:48:28 +02:00
|
|
|
|
|
|
|
if (this.num_appends === 0) {
|
|
|
|
// We can't figure out which messages need to be exempt from
|
|
|
|
// summarization until we get the first batch of messages.
|
|
|
|
this.start_summary_exemption();
|
|
|
|
}
|
|
|
|
this.num_appends += 1;
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
this._add_to_hash(messages);
|
2013-02-28 22:38:08 +01:00
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.append(messages, messages_are_new);
|
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-08-16 17:10:22 +02:00
|
|
|
this.view.prepend(messages);
|
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-08-16 17:10:22 +02:00
|
|
|
this.view.rerender_the_whole_thing();
|
2013-04-10 18:30:36 +02:00
|
|
|
},
|
|
|
|
|
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();
|
2013-07-16 23:36:31 +02:00
|
|
|
row.find(".message_edit_content").autosize();
|
2013-05-15 00:22:16 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
hide_edit_message: function MessageList_hide_edit_message(row) {
|
|
|
|
row.find(".message_content").show();
|
|
|
|
row.find(".message_edit").hide();
|
|
|
|
},
|
|
|
|
|
2013-08-16 23:45:13 +02:00
|
|
|
show_edit_topic: function MessageList_show_edit_topic(recipient_row, form) {
|
|
|
|
recipient_row.find(".topic_edit_form").empty().append(form);
|
|
|
|
recipient_row.find(".stream_topic").hide();
|
|
|
|
recipient_row.find(".topic_edit").show();
|
|
|
|
},
|
|
|
|
|
|
|
|
hide_edit_topic: function MessageList_hide_edit_topic(recipient_row) {
|
|
|
|
recipient_row.find(".stream_topic").show();
|
|
|
|
recipient_row.find(".topic_edit").hide();
|
|
|
|
},
|
|
|
|
|
2013-07-26 17:19:13 +02:00
|
|
|
show_message_as_read: function (message, options) {
|
2013-08-14 22:00:32 +02:00
|
|
|
var row = this.get_row(message.id);
|
2013-07-26 17:19:13 +02:00
|
|
|
if (options.from === 'pointer' && feature_flags.mark_read_at_bottom) {
|
|
|
|
row.find('.unread_marker').addClass('fast_fade');
|
|
|
|
} else {
|
|
|
|
row.find('.unread_marker').addClass('slow_fade');
|
|
|
|
}
|
|
|
|
row.removeClass('unread');
|
2013-07-01 23:28:27 +02:00
|
|
|
},
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
rerender: function MessageList_rerender() {
|
|
|
|
// We need to clear the rendering state, rather than just
|
2013-08-16 17:10:22 +02:00
|
|
|
// doing clear_table, since we want to potentially recollapse
|
2013-05-14 21:18:11 +02:00
|
|
|
// things.
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.clear_rendering_state(false);
|
|
|
|
this.view.update_render_window(this.selected_idx(), false);
|
|
|
|
this.view.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-08-14 22:00:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
get_row: function (id) {
|
2013-08-16 17:10:22 +02:00
|
|
|
return this.view.get_row(id);
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
|
|
|
};
|
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.
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('message_selected.zulip hashchange.zulip 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 */
|
2013-08-06 21:30:31 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = MessageList;
|
|
|
|
}
|