2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const DEFAULTS = {
|
2019-10-25 09:15:16 +02:00
|
|
|
INITIAL_RENDER_COUNT: 80,
|
|
|
|
LOAD_COUNT: 20,
|
2020-02-12 08:07:21 +01:00
|
|
|
instances: new Map(),
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
|
|
|
|
2020-05-28 19:43:59 +02:00
|
|
|
// ----------------------------------------------------
|
2020-08-11 01:47:44 +02:00
|
|
|
// This function describes (programmatically) how to use
|
2020-05-28 19:43:59 +02:00
|
|
|
// the list_render widget.
|
|
|
|
// ----------------------------------------------------
|
|
|
|
|
|
|
|
exports.validate_opts = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (opts.html_selector && typeof opts.html_selector !== "function") {
|
2020-05-28 19:43:59 +02:00
|
|
|
// We have an html_selector, but it is not a function.
|
|
|
|
// This is a programming error.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("html_selector should be a function.");
|
2020-05-28 19:43:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-04 10:07:43 +02:00
|
|
|
if (!opts.simplebar_container) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("simplebar_container is missing.");
|
2020-07-04 10:07:43 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-28 19:43:59 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-05-10 12:45:16 +02:00
|
|
|
exports.get_filtered_items = (value, list, opts) => {
|
2019-12-30 16:13:42 +01:00
|
|
|
/*
|
|
|
|
This is used by the main object (see `create`),
|
|
|
|
but we split it out to make it a bit easier
|
|
|
|
to test.
|
|
|
|
*/
|
2020-05-10 12:45:16 +02:00
|
|
|
const get_item = opts.get_item;
|
|
|
|
|
2020-04-13 16:13:06 +02:00
|
|
|
if (!opts.filter) {
|
2020-05-10 12:45:16 +02:00
|
|
|
if (get_item) {
|
|
|
|
return list.map(get_item);
|
|
|
|
}
|
2020-04-13 16:13:06 +02:00
|
|
|
return [...list];
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:45:53 +01:00
|
|
|
if (opts.filter.filterer) {
|
2020-05-10 12:45:16 +02:00
|
|
|
if (get_item) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return opts.filter.filterer(list.map(get_item), value);
|
2020-05-10 12:45:16 +02:00
|
|
|
}
|
2020-01-13 17:45:53 +01:00
|
|
|
return opts.filter.filterer(list, value);
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:41:40 +02:00
|
|
|
const predicate = (item) => opts.filter.predicate(item, value);
|
2020-05-10 12:45:16 +02:00
|
|
|
|
|
|
|
if (get_item) {
|
|
|
|
const result = [];
|
2019-12-30 16:13:42 +01:00
|
|
|
|
2020-05-10 12:45:16 +02:00
|
|
|
for (const key of list) {
|
|
|
|
const item = get_item(key);
|
|
|
|
if (predicate(item)) {
|
|
|
|
result.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.filter(predicate);
|
2019-12-30 16:13:42 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.alphabetic_sort = (prop) =>
|
|
|
|
function (a, b) {
|
|
|
|
// The conversion to uppercase helps make the sorting case insensitive.
|
|
|
|
const str1 = a[prop].toUpperCase();
|
|
|
|
const str2 = b[prop].toUpperCase();
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (str1 === str2) {
|
|
|
|
return 0;
|
|
|
|
} else if (str1 > str2) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
return -1;
|
|
|
|
};
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.numeric_sort = (prop) =>
|
|
|
|
function (a, b) {
|
|
|
|
if (parseFloat(a[prop]) > parseFloat(b[prop])) {
|
|
|
|
return 1;
|
|
|
|
} else if (parseFloat(a[prop]) === parseFloat(b[prop])) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
return -1;
|
|
|
|
};
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2020-04-24 15:57:41 +02:00
|
|
|
exports.valid_filter_opts = (opts) => {
|
2020-03-10 19:48:03 +01:00
|
|
|
if (!opts.filter) {
|
2020-04-24 15:57:41 +02:00
|
|
|
return true;
|
2020-03-10 19:48:03 +01:00
|
|
|
}
|
|
|
|
if (opts.filter.predicate) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof opts.filter.predicate !== "function") {
|
|
|
|
blueslip.error("Filter predicate is not a function.");
|
2020-04-24 15:57:41 +02:00
|
|
|
return false;
|
2020-03-10 19:48:03 +01:00
|
|
|
}
|
|
|
|
if (opts.filter.filterer) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Filterer and predicate are mutually exclusive.");
|
2020-04-24 15:57:41 +02:00
|
|
|
return false;
|
2020-03-10 19:48:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof opts.filter.filterer !== "function") {
|
|
|
|
blueslip.error("Filter filterer is not a function (or missing).");
|
2020-04-24 15:57:41 +02:00
|
|
|
return false;
|
2020-03-10 19:48:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 15:57:41 +02:00
|
|
|
|
|
|
|
return true;
|
2020-03-10 19:48:03 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
// @params
|
|
|
|
// container: jQuery object to append to.
|
|
|
|
// list: The list of items to progressively append.
|
|
|
|
// opts: An object of random preferences.
|
|
|
|
exports.create = function ($container, list, opts) {
|
2020-04-24 15:38:16 +02:00
|
|
|
if (!opts) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Need opts to create widget.");
|
2020-04-24 15:38:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:43:59 +02:00
|
|
|
if (!exports.validate_opts(opts)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 08:07:21 +01:00
|
|
|
if (opts.name && DEFAULTS.instances.get(opts.name)) {
|
2020-04-11 16:23:29 +02:00
|
|
|
// Clear event handlers for prior widget.
|
|
|
|
const old_widget = DEFAULTS.instances.get(opts.name);
|
|
|
|
old_widget.clear_event_handlers();
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2019-10-25 09:15:16 +02:00
|
|
|
sorting_function: null,
|
2020-02-12 08:08:25 +01:00
|
|
|
sorting_functions: new Map(),
|
2020-04-24 19:49:18 +02:00
|
|
|
generic_sorting_functions: {
|
|
|
|
alphabetic: exports.alphabetic_sort,
|
|
|
|
numeric: exports.numeric_sort,
|
|
|
|
},
|
2019-10-25 09:15:16 +02:00
|
|
|
offset: 0,
|
2020-07-20 22:18:43 +02:00
|
|
|
list,
|
2019-10-25 09:15:16 +02:00
|
|
|
filtered_list: list,
|
2020-04-13 16:13:06 +02:00
|
|
|
reverse_mode: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
filter_value: "",
|
2017-03-16 21:00:56 +01:00
|
|
|
};
|
|
|
|
|
2020-04-24 15:57:41 +02:00
|
|
|
if (!exports.valid_filter_opts(opts)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (opts.get_item && typeof opts.get_item !== "function") {
|
|
|
|
blueslip.error("get_item should be a function");
|
2020-05-10 12:45:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
const widget = {};
|
2017-05-05 06:20:38 +02:00
|
|
|
|
2020-04-13 16:13:06 +02:00
|
|
|
widget.filter_and_sort = function () {
|
2020-07-15 00:34:28 +02:00
|
|
|
meta.filtered_list = exports.get_filtered_items(meta.filter_value, meta.list, opts);
|
2020-04-13 16:13:06 +02:00
|
|
|
|
|
|
|
if (meta.sorting_function) {
|
2020-07-15 00:34:28 +02:00
|
|
|
meta.filtered_list.sort(meta.sorting_function);
|
2020-04-13 16:13:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meta.reverse_mode) {
|
|
|
|
meta.filtered_list.reverse();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
// Reads the provided list (in the scope directly above)
|
|
|
|
// and renders the next block of messages automatically
|
|
|
|
// into the specified container.
|
2020-04-12 19:45:33 +02:00
|
|
|
widget.render = function (how_many) {
|
|
|
|
const load_count = how_many || DEFAULTS.LOAD_COUNT;
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
// Stop once the offset reaches the length of the original list.
|
|
|
|
if (meta.offset >= meta.filtered_list.length) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-05 06:20:38 +02:00
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
const slice = meta.filtered_list.slice(meta.offset, meta.offset + load_count);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const finish = blueslip.start_timing("list_render " + opts.name);
|
2020-04-11 13:52:38 +02:00
|
|
|
let html = "";
|
|
|
|
for (const item of slice) {
|
2020-04-14 23:43:14 +02:00
|
|
|
const s = opts.modifier(item);
|
2019-10-25 09:15:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof s !== "string") {
|
|
|
|
blueslip.error("List item is not a string: " + s);
|
2020-04-14 23:43:14 +02:00
|
|
|
continue;
|
2020-04-11 13:52:38 +02:00
|
|
|
}
|
2020-01-15 15:05:44 +01:00
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
// append the HTML or nothing if corrupt (null, undef, etc.).
|
2020-04-14 23:43:14 +02:00
|
|
|
if (s) {
|
|
|
|
html += s;
|
|
|
|
}
|
2020-04-11 13:52:38 +02:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
finish();
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
$container.append($(html));
|
|
|
|
meta.offset += load_count;
|
|
|
|
};
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-05-28 19:58:51 +02:00
|
|
|
widget.render_item = (item) => {
|
|
|
|
if (!opts.html_selector) {
|
|
|
|
// We don't have any way to find the existing item.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const html_item = meta.scroll_container.find(opts.html_selector(item));
|
|
|
|
if (!html_item) {
|
|
|
|
// We don't have the item in the current scroll container; it'll be
|
|
|
|
// rendered with updated data when it is scrolled to.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.get_item) {
|
|
|
|
item = opts.get_item(item);
|
|
|
|
}
|
|
|
|
const html = opts.modifier(item);
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof html !== "string") {
|
|
|
|
blueslip.error("List item is not a string: " + html);
|
2020-05-28 19:58:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we have asserted we have all the information to replace
|
|
|
|
// the html now.
|
|
|
|
html_item.replaceWith(html);
|
|
|
|
};
|
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
widget.clear = function () {
|
|
|
|
$container.html("");
|
|
|
|
meta.offset = 0;
|
|
|
|
};
|
|
|
|
|
2020-04-13 16:13:06 +02:00
|
|
|
widget.set_filter_value = function (filter_value) {
|
|
|
|
meta.filter_value = filter_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.set_reverse_mode = function (reverse_mode) {
|
|
|
|
meta.reverse_mode = reverse_mode;
|
2020-04-11 13:52:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// the sorting function is either the function or string that calls the
|
|
|
|
// function to sort the list by. The prop is used for generic functions
|
|
|
|
// that can be called to sort with a particular prop.
|
2020-04-13 16:13:06 +02:00
|
|
|
widget.set_sorting_function = function (sorting_function, prop) {
|
2020-04-11 13:52:38 +02:00
|
|
|
if (typeof sorting_function === "function") {
|
|
|
|
meta.sorting_function = sorting_function;
|
|
|
|
} else if (typeof sorting_function === "string") {
|
|
|
|
if (typeof prop === "string") {
|
|
|
|
/* eslint-disable max-len */
|
2020-04-24 19:49:18 +02:00
|
|
|
meta.sorting_function = meta.generic_sorting_functions[sorting_function](prop);
|
2020-04-11 13:52:38 +02:00
|
|
|
} else {
|
|
|
|
meta.sorting_function = meta.sorting_functions.get(sorting_function);
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2020-04-11 13:52:38 +02:00
|
|
|
}
|
|
|
|
};
|
2017-05-05 06:20:38 +02:00
|
|
|
|
2020-04-11 14:07:05 +02:00
|
|
|
widget.set_up_event_handlers = function () {
|
2020-07-04 10:07:43 +02:00
|
|
|
meta.scroll_container = ui.get_scroll_element(opts.simplebar_container);
|
2020-04-11 13:52:38 +02:00
|
|
|
|
|
|
|
// on scroll of the nearest scrolling container, if it hits the bottom
|
|
|
|
// of the container then fetch a new block of items and render them.
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.scroll_container.on("scroll.list_widget_container", function () {
|
2020-04-11 13:52:38 +02:00
|
|
|
if (this.scrollHeight - (this.scrollTop + this.clientHeight) < 10) {
|
|
|
|
widget.render();
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2020-04-11 13:52:38 +02:00
|
|
|
});
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-04-11 13:52:38 +02:00
|
|
|
if (opts.parent_container) {
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.parent_container.on("click.list_widget_sort", "[data-sort]", function () {
|
2020-04-15 12:22:23 +02:00
|
|
|
exports.handle_sort($(this), widget);
|
|
|
|
});
|
2020-04-11 13:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.filter && opts.filter.element) {
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.filter.element.on("input.list_widget_filter", function () {
|
2020-04-11 14:07:05 +02:00
|
|
|
const value = this.value.toLocaleLowerCase();
|
2020-04-13 16:13:06 +02:00
|
|
|
widget.set_filter_value(value);
|
|
|
|
widget.hard_redraw();
|
2020-04-11 13:52:38 +02:00
|
|
|
});
|
|
|
|
}
|
2020-04-11 16:23:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
widget.clear_event_handlers = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.scroll_container.off("scroll.list_widget_container");
|
2020-04-11 16:23:29 +02:00
|
|
|
|
|
|
|
if (opts.parent_container) {
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.parent_container.off("click.list_widget_sort", "[data-sort]");
|
2020-04-11 16:23:29 +02:00
|
|
|
}
|
2020-04-13 11:55:25 +02:00
|
|
|
|
2020-04-11 16:23:29 +02:00
|
|
|
if (opts.filter && opts.filter.element) {
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.filter.element.off("input.list_widget_filter");
|
2020-04-11 16:23:29 +02:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-04-13 16:13:06 +02:00
|
|
|
widget.sort = function (sorting_function, prop) {
|
|
|
|
widget.set_sorting_function(sorting_function, prop);
|
|
|
|
widget.hard_redraw();
|
|
|
|
};
|
|
|
|
|
2020-04-11 16:23:29 +02:00
|
|
|
widget.clean_redraw = function () {
|
2020-04-13 16:13:06 +02:00
|
|
|
widget.filter_and_sort();
|
|
|
|
widget.clear();
|
|
|
|
widget.render(DEFAULTS.INITIAL_RENDER_COUNT);
|
2020-04-11 16:23:29 +02:00
|
|
|
};
|
2020-04-13 16:13:06 +02:00
|
|
|
|
2020-04-11 16:23:29 +02:00
|
|
|
widget.hard_redraw = function () {
|
|
|
|
widget.clean_redraw();
|
2020-04-13 16:13:06 +02:00
|
|
|
if (opts.filter && opts.filter.onupdate) {
|
|
|
|
opts.filter.onupdate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-15 01:29:34 +02:00
|
|
|
widget.replace_list_data = function (list) {
|
|
|
|
/*
|
|
|
|
We mostly use this widget for lists where you are
|
|
|
|
not adding or removing rows, so when you do modify
|
|
|
|
the list, we have a brute force solution.
|
|
|
|
*/
|
|
|
|
meta.list = list;
|
|
|
|
widget.hard_redraw();
|
|
|
|
};
|
|
|
|
|
2020-04-11 14:07:05 +02:00
|
|
|
widget.set_up_event_handlers();
|
|
|
|
|
2020-04-11 16:23:29 +02:00
|
|
|
if (opts.sort_fields) {
|
|
|
|
for (const [name, sorting_function] of Object.entries(opts.sort_fields)) {
|
|
|
|
meta.sorting_functions.set(name, sorting_function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.init_sort) {
|
|
|
|
widget.set_sorting_function(...opts.init_sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.clean_redraw();
|
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
// Save the instance for potential future retrieval if a name is provided.
|
|
|
|
if (opts.name) {
|
2020-04-11 13:44:34 +02:00
|
|
|
DEFAULTS.instances.set(opts.name, widget);
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-04-11 13:44:34 +02:00
|
|
|
return widget;
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
exports.get = function (name) {
|
2020-02-12 08:07:21 +01:00
|
|
|
return DEFAULTS.instances.get(name) || false;
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2017-03-16 21:00:56 +01:00
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
exports.handle_sort = function (th, list) {
|
2019-10-25 09:15:16 +02:00
|
|
|
/*
|
2018-06-21 06:58:19 +02:00
|
|
|
one would specify sort parameters like this:
|
|
|
|
- name => sort alphabetic.
|
|
|
|
- age => sort numeric.
|
2020-04-15 12:22:23 +02:00
|
|
|
- status => look up `status` in sort_fields
|
|
|
|
to find custom sort function
|
|
|
|
|
|
|
|
<thead>
|
|
|
|
<th data-sort="alphabetic" data-sort-prop="name"></th>
|
|
|
|
<th data-sort="numeric" data-sort-prop="age"></th>
|
|
|
|
<th data-sort="status"></th>
|
|
|
|
</thead>
|
2018-06-21 06:58:19 +02:00
|
|
|
*/
|
2020-04-15 12:22:23 +02:00
|
|
|
const sort_type = th.data("sort");
|
|
|
|
const prop_name = th.data("sort-prop");
|
2019-10-25 09:15:16 +02:00
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
if (th.hasClass("active")) {
|
|
|
|
if (!th.hasClass("descend")) {
|
|
|
|
th.addClass("descend");
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2020-04-15 12:22:23 +02:00
|
|
|
th.removeClass("descend");
|
2017-09-28 23:51:34 +02:00
|
|
|
}
|
2020-04-13 16:13:06 +02:00
|
|
|
} else {
|
2020-04-15 12:22:23 +02:00
|
|
|
th.siblings(".active").removeClass("active");
|
|
|
|
th.addClass("active");
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2017-10-22 19:58:08 +02:00
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
list.set_reverse_mode(th.hasClass("descend"));
|
2020-04-13 16:13:06 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
// if `prop_name` is defined, it will trigger the generic codepath,
|
|
|
|
// and not if it is undefined.
|
|
|
|
list.sort(sort_type, prop_name);
|
|
|
|
};
|
2018-06-21 06:58:19 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.list_render = exports;
|