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-07-14 14:06:30 +02:00
|
|
|
self.get_data_from_keys = function (opts) {
|
|
|
|
var keys = opts.keys;
|
|
|
|
var data = buddy_data.get_items_for_users(keys);
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
// Try to keep code below this line generic, so that we can
|
|
|
|
// extract a widget.
|
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
self.keys = [];
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.populate = function (opts) {
|
2018-07-14 14:06:30 +02:00
|
|
|
// We rely on our caller to give us items
|
|
|
|
// in already-sorted order.
|
|
|
|
self.keys = _.map(opts.keys, function (k) {
|
|
|
|
return k.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
var items = self.get_data_from_keys({
|
|
|
|
keys: self.keys,
|
|
|
|
});
|
|
|
|
var html = self.items_to_html({items: items});
|
2018-04-20 16:13:39 +02:00
|
|
|
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 () {
|
2018-07-14 18:08:13 +02:00
|
|
|
return self.keys[0];
|
2018-04-21 14:59:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.prev_key = function (key) {
|
2018-07-14 18:08:13 +02:00
|
|
|
var i = self.keys.indexOf(key.toString());
|
|
|
|
|
|
|
|
if (i <= 0) {
|
2018-04-21 14:59:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-14 18:08:13 +02:00
|
|
|
|
|
|
|
return self.keys[i - 1];
|
2018-04-21 14:59:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.next_key = function (key) {
|
2018-07-14 18:08:13 +02:00
|
|
|
var i = self.keys.indexOf(key.toString());
|
|
|
|
|
|
|
|
if (i < 0) {
|
2018-04-21 14:59:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-14 18:08:13 +02:00
|
|
|
|
|
|
|
return self.keys[i + 1];
|
2018-04-21 14:59:03 +02:00
|
|
|
};
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.maybe_remove_key = function (opts) {
|
2018-07-14 14:06:30 +02:00
|
|
|
var pos = self.keys.indexOf(opts.key);
|
|
|
|
|
|
|
|
if (pos < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.keys.splice(pos, 1);
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
var li = self.find_li({key: opts.key});
|
|
|
|
li.remove();
|
|
|
|
};
|
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
self.find_position = function (opts) {
|
2018-04-20 16:13:39 +02:00
|
|
|
var key = opts.key;
|
2018-04-19 14:17:22 +02:00
|
|
|
var compare_function = opts.compare_function;
|
2018-07-14 14:06:30 +02:00
|
|
|
var i;
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
for (i = 0; i < self.keys.length; i += 1) {
|
|
|
|
var list_key = self.keys[i];
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
if (compare_function(key, list_key) < 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 16:13:39 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
return self.keys.length;
|
|
|
|
};
|
2018-04-20 16:13:39 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
self.insert_new_html = function (opts) {
|
|
|
|
var other_key = opts.other_key;
|
|
|
|
var html = opts.html;
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
if (other_key === undefined) {
|
2018-04-20 16:13:39 +02:00
|
|
|
self.container.append(html);
|
2018-07-14 14:06:30 +02:00
|
|
|
return;
|
2018-04-19 14:17:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
var li = self.find_li({key: other_key});
|
|
|
|
li.before(html);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.insert_or_move = function (opts) {
|
|
|
|
var key = opts.key.toString();
|
|
|
|
var item = opts.item;
|
|
|
|
var compare_function = opts.compare_function;
|
|
|
|
|
|
|
|
self.maybe_remove_key({key: key});
|
|
|
|
|
|
|
|
var pos = self.find_position({
|
|
|
|
key: key,
|
|
|
|
compare_function: compare_function,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Order is important here--get the other_key
|
|
|
|
// before mutating our list. An undefined value
|
|
|
|
// corresponds to appending.
|
|
|
|
var other_key = self.keys[pos];
|
|
|
|
|
|
|
|
self.keys.splice(pos, 0, key);
|
|
|
|
|
|
|
|
var html = self.item_to_html({item: item});
|
|
|
|
self.insert_new_html({
|
|
|
|
html: html,
|
|
|
|
other_key: other_key,
|
|
|
|
});
|
2018-04-19 14:17:22 +02:00
|
|
|
};
|
|
|
|
|
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;
|