File size: 5,620 Bytes
b5ba7a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
/**
* Enum representing the location of the notifications
* @readonly
* @enum {string}
*/
const NotificationLocation = {
TOPLEFT: "top-left",
TOPCENTER: "top-center",
TOPRIGHT: "top-right",
BOTTOMLEFT: "bottom-left",
BOTTOMCENTER: "bottom-center",
BOTTOMRIGHT: "bottom-right",
};
/**
* Enum representing notification types
* @readonly
* @enum {string}
*/
const NotificationType = {
INFO: "info",
ERROR: "error",
WARN: "warn",
SUCCESS: "success",
};
/**
* Responsible for the notification system
*/
const notifications = {
/** @type {NotificationLocation} */
_location: NotificationLocation.BOTTOMLEFT,
/** @type {NotificationLocation} */
get location() {
return this.location;
},
/** @type {NotificationLocation} */
set location(location) {
this._location = location;
},
// Notification Area Element
_areaEl: null,
// Dialog BG Element
_dialogBGEl: null,
// Dialog Element
_dialogEl: null,
/**
* Creates a simple notification for the user. Equivalent to alert()
*
* @param {string | HTMLElement} message Message to display to the user.
* @param {Object} options Extra options for the notification.
* @param {NotificationType} options.type Notification type
* @param {boolean} options.collapsed Whether notification is collapsed by default
* @param {number} options.timeout Timeout for the notification
*/
notify(message, options = {}) {
defaultOpt(options, {
type: NotificationType.INFO,
collapsed: false,
timeout: config.notificationTimeout,
});
const notificationEl = document.createElement("div");
notificationEl.classList.add("notification", options.type);
if (!options.collapsed) notificationEl.classList.add("expanded");
notificationEl.title = new Date().toISOString();
notificationEl.addEventListener("click", () =>
notificationEl.classList.toggle("expanded")
);
const contentEl = document.createElement("div");
contentEl.classList.add("notification-content");
contentEl.innerHTML = message;
notificationEl.append(contentEl);
const closeBtn = document.createElement("button");
closeBtn.classList.add("notification-closebtn");
closeBtn.addEventListener("click", () => notificationEl.remove());
notificationEl.append(closeBtn);
if (
config.notificationHighlightAnimationDuration &&
config.notificationHighlightAnimationDuration > 0
) {
const notificationHighlightEl = document.createElement("div");
notificationHighlightEl.style.animationDuration = `${config.notificationHighlightAnimationDuration}ms`;
notificationHighlightEl.classList.add(
"notification-highlight",
`notification-${options.type}`
);
document.body.appendChild(notificationHighlightEl);
setTimeout(() => {
notificationHighlightEl.remove();
}, config.notificationHighlightAnimationDuration);
}
this._areaEl.prepend(notificationEl);
if (options.timeout)
setTimeout(() => {
if (this._areaEl.contains(notificationEl)) {
notificationEl.remove();
}
}, options.timeout);
},
/**
* Creates a dialog box for the user with set options.
*
* @param {string} title The title of the dialog box to be displayed to the user.
* @param {string | HTMLElement} message The message to be displayed to the user.
* @param {Object} options Extra options for the dialog.
* @param {Array<{label: string, value: any}>} options.choices The choices to be displayed to the user.
*/
async dialog(title, message, options = {}) {
defaultOpt(options, {
// By default, it is a await notifications.dialogation dialog
choices: [
{label: "No", value: false},
{label: "Yes", value: true},
],
});
const titleEl = this._dialogEl.querySelector(".dialog-title");
titleEl.textContent = title;
const contentEl = this._dialogEl.querySelector(".dialog-content");
contentEl.innerHTML = message;
const choicesEl = this._dialogEl.querySelector(".dialog-choices");
while (choicesEl.firstChild) {
choicesEl.firstChild.remove();
}
return new Promise((resolve, reject) => {
options.choices.forEach((choice) => {
const choiceBtn = document.createElement("button");
choiceBtn.textContent = choice.label;
choiceBtn.addEventListener("click", () => {
this._dialogBGEl.style.display = "none";
this._dialogEl.style.display = "none";
resolve(choice.value);
});
choicesEl.append(choiceBtn);
});
this._dialogBGEl.style.display = "flex";
this._dialogEl.style.display = "block";
});
},
};
var k = 0;
window.addEventListener("DOMContentLoaded", () => {
// Creates the notification area
const notificationArea = document.createElement("div");
notificationArea.classList.add(
"notification-area",
NotificationLocation.BOTTOMLEFT
);
notifications._areaEl = notificationArea;
document.body.appendChild(notificationArea);
// Creates the dialog box element
const dialogBG = document.createElement("div");
dialogBG.classList.add("dialog-bg");
dialogBG.style.display = "none";
const dialogEl = document.createElement("div");
dialogEl.classList.add("dialog");
dialogEl.style.display = "none";
const titleEl = document.createElement("div");
titleEl.classList.add("dialog-title");
const contentEl = document.createElement("div");
contentEl.classList.add("dialog-content");
const choicesEl = document.createElement("div");
choicesEl.classList.add("dialog-choices");
dialogEl.append(titleEl);
dialogEl.append(contentEl);
dialogEl.append(choicesEl);
dialogBG.append(dialogEl);
notifications._dialogEl = dialogEl;
notifications._dialogBGEl = dialogBG;
document.body.appendChild(dialogBG);
});
|