summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/droplab/hook_button.js
blob: af45eba74e7042de1502e07ed09c76d32ccdc234 (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
import Hook from './hook';

class HookButton extends Hook {
  constructor(trigger, list, plugins, config) {
    super(trigger, list, plugins, config);

    this.type = 'button';
    this.event = 'click';

    this.eventWrapper = {};

    this.addEvents();
    this.addPlugins();
  }

  addPlugins() {
    this.plugins.forEach(plugin => plugin.init(this));
  }

  clicked(e) {
    const buttonEvent = new CustomEvent('click.dl', {
      detail: {
        hook: this,
      },
      bubbles: true,
      cancelable: true,
    });
    e.target.dispatchEvent(buttonEvent);

    this.list.toggle();
  }

  addEvents() {
    this.eventWrapper.clicked = this.clicked.bind(this);
    this.trigger.addEventListener('click', this.eventWrapper.clicked);
  }

  removeEvents() {
    this.trigger.removeEventListener('click', this.eventWrapper.clicked);
  }

  restoreInitialState() {
    this.list.list.innerHTML = this.list.initialState;
  }

  removePlugins() {
    this.plugins.forEach(plugin => plugin.destroy());
  }

  destroy() {
    this.restoreInitialState();

    this.removeEvents();
    this.removePlugins();
  }
}

export default HookButton;