-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationSystem.js
More file actions
89 lines (82 loc) · 2.37 KB
/
notificationSystem.js
File metadata and controls
89 lines (82 loc) · 2.37 KB
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
console.log(this + ": Loading notification system")
var notifArea = document.createElement('div');
var style = document.createElement('link');
if (window.localStorage.getItem('notifications') == null || window.localStorage.getItem('notifications') == "undefined") {
window.localStorage.setItem('notifications', '[]')
}
var closingAnimations = 0
style.rel = 'stylesheet'
style.href = 'styles/css/notifications.css'
notifArea.classList.add('notifArea');
notifArea.id = 'notifArea';
document.body.append(style);
document.body.append(notifArea);
class Notifsys {
constructor(key) {
this.storageKey = key
}
get notifList() {
return JSON.parse(window.localStorage.getItem(this.storageKey))
}
set notifList(notifList) {
window.localStorage.setItem(this.storageKey, JSON.stringify(notifList))
}
appendNotif(content, refresh) {
let tmpnotif = {}
let tmplist = this.notifList
tmpnotif.content = content
tmplist.push(tmpnotif)
this.notifList = tmplist
if (refresh == true) {
refreshNotifications()
}
}
}
var notifsys = new Notifsys('notifications')
function removeNotif(notifID) {
if (typeof notifID !== "number") {
return console.error(this + ": TypeError, expected number, got " + typeof notifID)
}
let x = notifsys.notifList
x.splice(notifID, 1)
notifsys.notifList = x
}
function refreshNotifications() {
let notifArea = document.getElementById('notifArea');
notifArea.innerHTML = ""
let notifs = notifsys.notifList;
for (i in notifs) {
let x = notifs[i];
let y = document.createElement('div');
let text = document.createElement('div');
let closeButton = document.createElement('button');
text.id = i
closeButton.addEventListener('click', evt => {
evt.path[0].classList.add('closingNotif')
evt.path[2].classList.add('closingNotif')
let x = evt.path[1].id
x = parseInt(x, 10)
removeNotif(x)
closingAnimations += 1
setTimeout(function () {
evt.path[2].classList.add('closedNotif')
closingAnimations -= 1
if (closingAnimations <= 0) {
refreshNotifications()
}
}, 1000)
})
y.addEventListener('click', evt => {
console.log(evt.path)
})
text.innerHTML = x.content;
closeButton.classList.add('closeButton')
closeButton.innerHTML = 'Dismiss'
text.classList.add('text')
y.classList.add('notification')
notifArea.appendChild(y)
y.appendChild(text)
text.appendChild(closeButton)
};
};
refreshNotifications()