summaryrefslogtreecommitdiff
path: root/WebInterface/NodeJSServer/src/modules/ui/notification-banner.js
blob: d14845ed52a38a16a907eaa41f8c8bd8a7db9cbe (plain)
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
export default class BannerController {
  constructor(bannerId, textP, dismissBtn) {
    this.banner = document.getElementById(bannerId);
    this.bannerText = document.getElementById(textP);
    this.dismissBtn = document.getElementById(dismissBtn);
    this.bannerMsgs = [];

    // Hide Banner after JS loading finished
    this.banner.classList.add('hidden');
  }

  register() {
    this.dismissBtn.addEventListener('click', () => {
      this.dismissCurrent();
    });
  }

  show(name, text) {
    let bannerItem = {name, text, 'html': false};
    this.bannerMsgs.push(bannerItem);

    this.update();
  }

  hide(name) {
    if (!name) {
      this.bannerMsgs = [];
      this.banner.classList.add('hidden');
    } else {
      for (let i = 0; i < this.bannerMsgs.length; i++) {
        if (this.bannerMsgs[i].name == name) {
          this.bannerMsgs.splice(i, 1);
        }
      }

      this.update();
    }
  }

  dismissCurrent() {
    this.hide(this.current);
  }

  update() {
    if (this.bannerMsgs.length === 0) {
      this.banner.classList.add('hidden');
      return;
    }

    const lastNotification = this.bannerMsgs[this.bannerMsgs.length - 1];
    const name = lastNotification.name;
    const text = lastNotification.text;
    const html = lastNotification.html;
    this.banner.classList.remove('hidden');

    if (html) this.bannerText.innerHTML = text;
    else this.bannerText.innerText = text;

    this.current = name;
  }

  showPersistence() {
    let text = `Storage persistence is disabled, so in-browser storage of created Wikis might not work.\n` +
        `Click <a href="#" onclick="
          event.preventDefault();
          navigator.storage.persist().then((persistent) => {
            if (persistent) notificationManager.show('storageSuccess', 'Storage persistence successfully turned on.');
            else notificationManager.show('storageFail', 'Storage persistence has been rejected.');
          });
        ">here</a> to enable storage persistence.`;
    let bannerItem = {'name': 'persistence', text, 'html': true};
    this.bannerMsgs.push(bannerItem);
    this.update();
  }
}