1 | const CDN_BASE = 'https://unpkg.com/@code_monk/falert@latest/dist/';
|
2 |
|
3 | class Falert {
|
4 | static instances = new Set<HTMLElement>();
|
5 | static settings = {
|
6 | insertionNode: document.body,
|
7 | audio: {
|
8 | volume: 0.25
|
9 | },
|
10 | sounds: {}
|
11 | };
|
12 | static {
|
13 |
|
14 | let sounds = {
|
15 | fatal: CDN_BASE + 'assets/sounds/dramatic.mp3',
|
16 | warning: CDN_BASE + 'assets/sounds/concerning.mp3',
|
17 | notice: CDN_BASE + 'assets/sounds/noteworthy.mp3'
|
18 | };
|
19 | for (const [k, url] of Object.entries(sounds)) {
|
20 | const a = new Audio();
|
21 | a.addEventListener("error", console.error);
|
22 | a.volume = Falert.settings.audio.volume;
|
23 | a.src = url;
|
24 | Falert.settings.sounds[k] = a;
|
25 | }
|
26 | };
|
27 | static html = `
|
28 | <div class="falert container flyin">
|
29 | <div class="falert body breathing">
|
30 | <h2>Alert!</h2>
|
31 | <p>You can't put your finger in the socket</p>
|
32 | </div>
|
33 | </div>
|
34 | `;
|
35 |
|
36 | constructor(head: string, body: string, type: string = "notice") {
|
37 | const t = document.createElement('template');
|
38 | t.innerHTML = Falert.html.trim();
|
39 | const domNode = <HTMLDivElement>t.content.firstChild;
|
40 | domNode.style.zIndex = (Falert.getHighestZindex() + 1).toString(10);
|
41 |
|
42 |
|
43 | let newTop = 35;
|
44 | for (let i of Falert.instances) {
|
45 | newTop += i.offsetHeight;
|
46 | }
|
47 | domNode.style.top = newTop + 'px';
|
48 | domNode.querySelector('.body').classList.add(type);
|
49 | domNode.addEventListener("animationend", ev => {
|
50 | domNode.classList.remove('flyin');
|
51 | domNode.classList.add('swaying');
|
52 | });
|
53 | domNode.addEventListener('click', ev => {
|
54 | Falert.instances.delete(domNode);
|
55 | domNode.remove();
|
56 | });
|
57 | domNode.querySelector('.body h2').innerHTML = head;
|
58 | domNode.querySelector('.body p').innerHTML = body;
|
59 | domNode.classList.add(type);
|
60 | document.body.appendChild(domNode);
|
61 | Falert.instances.add(domNode);
|
62 | Falert.settings.sounds[type].play();
|
63 | }
|
64 |
|
65 | static getHighestZindex(): number {
|
66 | let z: number = 0;
|
67 | for (let f of Falert.instances) {
|
68 | let i = Number(f.style.zIndex);
|
69 | if (i > z) {
|
70 | z = i;
|
71 | }
|
72 | }
|
73 | return z;
|
74 | }
|
75 |
|
76 | }
|
77 |
|
78 | export { Falert }; |
\ | No newline at end of file |