zulip/static/js/buddy_list.js

167 lines
4.0 KiB
JavaScript
Raw Normal View History

/* eslint indent: "off" */
2018-04-19 14:17:22 +02:00
var buddy_list = (function () {
var self = {};
self.container_sel = '#user_presences';
self.item_sel = 'li.user_sidebar_entry';
2018-04-19 14:17:22 +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});
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;
var sel = self.item_sel + "[data-user-id='" + user_id + "']";
return self.container.find(sel);
2018-04-19 14:17:22 +02:00
};
self.get_key_from_li = function (opts) {
var user_id = opts.li.expectOne().attr('data-user-id');
return user_id;
};
self.get_data_from_keys = function (opts) {
var keys = opts.keys;
var data = buddy_data.get_items_for_users(keys);
return data;
};
self.compare_function = buddy_data.compare_function;
// Try to keep code below this line generic, so that we can
// extract a widget.
self.keys = [];
self.populate = function (opts) {
// 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});
self.container = $(self.container_sel);
self.container.html(html);
};
self.get_items = function () {
var obj = self.container.find(self.item_sel);
return obj.map(function (i, elem) {
return $(elem);
});
};
self.first_key = function () {
return self.keys[0];
};
self.prev_key = function (key) {
var i = self.keys.indexOf(key.toString());
if (i <= 0) {
return;
}
return self.keys[i - 1];
};
self.next_key = function (key) {
var i = self.keys.indexOf(key.toString());
if (i < 0) {
return;
}
return self.keys[i + 1];
};
self.maybe_remove_key = function (opts) {
var pos = self.keys.indexOf(opts.key);
if (pos < 0) {
return;
}
self.keys.splice(pos, 1);
var li = self.find_li({key: opts.key});
li.remove();
};
self.find_position = function (opts) {
var key = opts.key;
var i;
2018-04-19 14:17:22 +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
if (self.compare_function(key, list_key) < 0) {
return i;
}
}
return self.keys.length;
};
self.insert_new_html = function (opts) {
var other_key = opts.other_key;
var html = opts.html;
2018-04-19 14:17:22 +02:00
if (other_key === undefined) {
self.container.append(html);
return;
2018-04-19 14:17:22 +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;
self.maybe_remove_key({key: key});
var pos = self.find_position({
key: key,
});
// 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
};
// 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;
}
window.buddy_list = buddy_list;