| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class Notification { |
| 22 |
constructor(params) { |
| 23 |
this._timer = null; |
| 24 |
this._params = params; |
| 25 |
this._started = 0; |
| 26 |
this._element = this._create_element(); |
| 27 |
} |
| 28 |
|
| 29 |
element() { |
| 30 |
return this._element; |
| 31 |
} |
| 32 |
|
| 33 |
_create_element() { |
| 34 |
let el = document.createElement("div"); |
| 35 |
el.setAttribute("class", "notification"); |
| 36 |
if (this._params.type === "error") |
| 37 |
el.classList.add("notif-error"); |
| 38 |
else if (this._params.type === "warn") |
| 39 |
el.classList.add("notif-warn"); |
| 40 |
else |
| 41 |
el.classList.add("notif-info"); |
| 42 |
{ |
| 43 |
let text = this._params.text; |
| 44 |
if (typeof(text) !== "object") |
| 45 |
text = [ text ]; |
| 46 |
for (let i = 0; ; ) { |
| 47 |
el.appendChild(document.createTextNode(text[i])); |
| 48 |
++i; |
| 49 |
if (i == text.length) |
| 50 |
break; |
| 51 |
el.appendChild(document.createElement("br")); |
| 52 |
} |
| 53 |
} |
| 54 |
let btn = document.createElement("button"); |
| 55 |
btn.setAttribute("type", "button"); |
| 56 |
btn.setAttribute("class", "notif-close"); |
| 57 |
btn.appendChild(document.createTextNode("x")); |
| 58 |
el.appendChild(btn); |
| 59 |
el.addEventListener("click", function(event) { |
| 60 |
if (event.target.classList.contains("notif-close")) |
| 61 |
this._remove(); |
| 62 |
}.bind(this)); |
| 63 |
el.addEventListener("mouseover", function() { |
| 64 |
this._hold(); |
| 65 |
}.bind(this)); |
| 66 |
el.addEventListener("mouseout", function() { |
| 67 |
this._release(); |
| 68 |
}.bind(this)); |
| 69 |
if (this._params.delay > 0) |
| 70 |
this._set_timeout(); |
| 71 |
return el; |
| 72 |
} |
| 73 |
|
| 74 |
_set_timeout() { |
| 75 |
let delay = this._params.delay; |
| 76 |
if (this._started) |
| 77 |
delay -= Date.now() - this._started; |
| 78 |
else |
| 79 |
this._started = Date.now(); |
| 80 |
if (delay > 0) { |
| 81 |
this._timer = setTimeout(function() { |
| 82 |
this._dissolve(); |
| 83 |
}.bind(this), delay); |
| 84 |
} |
| 85 |
else |
| 86 |
this._dissolve(); |
| 87 |
} |
| 88 |
|
| 89 |
_hold() { |
| 90 |
if (this._timer) { |
| 91 |
clearTimeout(this._timer); |
| 92 |
this._timer = null; |
| 93 |
this._element.classList.remove("invisible"); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
_release () { |
| 98 |
if (this._params.delay > 0) |
| 99 |
this._set_timeout(); |
| 100 |
} |
| 101 |
|
| 102 |
_dissolve() { |
| 103 |
this._element.classList.add("invisible"); |
| 104 |
this._timer = setTimeout(function() { |
| 105 |
this._timer = null; |
| 106 |
this._remove(); |
| 107 |
}.bind(this), 2000); |
| 108 |
} |
| 109 |
|
| 110 |
_remove() { |
| 111 |
if (this._timer) { |
| 112 |
clearTimeout(this._timer); |
| 113 |
this._timer = null; |
| 114 |
} |
| 115 |
this._element.remove(); |
| 116 |
if (this._params.name) |
| 117 |
Notification.names.delete(this._params.name); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
Notification.add = function(params) { |
| 122 |
for (let key in Notification.defaults) { |
| 123 |
if (params[key] === undefined) |
| 124 |
params[key] = Notification.defaults[key]; |
| 125 |
} |
| 126 |
let notif = new Notification(params); |
| 127 |
document.getElementById("notifications-block").appendChild(notif.element()); |
| 128 |
if (params.name) { |
| 129 |
if (Notification.names.has(params.name)) |
| 130 |
Notification.names.get(params.name)._remove(); |
| 131 |
Notification.names.set(params.name, notif); |
| 132 |
} |
| 133 |
return notif; |
| 134 |
} |
| 135 |
|
| 136 |
Notification.defaults = { |
| 137 |
type: "info", |
| 138 |
delay: 5000 |
| 139 |
}; |
| 140 |
|
| 141 |
Notification.names = new Map(); |