Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

buddy_list: Convert buddy_list_create to an ES6 class BuddyList.

Anders Kaseorga1796325dee5
diff --git a/static/js/buddy_list.js b/static/js/buddy_list.js
index f89887c0239..0a05cfb609e 100644
--- a/static/js/buddy_list.js
+++ b/static/js/buddy_list.js
@@ -1,45 +1,43 @@
const render_user_presence_row = require("../templates/user_presence_row.hbs");
const render_user_presence_rows = require("../templates/user_presence_rows.hbs");
-function buddy_list_conf() {
- const conf = {};
+class BuddyListConf {
+ container_sel = "#user_presences";
+ scroll_container_sel = "#buddy_list_wrapper";
+ item_sel = "li.user_sidebar_entry";
+ padding_sel = "#buddy_list_wrapper_padding";
- 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";
-
- conf.items_to_html = function (opts) {
+ items_to_html(opts) {
const user_info = opts.items;
const html = render_user_presence_rows({users: user_info});
return html;
- };
+ }
- conf.item_to_html = function (opts) {
+ item_to_html(opts) {
const html = render_user_presence_row(opts.item);
return html;
- };
+ }
- conf.get_li_from_key = function (opts) {
+ get_li_from_key(opts) {
const user_id = opts.key;
- const container = $(conf.container_sel);
- const sel = conf.item_sel + "[data-user-id='" + user_id + "']";
+ const container = $(this.container_sel);
+ const sel = this.item_sel + "[data-user-id='" + user_id + "']";
return container.find(sel);
- };
+ }
- conf.get_key_from_li = function (opts) {
+ get_key_from_li(opts) {
return parseInt(opts.li.expectOne().attr("data-user-id"), 10);
- };
+ }
- conf.get_data_from_keys = function (opts) {
+ get_data_from_keys(opts) {
const keys = opts.keys;
const data = buddy_data.get_items_for_users(keys);
return data;
- };
+ }
- conf.compare_function = buddy_data.compare_function;
+ compare_function = buddy_data.compare_function;
- conf.height_to_fill = function () {
+ height_to_fill() {
// 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
@@ -47,69 +45,44 @@ function buddy_list_conf() {
// users.
const height = message_viewport.height();
return height;
- };
-
- return conf;
-}
-
-function buddy_list_create() {
- const conf = buddy_list_conf();
-
- const self = {};
-
- 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;
-
- const func_names = [
- "items_to_html",
- "item_to_html",
- "get_li_from_key",
- "get_key_from_li",
- "get_data_from_keys",
- "compare_function",
- "height_to_fill",
- ];
-
- for (const func_name of func_names) {
- self[func_name] = conf[func_name];
}
+}
- self.keys = [];
+class BuddyList extends BuddyListConf {
+ keys = [];
- self.populate = function (opts) {
- self.render_count = 0;
- self.container.html("");
+ populate(opts) {
+ this.render_count = 0;
+ this.container.html("");
// We rely on our caller to give us items
// in already-sorted order.
- self.keys = opts.keys;
+ this.keys = opts.keys;
- self.fill_screen_with_content();
- };
+ this.fill_screen_with_content();
+ }
- self.render_more = function (opts) {
+ render_more(opts) {
const chunk_size = opts.chunk_size;
- const begin = self.render_count;
+ const begin = this.render_count;
const end = begin + chunk_size;
- const more_keys = self.keys.slice(begin, end);
+ const more_keys = this.keys.slice(begin, end);
if (more_keys.length === 0) {
return;
}
- const items = self.get_data_from_keys({
+ const items = this.get_data_from_keys({
keys: more_keys,
});
- const html = self.items_to_html({
+ const html = this.items_to_html({
items,
});
- self.container = $(self.container_sel);
- self.container.append(html);
+ this.container = $(this.container_sel);
+ this.container.append(html);
// Invariant: more_keys.length >= items.length.
// (Usually they're the same, but occasionally keys
@@ -117,92 +90,92 @@ function buddy_list_create() {
// actually render these keys, we still "count" them
// as rendered.
- self.render_count += more_keys.length;
- self.update_padding();
- };
+ this.render_count += more_keys.length;
+ this.update_padding();
+ }
- self.get_items = function () {
- const obj = self.container.find(self.item_sel);
+ get_items() {
+ const obj = this.container.find(this.item_sel);
return obj.map((i, elem) => $(elem));
- };
+ }
- self.first_key = function () {
- return self.keys[0];
- };
+ first_key() {
+ return this.keys[0];
+ }
- self.prev_key = function (key) {
- const i = self.keys.indexOf(key);
+ prev_key(key) {
+ const i = this.keys.indexOf(key);
if (i <= 0) {
return;
}
- return self.keys[i - 1];
- };
+ return this.keys[i - 1];
+ }
- self.next_key = function (key) {
- const i = self.keys.indexOf(key);
+ next_key(key) {
+ const i = this.keys.indexOf(key);
if (i < 0) {
return;
}
- return self.keys[i + 1];
- };
+ return this.keys[i + 1];
+ }
- self.maybe_remove_key = function (opts) {
- const pos = self.keys.indexOf(opts.key);
+ maybe_remove_key(opts) {
+ const pos = this.keys.indexOf(opts.key);
if (pos < 0) {
return;
}
- self.keys.splice(pos, 1);
+ this.keys.splice(pos, 1);
- if (pos < self.render_count) {
- self.render_count -= 1;
- const li = self.find_li({key: opts.key});
+ if (pos < this.render_count) {
+ this.render_count -= 1;
+ const li = this.find_li({key: opts.key});
li.remove();
- self.update_padding();
+ this.update_padding();
}
- };
+ }
- self.find_position = function (opts) {
+ find_position(opts) {
const key = opts.key;
let i;
- for (i = 0; i < self.keys.length; i += 1) {
- const list_key = self.keys[i];
+ for (i = 0; i < this.keys.length; i += 1) {
+ const list_key = this.keys[i];
- if (self.compare_function(key, list_key) < 0) {
+ if (this.compare_function(key, list_key) < 0) {
return i;
}
}
- return self.keys.length;
- };
+ return this.keys.length;
+ }
- self.force_render = function (opts) {
+ force_render(opts) {
const pos = opts.pos;
// Try to render a bit optimistically here.
const cushion_size = 3;
- const chunk_size = pos + cushion_size - self.render_count;
+ const chunk_size = pos + cushion_size - this.render_count;
if (chunk_size <= 0) {
blueslip.error("cannot show key at this position: " + pos);
}
- self.render_more({
+ this.render_more({
chunk_size,
});
- };
+ }
- self.find_li = function (opts) {
+ find_li(opts) {
const key = opts.key;
// Try direct DOM lookup first for speed.
- let li = self.get_li_from_key({
+ let li = this.get_li_from_key({
key,
});
@@ -216,7 +189,7 @@ function buddy_list_create() {
return li;
}
- const pos = self.keys.indexOf(key);
+ const pos = this.keys.indexOf(key);
if (pos < 0) {
// TODO: See list_cursor.get_row() for why this is
@@ -224,74 +197,74 @@ function buddy_list_create() {
return [];
}
- self.force_render({
+ this.force_render({
pos,
});
- li = self.get_li_from_key({
+ li = this.get_li_from_key({
key,
});
return li;
- };
+ }
- self.insert_new_html = function (opts) {
+ insert_new_html(opts) {
const other_key = opts.other_key;
const html = opts.html;
const pos = opts.pos;
if (other_key === undefined) {
- if (pos === self.render_count) {
- self.render_count += 1;
- self.container.append(html);
- self.update_padding();
+ if (pos === this.render_count) {
+ this.render_count += 1;
+ this.container.append(html);
+ this.update_padding();
}
return;
}
- if (pos < self.render_count) {
- self.render_count += 1;
- const li = self.find_li({key: other_key});
+ if (pos < this.render_count) {
+ this.render_count += 1;
+ const li = this.find_li({key: other_key});
li.before(html);
- self.update_padding();
+ this.update_padding();
}
- };
+ }
- self.insert_or_move = function (opts) {
+ insert_or_move(opts) {
const key = opts.key;
const item = opts.item;
- self.maybe_remove_key({key});
+ this.maybe_remove_key({key});
- const pos = self.find_position({
+ const pos = this.find_position({
key,
});
// Order is important here--get the other_key
// before mutating our list. An undefined value
// corresponds to appending.
- const other_key = self.keys[pos];
+ const other_key = this.keys[pos];
- self.keys.splice(pos, 0, key);
+ this.keys.splice(pos, 0, key);
- const html = self.item_to_html({item});
- self.insert_new_html({
+ const html = this.item_to_html({item});
+ this.insert_new_html({
pos,
html,
other_key,
});
- };
+ }
- self.fill_screen_with_content = function () {
- let height = self.height_to_fill();
+ fill_screen_with_content() {
+ let height = this.height_to_fill();
- const elem = ui.get_scroll_element($(self.scroll_container_sel)).expectOne()[0];
+ const elem = ui.get_scroll_element($(this.scroll_container_sel)).expectOne()[0];
// Add a fudge factor.
height += 10;
- while (self.render_count < self.keys.length) {
- const padding_height = $(self.padding_sel).height();
+ while (this.render_count < this.keys.length) {
+ const padding_height = $(this.padding_sel).height();
const bottom_offset = elem.scrollHeight - elem.scrollTop - padding_height;
if (bottom_offset > height) {
@@ -300,39 +273,37 @@ function buddy_list_create() {
const chunk_size = 20;
- self.render_more({
+ this.render_more({
chunk_size,
});
}
- };
+ }
// 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);
+ container = $(this.container_sel);
- self.start_scroll_handler = function () {
+ start_scroll_handler() {
// We have our caller explicitly call this to make
// sure everything's in place.
- const scroll_container = ui.get_scroll_element($(self.scroll_container_sel));
+ const scroll_container = ui.get_scroll_element($(this.scroll_container_sel));
scroll_container.on("scroll", () => {
- self.fill_screen_with_content();
+ this.fill_screen_with_content();
});
- };
+ }
- self.update_padding = function () {
+ update_padding() {
padded_widget.update_padding({
- shown_rows: self.render_count,
- total_rows: self.keys.length,
- content_sel: self.container_sel,
- padding_sel: self.padding_sel,
+ shown_rows: this.render_count,
+ total_rows: this.keys.length,
+ content_sel: this.container_sel,
+ padding_sel: this.padding_sel,
});
- };
-
- return self;
+ }
}
-const buddy_list = buddy_list_create();
+const buddy_list = new BuddyList();
module.exports = buddy_list;