summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/ui/components/backdrop.js
blob: 82ca64f66ff03ad7e356fa28016b6e5194cd30bd (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
/**
 * 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.ids = {backdropMenu, frontLayer, menuButton};
  }

  /**
   * Initializes the components from the ids defined in the constructor
   */
  initialize() {
    this.open = false;
    this.backdrop = document.getElementById(this.ids.backdropMenu);
    this.frontLayer = document.getElementById(this.ids.frontLayer);
    this.menuButton = document.getElementById(this.ids.menuButton);

    this.registerEvents();
  }

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

  /**
   * Registers showing / hiding through menu button
   */
  registerButtonEvent() {
    this.menuButton.addEventListener('click', () => {
      // Change open state
      this.open = !this.open;

      // Hide / Unhide Backdrop Menu
      this.open ? this.backdrop.classList.remove('hidden') :
          this.backdrop.classList.add('hidden');

      // Set open state for menu button
      this.open ? this.menuButton.classList.add('open') :
          this.menuButton.classList.remove('open');
    });
  }

  /**
   * Registers hiding upon front layer interaction
   */
  registerFrontLayerEvent() {
    this.frontLayer.addEventListener('click', () => {
      if (!this.open) return; // It's already closed

      this.open = false;
      this.backdrop.classList.add('hidden');
      this.menuButton.classList.remove('open');
    });
  }
}