update to use new clusterize class

This commit is contained in:
Sj-Si 2024-04-09 07:19:07 -04:00
parent 806bbff5a5
commit ec80d57e8b
6 changed files with 1636 additions and 2037 deletions

View file

@ -5,29 +5,29 @@ const isNumberLogError = x => {
if (isNumber(x)) {
return true;
}
console.error("expected number, got:", typeof x);
console.error(`expected number, got: ${typeof x}`);
return false;
}
};
const isNumberThrowError = x => {
if (isNumber(x)) {
return;
}
throw new Error("expected number, got:", typeof x);
}
throw new Error(`expected number, got: ${typeof x}`);
};
const isString = x => typeof x === "string" || x instanceof String;
const isStringLogError = x => {
if (isString(x)) {
return true;
}
console.error("expected string, got:", typeof x);
console.error(`expected string, got: ${typeof x}`);
return false;
};
const isStringThrowError = x => {
if (isString(x)) {
return;
}
throw new Error("expected string, got:", typeof x);
throw new Error(`expected string, got: ${typeof x}`);
};
const isNull = x => x === null;
@ -53,14 +53,14 @@ const isElementLogError = x => {
if (isElement(x)) {
return true;
}
console.error("expected element type, got:", typeof x);
console.error(`expected element type, got: ${typeof x}`);
return false;
};
const isElementThrowError = x => {
if (isElement(x)) {
return;
}
throw new Error("expected element type, got:", typeof x);
throw new Error(`expected element type, got: ${typeof x}`);
};
const isFunction = x => typeof x === "function";
@ -68,14 +68,62 @@ const isFunctionLogError = x => {
if (isFunction(x)) {
return true;
}
console.error("expected function type, got:", typeof x);
console.error(`expected function type, got: ${typeof x}`);
return false;
};
const isFunctionThrowError = x => {
if (isFunction(x)) {
return;
}
throw new Error("expected function type, got:", typeof x);
throw new Error(`expected function type, got: ${typeof x}`);
};
const isObject = x => typeof x === "object" && !Array.isArray(x);
const isObjectLogError = x => {
if (isObject(x)) {
return true;
}
console.error(`expected object type, got: ${typeof x}`);
return false;
};
const isObjectThrowError = x => {
if (isObject(x)) {
return;
}
throw new Error(`expected object type, got: ${typeof x}`);
};
const keyExists = (obj, k) => isObject(obj) && isString(k) && k in obj;
const keyExistsLogError = (obj, k) => {
if (keyExists(obj, k)) {
return true;
}
console.error(`key does not exist in object: ${k}`);
return false;
};
const keyExistsThrowError = (obj, k) => {
if (keyExists(obj, k)) {
return;
}
throw new Error(`key does not exist in object: ${k}`)
};
const getValue = (obj, k) => {
/** Returns value of object for given key if it exists, otherwise returns null. */
if (keyExists(obj, k)) {
return obj[k];
}
return null;
};
const getValueLogError = (obj, k) => {
if (keyExistsLogError(obj, k)) {
return obj[k];
}
return null;
};
const getValueThrowError = (obj, k) => {
keyExistsThrowError(obj, k);
return obj[k];
};
const getElementByIdLogError = selector => {
@ -156,17 +204,17 @@ const getComputedDims = elem => {
};
};
const calcColsPerRow = function(parent, child) {
const calcColsPerRow = function (parent, child) {
/** Calculates the number of columns of children that can fit in a parent's visible width. */
const parent_inner_width = parent.offsetWidth - getComputedPaddingDims(parent).width;
return parseInt(parent_inner_width / getComputedDims(child).width);
return parseInt(parent_inner_width / getComputedDims(child).width, 10);
};
const calcRowsPerCol = function(parent, child) {
const calcRowsPerCol = function (parent, child) {
/** Calculates the number of rows of children that can fit in a parent's visible height. */
const parent_inner_height = parent.offsetHeight - getComputedPaddingDims(parent).height;
return parseInt(parent_inner_height / getComputedDims(child).height);
return parseInt(parent_inner_height / getComputedDims(child).height, 10);
};
/** Functions for asynchronous operations. */
@ -271,7 +319,7 @@ const waitForValueInObject = o => {
* Resolves when obj[k] == v
*/
return new Promise(resolve => {
waitForKeyInObject({k: o.k, obj: o.obj}).then(() => {
waitForKeyInObject({ k: o.k, obj: o.obj }).then(() => {
(function _waitForValueInObject() {
if (o.k in o.obj && o.obj[o.k] == o.v) {
@ -283,17 +331,93 @@ const waitForValueInObject = o => {
});
};
/** Requests */
const requestGet = (url, data, handler, errorHandler) => {
var xhr = new XMLHttpRequest();
var args = Object.keys(data).map(function (k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
}).join('&');
xhr.open("GET", url + "?" + args, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
var js = JSON.parse(xhr.responseText);
handler(js);
} catch (error) {
console.error(error);
errorHandler();
}
} else {
errorHandler();
}
}
};
var js = JSON.stringify(data);
xhr.send(js);
};
const requestGetPromise = (url, data) => {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
let args = Object.keys(data).map(k => {
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
}).join("&");
xhr.open("GET", url + "?" + args, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
resolve(xhr.responseText);
} catch (error) {
reject(error);
}
} else {
reject({ status: this.status, statusText: xhr.statusText });
}
}
};
xhr.send(JSON.stringify(data));
});
};
/** Misc helper functions. */
const clamp = (x, min, max) => Math.max(min, Math.min(x, max));
const getStyle = (prop, elem) => {
return window.getComputedStyle ? window.getComputedStyle(elem)[prop] : elem.currentStyle[prop];
}
};
const htmlStringToElement = function(str) {
const htmlStringToElement = function (str) {
/** Converts an HTML string into an Element type. */
let parser = new DOMParser();
let tmp = parser.parseFromString(str, "text/html");
return tmp.body.firstElementChild;
};
const toggleCss = (key, css, enable) => {
var style = document.getElementById(key);
if (enable && !style) {
style = document.createElement('style');
style.id = key;
style.type = 'text/css';
document.head.appendChild(style);
}
if (style && !enable) {
document.head.removeChild(style);
}
if (style) {
style.innerHTML == '';
style.appendChild(document.createTextNode(css));
}
};
const copyToClipboard = s => {
/** Copies the passed string to the clipboard. */
isStringThrowError(s);
navigator.clipboard.writeText(s);
};