2024-04-05 12:40:49 +00:00
|
|
|
/* eslint-disable */
|
|
|
|
/*
|
|
|
|
Heavily modified Clusterize.js v1.0.0.
|
|
|
|
Original: http://NeXTs.github.com/Clusterize.js/
|
|
|
|
|
|
|
|
This has been modified to allow for an asynchronous data loader implementation.
|
|
|
|
This differs from the original Clusterize.js which would store the entire dataset
|
|
|
|
in an array and load from that; this caused a large memory overhead in the client.
|
|
|
|
*/
|
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
// Many operations can be lenghty. Try to limit their frequency by debouncing.
|
2024-04-05 12:40:49 +00:00
|
|
|
const SCROLL_DEBOUNCE_TIME_MS = 50;
|
2024-04-18 17:09:58 +00:00
|
|
|
const RESIZE_OBSERVER_DEBOUNCE_TIME_MS = 100; // should be <= refresh debounce time
|
2024-04-05 12:40:49 +00:00
|
|
|
const ELEMENT_OBSERVER_DEBOUNCE_TIME_MS = 100;
|
2024-04-18 17:09:58 +00:00
|
|
|
const REFRESH_DEBOUNCE_TIME_MS = 100;
|
2024-04-05 12:40:49 +00:00
|
|
|
|
|
|
|
class Clusterize {
|
|
|
|
scroll_elem = null;
|
|
|
|
content_elem = null;
|
|
|
|
scroll_id = null;
|
|
|
|
content_id = null;
|
2024-04-09 11:19:07 +00:00
|
|
|
options = {
|
|
|
|
rows_in_block: 50,
|
|
|
|
cols_in_block: 1,
|
|
|
|
blocks_in_cluster: 5,
|
2024-05-03 17:13:25 +00:00
|
|
|
rows_in_cluster: 50 * 5, // default is rows_in_block * blocks_in_cluster
|
2024-05-03 18:41:27 +00:00
|
|
|
tag: "div",
|
2024-04-12 16:40:20 +00:00
|
|
|
id_attr: "data-div-id",
|
2024-05-03 17:13:25 +00:00
|
|
|
no_data_class: "clusterize-no-data",
|
2024-05-03 18:41:27 +00:00
|
|
|
no_data_html: "No Data",
|
|
|
|
error_class: "clusterize-error",
|
|
|
|
error_html: "Data Error",
|
2024-04-09 11:19:07 +00:00
|
|
|
show_no_data_row: true,
|
|
|
|
keep_parity: true,
|
2024-04-09 21:26:56 +00:00
|
|
|
callbacks: {},
|
2024-04-09 11:19:07 +00:00
|
|
|
};
|
2024-04-09 21:26:56 +00:00
|
|
|
setup_has_run = false;
|
2024-04-12 16:40:20 +00:00
|
|
|
enabled = false;
|
2024-04-05 12:40:49 +00:00
|
|
|
#is_mac = null;
|
|
|
|
#ie = null;
|
2024-04-09 11:19:07 +00:00
|
|
|
#max_items = null;
|
|
|
|
#max_rows = null;
|
2024-04-05 12:40:49 +00:00
|
|
|
#cache = {};
|
|
|
|
#scroll_top = 0;
|
|
|
|
#last_cluster = false;
|
|
|
|
#scroll_debounce = 0;
|
2024-04-12 16:40:20 +00:00
|
|
|
#refresh_debounce_timer = null;
|
2024-04-05 12:40:49 +00:00
|
|
|
#resize_observer = null;
|
|
|
|
#resize_observer_timer = null;
|
|
|
|
#element_observer = null;
|
|
|
|
#element_observer_timer = null;
|
|
|
|
#pointer_events_set = false;
|
2024-04-14 18:43:31 +00:00
|
|
|
#on_scroll_bound;
|
2024-04-05 12:40:49 +00:00
|
|
|
|
|
|
|
constructor(args) {
|
2024-04-09 11:19:07 +00:00
|
|
|
for (const option of Object.keys(this.options)) {
|
|
|
|
if (keyExists(args, option)) {
|
|
|
|
this.options[option] = args[option];
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 12:40:49 +00:00
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
if (isNullOrUndefined(this.options.callbacks.initData)) {
|
2024-04-14 18:43:31 +00:00
|
|
|
this.options.callbacks.initData = this.initDataDefaultCallback.bind(this);
|
2024-04-09 11:19:07 +00:00
|
|
|
}
|
2024-04-09 21:26:56 +00:00
|
|
|
if (isNullOrUndefined(this.options.callbacks.fetchData)) {
|
2024-04-14 18:43:31 +00:00
|
|
|
this.options.callbacks.fetchData = this.fetchDataDefaultCallback.bind(this);
|
2024-04-09 11:19:07 +00:00
|
|
|
}
|
2024-04-09 21:26:56 +00:00
|
|
|
if (isNullOrUndefined(this.options.callbacks.sortData)) {
|
2024-04-14 18:43:31 +00:00
|
|
|
this.options.callbacks.sortData = this.sortDataDefaultCallback.bind(this);
|
2024-04-09 11:19:07 +00:00
|
|
|
}
|
2024-04-09 21:26:56 +00:00
|
|
|
if (isNullOrUndefined(this.options.callbacks.filterData)) {
|
2024-04-14 18:43:31 +00:00
|
|
|
this.options.callbacks.filterData = this.filterDataDefaultCallback.bind(this);
|
2024-04-09 11:19:07 +00:00
|
|
|
}
|
2024-04-05 12:40:49 +00:00
|
|
|
|
|
|
|
// detect ie9 and lower
|
|
|
|
// https://gist.github.com/padolsey/527683#comment-786682
|
|
|
|
this.#ie = (function () {
|
|
|
|
for (var v = 3,
|
2024-04-09 11:19:07 +00:00
|
|
|
el = document.createElement("b"),
|
2024-04-05 12:40:49 +00:00
|
|
|
all = el.all || [];
|
2024-04-09 11:19:07 +00:00
|
|
|
el.innerHTML = `<!--[if gt IE ${++v}]><i><![endif]-->`,
|
2024-04-05 12:40:49 +00:00
|
|
|
all[0];
|
|
|
|
) { }
|
|
|
|
return v > 4 ? v : document.documentMode;
|
|
|
|
}())
|
2024-04-09 11:19:07 +00:00
|
|
|
this.#is_mac = navigator.platform.toLowerCase().indexOf("mac") + 1;
|
2024-04-05 12:40:49 +00:00
|
|
|
|
|
|
|
this.scroll_elem = args["scrollId"] ? document.getElementById(args["scrollId"]) : args["scrollElem"];
|
2024-04-09 11:19:07 +00:00
|
|
|
isElementThrowError(this.scroll_elem);
|
2024-04-05 12:40:49 +00:00
|
|
|
this.scroll_id = this.scroll_elem.id;
|
|
|
|
|
|
|
|
this.content_elem = args["contentId"] ? document.getElementById(args["contentId"]) : args["contentElem"];
|
2024-04-09 11:19:07 +00:00
|
|
|
isElementThrowError(this.content_elem);
|
2024-04-05 12:40:49 +00:00
|
|
|
this.content_id = this.content_elem.id;
|
|
|
|
|
|
|
|
if (!this.content_elem.hasAttribute("tabindex")) {
|
|
|
|
this.content_elem.setAttribute("tabindex", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#scroll_top = this.scroll_elem.scrollTop;
|
|
|
|
|
2024-04-09 11:19:07 +00:00
|
|
|
this.#max_items = args.max_items;
|
2024-04-14 18:43:31 +00:00
|
|
|
|
|
|
|
this.#on_scroll_bound = this.#onScroll.bind(this);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ==== PUBLIC FUNCTIONS ====
|
2024-04-12 16:40:20 +00:00
|
|
|
enable(state) {
|
|
|
|
// if no state is passed, we enable by default.
|
|
|
|
this.enabled = state !== false;
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
async setup() {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (this.setup_has_run || !this.enabled) {
|
2024-04-09 21:26:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
this.#fixElementReferences();
|
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
await this.#insertToDOM();
|
|
|
|
this.scroll_elem.scrollTop = this.#scroll_top;
|
|
|
|
|
2024-04-14 18:43:31 +00:00
|
|
|
this.#setupEvent("scroll", this.scroll_elem, this.#on_scroll_bound);
|
2024-04-05 12:40:49 +00:00
|
|
|
this.#setupElementObservers();
|
|
|
|
this.#setupResizeObservers();
|
2024-04-09 21:26:56 +00:00
|
|
|
|
|
|
|
this.setup_has_run = true;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 17:13:25 +00:00
|
|
|
clear() {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.setup_has_run || !this.enabled) {
|
2024-04-09 21:26:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-03 17:13:25 +00:00
|
|
|
this.#html(this.#generateEmptyRow().join(""));
|
2024-04-09 11:19:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-14 18:43:31 +00:00
|
|
|
destroy() {
|
|
|
|
this.#teardownEvent("scroll", this.scroll_elem, this.#on_scroll_bound);
|
2024-04-05 12:40:49 +00:00
|
|
|
this.#teardownElementObservers();
|
|
|
|
this.#teardownResizeObservers();
|
2024-04-09 21:26:56 +00:00
|
|
|
|
2024-05-03 17:13:25 +00:00
|
|
|
this.#html(this.#generateEmptyRow().join(""));
|
2024-04-14 18:43:31 +00:00
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
this.setup_has_run = false;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 11:19:07 +00:00
|
|
|
async refresh(force) {
|
2024-05-03 18:41:27 +00:00
|
|
|
if (!this.setup_has_run || !this.enabled) {
|
2024-04-09 21:26:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-05-03 18:41:27 +00:00
|
|
|
|
2024-05-03 17:21:56 +00:00
|
|
|
// Refresh can be a longer operation so we want to debounce it to
|
|
|
|
// avoid refreshing too often.
|
2024-04-12 16:40:20 +00:00
|
|
|
clearTimeout(this.#refresh_debounce_timer);
|
|
|
|
this.#refresh_debounce_timer = setTimeout(
|
|
|
|
async () => {
|
2024-05-03 18:41:27 +00:00
|
|
|
if (!isElement(this.content_elem.offsetParent)) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
|
|
|
|
if (this.#recalculateDims() || force) {
|
|
|
|
await this.update()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
REFRESH_DEBOUNCE_TIME_MS,
|
|
|
|
)
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async update() {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.setup_has_run || !this.enabled) {
|
2024-04-09 21:26:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
this.#scroll_top = this.scroll_elem.scrollTop;
|
|
|
|
// fixes #39
|
2024-04-09 11:19:07 +00:00
|
|
|
if (this.#max_rows * this.options.item_height < this.#scroll_top) {
|
2024-04-05 12:40:49 +00:00
|
|
|
this.scroll_elem.scrollTop = 0;
|
|
|
|
this.#last_cluster = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.#insertToDOM();
|
|
|
|
this.scroll_elem.scrollTop = this.#scroll_top;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsAmount() {
|
2024-04-09 11:19:07 +00:00
|
|
|
return this.#max_rows;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getScrollProgress() {
|
2024-04-09 11:19:07 +00:00
|
|
|
return this.options.scroll_top / (this.#max_rows * this.options.item_height) * 100 || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setMaxItems(max_items) {
|
2024-04-21 16:07:53 +00:00
|
|
|
/** Sets the new max number of items.
|
|
|
|
*
|
|
|
|
* This is used to control the scroll bar's length.
|
|
|
|
*
|
|
|
|
* Returns whether the number of max items changed.
|
|
|
|
*/
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.setup_has_run || !this.enabled) {
|
2024-04-09 21:26:56 +00:00
|
|
|
this.#max_items = max_items;
|
2024-04-21 16:07:53 +00:00
|
|
|
return this.#max_items !== max_items;
|
2024-04-09 21:26:56 +00:00
|
|
|
}
|
2024-04-22 18:01:19 +00:00
|
|
|
|
2024-04-09 11:19:07 +00:00
|
|
|
this.#max_items = max_items;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
// ==== PRIVATE FUNCTIONS ====
|
|
|
|
initDataDefaultCallback() {
|
|
|
|
return Promise.resolve({});
|
|
|
|
}
|
2024-04-09 11:19:07 +00:00
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
async initData() {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-14 18:43:31 +00:00
|
|
|
return await this.options.callbacks.initData();
|
2024-04-09 21:26:56 +00:00
|
|
|
}
|
2024-04-09 11:19:07 +00:00
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
fetchDataDefaultCallback() {
|
|
|
|
return Promise.resolve([]);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
async fetchData(idx_start, idx_end) {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-18 17:09:58 +00:00
|
|
|
try {
|
|
|
|
return await this.options.callbacks.fetchData(idx_start, idx_end);
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
2024-04-09 21:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sortDataDefaultCallback() {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
async sortData() {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.setup_has_run || !this.enabled) {
|
2024-04-09 11:19:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-05 12:40:49 +00:00
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
this.#fixElementReferences();
|
|
|
|
|
2024-04-09 11:19:07 +00:00
|
|
|
// Sort is applied to the filtered data.
|
2024-04-14 18:43:31 +00:00
|
|
|
await this.options.callbacks.sortData();
|
2024-04-12 16:40:20 +00:00
|
|
|
this.#recalculateDims();
|
2024-04-05 12:40:49 +00:00
|
|
|
await this.#insertToDOM();
|
|
|
|
}
|
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
filterDataDefaultCallback() {
|
|
|
|
return Promise.resolve(0);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
async filterData() {
|
2024-04-12 16:40:20 +00:00
|
|
|
if (!this.setup_has_run || !this.enabled) {
|
2024-04-09 21:26:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-05 12:40:49 +00:00
|
|
|
|
2024-04-09 21:26:56 +00:00
|
|
|
// Filter is applied to entire dataset.
|
2024-04-14 18:43:31 +00:00
|
|
|
const max_items = await this.options.callbacks.filterData();
|
2024-04-09 21:26:56 +00:00
|
|
|
await this.setMaxItems(max_items);
|
2024-04-22 18:01:19 +00:00
|
|
|
await this.refresh(true);
|
|
|
|
await this.sortData();
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#exploreEnvironment(rows, cache) {
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.content_tag = this.content_elem.tagName.toLowerCase();
|
2024-04-09 21:26:56 +00:00
|
|
|
if (isNullOrUndefined(rows) || !rows.length) {
|
2024-04-05 12:40:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-09 11:19:07 +00:00
|
|
|
if (this.#ie && this.#ie <= 9 && !this.options.tag) {
|
|
|
|
this.options.tag = rows[0].match(/<([^>\s/]*)/)[1].toLowerCase();
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
2024-05-03 17:13:25 +00:00
|
|
|
// Temporarily add one row so that we can calculate row dimensions.
|
2024-04-05 12:40:49 +00:00
|
|
|
if (this.content_elem.children.length <= 1) {
|
2024-04-16 16:43:30 +00:00
|
|
|
cache.data = this.#html(rows[0]);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
2024-04-09 11:19:07 +00:00
|
|
|
if (!this.options.tag) {
|
|
|
|
this.options.tag = this.content_elem.children[0].tagName.toLowerCase();
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
this.#recalculateDims();
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
#recalculateDims() {
|
|
|
|
const prev_options = JSON.stringify(this.options);
|
2024-04-09 11:19:07 +00:00
|
|
|
|
|
|
|
this.options.cluster_height = 0;
|
|
|
|
this.options.cluster_width = 0;
|
2024-04-12 16:40:20 +00:00
|
|
|
|
2024-04-09 11:19:07 +00:00
|
|
|
if (!this.#max_items) {
|
2024-04-05 12:40:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
// Get the first element that isn't one of our placeholder rows.
|
2024-05-02 19:51:52 +00:00
|
|
|
const node = this.content_elem.querySelector(
|
2024-05-03 17:13:25 +00:00
|
|
|
`:scope > :not(.clusterize-extra-row,.${this.options.no_data_class})`
|
2024-05-02 19:51:52 +00:00
|
|
|
);
|
2024-04-15 13:31:30 +00:00
|
|
|
if (!isElement(node)) {
|
|
|
|
// dont attempt to compute dims if we have no data.
|
2024-04-05 12:40:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-09 11:19:07 +00:00
|
|
|
|
|
|
|
const node_dims = getComputedDims(node);
|
|
|
|
this.options.item_height = node_dims.height;
|
|
|
|
this.options.item_width = node_dims.width;
|
2024-04-12 16:40:20 +00:00
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
// consider table's browser spacing
|
2024-04-12 16:40:20 +00:00
|
|
|
if (this.options.tag === "tr" && getComputedProperty(this.content_elem, "borderCollapse") !== "collapse") {
|
|
|
|
const spacing = parseInt(getComputedProperty(this.content_elem, "borderSpacing"), 10) || 0;
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.item_height += spacing;
|
|
|
|
this.options.item_width += spacing;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
|
|
|
|
// Update rows in block to match the number of elements that can fit in the view.
|
|
|
|
const content_padding = getComputedPaddingDims(this.content_elem);
|
2024-04-14 18:43:31 +00:00
|
|
|
const column_gap = parseFloat(getComputedProperty(this.content_elem, "column-gap"));
|
|
|
|
const row_gap = parseFloat(getComputedProperty(this.content_elem, "row-gap"));
|
|
|
|
if (isNumber(column_gap)) {
|
|
|
|
this.options.item_width += column_gap;
|
|
|
|
}
|
|
|
|
if (isNumber(row_gap)) {
|
|
|
|
this.options.item_height += row_gap;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
const inner_width = this.scroll_elem.clientWidth - content_padding.width;
|
|
|
|
const inner_height = this.scroll_elem.clientHeight - content_padding.height;
|
|
|
|
// Since we don't allow horizontal scrolling, we want to round down for columns.
|
|
|
|
const cols_in_block = Math.floor(inner_width / this.options.item_width);
|
|
|
|
// Round up for rows so that we don't cut rows off from the view.
|
|
|
|
const rows_in_block = Math.ceil(inner_height / this.options.item_height);
|
|
|
|
|
|
|
|
// Always need at least 1 row/col in block
|
|
|
|
this.options.cols_in_block = Math.max(1, cols_in_block);
|
|
|
|
this.options.rows_in_block = Math.max(1, rows_in_block);
|
2024-04-09 11:19:07 +00:00
|
|
|
|
|
|
|
this.options.block_height = this.options.item_height * this.options.rows_in_block;
|
|
|
|
this.options.block_width = this.options.item_width * this.options.cols_in_block;
|
|
|
|
this.options.rows_in_cluster = this.options.blocks_in_cluster * this.options.rows_in_block;
|
|
|
|
this.options.cluster_height = this.options.blocks_in_cluster * this.options.block_height;
|
|
|
|
this.options.cluster_width = this.options.block_width;
|
|
|
|
|
2024-04-12 16:40:20 +00:00
|
|
|
this.#max_rows = Math.ceil(this.#max_items / this.options.cols_in_block, 10);
|
2024-04-09 11:19:07 +00:00
|
|
|
|
2024-04-18 17:09:58 +00:00
|
|
|
return prev_options !== JSON.stringify(this.options);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 18:41:27 +00:00
|
|
|
#generateEmptyRow({is_error}={}) {
|
|
|
|
const row = document.createElement(is_error ? "div" : this.options.tag);
|
|
|
|
row.className = is_error ? this.options.error_class : this.options.no_data_class;
|
2024-05-03 17:13:25 +00:00
|
|
|
if (this.options.tag === "tr") {
|
|
|
|
const td = document.createElement("td");
|
|
|
|
td.colSpan = 100;
|
2024-05-03 18:41:27 +00:00
|
|
|
td.innerHTML = is_error ? this.options.error_html : this.options.no_data_html;
|
2024-05-03 17:13:25 +00:00
|
|
|
row.appendChild(td);
|
|
|
|
} else {
|
2024-05-03 18:41:27 +00:00
|
|
|
row.innerHTML = is_error ? this.options.error_html : this.options.no_data_html;
|
2024-05-03 17:13:25 +00:00
|
|
|
}
|
|
|
|
return [row.outerHTML];
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
#getClusterNum() {
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.scroll_top = this.scroll_elem.scrollTop;
|
|
|
|
const cluster_divider = this.options.cluster_height - this.options.block_height;
|
|
|
|
const current_cluster = Math.floor(this.options.scroll_top / cluster_divider);
|
|
|
|
const max_cluster = Math.floor((this.#max_rows * this.options.item_height) / cluster_divider);
|
2024-04-05 12:40:49 +00:00
|
|
|
return Math.min(current_cluster, max_cluster);
|
|
|
|
}
|
|
|
|
|
|
|
|
async #generate() {
|
2024-04-09 11:19:07 +00:00
|
|
|
const rows_start = Math.max(0, (this.options.rows_in_cluster - this.options.rows_in_block) * this.#getClusterNum());
|
|
|
|
const rows_end = rows_start + this.options.rows_in_cluster;
|
|
|
|
const top_offset = Math.max(0, rows_start * this.options.item_height);
|
|
|
|
const bottom_offset = Math.max(0, (this.#max_rows - rows_end) * this.options.item_height);
|
|
|
|
const rows_above = top_offset < 1 ? rows_start + 1 : rows_start;
|
|
|
|
|
|
|
|
const idx_start = Math.max(0, rows_start * this.options.cols_in_block);
|
2024-04-12 16:40:20 +00:00
|
|
|
const idx_end = Math.min(this.#max_items, rows_end * this.options.cols_in_block);
|
|
|
|
|
2024-04-18 17:09:58 +00:00
|
|
|
let this_cluster_rows = await this.fetchData(idx_start, idx_end);
|
|
|
|
if (!Array.isArray(this_cluster_rows) || !this_cluster_rows.length) {
|
|
|
|
console.error(`Failed to fetch data for idx range (${idx_start},${idx_end})`);
|
|
|
|
this_cluster_rows = [];
|
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
|
|
|
|
if (this_cluster_rows.length < this.options.rows_in_block) {
|
|
|
|
return {
|
|
|
|
top_offset: 0,
|
|
|
|
bottom_offset: 0,
|
|
|
|
rows_above: 0,
|
2024-05-03 18:41:27 +00:00
|
|
|
rows: this_cluster_rows.length ? this_cluster_rows : this.#generateEmptyRow({is_error: true}),
|
2024-04-12 16:40:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
return {
|
|
|
|
top_offset: top_offset,
|
|
|
|
bottom_offset: bottom_offset,
|
|
|
|
rows_above: rows_above,
|
2024-04-12 16:40:20 +00:00
|
|
|
rows: this_cluster_rows,
|
2024-04-05 12:40:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async #insertToDOM() {
|
2024-04-09 11:19:07 +00:00
|
|
|
if (!this.options.cluster_height || !this.options.cluster_width) {
|
2024-04-19 20:11:47 +00:00
|
|
|
// We need to fetch a single item so that we can calculate the dimensions
|
|
|
|
// for our list.
|
2024-04-09 21:26:56 +00:00
|
|
|
const rows = await this.fetchData(0, 1);
|
2024-04-18 17:09:58 +00:00
|
|
|
if (!Array.isArray(rows) || !rows.length) {
|
2024-04-19 20:11:47 +00:00
|
|
|
// This implies there is no data for this list. Not an error.
|
|
|
|
// Errors should be handled in the fetchData callback, not here.
|
2024-05-03 17:13:25 +00:00
|
|
|
this.#html(this.#generateEmptyRow().join(""));
|
2024-04-18 17:09:58 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2024-05-03 17:13:25 +00:00
|
|
|
this.#html(rows.join(""));
|
2024-04-18 17:09:58 +00:00
|
|
|
this.#exploreEnvironment(rows, this.#cache);
|
2024-04-19 20:11:47 +00:00
|
|
|
// Remove the temporary item from the data since we calculated its size.
|
2024-05-03 17:13:25 +00:00
|
|
|
this.#html(this.#generateEmptyRow().join(""));
|
2024-04-18 17:09:58 +00:00
|
|
|
}
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = await this.#generate();
|
2024-04-09 11:19:07 +00:00
|
|
|
let this_cluster_rows = [];
|
|
|
|
for (let i = 0; i < data.rows.length; i += this.options.cols_in_block) {
|
|
|
|
const new_row = data.rows.slice(i, i + this.options.cols_in_block).join("");
|
2024-04-12 16:40:20 +00:00
|
|
|
this_cluster_rows.push(new_row);
|
2024-04-09 11:19:07 +00:00
|
|
|
}
|
|
|
|
this_cluster_rows = this_cluster_rows.join("");
|
2024-04-05 12:40:49 +00:00
|
|
|
const this_cluster_content_changed = this.#checkChanges("data", this_cluster_rows, this.#cache);
|
|
|
|
const top_offset_changed = this.#checkChanges("top", data.top_offset, this.#cache);
|
|
|
|
const only_bottom_offset_changed = this.#checkChanges("bottom", data.bottom_offset, this.#cache);
|
|
|
|
const layout = [];
|
|
|
|
|
|
|
|
if (this_cluster_content_changed || top_offset_changed) {
|
|
|
|
if (data.top_offset) {
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.keep_parity && layout.push(this.#renderExtraTag("keep-parity"));
|
2024-04-05 12:40:49 +00:00
|
|
|
layout.push(this.#renderExtraTag("top-space", data.top_offset));
|
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
layout.push(this_cluster_rows);
|
|
|
|
data.bottom_offset && layout.push(this.#renderExtraTag("bottom-space", data.bottom_offset));
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.callbacks.clusterWillChange && this.options.callbacks.clusterWillChange();
|
2024-04-05 12:40:49 +00:00
|
|
|
this.#html(layout.join(""));
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.content_tag === "ol" && this.content_elem.setAttribute("start", data.rows_above);
|
2024-04-05 12:40:49 +00:00
|
|
|
this.content_elem.style["counter-increment"] = `clusterize-counter ${data.rows_above - 1}`;
|
2024-04-09 11:19:07 +00:00
|
|
|
this.options.callbacks.clusterChanged && this.options.callbacks.clusterChanged();
|
2024-04-05 12:40:49 +00:00
|
|
|
} else if (only_bottom_offset_changed) {
|
2024-04-12 16:40:20 +00:00
|
|
|
this.content_elem.lastElementChild.style.height = `${data.bottom_offset}px`;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#html(data) {
|
|
|
|
const content_elem = this.content_elem;
|
2024-04-09 11:19:07 +00:00
|
|
|
if (this.#ie && this.#ie <= 9 && this.options.tag === "tr") {
|
2024-04-05 12:40:49 +00:00
|
|
|
const div = document.createElement("div");
|
|
|
|
let last;
|
|
|
|
div.innerHTML = `<table><tbody>${data}</tbody></table>`;
|
2024-04-12 16:40:20 +00:00
|
|
|
while ((last = content_elem.lastElementChild)) {
|
2024-04-05 12:40:49 +00:00
|
|
|
content_elem.removeChild(last);
|
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
const rows_nodes = this.#getChildNodes(div.firstElementChild.firstElementChild);
|
2024-04-05 12:40:49 +00:00
|
|
|
while (rows_nodes.length) {
|
|
|
|
content_elem.appendChild(rows_nodes.shift());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content_elem.innerHTML = data;
|
|
|
|
}
|
2024-05-03 17:13:25 +00:00
|
|
|
return content_elem.innerHTML;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#renderExtraTag(class_name, height) {
|
2024-04-09 11:19:07 +00:00
|
|
|
const tag = document.createElement(this.options.tag);
|
2024-04-05 12:40:49 +00:00
|
|
|
const clusterize_prefix = "clusterize-";
|
|
|
|
tag.className = [
|
|
|
|
`${clusterize_prefix}extra-row`,
|
|
|
|
`${clusterize_prefix}${class_name}`,
|
|
|
|
].join(" ");
|
|
|
|
height && (tag.style.height = `${height}px`);
|
|
|
|
return tag.outerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
#getChildNodes(tag) {
|
|
|
|
const child_nodes = tag.children;
|
|
|
|
const nodes = [];
|
|
|
|
for (let i = 0, j = child_nodes.length; i < j; i++) {
|
|
|
|
nodes.push(child_nodes[i]);
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
#checkChanges(type, value, cache) {
|
|
|
|
const changed = value !== cache[type];
|
|
|
|
cache[type] = value;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==== EVENT HANDLERS ====
|
|
|
|
|
|
|
|
async #onScroll() {
|
|
|
|
if (this.#is_mac) {
|
|
|
|
if (!this.#pointer_events_set) {
|
|
|
|
this.content_elem.style.pointerEvents = "none";
|
|
|
|
this.#pointer_events_set = true;
|
|
|
|
clearTimeout(this.#scroll_debounce);
|
|
|
|
this.#scroll_debounce = setTimeout(() => {
|
|
|
|
this.content_elem.style.pointerEvents = "auto";
|
|
|
|
this.#pointer_events_set = false;
|
|
|
|
}, SCROLL_DEBOUNCE_TIME_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.#last_cluster !== (this.#last_cluster = this.#getClusterNum())) {
|
|
|
|
await this.#insertToDOM();
|
|
|
|
}
|
2024-04-09 11:19:07 +00:00
|
|
|
if (this.options.callbacks.scrollingProgress) {
|
|
|
|
this.options.callbacks.scrollingProgress(this.getScrollProgress());
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async #onResize() {
|
2024-04-18 17:09:58 +00:00
|
|
|
await this.refresh();
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#fixElementReferences() {
|
|
|
|
if (!isElement(this.scroll_elem) || !isElement(this.content_elem)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-03 18:41:27 +00:00
|
|
|
// Element is already in DOM. Don't need to do anything.
|
|
|
|
if (isElement(this.content_elem.offsetParent)) {
|
2024-04-12 16:40:20 +00:00
|
|
|
return;
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
2024-04-12 16:40:20 +00:00
|
|
|
|
|
|
|
// If association for elements is broken, replace them with instance version.
|
|
|
|
document.getElementById(this.scroll_id).replaceWith(this.scroll_elem);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#setupElementObservers() {
|
|
|
|
/** Listens for changes to the scroll and content elements.
|
|
|
|
*
|
|
|
|
* During testing, the scroll/content elements would frequently get removed from
|
|
|
|
* the DOM. This instance stores a reference to these elements
|
|
|
|
* which breaks whenever these elements are removed from the DOM. To fix this,
|
|
|
|
* we need to check for these changes and re-attach our stores elements by
|
|
|
|
* replacing the ones in the DOM with the ones in our clusterize instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.#element_observer = new MutationObserver((mutations) => {
|
|
|
|
const scroll_elem = document.getElementById(this.scroll_id);
|
|
|
|
if (isElement(scroll_elem) && scroll_elem !== this.scroll_elem) {
|
|
|
|
clearTimeout(this.#element_observer_timer);
|
|
|
|
this.#element_observer_timer = setTimeout(
|
|
|
|
this.#fixElementReferences,
|
|
|
|
ELEMENT_OBSERVER_DEBOUNCE_TIME_MS,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const content_elem = document.getElementById(this.content_id);
|
|
|
|
if (isElement(content_elem) && content_elem !== this.content_elem) {
|
|
|
|
clearTimeout(this.#element_observer_timer);
|
|
|
|
this.#element_observer_timer = setTimeout(
|
|
|
|
this.#fixElementReferences,
|
|
|
|
ELEMENT_OBSERVER_DEBOUNCE_TIME_MS,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const options = { subtree: true, childList: true, attributes: true };
|
|
|
|
this.#element_observer.observe(document, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
#teardownElementObservers() {
|
|
|
|
if (!isNullOrUndefined(this.#element_observer)) {
|
|
|
|
this.#element_observer.takeRecords();
|
|
|
|
this.#element_observer.disconnect();
|
|
|
|
}
|
|
|
|
this.#element_observer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
#setupResizeObservers() {
|
|
|
|
/** Handles any updates to the size of both the Scroll and Content elements. */
|
|
|
|
this.#resize_observer = new ResizeObserver((entries) => {
|
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.target.id === this.scroll_id || entry.target.id === this.content_id) {
|
|
|
|
// debounce the event
|
|
|
|
clearTimeout(this.#resize_observer_timer);
|
|
|
|
this.#resize_observer_timer = setTimeout(
|
|
|
|
() => this.#onResize(),
|
|
|
|
RESIZE_OBSERVER_DEBOUNCE_TIME_MS,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.#resize_observer.observe(this.scroll_elem);
|
|
|
|
this.#resize_observer.observe(this.content_elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
#teardownResizeObservers() {
|
|
|
|
if (!isNullOrUndefined(this.#resize_observer)) {
|
|
|
|
this.#resize_observer.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNullOrUndefined(this.#resize_observer_timer)) {
|
|
|
|
clearTimeout(this.#resize_observer_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#resize_observer = null;
|
|
|
|
this.#resize_observer_timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==== HELPER FUNCTIONS ====
|
|
|
|
|
|
|
|
#setupEvent(type, elem, listener) {
|
|
|
|
if (elem.addEventListener) {
|
2024-04-14 18:43:31 +00:00
|
|
|
return elem.addEventListener(type, listener, false);
|
2024-04-05 12:40:49 +00:00
|
|
|
} else {
|
2024-04-14 18:43:31 +00:00
|
|
|
return elem.attachEvent(`on${type}`, listener);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#teardownEvent(type, elem, listener) {
|
|
|
|
if (elem.removeEventListener) {
|
2024-04-14 18:43:31 +00:00
|
|
|
return elem.removeEventListener(type, listener, false);
|
2024-04-05 12:40:49 +00:00
|
|
|
} else {
|
2024-04-14 18:43:31 +00:00
|
|
|
return elem.detachEvent(`on${type}`, listener);
|
2024-04-05 12:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|