2019-11-02 00:06:25 +01:00
|
|
|
const render_user_presence_row = require('../templates/user_presence_row.hbs');
|
|
|
|
const render_user_presence_rows = require('../templates/user_presence_rows.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
function buddy_list_conf() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const conf = {};
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.container_sel = '#user_presences';
|
|
|
|
conf.scroll_container_sel = '#buddy_list_wrapper';
|
|
|
|
conf.item_sel = 'li.user_sidebar_entry';
|
|
|
|
conf.padding_sel = '#buddy_list_wrapper_padding';
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.items_to_html = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_info = opts.items;
|
|
|
|
const html = render_user_presence_rows({users: user_info});
|
2018-04-20 16:13:39 +02:00
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.item_to_html = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_user_presence_row(opts.item);
|
2018-04-20 16:13:39 +02:00
|
|
|
return html;
|
2018-04-19 14:17:22 +02:00
|
|
|
};
|
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.get_li_from_key = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = opts.key;
|
|
|
|
const container = $(conf.container_sel);
|
|
|
|
const sel = conf.item_sel + "[data-user-id='" + user_id + "']";
|
2018-08-07 16:59:10 +02:00
|
|
|
return container.find(sel);
|
2018-04-19 14:17:22 +02:00
|
|
|
};
|
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.get_key_from_li = function (opts) {
|
2020-01-16 20:40:20 +01:00
|
|
|
return parseInt(opts.li.expectOne().attr('data-user-id'), 10);
|
2018-04-19 23:14:58 +02:00
|
|
|
};
|
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.get_data_from_keys = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const keys = opts.keys;
|
|
|
|
const data = buddy_data.get_items_for_users(keys);
|
2018-07-14 14:06:30 +02:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.compare_function = buddy_data.compare_function;
|
2018-07-16 17:42:20 +02:00
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
conf.height_to_fill = function () {
|
2018-07-16 15:16:33 +02:00
|
|
|
// Because the buddy list gets sized dynamically, we err on the side
|
|
|
|
// of using the height of the entire viewport for deciding
|
|
|
|
// how much content to render. Even on tall monitors this should
|
|
|
|
// still be a significant optimization for orgs with thousands of
|
|
|
|
// users.
|
2019-11-02 00:06:25 +01:00
|
|
|
const height = message_viewport.height();
|
2018-07-16 15:16:33 +02:00
|
|
|
return height;
|
|
|
|
};
|
|
|
|
|
2018-08-07 16:59:10 +02:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
function buddy_list_create() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const conf = buddy_list_conf();
|
2018-08-07 16:59:10 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2018-08-07 16:59:10 +02:00
|
|
|
|
|
|
|
self.container_sel = conf.container_sel;
|
|
|
|
self.scroll_container_sel = conf.scroll_container_sel;
|
|
|
|
self.item_sel = conf.item_sel;
|
|
|
|
self.padding_sel = conf.padding_sel;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const func_names = [
|
2018-08-07 16:59:10 +02:00
|
|
|
'items_to_html',
|
|
|
|
'item_to_html',
|
|
|
|
'get_li_from_key',
|
|
|
|
'get_key_from_li',
|
|
|
|
'get_data_from_keys',
|
|
|
|
'compare_function',
|
|
|
|
'height_to_fill',
|
|
|
|
];
|
|
|
|
|
|
|
|
_.each(func_names, function (func_name) {
|
|
|
|
self[func_name] = conf[func_name];
|
|
|
|
});
|
2018-04-20 16:13:39 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
self.keys = [];
|
|
|
|
|
2018-04-20 16:13:39 +02:00
|
|
|
self.populate = function (opts) {
|
2018-07-16 15:16:33 +02:00
|
|
|
self.render_count = 0;
|
|
|
|
self.container.html('');
|
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
// We rely on our caller to give us items
|
|
|
|
// in already-sorted order.
|
2019-12-29 15:07:05 +01:00
|
|
|
self.keys = opts.keys;
|
2018-07-14 14:06:30 +02:00
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
self.fill_screen_with_content();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.render_more = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const chunk_size = opts.chunk_size;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const begin = self.render_count;
|
|
|
|
const end = begin + chunk_size;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const more_keys = self.keys.slice(begin, end);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
if (more_keys.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = self.get_data_from_keys({
|
2018-07-16 15:16:33 +02:00
|
|
|
keys: more_keys,
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = self.items_to_html({
|
2018-07-16 15:16:33 +02:00
|
|
|
items: items,
|
2018-07-14 14:06:30 +02:00
|
|
|
});
|
2018-04-20 16:13:39 +02:00
|
|
|
self.container = $(self.container_sel);
|
2018-07-16 15:16:33 +02:00
|
|
|
self.container.append(html);
|
|
|
|
|
|
|
|
// Invariant: more_keys.length >= items.length.
|
|
|
|
// (Usually they're the same, but occasionally keys
|
|
|
|
// won't return valid items. Even though we don't
|
|
|
|
// actually render these keys, we still "count" them
|
|
|
|
// as rendered.
|
|
|
|
|
|
|
|
self.render_count += more_keys.length;
|
2018-07-26 19:50:15 +02:00
|
|
|
self.update_padding();
|
2018-04-20 16:13:39 +02:00
|
|
|
};
|
|
|
|
|
2018-04-22 14:12:08 +02:00
|
|
|
self.get_items = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const obj = self.container.find(self.item_sel);
|
2018-04-22 14:12:08 +02:00
|
|
|
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) {
|
2019-12-29 15:07:05 +01:00
|
|
|
const i = self.keys.indexOf(key);
|
2018-07-14 18:08:13 +02:00
|
|
|
|
|
|
|
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) {
|
2019-12-29 15:07:05 +01:00
|
|
|
const i = self.keys.indexOf(key);
|
2018-07-14 18:08:13 +02:00
|
|
|
|
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pos = self.keys.indexOf(opts.key);
|
2018-07-14 14:06:30 +02:00
|
|
|
|
|
|
|
if (pos < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.keys.splice(pos, 1);
|
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
if (pos < self.render_count) {
|
|
|
|
self.render_count -= 1;
|
2019-11-02 00:06:25 +01:00
|
|
|
const li = self.find_li({key: opts.key});
|
2018-07-16 15:16:33 +02:00
|
|
|
li.remove();
|
2018-07-26 19:50:15 +02:00
|
|
|
self.update_padding();
|
2018-07-16 15:16:33 +02:00
|
|
|
}
|
2018-04-20 16:13:39 +02:00
|
|
|
};
|
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
self.find_position = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = opts.key;
|
|
|
|
let 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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const list_key = self.keys[i];
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-07-16 17:42:20 +02:00
|
|
|
if (self.compare_function(key, list_key) < 0) {
|
2018-07-14 14:06:30 +02:00
|
|
|
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-16 15:16:33 +02:00
|
|
|
self.force_render = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pos = opts.pos;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
// Try to render a bit optimistically here.
|
2019-11-02 00:06:25 +01:00
|
|
|
const cushion_size = 3;
|
|
|
|
const chunk_size = pos + cushion_size - self.render_count;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
if (chunk_size <= 0) {
|
|
|
|
blueslip.error('cannot show key at this position: ' + pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.render_more({
|
|
|
|
chunk_size: chunk_size,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.find_li = function (opts) {
|
2019-12-29 15:07:05 +01:00
|
|
|
const key = opts.key;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
// Try direct DOM lookup first for speed.
|
2019-11-02 00:06:25 +01:00
|
|
|
let li = self.get_li_from_key({
|
2018-07-16 15:16:33 +02:00
|
|
|
key: key,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (li.length === 1) {
|
|
|
|
return li;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.force_render) {
|
|
|
|
// Most callers don't force us to render a list
|
|
|
|
// item that wouldn't be on-screen anyway.
|
|
|
|
return li;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pos = self.keys.indexOf(key);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
if (pos < 0) {
|
|
|
|
// TODO: See list_cursor.get_row() for why this is
|
|
|
|
// a bit janky now.
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.force_render({
|
|
|
|
pos: pos,
|
|
|
|
});
|
|
|
|
|
|
|
|
li = self.get_li_from_key({
|
|
|
|
key: key,
|
|
|
|
});
|
|
|
|
|
|
|
|
return li;
|
|
|
|
};
|
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
self.insert_new_html = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const other_key = opts.other_key;
|
|
|
|
const html = opts.html;
|
|
|
|
const pos = opts.pos;
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-07-14 14:06:30 +02:00
|
|
|
if (other_key === undefined) {
|
2018-07-16 15:16:33 +02:00
|
|
|
if (pos === self.render_count) {
|
|
|
|
self.render_count += 1;
|
|
|
|
self.container.append(html);
|
2018-07-26 19:50:15 +02:00
|
|
|
self.update_padding();
|
2018-07-16 15:16:33 +02:00
|
|
|
}
|
2018-07-14 14:06:30 +02:00
|
|
|
return;
|
2018-04-19 14:17:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
if (pos < self.render_count) {
|
|
|
|
self.render_count += 1;
|
2019-11-02 00:06:25 +01:00
|
|
|
const li = self.find_li({key: other_key});
|
2018-07-16 15:16:33 +02:00
|
|
|
li.before(html);
|
2018-07-26 19:50:15 +02:00
|
|
|
self.update_padding();
|
2018-07-16 15:16:33 +02:00
|
|
|
}
|
2018-07-14 14:06:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.insert_or_move = function (opts) {
|
2019-12-29 15:07:05 +01:00
|
|
|
const key = opts.key;
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = opts.item;
|
2018-07-14 14:06:30 +02:00
|
|
|
|
|
|
|
self.maybe_remove_key({key: key});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pos = self.find_position({
|
2018-07-14 14:06:30 +02:00
|
|
|
key: key,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Order is important here--get the other_key
|
|
|
|
// before mutating our list. An undefined value
|
|
|
|
// corresponds to appending.
|
2019-11-02 00:06:25 +01:00
|
|
|
const other_key = self.keys[pos];
|
2018-07-14 14:06:30 +02:00
|
|
|
|
|
|
|
self.keys.splice(pos, 0, key);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = self.item_to_html({item: item});
|
2018-07-14 14:06:30 +02:00
|
|
|
self.insert_new_html({
|
2018-07-16 15:16:33 +02:00
|
|
|
pos: pos,
|
2018-07-14 14:06:30 +02:00
|
|
|
html: html,
|
|
|
|
other_key: other_key,
|
|
|
|
});
|
2018-04-19 14:17:22 +02:00
|
|
|
};
|
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
self.fill_screen_with_content = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
let height = self.height_to_fill();
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = ui.get_scroll_element($(self.scroll_container_sel)).expectOne()[0];
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
// Add a fudge factor.
|
2018-08-04 09:07:46 +02:00
|
|
|
height += 10;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
while (self.render_count < self.keys.length) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const padding_height = $(self.padding_sel).height();
|
|
|
|
const bottom_offset = elem.scrollHeight - elem.scrollTop - padding_height;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2018-08-04 09:07:46 +02:00
|
|
|
if (bottom_offset > height) {
|
2018-07-16 15:16:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const chunk_size = 20;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
self.render_more({
|
|
|
|
chunk_size: chunk_size,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-07-16 15:16:33 +02:00
|
|
|
self.start_scroll_handler = function () {
|
|
|
|
// We have our caller explicitly call this to make
|
|
|
|
// sure everything's in place.
|
2019-11-02 00:06:25 +01:00
|
|
|
const scroll_container = ui.get_scroll_element($(self.scroll_container_sel));
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
scroll_container.scroll(function () {
|
|
|
|
self.fill_screen_with_content();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-26 19:50:15 +02:00
|
|
|
self.update_padding = function () {
|
|
|
|
padded_widget.update_padding({
|
|
|
|
shown_rows: self.render_count,
|
|
|
|
total_rows: self.keys.length,
|
|
|
|
content_sel: self.container_sel,
|
|
|
|
padding_sel: self.padding_sel,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-19 14:17:22 +02:00
|
|
|
return self;
|
2018-08-07 16:22:13 +02:00
|
|
|
}
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const buddy_list = buddy_list_create();
|
2018-08-07 16:22:13 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
module.exports = buddy_list;
|
2018-04-19 14:17:22 +02:00
|
|
|
|
2018-05-28 08:04:36 +02:00
|
|
|
window.buddy_list = buddy_list;
|