2018-04-19 14:17:22 +02:00
|
|
|
var buddy_list = (function () {
|
|
|
|
var self = {};
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.container_sel = '#user_presences';
|
|
|
|
self.item_sel = 'li.user_sidebar_entry';
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.items_to_html = function (opts) {
|
|
|
|
var user_info = opts.items;
|
2018-04-19 14:17:22 +02:00
|
|
|
var html = templates.render('user_presence_rows', {users: user_info});
|
2018-04-20 16:13:39 +02:00
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.item_to_html = function (opts) {
|
|
|
|
var html = templates.render('user_presence_row', opts.item);
|
|
|
|
return html;
|
2018-04-19 14:17:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.find_li = function (opts) {
|
|
|
|
var user_id = opts.key;
|
2018-04-20 16:13:39 +02:00
|
|
|
var sel = self.item_sel + "[data-user-id='" + user_id + "']";
|
|
|
|
return self.container.find(sel);
|
2018-04-19 14:17:22 +02:00
|
|
|
};
|
|
|
|
|
2018-04-19 23:14:58 +02:00
|
|
|
self.get_key_from_li = function (opts) {
|
|
|
|
var user_id = opts.li.expectOne().attr('data-user-id');
|
|
|
|
return user_id;
|
|
|
|
};
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
// Try to keep code below this line generic, so that we can
|
|
|
|
// extract a widget.
|
|
|
|
|
|
|
|
self.populate = function (opts) {
|
|
|
|
var html = self.items_to_html({items: opts.items});
|
|
|
|
self.container = $(self.container_sel);
|
|
|
|
self.container.html(html);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.maybe_remove_key = function (opts) {
|
|
|
|
var li = self.find_li({key: opts.key});
|
|
|
|
li.remove();
|
|
|
|
};
|
|
|
|
|
2018-04-19 14:17:22 +02:00
|
|
|
self.insert_or_move = function (opts) {
|
2018-04-20 16:13:39 +02:00
|
|
|
var key = opts.key;
|
|
|
|
var item = opts.item;
|
2018-04-19 14:17:22 +02:00
|
|
|
var compare_function = opts.compare_function;
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.maybe_remove_key({key: key});
|
|
|
|
var html = self.item_to_html({item: item});
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
var list_items = self.container.find(self.item_sel);
|
2018-04-19 14:17:22 +02:00
|
|
|
|
|
|
|
function insert() {
|
|
|
|
var i = 0;
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
for (i = 0; i < list_items.length; i += 1) {
|
|
|
|
var li = list_items.eq(i);
|
|
|
|
|
|
|
|
var list_key = self.get_key_from_li({li: li});
|
|
|
|
|
|
|
|
if (compare_function(key, list_key) < 0) {
|
2018-04-19 14:17:22 +02:00
|
|
|
li.before(html);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.container.append(html);
|
2018-04-19 14:17:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
insert();
|
|
|
|
};
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
// This is a bit of a hack to make sure we at least have
|
|
|
|
// an empty list to start, before we get the initial payload.
|
|
|
|
self.container = $(self.container_sel);
|
|
|
|
|
2018-04-19 14:17:22 +02:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = buddy_list;
|
|
|
|
}
|
|
|
|
|