fixing enabled state

This commit is contained in:
Alex Yatskov 2017-01-27 21:21:05 -08:00
parent 79194bcafc
commit 1d49473c1f
2 changed files with 13 additions and 37 deletions

View File

@ -26,16 +26,16 @@ class Yomichan {
this.translator = new Translator(); this.translator = new Translator();
this.anki = new AnkiNull(); this.anki = new AnkiNull();
this.options = null; this.options = null;
this.setState('disabled'); this.setEnabled(false);
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
optionsLoad().then(options => { this.translator.prepare().then(optionsLoad).then(options => {
this.setOptions(options); this.setOptions(options);
if (this.options.general.autoStart) { if (this.options.general.autoStart) {
this.setState('loading'); this.setEnabled(true);
} }
}); });
} }
@ -58,37 +58,13 @@ class Yomichan {
} }
onBrowserAction() { onBrowserAction() {
switch (this.state) { this.setEnabled(!this.enabled);
case 'disabled':
this.setState('loading');
break;
case 'enabled':
this.setState('disabled');
break;
}
} }
setState(state) { setEnabled(enabled) {
if (this.state === state) { this.enabled = enabled;
return; this.tabInvokeAll('setEnabled', this.enabled);
} chrome.browserAction.setBadgeText({text: enabled ? '' : 'off'});
this.state = state;
switch (state) {
case 'disabled':
chrome.browserAction.setBadgeText({text: 'off'});
break;
case 'enabled':
chrome.browserAction.setBadgeText({text: ''});
break;
case 'loading':
chrome.browserAction.setBadgeText({text: '...'});
this.translator.prepare().then(this.setState('enabled'));
break;
}
this.tabInvokeAll('setEnabled', this.state === 'enabled');
} }
setOptions(options) { setOptions(options) {
@ -154,7 +130,7 @@ class Yomichan {
} }
api_getEnabled({callback}) { api_getEnabled({callback}) {
callback({result: this.state === 'enabled'}); callback({result: this.enabled});
} }
api_getOptions({callback}) { api_getOptions({callback}) {

View File

@ -34,8 +34,8 @@ class Driver {
window.addEventListener('keydown', this.onKeyDown.bind(this)); window.addEventListener('keydown', this.onKeyDown.bind(this));
window.addEventListener('resize', e => this.searchClear()); window.addEventListener('resize', e => this.searchClear());
getOptions().then(opts => { getOptions().then(options => {
this.options = opts; this.options = options;
return isEnabled(); return isEnabled();
}).then(enabled => { }).then(enabled => {
this.enabled = enabled; this.enabled = enabled;
@ -208,8 +208,8 @@ class Driver {
this.lastTextSource = null; this.lastTextSource = null;
} }
api_setOptions(opts) { api_setOptions(options) {
this.options = opts; this.options = options;
} }
api_setEnabled(enabled) { api_setEnabled(enabled) {