diff options
| author | Karan Jayachandra <mail@karanjayachandra.com> | 2025-11-13 21:48:40 +0100 |
|---|---|---|
| committer | Karan Jayachandra <mail@karanjayachandra.com> | 2025-11-13 21:48:40 +0100 |
| commit | 944fc2715e967a51e4ccca49d87e987ee2727bc2 (patch) | |
| tree | 13ed83b34182d54f9aa85a63c2474a927a6e06b2 /src/utils.mjs | |
| parent | b2befab1d7c0bb19ac7373ea27d22cec0eea5163 (diff) | |
Transitioned to npm
Diffstat (limited to 'src/utils.mjs')
| -rw-r--r-- | src/utils.mjs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/utils.mjs b/src/utils.mjs new file mode 100644 index 0000000..06a2519 --- /dev/null +++ b/src/utils.mjs @@ -0,0 +1,51 @@ +export class Manager { + constructor(description, listData) { + this.description = description; + this.listData = listData; + this.count = listData.length; + } + store() { + let stringData = JSON.stringify(this.listData); + localStorage.setItem(this.description, stringData); + } + render() { + let renderedHTML = this.listData + .map((i) => { + return i.render(); + }) + .join(""); + let elementId = this.description + "_list"; + document.getElementById(elementId).innerHTML = renderedHTML; + this.addBehaviour(); + } + addBehaviour() { + for (let idx = 0; idx < this.listData.length; ++idx) { + const elementId = this.description + "_" + this.listData[idx].id; + const p = document.getElementById(elementId); + p.addEventListener("click", () => { + this.listData[idx].toggle(); + this.store(); + p.classList.toggle("is-success"); + p.classList.toggle("is-danger"); + }); + } + } + active() { + return this.listData.filter(function (c) { + return c.status; + }); + } + inactive() { + return this.listData.filter(function (c) { + return !c.status; + }); + } +} + +export function shuffle(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; +} |
