2023-04-27 19:56:56 +02:00
|
|
|
"use strict"
|
2022-12-06 12:34:08 +01:00
|
|
|
|
2022-10-18 18:58:04 +02:00
|
|
|
// https://gomakethings.com/finding-the-next-and-previous-sibling-elements-that-match-a-selector-with-vanilla-js/
|
|
|
|
function getNextSibling(elem, selector) {
|
2022-11-30 07:48:34 +01:00
|
|
|
// Get the next sibling element
|
2022-12-06 12:34:08 +01:00
|
|
|
let sibling = elem.nextElementSibling
|
2022-10-18 18:58:04 +02:00
|
|
|
|
2022-11-30 07:48:34 +01:00
|
|
|
// If there's no selector, return the first sibling
|
2022-12-06 12:34:08 +01:00
|
|
|
if (!selector) {
|
|
|
|
return sibling
|
|
|
|
}
|
2022-10-18 18:58:04 +02:00
|
|
|
|
2022-11-30 07:48:34 +01:00
|
|
|
// If the sibling matches our selector, use it
|
|
|
|
// If not, jump to the next sibling and continue the loop
|
|
|
|
while (sibling) {
|
2022-12-06 12:34:08 +01:00
|
|
|
if (sibling.matches(selector)) {
|
|
|
|
return sibling
|
|
|
|
}
|
2022-11-30 07:48:34 +01:00
|
|
|
sibling = sibling.nextElementSibling
|
|
|
|
}
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 05:49:58 +02:00
|
|
|
/* Panel Stuff */
|
|
|
|
|
|
|
|
// true = open
|
2023-04-27 19:56:56 +02:00
|
|
|
let COLLAPSIBLES_INITIALIZED = false
|
|
|
|
const COLLAPSIBLES_KEY = "collapsibles"
|
|
|
|
const COLLAPSIBLE_PANELS = [] // filled in by createCollapsibles with all the elements matching .collapsible
|
2022-10-19 05:49:58 +02:00
|
|
|
|
|
|
|
// on-init call this for any panels that are marked open
|
|
|
|
function toggleCollapsible(element) {
|
2023-04-27 19:56:56 +02:00
|
|
|
const collapsibleHeader = element.querySelector(".collapsible")
|
|
|
|
const handle = element.querySelector(".collapsible-handle")
|
2022-10-19 05:49:58 +02:00
|
|
|
collapsibleHeader.classList.toggle("active")
|
2023-04-27 19:56:56 +02:00
|
|
|
let content = getNextSibling(collapsibleHeader, ".collapsible-content")
|
2022-11-02 07:03:05 +01:00
|
|
|
if (!collapsibleHeader.classList.contains("active")) {
|
2022-10-19 05:49:58 +02:00
|
|
|
content.style.display = "none"
|
2023-04-27 19:56:56 +02:00
|
|
|
if (handle != null) {
|
|
|
|
// render results don't have a handle
|
|
|
|
handle.innerHTML = "➕" // plus
|
2022-11-08 19:05:28 +01:00
|
|
|
}
|
2022-10-19 05:49:58 +02:00
|
|
|
} else {
|
|
|
|
content.style.display = "block"
|
2023-04-27 19:56:56 +02:00
|
|
|
if (handle != null) {
|
|
|
|
// render results don't have a handle
|
|
|
|
handle.innerHTML = "➖" // minus
|
2022-11-08 19:05:28 +01:00
|
|
|
}
|
2022-10-19 05:49:58 +02:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
document.dispatchEvent(new CustomEvent("collapsibleClick", { detail: collapsibleHeader }))
|
|
|
|
|
2022-10-19 07:13:45 +02:00
|
|
|
if (COLLAPSIBLES_INITIALIZED && COLLAPSIBLE_PANELS.includes(element)) {
|
2022-10-20 06:12:01 +02:00
|
|
|
saveCollapsibles()
|
2022-10-19 05:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveCollapsibles() {
|
2022-12-06 12:34:08 +01:00
|
|
|
let values = {}
|
2023-04-27 19:56:56 +02:00
|
|
|
COLLAPSIBLE_PANELS.forEach((element) => {
|
2022-12-06 12:34:08 +01:00
|
|
|
let value = element.querySelector(".collapsible").className.indexOf("active") !== -1
|
2022-10-20 06:12:01 +02:00
|
|
|
values[element.id] = value
|
|
|
|
})
|
|
|
|
localStorage.setItem(COLLAPSIBLES_KEY, JSON.stringify(values))
|
2022-10-19 05:49:58 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:58:04 +02:00
|
|
|
function createCollapsibles(node) {
|
2022-12-06 12:34:08 +01:00
|
|
|
let save = false
|
2022-10-18 18:58:04 +02:00
|
|
|
if (!node) {
|
2022-10-20 06:12:01 +02:00
|
|
|
node = document
|
|
|
|
save = true
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
|
|
|
let collapsibles = node.querySelectorAll(".collapsible")
|
|
|
|
collapsibles.forEach(function(c) {
|
2022-10-19 05:49:58 +02:00
|
|
|
if (save && c.parentElement.id) {
|
2022-10-20 06:12:01 +02:00
|
|
|
COLLAPSIBLE_PANELS.push(c.parentElement)
|
2022-10-19 05:49:58 +02:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
let handle = document.createElement("span")
|
|
|
|
handle.className = "collapsible-handle"
|
2022-10-18 18:58:04 +02:00
|
|
|
|
2022-11-02 07:03:05 +01:00
|
|
|
if (c.classList.contains("active")) {
|
2023-04-27 19:56:56 +02:00
|
|
|
handle.innerHTML = "➖" // minus
|
2022-10-18 18:58:04 +02:00
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
handle.innerHTML = "➕" // plus
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
|
|
|
c.insertBefore(handle, c.firstChild)
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
c.addEventListener("click", function() {
|
2022-10-20 06:12:01 +02:00
|
|
|
toggleCollapsible(c.parentElement)
|
|
|
|
})
|
|
|
|
})
|
2022-10-19 05:49:58 +02:00
|
|
|
if (save) {
|
2022-12-06 12:34:08 +01:00
|
|
|
let saved = localStorage.getItem(COLLAPSIBLES_KEY)
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!saved) {
|
|
|
|
saved = tryLoadOldCollapsibles()
|
2022-10-22 02:13:13 +02:00
|
|
|
}
|
2022-10-19 05:49:58 +02:00
|
|
|
if (!saved) {
|
2022-10-20 06:12:01 +02:00
|
|
|
saveCollapsibles()
|
|
|
|
saved = localStorage.getItem(COLLAPSIBLES_KEY)
|
2022-10-19 05:49:58 +02:00
|
|
|
}
|
2022-12-06 12:34:08 +01:00
|
|
|
let values = JSON.parse(saved)
|
2023-04-27 19:56:56 +02:00
|
|
|
COLLAPSIBLE_PANELS.forEach((element) => {
|
2022-12-06 12:34:08 +01:00
|
|
|
let value = element.querySelector(".collapsible").className.indexOf("active") !== -1
|
2022-10-19 07:13:45 +02:00
|
|
|
if (values[element.id] != value) {
|
2022-10-20 06:12:01 +02:00
|
|
|
toggleCollapsible(element)
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
2022-10-19 07:13:45 +02:00
|
|
|
})
|
2022-10-20 06:12:01 +02:00
|
|
|
COLLAPSIBLES_INITIALIZED = true
|
2022-10-19 05:49:58 +02:00
|
|
|
}
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:13:13 +02:00
|
|
|
function tryLoadOldCollapsibles() {
|
2022-12-06 12:34:08 +01:00
|
|
|
const old_map = {
|
2023-04-27 19:56:56 +02:00
|
|
|
advancedPanelOpen: "editor-settings",
|
|
|
|
modifiersPanelOpen: "editor-modifiers",
|
2023-04-28 12:20:44 +02:00
|
|
|
negativePromptPanelOpen: "editor-inputs-prompt",
|
2023-04-27 19:56:56 +02:00
|
|
|
}
|
2022-10-22 02:13:13 +02:00
|
|
|
if (localStorage.getItem(Object.keys(old_map)[0])) {
|
2023-04-27 19:56:56 +02:00
|
|
|
let result = {}
|
|
|
|
Object.keys(old_map).forEach((key) => {
|
|
|
|
const value = localStorage.getItem(key)
|
2022-10-22 02:13:13 +02:00
|
|
|
if (value !== null) {
|
2023-04-27 19:56:56 +02:00
|
|
|
result[old_map[key]] = value == true || value == "true"
|
2022-10-22 02:13:13 +02:00
|
|
|
localStorage.removeItem(key)
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
})
|
2022-10-22 02:13:13 +02:00
|
|
|
result = JSON.stringify(result)
|
|
|
|
localStorage.setItem(COLLAPSIBLES_KEY, result)
|
|
|
|
return result
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
return null
|
2022-10-22 02:13:13 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:58:04 +02:00
|
|
|
function permute(arr) {
|
|
|
|
let permutations = []
|
|
|
|
let n = arr.length
|
|
|
|
let n_permutations = Math.pow(2, n)
|
|
|
|
for (let i = 0; i < n_permutations; i++) {
|
|
|
|
let perm = []
|
2023-04-27 19:56:56 +02:00
|
|
|
let mask = Number(i)
|
|
|
|
.toString(2)
|
|
|
|
.padStart(n, "0")
|
2022-10-18 18:58:04 +02:00
|
|
|
|
|
|
|
for (let idx = 0; idx < mask.length; idx++) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (mask[idx] === "1" && arr[idx].trim() !== "") {
|
2022-10-18 18:58:04 +02:00
|
|
|
perm.push(arr[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perm.length > 0) {
|
|
|
|
permutations.push(perm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return permutations
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/8212878
|
|
|
|
function millisecondsToStr(milliseconds) {
|
2023-04-27 19:56:56 +02:00
|
|
|
function numberEnding(number) {
|
|
|
|
return number > 1 ? "s" : ""
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:34:08 +01:00
|
|
|
let temp = Math.floor(milliseconds / 1000)
|
|
|
|
let hours = Math.floor((temp %= 86400) / 3600)
|
2023-04-27 19:56:56 +02:00
|
|
|
let s = ""
|
2022-10-18 18:58:04 +02:00
|
|
|
if (hours) {
|
2023-04-27 19:56:56 +02:00
|
|
|
s += hours + " hour" + numberEnding(hours) + " "
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
2022-12-06 12:34:08 +01:00
|
|
|
let minutes = Math.floor((temp %= 3600) / 60)
|
2022-10-18 18:58:04 +02:00
|
|
|
if (minutes) {
|
2023-04-27 19:56:56 +02:00
|
|
|
s += minutes + " minute" + numberEnding(minutes) + " "
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
2022-12-06 12:34:08 +01:00
|
|
|
let seconds = temp % 60
|
2022-10-18 18:58:04 +02:00
|
|
|
if (!hours && minutes < 4 && seconds) {
|
2023-04-27 19:56:56 +02:00
|
|
|
s += seconds + " second" + numberEnding(seconds)
|
2022-10-18 18:58:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://rosettacode.org/wiki/Brace_expansion#JavaScript
|
|
|
|
function BraceExpander() {
|
2023-04-27 19:56:56 +02:00
|
|
|
"use strict"
|
2022-10-19 10:20:05 +02:00
|
|
|
|
|
|
|
// Index of any closing brace matching the opening
|
|
|
|
// brace at iPosn,
|
|
|
|
// with the indices of any immediately-enclosed commas.
|
|
|
|
function bracePair(tkns, iPosn, iNest, lstCommas) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (iPosn >= tkns.length || iPosn < 0) return null
|
2022-10-19 10:20:05 +02:00
|
|
|
|
2022-12-06 12:34:08 +01:00
|
|
|
let t = tkns[iPosn],
|
2023-04-27 19:56:56 +02:00
|
|
|
n = t === "{" ? iNest + 1 : t === "}" ? iNest - 1 : iNest,
|
|
|
|
lst = t === "," && iNest === 1 ? lstCommas.concat(iPosn) : lstCommas
|
2022-10-19 10:20:05 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
return n
|
|
|
|
? bracePair(tkns, iPosn + 1, n, lst)
|
|
|
|
: {
|
|
|
|
close: iPosn,
|
2023-04-28 12:20:44 +02:00
|
|
|
commas: lst,
|
2023-04-27 19:56:56 +02:00
|
|
|
}
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse of a SYNTAGM subtree
|
|
|
|
function andTree(dctSofar, tkns) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!tkns.length) return [dctSofar, []]
|
|
|
|
|
|
|
|
let dctParse = dctSofar
|
|
|
|
? dctSofar
|
|
|
|
: {
|
|
|
|
fn: and,
|
2023-04-28 12:20:44 +02:00
|
|
|
args: [],
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
2022-10-19 10:20:05 +02:00
|
|
|
head = tkns[0],
|
|
|
|
tail = head ? tkns.slice(1) : [],
|
2023-04-27 19:56:56 +02:00
|
|
|
dctBrace = head === "{" ? bracePair(tkns, 0, 0, []) : null,
|
|
|
|
lstOR = dctBrace && dctBrace.close && dctBrace.commas.length ? splitAt(dctBrace.close + 1, tkns) : null
|
2022-10-19 10:20:05 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
return andTree(
|
|
|
|
{
|
|
|
|
fn: and,
|
2023-04-28 12:20:44 +02:00
|
|
|
args: dctParse.args.concat(lstOR ? orTree(dctParse, lstOR[0], dctBrace.commas) : head),
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
|
|
|
lstOR ? lstOR[1] : tail
|
|
|
|
)
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse of a PARADIGM subtree
|
|
|
|
function orTree(dctSofar, tkns, lstCommas) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!tkns.length) return [dctSofar, []]
|
|
|
|
let iLast = lstCommas.length
|
2022-10-19 10:20:05 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
fn: or,
|
2023-04-27 19:56:56 +02:00
|
|
|
args: splitsAt(lstCommas, tkns)
|
|
|
|
.map(function(x, i) {
|
|
|
|
let ts = x.slice(1, i === iLast ? -1 : void 0)
|
|
|
|
|
|
|
|
return ts.length ? ts : [""]
|
|
|
|
})
|
|
|
|
.map(function(ts) {
|
|
|
|
return ts.length > 1 ? andTree(null, ts)[0] : ts[0]
|
2023-04-28 12:20:44 +02:00
|
|
|
}),
|
2023-04-27 19:56:56 +02:00
|
|
|
}
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// List of unescaped braces and commas, and remaining strings
|
|
|
|
function tokens(str) {
|
|
|
|
// Filter function excludes empty splitting artefacts
|
2023-04-27 19:56:56 +02:00
|
|
|
let toS = function(x) {
|
|
|
|
return x.toString()
|
|
|
|
}
|
2022-10-19 10:20:05 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
return str
|
|
|
|
.split(/(\\\\)/)
|
|
|
|
.filter(toS)
|
|
|
|
.reduce(function(a, s) {
|
|
|
|
return a.concat(s.charAt(0) === "\\" ? s : s.split(/(\\*[{,}])/).filter(toS))
|
|
|
|
}, [])
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PARSE TREE OPERATOR (1 of 2)
|
|
|
|
// Each possible head * each possible tail
|
|
|
|
function and(args) {
|
2022-12-06 12:34:08 +01:00
|
|
|
let lng = args.length,
|
2022-10-19 10:20:05 +02:00
|
|
|
head = lng ? args[0] : null,
|
2023-04-27 19:56:56 +02:00
|
|
|
lstHead = "string" === typeof head ? [head] : head
|
2022-10-19 10:20:05 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
return lng
|
|
|
|
? 1 < lng
|
|
|
|
? lstHead.reduce(function(a, h) {
|
|
|
|
return a.concat(
|
|
|
|
and(args.slice(1)).map(function(t) {
|
|
|
|
return h + t
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}, [])
|
|
|
|
: lstHead
|
|
|
|
: []
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PARSE TREE OPERATOR (2 of 2)
|
|
|
|
// Each option flattened
|
|
|
|
function or(args) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return args.reduce(function(a, b) {
|
|
|
|
return a.concat(b)
|
|
|
|
}, [])
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// One list split into two (first sublist length n)
|
|
|
|
function splitAt(n, lst) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return n < lst.length + 1 ? [lst.slice(0, n), lst.slice(n)] : [lst, []]
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// One list split into several (sublist lengths [n])
|
|
|
|
function splitsAt(lstN, lst) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return lstN.reduceRight(
|
|
|
|
function(a, x) {
|
|
|
|
return splitAt(x, a[0]).concat(a.slice(1))
|
|
|
|
},
|
|
|
|
[lst]
|
|
|
|
)
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value of the parse tree
|
|
|
|
function evaluated(e) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return typeof e === "string" ? e : e.fn(e.args.map(evaluated))
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSON prettyprint (for parse tree, token list etc)
|
|
|
|
function pp(e) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return JSON.stringify(
|
|
|
|
e,
|
|
|
|
function(k, v) {
|
|
|
|
return typeof v === "function" ? "[function " + v.name + "]" : v
|
|
|
|
},
|
|
|
|
2
|
|
|
|
)
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------- MAIN ------------------------
|
|
|
|
|
|
|
|
// s -> [s]
|
|
|
|
this.expand = function(s) {
|
|
|
|
// BRACE EXPRESSION PARSED
|
2023-04-27 19:56:56 +02:00
|
|
|
let dctParse = andTree(null, tokens(s))[0]
|
2022-10-19 10:20:05 +02:00
|
|
|
|
|
|
|
// ABSTRACT SYNTAX TREE LOGGED
|
|
|
|
// console.log(pp(dctParse));
|
|
|
|
|
|
|
|
// AST EVALUATED TO LIST OF STRINGS
|
2023-04-27 19:56:56 +02:00
|
|
|
return evaluated(dctParse)
|
2022-10-19 10:20:05 +02:00
|
|
|
}
|
2022-10-20 13:52:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:34:08 +01:00
|
|
|
/** Pause the execution of an async function until timer elapse.
|
|
|
|
* @Returns a promise that will resolve after the specified timeout.
|
|
|
|
*/
|
2022-10-20 13:52:01 +02:00
|
|
|
function asyncDelay(timeout) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(resolve, timeout, true)
|
|
|
|
})
|
|
|
|
}
|
2022-11-04 15:18:34 +01:00
|
|
|
|
2022-12-06 12:34:08 +01:00
|
|
|
function PromiseSource() {
|
|
|
|
const srcPromise = new Promise((resolve, reject) => {
|
|
|
|
Object.defineProperties(this, {
|
2023-04-27 19:56:56 +02:00
|
|
|
resolve: { value: resolve, writable: false },
|
2023-04-28 12:20:44 +02:00
|
|
|
reject: { value: reject, writable: false },
|
2022-12-06 12:34:08 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
Object.defineProperties(this, {
|
2023-04-28 12:20:44 +02:00
|
|
|
promise: { value: makeQuerablePromise(srcPromise), writable: false },
|
2022-12-06 12:34:08 +01:00
|
|
|
})
|
2022-12-05 06:32:33 +01:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:34:08 +01:00
|
|
|
/** A debounce is a higher-order function, which is a function that returns another function
|
|
|
|
* that, as long as it continues to be invoked, will not be triggered.
|
|
|
|
* The function will be called after it stops being called for N milliseconds.
|
|
|
|
* If `immediate` is passed, trigger the function on the leading edge, instead of the trailing.
|
|
|
|
* @Returns a promise that will resolve to func return value.
|
|
|
|
*/
|
2023-04-27 19:56:56 +02:00
|
|
|
function debounce(func, wait, immediate) {
|
2022-12-06 12:34:08 +01:00
|
|
|
if (typeof wait === "undefined") {
|
|
|
|
wait = 40
|
|
|
|
}
|
|
|
|
if (typeof wait !== "number") {
|
|
|
|
throw new Error("wait is not an number.")
|
|
|
|
}
|
|
|
|
let timeout = null
|
|
|
|
let lastPromiseSrc = new PromiseSource()
|
|
|
|
const applyFn = function(context, args) {
|
|
|
|
let result = undefined
|
|
|
|
try {
|
|
|
|
result = func.apply(context, args)
|
|
|
|
} catch (err) {
|
|
|
|
lastPromiseSrc.reject(err)
|
|
|
|
}
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
result.then(lastPromiseSrc.resolve, lastPromiseSrc.reject)
|
|
|
|
} else {
|
|
|
|
lastPromiseSrc.resolve(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return function(...args) {
|
|
|
|
const callNow = Boolean(immediate && !timeout)
|
2023-04-27 19:56:56 +02:00
|
|
|
const context = this
|
2022-12-06 12:34:08 +01:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
timeout = setTimeout(function() {
|
2022-12-06 12:34:08 +01:00
|
|
|
if (!immediate) {
|
|
|
|
applyFn(context, args)
|
|
|
|
}
|
|
|
|
lastPromiseSrc = new PromiseSource()
|
|
|
|
timeout = null
|
|
|
|
}, wait)
|
|
|
|
if (callNow) {
|
|
|
|
applyFn(context, args)
|
|
|
|
}
|
|
|
|
return lastPromiseSrc.promise
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 06:32:33 +01:00
|
|
|
|
2022-11-04 15:18:34 +01:00
|
|
|
function preventNonNumericalInput(e) {
|
2023-04-27 19:56:56 +02:00
|
|
|
e = e || window.event
|
|
|
|
let charCode = typeof e.which == "undefined" ? e.keyCode : e.which
|
|
|
|
let charStr = String.fromCharCode(charCode)
|
|
|
|
let re = e.target.getAttribute("pattern") || "^[0-9]+$"
|
2022-11-04 15:18:34 +01:00
|
|
|
re = new RegExp(re)
|
|
|
|
|
|
|
|
if (!charStr.match(re)) {
|
2023-04-27 19:56:56 +02:00
|
|
|
e.preventDefault()
|
2022-11-04 15:18:34 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 02:58:09 +01:00
|
|
|
|
2022-12-06 12:34:08 +01:00
|
|
|
/** Returns the global object for the current execution environement.
|
|
|
|
* @Returns window in a browser, global in node and self in a ServiceWorker.
|
|
|
|
* @Notes Allows unit testing and use of the engine outside of a browser.
|
|
|
|
*/
|
|
|
|
function getGlobal() {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof globalThis === "object") {
|
2022-12-06 12:34:08 +01:00
|
|
|
return globalThis
|
2023-04-27 19:56:56 +02:00
|
|
|
} else if (typeof global === "object") {
|
2022-12-06 12:34:08 +01:00
|
|
|
return global
|
2023-04-27 19:56:56 +02:00
|
|
|
} else if (typeof self === "object") {
|
2022-12-06 12:34:08 +01:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
try {
|
2023-04-27 19:56:56 +02:00
|
|
|
return Function("return this")()
|
2022-12-06 12:34:08 +01:00
|
|
|
} catch {
|
|
|
|
// If the Function constructor fails, we're in a browser with eval disabled by CSP headers.
|
|
|
|
return window
|
|
|
|
} // Returns undefined if global can't be found.
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if x is an Array or a TypedArray.
|
|
|
|
* @Returns true if x is an Array or a TypedArray, false otherwise.
|
|
|
|
*/
|
|
|
|
function isArrayOrTypedArray(x) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return Boolean(typeof x === "object" && (Array.isArray(x) || (ArrayBuffer.isView(x) && !(x instanceof DataView))))
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeQuerablePromise(promise) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof promise !== "object") {
|
|
|
|
throw new Error("promise is not an object.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
if (!(promise instanceof Promise)) {
|
2023-04-27 19:56:56 +02:00
|
|
|
throw new Error("Argument is not a promise.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
// Don't modify a promise that's been already modified.
|
2023-04-27 19:56:56 +02:00
|
|
|
if ("isResolved" in promise || "isRejected" in promise || "isPending" in promise) {
|
2022-12-06 12:34:08 +01:00
|
|
|
return promise
|
|
|
|
}
|
|
|
|
let isPending = true
|
|
|
|
let isRejected = false
|
|
|
|
let rejectReason = undefined
|
|
|
|
let isResolved = false
|
|
|
|
let resolvedValue = undefined
|
|
|
|
const qurPro = promise.then(
|
2023-04-27 19:56:56 +02:00
|
|
|
function(val) {
|
2022-12-06 12:34:08 +01:00
|
|
|
isResolved = true
|
|
|
|
isPending = false
|
|
|
|
resolvedValue = val
|
|
|
|
return val
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
|
|
|
function(reason) {
|
2022-12-06 12:34:08 +01:00
|
|
|
rejectReason = reason
|
|
|
|
isRejected = true
|
|
|
|
isPending = false
|
|
|
|
throw reason
|
|
|
|
}
|
|
|
|
)
|
|
|
|
Object.defineProperties(qurPro, {
|
2023-04-27 19:56:56 +02:00
|
|
|
isResolved: {
|
2023-04-28 12:20:44 +02:00
|
|
|
get: () => isResolved,
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
|
|
|
resolvedValue: {
|
2023-04-28 12:20:44 +02:00
|
|
|
get: () => resolvedValue,
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
|
|
|
isPending: {
|
2023-04-28 12:20:44 +02:00
|
|
|
get: () => isPending,
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
|
|
|
isRejected: {
|
2023-04-28 12:20:44 +02:00
|
|
|
get: () => isRejected,
|
2023-04-27 19:56:56 +02:00
|
|
|
},
|
|
|
|
rejectReason: {
|
2023-04-28 12:20:44 +02:00
|
|
|
get: () => rejectReason,
|
|
|
|
},
|
2022-12-06 12:34:08 +01:00
|
|
|
})
|
|
|
|
return qurPro
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:58:09 +01:00
|
|
|
/* inserts custom html to allow prettifying of inputs */
|
|
|
|
function prettifyInputs(root_element) {
|
2023-04-27 19:56:56 +02:00
|
|
|
root_element.querySelectorAll(`input[type="checkbox"]`).forEach((element) => {
|
2023-03-01 13:57:48 +01:00
|
|
|
if (element.style.display === "none") {
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
var parent = element.parentNode
|
2022-11-18 02:58:09 +01:00
|
|
|
if (!parent.classList.contains("input-toggle")) {
|
2023-04-27 19:56:56 +02:00
|
|
|
var wrapper = document.createElement("div")
|
|
|
|
wrapper.classList.add("input-toggle")
|
|
|
|
parent.replaceChild(wrapper, element)
|
|
|
|
wrapper.appendChild(element)
|
|
|
|
var label = document.createElement("label")
|
|
|
|
label.htmlFor = element.id
|
|
|
|
wrapper.appendChild(label)
|
2022-11-18 02:58:09 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-12-06 12:34:08 +01:00
|
|
|
|
|
|
|
class GenericEventSource {
|
2023-04-27 19:56:56 +02:00
|
|
|
#events = {}
|
2022-12-06 12:34:08 +01:00
|
|
|
#types = []
|
|
|
|
constructor(...eventsTypes) {
|
|
|
|
if (Array.isArray(eventsTypes) && eventsTypes.length === 1 && Array.isArray(eventsTypes[0])) {
|
|
|
|
eventsTypes = eventsTypes[0]
|
|
|
|
}
|
|
|
|
this.#types.push(...eventsTypes)
|
|
|
|
}
|
|
|
|
get eventTypes() {
|
|
|
|
return this.#types
|
|
|
|
}
|
|
|
|
/** Add a new event listener
|
|
|
|
*/
|
|
|
|
addEventListener(name, handler) {
|
|
|
|
if (!this.#types.includes(name)) {
|
2023-04-27 19:56:56 +02:00
|
|
|
throw new Error("Invalid event name.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
if (this.#events.hasOwnProperty(name)) {
|
|
|
|
this.#events[name].push(handler)
|
|
|
|
} else {
|
|
|
|
this.#events[name] = [handler]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** Remove the event listener
|
|
|
|
*/
|
|
|
|
removeEventListener(name, handler) {
|
|
|
|
if (!this.#events.hasOwnProperty(name)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const index = this.#events[name].indexOf(handler)
|
|
|
|
if (index != -1) {
|
|
|
|
this.#events[name].splice(index, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fireEvent(name, ...args) {
|
|
|
|
if (!this.#types.includes(name)) {
|
|
|
|
throw new Error(`Event ${String(name)} missing from Events.types`)
|
|
|
|
}
|
|
|
|
if (!this.#events.hasOwnProperty(name)) {
|
2022-12-11 06:52:52 +01:00
|
|
|
return Promise.resolve()
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
if (!args || !args.length) {
|
|
|
|
args = []
|
|
|
|
}
|
|
|
|
const evs = this.#events[name]
|
2022-12-11 06:52:52 +01:00
|
|
|
if (evs.length <= 0) {
|
|
|
|
return Promise.resolve()
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
return Promise.allSettled(
|
|
|
|
evs.map((callback) => {
|
|
|
|
try {
|
|
|
|
return Promise.resolve(callback.apply(SD, args))
|
|
|
|
} catch (ex) {
|
|
|
|
return Promise.reject(ex)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ServiceContainer {
|
|
|
|
#services = new Map()
|
|
|
|
#singletons = new Map()
|
|
|
|
constructor(...servicesParams) {
|
|
|
|
servicesParams.forEach(this.register.bind(this))
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
get services() {
|
2022-12-06 12:34:08 +01:00
|
|
|
return this.#services
|
|
|
|
}
|
|
|
|
get singletons() {
|
|
|
|
return this.#singletons
|
|
|
|
}
|
|
|
|
register(params) {
|
|
|
|
if (ServiceContainer.isConstructor(params)) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof params.name !== "string") {
|
|
|
|
throw new Error("params.name is not a string.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
params = { name: params.name, definition: params }
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof params !== "object") {
|
|
|
|
throw new Error("params is not an object.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
;["name", "definition"].forEach((key) => {
|
2022-12-06 12:34:08 +01:00
|
|
|
if (!(key in params)) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.error("Invalid service %o registration.", params)
|
2022-12-06 12:34:08 +01:00
|
|
|
throw new Error(`params.${key} is not defined.`)
|
|
|
|
}
|
|
|
|
})
|
2023-04-27 19:56:56 +02:00
|
|
|
const opts = { definition: params.definition }
|
|
|
|
if ("dependencies" in params) {
|
2022-12-06 12:34:08 +01:00
|
|
|
if (Array.isArray(params.dependencies)) {
|
|
|
|
params.dependencies.forEach((dep) => {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof dep !== "string") {
|
|
|
|
throw new Error("dependency name is not a string.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
opts.dependencies = params.dependencies
|
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
throw new Error("params.dependencies is not an array.")
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (params.singleton) {
|
|
|
|
opts.singleton = true
|
|
|
|
}
|
|
|
|
this.#services.set(params.name, opts)
|
2023-04-27 19:56:56 +02:00
|
|
|
return Object.assign({ name: params.name }, opts)
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
get(name) {
|
|
|
|
const ctorInfos = this.#services.get(name)
|
|
|
|
if (!ctorInfos) {
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!ServiceContainer.isConstructor(ctorInfos.definition)) {
|
2022-12-06 12:34:08 +01:00
|
|
|
return ctorInfos.definition
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!ctorInfos.singleton) {
|
2022-12-06 12:34:08 +01:00
|
|
|
return this._createInstance(ctorInfos)
|
|
|
|
}
|
|
|
|
const singletonInstance = this.#singletons.get(name)
|
2023-04-27 19:56:56 +02:00
|
|
|
if (singletonInstance) {
|
2022-12-06 12:34:08 +01:00
|
|
|
return singletonInstance
|
|
|
|
}
|
|
|
|
const newSingletonInstance = this._createInstance(ctorInfos)
|
|
|
|
this.#singletons.set(name, newSingletonInstance)
|
|
|
|
return newSingletonInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
_getResolvedDependencies(service) {
|
|
|
|
let classDependencies = []
|
2023-04-27 19:56:56 +02:00
|
|
|
if (service.dependencies) {
|
2022-12-06 12:34:08 +01:00
|
|
|
classDependencies = service.dependencies.map(this.get.bind(this))
|
|
|
|
}
|
|
|
|
return classDependencies
|
|
|
|
}
|
|
|
|
|
|
|
|
_createInstance(service) {
|
|
|
|
if (!ServiceContainer.isClass(service.definition)) {
|
|
|
|
// Call as normal function.
|
|
|
|
return service.definition(...this._getResolvedDependencies(service))
|
|
|
|
}
|
|
|
|
// Use new
|
|
|
|
return new service.definition(...this._getResolvedDependencies(service))
|
|
|
|
}
|
|
|
|
|
|
|
|
static isClass(definition) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return (
|
|
|
|
typeof definition === "function" &&
|
|
|
|
Boolean(definition.prototype) &&
|
|
|
|
definition.prototype.constructor === definition
|
|
|
|
)
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
static isConstructor(definition) {
|
2023-04-27 19:56:56 +02:00
|
|
|
return typeof definition === "function"
|
2022-12-06 12:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-15 17:00:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} tag
|
|
|
|
* @param {object} attributes
|
|
|
|
* @param {string | Array<string>} classes
|
2023-04-03 23:39:34 +02:00
|
|
|
* @param {string | Node | Array<string | Node>}
|
2023-03-15 17:00:12 +01:00
|
|
|
* @returns {HTMLElement}
|
|
|
|
*/
|
|
|
|
function createElement(tagName, attributes, classes, textOrElements) {
|
|
|
|
const element = document.createElement(tagName)
|
|
|
|
if (attributes) {
|
|
|
|
Object.entries(attributes).forEach(([key, value]) => {
|
2023-03-30 02:56:24 +02:00
|
|
|
if (value !== undefined && value !== null) {
|
|
|
|
element.setAttribute(key, value)
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
})
|
2023-03-15 17:00:12 +01:00
|
|
|
}
|
|
|
|
if (classes) {
|
2023-04-27 19:56:56 +02:00
|
|
|
;(Array.isArray(classes) ? classes : [classes]).forEach((className) => element.classList.add(className))
|
2023-03-15 17:00:12 +01:00
|
|
|
}
|
|
|
|
if (textOrElements) {
|
|
|
|
const children = Array.isArray(textOrElements) ? textOrElements : [textOrElements]
|
2023-04-27 19:56:56 +02:00
|
|
|
children.forEach((textOrElem) => {
|
2023-04-03 23:39:34 +02:00
|
|
|
if (textOrElem instanceof Node) {
|
2023-03-15 17:00:12 +01:00
|
|
|
element.appendChild(textOrElem)
|
|
|
|
} else {
|
|
|
|
element.appendChild(document.createTextNode(textOrElem))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return element
|
|
|
|
}
|
2023-04-03 23:39:34 +02:00
|
|
|
|
2023-04-06 22:37:17 +02:00
|
|
|
/**
|
2023-03-30 02:56:24 +02:00
|
|
|
* Add a listener for arrays
|
|
|
|
* @param {keyof Array} method
|
|
|
|
* @param {(args) => {}} callback
|
|
|
|
*/
|
|
|
|
Array.prototype.addEventListener = function(method, callback) {
|
|
|
|
const originalFunction = this[method]
|
|
|
|
if (originalFunction) {
|
|
|
|
this[method] = function() {
|
|
|
|
originalFunction.apply(this, arguments)
|
|
|
|
callback.apply(this, arguments)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 22:36:25 +02:00
|
|
|
|
2023-04-03 23:39:34 +02:00
|
|
|
/**
|
|
|
|
* @typedef {object} TabOpenDetails
|
|
|
|
* @property {HTMLElement} contentElement
|
|
|
|
* @property {HTMLElement} labelElement
|
|
|
|
* @property {number} timesOpened
|
|
|
|
* @property {boolean} firstOpen
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} CreateTabRequest
|
|
|
|
* @property {string} id
|
|
|
|
* @property {string | Node | (() => (string | Node))} label
|
|
|
|
* Label text or an HTML element
|
|
|
|
* @property {string} icon
|
|
|
|
* @property {string | Node | Promise<string | Node> | (() => (string | Node | Promise<string | Node>)) | undefined} content
|
|
|
|
* HTML string or HTML element
|
|
|
|
* @property {((TabOpenDetails, Event) => (undefined | string | Node | Promise<string | Node>)) | undefined} onOpen
|
|
|
|
* If an HTML string or HTML element is returned, then that will replace the tab content
|
|
|
|
* @property {string | undefined} css
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {CreateTabRequest} request
|
|
|
|
*/
|
2023-04-27 19:56:56 +02:00
|
|
|
function createTab(request) {
|
2023-04-03 23:39:34 +02:00
|
|
|
if (!request?.id) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.error("createTab() error - id is required", Error().stack)
|
2023-04-03 23:39:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request.label) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.error("createTab() error - label is required", Error().stack)
|
2023-04-03 23:39:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request.icon) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.error("createTab() error - icon is required", Error().stack)
|
2023-04-03 23:39:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request.content && !request.onOpen) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.error("createTab() error - content or onOpen required", Error().stack)
|
2023-04-03 23:39:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const tabsContainer = document.querySelector(".tab-container")
|
2023-04-03 23:39:34 +02:00
|
|
|
if (!tabsContainer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const tabsContentWrapper = document.querySelector("#tab-content-wrapper")
|
2023-04-03 23:39:34 +02:00
|
|
|
if (!tabsContentWrapper) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-26 12:55:02 +02:00
|
|
|
// console.debug('creating tab: ', request)
|
2023-04-03 23:39:34 +02:00
|
|
|
|
|
|
|
if (request.css) {
|
2023-04-27 19:56:56 +02:00
|
|
|
document
|
|
|
|
.querySelector("body")
|
|
|
|
.insertAdjacentElement(
|
|
|
|
"beforeend",
|
|
|
|
createElement("style", { id: `tab-${request.id}-css` }, undefined, request.css)
|
|
|
|
)
|
2023-04-03 23:39:34 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const label = typeof request.label === "function" ? request.label() : request.label
|
|
|
|
const labelElement = label instanceof Node ? label : createElement("span", undefined, undefined, label)
|
2023-04-03 23:39:34 +02:00
|
|
|
|
|
|
|
const tab = createElement(
|
2023-04-27 19:56:56 +02:00
|
|
|
"span",
|
|
|
|
{ id: `tab-${request.id}`, "data-times-opened": 0 },
|
|
|
|
["tab"],
|
|
|
|
createElement("span", undefined, undefined, [
|
|
|
|
createElement("i", { style: "margin-right: 0.25em" }, [
|
|
|
|
"fa-solid",
|
|
|
|
`${request.icon.startsWith("fa-") ? "" : "fa-"}${request.icon}`,
|
2023-04-28 12:20:44 +02:00
|
|
|
"icon",
|
2023-04-27 19:56:56 +02:00
|
|
|
]),
|
2023-04-28 12:20:44 +02:00
|
|
|
labelElement,
|
2023-04-27 19:56:56 +02:00
|
|
|
])
|
2023-04-03 23:39:34 +02:00
|
|
|
)
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
tabsContainer.insertAdjacentElement("beforeend", tab)
|
2023-04-03 23:39:34 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const wrapper = createElement("div", { id: request.id }, ["tab-content-inner"], "Loading..")
|
2023-04-03 23:39:34 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const tabContent = createElement("div", { id: `tab-content-${request.id}` }, ["tab-content"], wrapper)
|
|
|
|
tabsContentWrapper.insertAdjacentElement("beforeend", tabContent)
|
2023-04-03 23:39:34 +02:00
|
|
|
|
|
|
|
linkTabContents(tab)
|
|
|
|
|
|
|
|
function replaceContent(resultFactory) {
|
|
|
|
if (resultFactory === undefined || resultFactory === null) {
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
const result = typeof resultFactory === "function" ? resultFactory() : resultFactory
|
2023-04-03 23:39:34 +02:00
|
|
|
if (result instanceof Promise) {
|
|
|
|
result.then(replaceContent)
|
|
|
|
} else if (result instanceof Node) {
|
|
|
|
wrapper.replaceChildren(result)
|
|
|
|
} else {
|
|
|
|
wrapper.innerHTML = result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceContent(request.content)
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
tab.addEventListener("click", (e) => {
|
2023-04-03 23:39:34 +02:00
|
|
|
const timesOpened = +(tab.dataset.timesOpened || 0) + 1
|
|
|
|
tab.dataset.timesOpened = timesOpened
|
|
|
|
|
|
|
|
if (request.onOpen) {
|
|
|
|
const result = request.onOpen(
|
|
|
|
{
|
|
|
|
contentElement: wrapper,
|
|
|
|
labelElement,
|
|
|
|
timesOpened,
|
2023-04-28 12:20:44 +02:00
|
|
|
firstOpen: timesOpened === 1,
|
2023-04-03 23:39:34 +02:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
e
|
2023-04-03 23:39:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
replaceContent(result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-05-02 12:30:16 +02:00
|
|
|
|
|
|
|
/* TOAST NOTIFICATIONS */
|
|
|
|
function showToast(message, duration = 5000, error = false) {
|
|
|
|
const toast = document.createElement("div");
|
|
|
|
toast.classList.add("toast-notification");
|
|
|
|
if (error === true) {
|
|
|
|
toast.classList.add("toast-notification-error");
|
|
|
|
}
|
|
|
|
toast.innerHTML = message;
|
|
|
|
document.body.appendChild(toast);
|
|
|
|
|
|
|
|
// Set the position of the toast on the screen
|
|
|
|
const toastCount = document.querySelectorAll(".toast-notification").length;
|
|
|
|
const toastHeight = toast.offsetHeight;
|
|
|
|
const previousToastsHeight = Array.from(document.querySelectorAll(".toast-notification"))
|
|
|
|
.slice(0, -1) // exclude current toast
|
|
|
|
.reduce((totalHeight, toast) => totalHeight + toast.offsetHeight + 10, 0); // add 10 pixels for spacing
|
|
|
|
toast.style.bottom = `${10 + previousToastsHeight}px`;
|
|
|
|
toast.style.right = "10px";
|
|
|
|
|
|
|
|
// Delay the removal of the toast until animation has completed
|
|
|
|
const removeToast = () => {
|
|
|
|
toast.classList.add("hide");
|
|
|
|
const removeTimeoutId = setTimeout(() => {
|
|
|
|
toast.remove();
|
|
|
|
// Adjust the position of remaining toasts
|
|
|
|
const remainingToasts = document.querySelectorAll(".toast-notification");
|
|
|
|
const removedToastBottom = toast.getBoundingClientRect().bottom;
|
|
|
|
|
|
|
|
remainingToasts.forEach((toast) => {
|
|
|
|
if (toast.getBoundingClientRect().bottom < removedToastBottom) {
|
|
|
|
toast.classList.add("slide-down");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for the slide-down animation to complete
|
|
|
|
setTimeout(() => {
|
|
|
|
// Remove the slide-down class after the animation has completed
|
|
|
|
const slidingToasts = document.querySelectorAll(".slide-down");
|
|
|
|
slidingToasts.forEach((toast) => {
|
|
|
|
toast.classList.remove("slide-down");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Adjust the position of remaining toasts again, in case there are multiple toasts being removed at once
|
|
|
|
const remainingToastsDown = document.querySelectorAll(".toast-notification");
|
|
|
|
let heightSoFar = 0;
|
|
|
|
remainingToastsDown.forEach((toast) => {
|
|
|
|
toast.style.bottom = `${10 + heightSoFar}px`;
|
|
|
|
heightSoFar += toast.offsetHeight + 10; // add 10 pixels for spacing
|
|
|
|
});
|
|
|
|
}, 0); // The duration of the slide-down animation (in milliseconds)
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove the toast after specified duration
|
|
|
|
setTimeout(removeToast, duration);
|
|
|
|
}
|