aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/common/index.js
blob: 98a23374f6e7d89802a81fe258452dc10ea76da5 (plain) (blame)
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
import InputComponent from './input';
import KeymapperComponent from './keymapper';
import FollowComponent from './follow';
import * as inputActions from 'content/actions/input';
import messages from 'shared/messages';

export default class Common {
  constructor(win, store) {
    const follow = new FollowComponent(win, store);
    const input = new InputComponent(win.document.body, store);
    const keymapper = new KeymapperComponent(store);

    input.onKey((key, ctrl) => follow.key(key, ctrl));
    input.onKey((key, ctrl) => keymapper.key(key, ctrl));

    this.store = store;
    this.children = [
      follow,
      input,
      keymapper,
    ];

    this.reloadSettings();

    messages.onMessage(this.onMessage.bind(this));
  }

  update() {
    this.children.forEach(c => c.update());
  }

  onMessage(message) {
    switch (message.type) {
    case messages.SETTINGS_CHANGED:
      this.reloadSettings();
    }
  }

  reloadSettings() {
    browser.runtime.sendMessage({
      type: messages.SETTINGS_QUERY,
    }).then((settings) => {
      this.store.dispatch(inputActions.setKeymaps(settings.keymaps));
    });
  }
}