|
| 1 | +Upcase.PersistentToast = class { |
| 2 | + static dismissedIdsLocalStorageKey = "persistent_toasts-dismissed_ids"; |
| 3 | + |
| 4 | + static get all() { |
| 5 | + return [...document.getElementsByClassName("persistent-toast")] |
| 6 | + .map(element => new this(element)); |
| 7 | + } |
| 8 | + |
| 9 | + static get dismissedIds() { |
| 10 | + return new Set( |
| 11 | + JSON.parse( |
| 12 | + localStorage.getItem(this.dismissedIdsLocalStorageKey) |
| 13 | + ) |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + static set dismissedIds(value) { |
| 18 | + localStorage.setItem( |
| 19 | + this.dismissedIdsLocalStorageKey, |
| 20 | + JSON.stringify( |
| 21 | + Array.from(value) |
| 22 | + ) |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + static addDismissedId(value) { |
| 27 | + if (!this.dismissedIds.has(value)) { |
| 28 | + this.dismissedIds = this.dismissedIds.add(value); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static initializeAll() { |
| 33 | + this.all.forEach((persistentToast) => { |
| 34 | + persistentToast.initialize(); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + constructor(element) { |
| 39 | + this.element = element; |
| 40 | + this.id = element.id; |
| 41 | + } |
| 42 | + |
| 43 | + get dismissButtonElements() { |
| 44 | + return this.element.getElementsByClassName("toast-button-dismiss"); |
| 45 | + } |
| 46 | + |
| 47 | + initialize() { |
| 48 | + if (this.isDismissed()) { |
| 49 | + this.remove(); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + [...this.dismissButtonElements].forEach((dismissButtonElement) => { |
| 54 | + dismissButtonElement.addEventListener("click", () => { |
| 55 | + this.dismiss(); |
| 56 | + }); |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + isDismissed() { |
| 61 | + return this.constructor.dismissedIds.has(this.id); |
| 62 | + } |
| 63 | + |
| 64 | + dismiss() { |
| 65 | + this.remove(); |
| 66 | + this.constructor.addDismissedId(this.id); |
| 67 | + } |
| 68 | + |
| 69 | + remove() { |
| 70 | + this.element.remove(); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +window.addEventListener("DOMContentLoaded", () => { |
| 75 | + Upcase.PersistentToast.initializeAll(); |
| 76 | +}); |
0 commit comments