2018-05-06 21:43:17 +02:00
|
|
|
/* eslint indent: "off" */
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2018-04-22 14:12:08 +02:00
|
|
|
self.get_items = function () {
|
|
|
|
var obj = self.container.find(self.item_sel);
|
|
|
|
return obj.map(function (i, elem) {
|
|
|
|
return $(elem);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-21 14:59:03 +02:00
|
|
|
self.first_key = function () {
|
|
|
|
var list_items = self.container.find(self.item_sel);
|
|
|
|
var li = list_items.first();
|
|
|
|
if (li.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var key = self.get_key_from_li({li: li});
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.prev_key = function (key) {
|
|
|
|
var li = self.find_li({key: key});
|
|
|
|
var prev_li = li.prev();
|
|
|
|
if (prev_li.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var prev_key = self.get_key_from_li({li: prev_li});
|
|
|
|
return prev_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.next_key = function (key) {
|
|
|
|
var li = self.find_li({key: key});
|
|
|
|
var next_li = li.next();
|
|
|
|
if (next_li.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var next_key = self.get_key_from_li({li: next_li});
|
|
|
|
return next_key;
|
|
|
|
};
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-28 08:04:36 +02:00
|
|
|
window.buddy_list = buddy_list;
|