diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-02-20 16:11:50 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-02-20 16:15:08 +0100 |
| commit | 73a5c7fae9ffbe9ada721148c8c454a643aceebe (patch) | |
| tree | c8fad013ed9b5dd589add87f8d45cf02bbfc6e91 /public/projects/js-small-apps/budget-app/lib/class-notification.js | |
| parent | b01239fbdcc5bbc5921f73ec0e8fee7bedd5c8e8 (diff) | |
chore!: restructure repo
I separated public files from the config/dev files. It improves repo
readability.
I also moved dotenv helper to public/inc directory and extract the
Matomo tracker in the same directory.
Diffstat (limited to 'public/projects/js-small-apps/budget-app/lib/class-notification.js')
| -rw-r--r-- | public/projects/js-small-apps/budget-app/lib/class-notification.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/public/projects/js-small-apps/budget-app/lib/class-notification.js b/public/projects/js-small-apps/budget-app/lib/class-notification.js new file mode 100644 index 0000000..17a32a0 --- /dev/null +++ b/public/projects/js-small-apps/budget-app/lib/class-notification.js @@ -0,0 +1,89 @@ +class Notification { + #id = 0; + #title = ""; + #message = ""; + #type = ""; + #duration = 0; + #position = ""; + + constructor(message, type) { + this.#message = message; + this.#type = type; + } + + get id() { + return this.#id; + } + + set title(string) { + this.#title = string; + } + + get title() { + return this.#title; + } + + set message(text) { + this.#message = text; + } + + get message() { + return this.#message; + } + + set type(string) { + this.#type = string; + } + + get type() { + return this.#type; + } + + set duration(number) { + this.#duration = number; + } + + get duration() { + return this.#duration; + } + + set position(string) { + this.#position = string; + } + + get position() { + return this.#position; + } + + #getWrapper() { + let wrapper = document.getElementById("notifications-center"); + + if (!wrapper) { + wrapper = document.createElement("div"); + wrapper.id = "notifications-center"; + wrapper.classList = "notifications"; + document.body.appendChild(wrapper); + } + + return wrapper; + } + + notify() { + const notification = document.createElement("div"); + notification.textContent = this.message; + notification.classList.add("notification", `notification--${this.type}`); + + const wrapper = this.#getWrapper(); + document.body.style.position = "relative"; + wrapper.style.cssText = `position: fixed;${this.position}: 1rem;right: 1rem;display: flex;flex-flow: column wrap;gap: 1rem;`; + wrapper.appendChild(notification); + + if (this.duration && Number(this.duration) !== 0) { + setTimeout(() => { + notification.remove(); + }, this.duration); + } + } +} + +export default Notification; |
