summaryrefslogtreecommitdiff
path: root/WebInterface/NodeJSServer/src/js/modules/ui/components/notification-banner.js
blob: a527725be445e6c40bb815143e7372165c685f38 (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
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
/**
 * Object containing a message for the notification banner
 */
class BannerItem {
  /**
   * Creates new Banner Message Items
   * @param {String} name Name the message will be referenced under
   * @param {String} content Content, either formatted as plain text or html
   * @param {Boolean} html Is content formatted as html?
   */
  constructor(name, content, html) {
    this.name = name;
    this.content = content;
    this.html = html;
  }
}

/**
 * Class for controlling the Notification banner
 */
export default class BannerController {
  /**
   * Creates references to objects and hides notification banner
   * @param {Interface} iface Interface to receive comm. from
   * @param {string} bannerId ID of Notification Banner
   * @param {string} textP ID of Notification Banner text field
   * @param {string} dismissBtn ID of dismiss button
   * @param {string} badge ID of badge (# of notifications)
   */
  constructor(iface, bannerId, textP, dismissBtn, badge) {
    iface.addObject(this, 'notifications', ['show', 'hide']);
    this.iface = iface;

    this.ids = {bannerId, textP, dismissBtn, badge};
  }

  /**
   * Initializes the Banner in the DOM
   */
  initialize() {
    this.banner = document.getElementById(this.ids.bannerId);
    this.bannerText = document.getElementById(this.ids.textP);
    this.dismissBtn = document.getElementById(this.ids.dismissBtn);
    this.notificationBadge = document.getElementById(this.ids.badge);
    this.bannerMsgs = [];

    this.banner.classList.add('hidden'); // Hide banner by default
    this.registerEvents();
  }

  /**
   * Registers events for notification banner
   */
  registerEvents() {
    this.registerDismissEvent();
  }

  /**
   * Registers dismissing via dismiss button
   */
  registerDismissEvent() {
    this.dismissBtn.addEventListener('click', () => {
      this.dismissCurrent();
    });
  }

  /**
   * Pushes a new message to the notification banner and shows it
   * @param {string} name Name to register notification (referenced in hide)
   * @param {string} text Notification text
   */
  show(name, text) {
    let bannerItem = new BannerItem(name, text, false);
    this.bannerMsgs.push(bannerItem);

    this.update();
  }

  /**
   * Removes notification from banner
   * @param {string} name The name the notification was registered under
   */
  hide(name) {
    if (name) this.bannerMsgs = this.bannerMsgs.filter(elt => elt.name != name);
    else this.bannerMsgs = [];

    this.update();
  }

  /**
   * Dismisses the currently shown message
   */
  dismissCurrent() {
    this.hide(this.current);
  }

  /**
   * Updates the notification banner with the most recent message
   */
  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.content;
    const isHtml = lastNotification.html;
    this.banner.classList.remove('hidden');

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

    this.current = name;
    this.updateNotificationBadge();
  }

  /**
   * Updates the notification badge number
   */
  updateNotificationBadge() {
    if (this.bannerMsgs.length < 2) {
      this.notificationBadge.classList.add('hidden');
    } else if (this.bannerMsgs.length > 9) {
      this.notificationBadge.classList.remove('hidden');
      this.notificationBadge.textContent = '∞';
    } else {
      this.notificationBadge.classList.remove('hidden');
      this.notificationBadge.textContent = this.bannerMsgs.length.toString();
    }
  }
}