aboutsummaryrefslogtreecommitdiff
path: root/src/background/components/indicator.js
blob: cceb11927796538caa264181a02baa6aa1ba8ac6 (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
import * as indicators from '../shared/indicators';
import messages from 'shared/messages';

export default class IndicatorComponent {
  constructor(store) {
    this.store = store;

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

    browser.browserAction.onClicked.addListener(this.onClicked);
    browser.tabs.onActivated.addListener((info) => {
      return browser.tabs.query({ currentWindow: true }).then(() => {
        return this.onTabActivated(info);
      });
    });
  }

  onTabActivated(info) {
    return browser.tabs.sendMessage(info.tabId, {
      type: messages.ADDON_ENABLED_QUERY,
    }).then((resp) => {
      return this.updateIndicator(resp.enabled);
    });
  }

  onClicked(tab) {
    browser.tabs.sendMessage(tab.id, {
      type: messages.ADDON_TOGGLE_ENABLED,
    });
  }

  onMessage(message) {
    switch (message.type) {
    case messages.ADDON_ENABLED_RESPONSE:
      return this.updateIndicator(message.enabled);
    }
  }

  updateIndicator(enabled) {
    if (enabled) {
      return indicators.enable();
    }
    return indicators.disable();
  }
}