-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
184 lines (178 loc) · 5.31 KB
/
util.js
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
// Q: how do i import all files from ./assets into the project's ./assets/toast folder when installed?
// A:
// rewite the above into the new syntax
import $ from "jquery";
import "./util.css";
import "./theme.css";
("use strict");
/** TOAST **/
class Toast {
constructor(message, type, duration, iconPath = "", action = "") {
this.message = message;
this.type = type;
this.duration = duration;
this.icon = iconPath;
this.action = action;
this.showToast();
}
showToast() {
$(".toast:not(.noremove)").remove();
let overlay = document.createElement("div"),
toast = document.createElement("div");
toast.classList.add("toast");
overlay.classList.add("toast-overlay");
toast.classList.add(this.type);
if (this.icon != "") {
toast.innerHTML += `<img alt="icon" src="${this.icon}" class="toast-icon" alt="Toast Popup Icon">`;
}
toast.innerHTML += this.message;
if (this.action != "") {
document.body.appendChild(overlay);
}
document.body.appendChild(toast);
setTimeout(() => {
$(toast).css({ animation: "slideOut 0.5s forwards" });
}, this.duration);
setTimeout(() => {
overlay.remove();
toast.remove();
if (this.action != "" && this.action != "") {
window.location.href = this.action;
}
}, this.duration + 500);
}
}
class ErrorToast extends Toast {
constructor(message, err, duration, action = "") {
message += ": " + err;
super(message, "default", duration, require("./assets/error-icon.svg"), action);
}
}
class WarningToast extends Toast {
constructor(message, duration, action = "") {
super(message, "default", duration, require("./assets/warning-icon.svg"), action);
}
}
class SuccessToast extends Toast {
constructor(message, duration, action = "") {
super(message, "default", duration, require("./assets/success-icon.svg"), action);
}
}
/** POPUP **/
class Popup {
constructor(message, type, duration, iconPath = "", action = "") {
if (typeof message == "object") {
this.title = message[0];
this.message = message[1];
} else {
this.message = message;
}
this.type = type;
this.duration = duration;
this.icon = iconPath;
this.action = action;
this.showPopup();
}
showPopup() {
$(".popup:not(.noremove), .popup-overlay:not(.noremove)").remove();
let overlay = $("<div></div>", { class: "popup-overlay " + this.type }),
toast = $("<div></div>", { class: "popup" }),
buttons = $("<div></div>", { id: "popup-buttons" });
for (let actionInfo of this.action) {
$("<button></button>", {
class: "popup-button " + (actionInfo[2] ? actionInfo[2] : ""),
onclick: actionInfo[0],
text: actionInfo[1],
}).appendTo(buttons);
}
if (this.icon) {
$(`<img alt='Popup Icon' src="${this.icon}" class="popup-icon">`).appendTo(toast);
}
if (this.title) {
$("<div></div>", { text: this.title, class: "popup-title" }).appendTo(toast);
}
$("<div></div>", { text: this.message, class: "popup-text" }).appendTo(toast);
buttons.appendTo(toast);
if (this.action) {
overlay.appendTo(document.body);
}
toast.appendTo(document.body);
setTimeout(() => {
$(toast).css({ animation: "popupout 0.25s forwards" });
$(overlay).css({ animation: "fadeout 0.5s forwards" });
}, this.duration);
setTimeout(() => {
toast.remove();
overlay.remove();
}, this.duration + 500);
}
}
function removePopup() {
$(".popup").css({ animation: "popupout 0.25s forwards" });
$(".popup-overlay").css({ animation: "fadeout 0.5s forwards" });
setTimeout(() => {
$(".popup:not(.noremove)").remove();
$(".popup-overlay:not(.noremove)").remove();
}, 500);
}
$(document.body).on("click", ".popup-overlay", function () {
removePopup();
});
/** Other **/
function placeholderToast() {
return new Toast(
"This feature hasn't been implemented yet, sorry! 🤫",
"default",
1500,
require("./assets/unimplemented-icon.svg")
);
}
//! placeholder action
$("[placeholdaction]").click(function () {
placeholderToast();
});
function cleanError(error) {
switch (error.code) {
case "auth/invalid-email":
return "Invalid email";
case "auth/user-disabled":
return "User disabled";
case "auth/user-not-found":
return "User not found";
case "auth/wrong-password":
return "Incorrect password";
case "auth/email-already-in-use":
return "Email already in use, try logging in";
case "auth/weak-password":
return "Password is too weak";
case "auth/operation-not-allowed":
return "Operation not allowed";
case "auth/too-many-requests":
return "Too many requests";
case "auth/popup-closed-by-user":
return "Popup closed by user";
case "auth/popup-blocked":
return "Popup blocked";
case "auth/user-cancelled":
return "Action cancelled by user";
case "auth/unauthorized-domain":
return "Can't login from this domain";
case "auth/cancelled-popup-request":
return "New popup opened, cancelling previous request";
default:
return error.message.replace("Error ", "");
}
}
// expose functions
window.removePopup = removePopup;
export {
removePopup,
Popup,
Toast,
ErrorToast,
WarningToast,
SuccessToast,
cleanError,
$,
placeholderToast,
};