BigW Consortium Gitlab

hook_input.js 2.6 KB
Newer Older
Luke "Jared" Bennett committed
1 2
import Hook from './hook';

3 4 5
class HookInput extends Hook {
  constructor(trigger, list, plugins, config) {
    super(trigger, list, plugins, config);
Luke "Jared" Bennett committed
6

7 8
    this.type = 'input';
    this.event = 'input';
Luke "Jared" Bennett committed
9

10
    this.eventWrapper = {};
Luke "Jared" Bennett committed
11

12 13 14
    this.addEvents();
    this.addPlugins();
  }
Luke "Jared" Bennett committed
15

16
  addPlugins() {
Luke "Jared" Bennett committed
17
    this.plugins.forEach(plugin => plugin.init(this));
18
  }
Luke "Jared" Bennett committed
19

20
  addEvents() {
Luke "Jared" Bennett committed
21 22 23 24 25 26 27 28 29
    this.eventWrapper.mousedown = this.mousedown.bind(this);
    this.eventWrapper.input = this.input.bind(this);
    this.eventWrapper.keyup = this.keyup.bind(this);
    this.eventWrapper.keydown = this.keydown.bind(this);

    this.trigger.addEventListener('mousedown', this.eventWrapper.mousedown);
    this.trigger.addEventListener('input', this.eventWrapper.input);
    this.trigger.addEventListener('keyup', this.eventWrapper.keyup);
    this.trigger.addEventListener('keydown', this.eventWrapper.keydown);
30
  }
Luke "Jared" Bennett committed
31

32
  removeEvents() {
Luke "Jared" Bennett committed
33 34 35 36 37 38
    this.hasRemovedEvents = true;

    this.trigger.removeEventListener('mousedown', this.eventWrapper.mousedown);
    this.trigger.removeEventListener('input', this.eventWrapper.input);
    this.trigger.removeEventListener('keyup', this.eventWrapper.keyup);
    this.trigger.removeEventListener('keydown', this.eventWrapper.keydown);
39
  }
Luke "Jared" Bennett committed
40

41 42
  input(e) {
    if (this.hasRemovedEvents) return;
Luke "Jared" Bennett committed
43 44 45 46 47 48 49 50 51

    this.list.show();

    const inputEvent = new CustomEvent('input.dl', {
      detail: {
        hook: this,
        text: e.target.value,
      },
      bubbles: true,
52
      cancelable: true,
Luke "Jared" Bennett committed
53 54
    });
    e.target.dispatchEvent(inputEvent);
55
  }
Luke "Jared" Bennett committed
56

57
  mousedown(e) {
Luke "Jared" Bennett committed
58 59 60 61 62 63 64 65 66 67 68
    if (this.hasRemovedEvents) return;

    const mouseEvent = new CustomEvent('mousedown.dl', {
      detail: {
        hook: this,
        text: e.target.value,
      },
      bubbles: true,
      cancelable: true,
    });
    e.target.dispatchEvent(mouseEvent);
69
  }
Luke "Jared" Bennett committed
70

71
  keyup(e) {
Luke "Jared" Bennett committed
72 73 74
    if (this.hasRemovedEvents) return;

    this.keyEvent(e, 'keyup.dl');
75
  }
Luke "Jared" Bennett committed
76

77
  keydown(e) {
Luke "Jared" Bennett committed
78 79 80
    if (this.hasRemovedEvents) return;

    this.keyEvent(e, 'keydown.dl');
81
  }
Luke "Jared" Bennett committed
82

83
  keyEvent(e, eventName) {
Luke "Jared" Bennett committed
84 85 86 87 88 89 90 91 92 93 94 95 96
    this.list.show();

    const keyEvent = new CustomEvent(eventName, {
      detail: {
        hook: this,
        text: e.target.value,
        which: e.which,
        key: e.key,
      },
      bubbles: true,
      cancelable: true,
    });
    e.target.dispatchEvent(keyEvent);
97
  }
Luke "Jared" Bennett committed
98

99
  restoreInitialState() {
Luke "Jared" Bennett committed
100
    this.list.list.innerHTML = this.list.initialState;
101
  }
Luke "Jared" Bennett committed
102

103
  removePlugins() {
Luke "Jared" Bennett committed
104
    this.plugins.forEach(plugin => plugin.destroy());
105
  }
Luke "Jared" Bennett committed
106

107
  destroy() {
Luke "Jared" Bennett committed
108 109 110 111 112 113 114
    this.restoreInitialState();

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

    this.list.destroy();
  }
115
}
Luke "Jared" Bennett committed
116 117

export default HookInput;