2012-12-05 23:54:49 +01:00
|
|
|
/*jslint nomen: true */
|
|
|
|
function MessageList(table_name) {
|
|
|
|
this._items = [];
|
|
|
|
this._hash = {};
|
|
|
|
this.table_name = table_name;
|
2013-02-20 18:26:50 +01:00
|
|
|
this._selected_id = -1;
|
2012-12-05 23:54:49 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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) {
|
|
|
|
id = parseInt(id, 10);
|
|
|
|
if (isNaN(id)) {
|
|
|
|
throw (new Error("Bad message id"));
|
|
|
|
}
|
|
|
|
if (this.get(id) === undefined) {
|
|
|
|
throw (new Error("Selected message id not in MessageList"));
|
|
|
|
}
|
|
|
|
this._selected_id = id;
|
2013-02-20 20:49:49 +01:00
|
|
|
opts = $.extend({then_scroll: false}, opts, {id: id, msg_list: this});
|
|
|
|
$(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)) {
|
|
|
|
throw (new Error("Bad message id"));
|
|
|
|
}
|
|
|
|
if (self._hash[id] !== undefined) {
|
|
|
|
throw (new Error("Duplicate message added to MessageList"));
|
|
|
|
}
|
|
|
|
self._hash[id] = elem;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
append: function MessageList_append(messages) {
|
|
|
|
this._items = this._items.concat(messages);
|
|
|
|
this._add_to_hash(messages);
|
|
|
|
},
|
|
|
|
|
|
|
|
prepend: function MessageList_prepend(messages) {
|
|
|
|
this._items = messages.concat(this._items);
|
|
|
|
this._add_to_hash(messages);
|
|
|
|
},
|
|
|
|
|
|
|
|
all: function MessageList_all() {
|
|
|
|
return this._items;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/*jslint nomen: false */
|