summaryrefslogtreecommitdiff
path: root/WebInterface/NodeJSServer/src/modules/ui/backdrop.js
blob: 1a24bd21ae8b68e565272a8047ac7c5615bea1b8 (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
// Showing / Hiding the backdrop menu

/**
 * Class for adding functionality to backdrop elements
 */
export default class Backdrop {
  /**
   * Registers all important elements in the backdrop
   * @param {string} backdropMenu ID of Backdrop Menu
   * @param {string} frontLayer ID of Front Layer
   * @param {string} menuButton ID of Show / Hide Menu Button
   */
  constructor(backdropMenu, frontLayer, menuButton) {
    this.backdrop = document.getElementById(backdropMenu);
    this.frontLayer = document.getElementById(frontLayer);
    this.menuButton = document.getElementById(menuButton);
  }

  /**
   * Registers all neccessary events
   */
  register() {
    this.registerButtonEvent();
    this.registerFrontLayerEvent();
  }

  /**
   * Registers showing / hiding through menu button
   */
  registerButtonEvent() {
    this.menuButton.addEventListener('click', () => {
      // Hide / Unhide Backdrop Menu
      if (this.backdrop.classList.contains('hidden')) {
        this.backdrop.classList.remove('hidden');
      } else {
        this.backdrop.classList.add('hidden');
      }

      // Set open state for menu button
      if (this.menuButton.classList.contains('open')) {
        this.menuButton.classList.remove('open');
      } else {
        this.menuButton.classList.add('open');
      }
    });
  }

  /**
   * Registers hiding, when clicking on the front layer
   */
  registerFrontLayerEvent() {
    // Hide menu when interacting with front layer
    this.frontLayer.addEventListener('click', () => {
      this.backdrop.classList.add('hidden');
      this.menuButton.classList.remove('open');
    });
  }
}