usability improvements

This commit is contained in:
Alex Yatskov 2017-02-25 19:14:44 -08:00
parent e2d49a975b
commit 465a8e21c0
6 changed files with 45 additions and 81 deletions

View File

@ -28,7 +28,7 @@ function getFormData() {
return optionsLoad().then(optionsOld => { return optionsLoad().then(optionsOld => {
const optionsNew = $.extend(true, {}, optionsOld); const optionsNew = $.extend(true, {}, optionsOld);
optionsNew.general.autoStart = $('#activate-on-startup').prop('checked'); optionsNew.general.enable = $('#enable-search').prop('checked');
optionsNew.general.audioPlayback = $('#audio-playback-buttons').prop('checked'); optionsNew.general.audioPlayback = $('#audio-playback-buttons').prop('checked');
optionsNew.general.groupResults = $('#group-terms-results').prop('checked'); optionsNew.general.groupResults = $('#group-terms-results').prop('checked');
optionsNew.general.softKatakana = $('#soft-katakana-search').prop('checked'); optionsNew.general.softKatakana = $('#soft-katakana-search').prop('checked');
@ -87,7 +87,7 @@ $(document).ready(() => {
Handlebars.partials = Handlebars.templates; Handlebars.partials = Handlebars.templates;
optionsLoad().then(options => { optionsLoad().then(options => {
$('#activate-on-startup').prop('checked', options.general.autoStart); $('#enable-search').prop('checked', options.general.enable);
$('#audio-playback-buttons').prop('checked', options.general.audioPlayback); $('#audio-playback-buttons').prop('checked', options.general.audioPlayback);
$('#group-terms-results').prop('checked', options.general.groupResults); $('#group-terms-results').prop('checked', options.general.groupResults);
$('#soft-katakana-search').prop('checked', options.general.softKatakana); $('#soft-katakana-search').prop('checked', options.general.softKatakana);
@ -172,9 +172,7 @@ function populateDictionaries(options) {
$('.dict-enabled, .dict-priority').change(onOptionsChanged); $('.dict-enabled, .dict-priority').change(onOptionsChanged);
$('.dict-delete').click(onDictionaryDelete); $('.dict-delete').click(onDictionaryDelete);
}).catch(error => { }).catch(showDictionaryError).then(() => {
showDictionaryError(error);
}).then(() => {
showDictionarySpinner(false); showDictionarySpinner(false);
if (dictCount === 0) { if (dictCount === 0) {
dictWarning.show(); dictWarning.show();
@ -191,13 +189,17 @@ function onDictionaryPurge(e) {
const dictControls = $('#dict-importer, #dict-groups').hide(); const dictControls = $('#dict-importer, #dict-groups').hide();
const dictProgress = $('#dict-purge-progress').show(); const dictProgress = $('#dict-purge-progress').show();
return database().purge().catch(error => { return database().purge().catch(showDictionaryError).then(() => {
showDictionaryError(error);
}).then(() => {
showDictionarySpinner(false); showDictionarySpinner(false);
dictControls.show(); dictControls.show();
dictProgress.hide(); dictProgress.hide();
return optionsLoad().then(options => populateDictionaries(options)); return optionsLoad();
}).then(options => {
options.dictionaries = {};
return optionsSave(options).then(() => {
yomichan().setOptions(options);
populateDictionaries(options);
});
}); });
} }
@ -214,13 +216,11 @@ function onDictionaryDelete() {
setProgress(0.0); setProgress(0.0);
database().deleteDictionary(dictGroup.data('title'), (total, current) => setProgress(current / total * 100.0)).catch(error => { database().deleteDictionary(dictGroup.data('title'), (total, current) => setProgress(current / total * 100.0)).catch(showDictionaryError).then(() => {
showDictionaryError(error);
}).then(() => {
showDictionarySpinner(false); showDictionarySpinner(false);
dictProgress.hide(); dictProgress.hide();
dictControls.show(); dictControls.show();
return optionsLoad().then(options => populateDictionaries(options)); return optionsLoad().then(populateDictionaries);
}); });
} }
@ -241,11 +241,7 @@ function onDictionaryImport() {
database().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { database().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => {
options.dictionaries[summary.title] = {enabled: true, priority: 0}; options.dictionaries[summary.title] = {enabled: true, priority: 0};
return optionsSave(options).then(() => yomichan().setOptions(options)); return optionsSave(options).then(() => yomichan().setOptions(options));
}).then(() => { }).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => {
return populateDictionaries(options);
}).catch(error => {
showDictionaryError(error);
}).then(() => {
showDictionarySpinner(false); showDictionarySpinner(false);
dictProgress.hide(); dictProgress.hide();
dictImporter.show(); dictImporter.show();
@ -330,13 +326,7 @@ function populateAnkiDeckAndModel(options) {
populateAnkiFields($('#anki-terms-model').val(options.anki.terms.model), options), populateAnkiFields($('#anki-terms-model').val(options.anki.terms.model), options),
populateAnkiFields($('#anki-kanji-model').val(options.anki.kanji.model), options) populateAnkiFields($('#anki-kanji-model').val(options.anki.kanji.model), options)
]); ]);
}).then(() => { }).then(() => ankiFormat.show()).catch(showAnkiError).then(() => showAnkiSpinner(false));
ankiFormat.show();
}).catch(error => {
showAnkiError(error);
}).then(() => {
showAnkiSpinner(false);
});
} }
function populateAnkiFields(element, options) { function populateAnkiFields(element, options) {
@ -386,11 +376,7 @@ function onAnkiModelChanged(e) {
optionsNew.anki[tabId].fields = {}; optionsNew.anki[tabId].fields = {};
populateAnkiFields(element, optionsNew).then(() => { populateAnkiFields(element, optionsNew).then(() => {
optionsSave(optionsNew).then(() => yomichan().setOptions(optionsNew)); optionsSave(optionsNew).then(() => yomichan().setOptions(optionsNew));
}).catch(error => { }).catch(showAnkiError).then(() => showAnkiSpinner(false));
showAnkiError(error);
}).then(() => {
showAnkiSpinner(false);
});
}); });
} }
@ -409,9 +395,5 @@ function onOptionsChanged(e) {
return populateAnkiDeckAndModel(optionsNew); return populateAnkiDeckAndModel(optionsNew);
} }
}); });
}).catch(error => { }).catch(showAnkiError).then(() => showAnkiSpinner(false));
showAnkiError(error);
}).then(() => {
showAnkiSpinner(false);
});
} }

View File

@ -20,7 +20,7 @@
function optionsSetDefaults(options) { function optionsSetDefaults(options) {
const defaults = { const defaults = {
general: { general: {
autoStart: true, enable: true,
audioPlayback: true, audioPlayback: true,
groupResults: true, groupResults: true,
softKatakana: true, softKatakana: true,

View File

@ -26,18 +26,12 @@ 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.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.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
chrome.browserAction.onClicked.addListener(e => chrome.runtime.openOptionsPage());
this.translator.prepare().then(optionsLoad).then(options => { this.translator.prepare().then(optionsLoad).then(this.setOptions.bind(this));
this.setOptions(options);
if (this.options.general.autoStart) {
this.setEnabled(true);
}
});
} }
onInstalled(details) { onInstalled(details) {
@ -57,19 +51,26 @@ class Yomichan {
return true; return true;
} }
onBrowserAction() { // setEnabled(enabled) {
this.setEnabled(!this.enabled); // this.enabled = enabled;
} // this.tabInvokeAll('setEnabled', this.enabled);
// chrome.browserAction.setBadgeText({text: enabled ? '' : 'off'});
setEnabled(enabled) { // }
this.enabled = enabled;
this.tabInvokeAll('setEnabled', this.enabled);
chrome.browserAction.setBadgeText({text: enabled ? '' : 'off'});
}
setOptions(options) { setOptions(options) {
this.options = options; this.options = options;
let usable = false;
for (const title in options.dictionaries) {
if (options.dictionaries[title].enabled) {
usable = true;
break;
}
}
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
chrome.browserAction.setBadgeText({text: usable ? '' : '!'});
if (options.anki.enable) { if (options.anki.enable) {
this.anki = new AnkiConnect(this.options.anki.server); this.anki = new AnkiConnect(this.options.anki.server);
} else { } else {
@ -132,10 +133,6 @@ class Yomichan {
return note; return note;
} }
api_getEnabled({callback}) {
callback({result: this.enabled});
}
api_getOptions({callback}) { api_getOptions({callback}) {
promiseCallback(optionsLoad(), callback); promiseCallback(optionsLoad(), callback);
} }

View File

@ -26,7 +26,7 @@
<h3>General Options</h3> <h3>General Options</h3>
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" id="activate-on-startup"> Activate on startup</label> <label><input type="checkbox" id="enable-search"> Enable search</label>
</div> </div>
<div class="checkbox"> <div class="checkbox">

View File

@ -24,21 +24,16 @@ class Driver {
this.lastMousePos = null; this.lastMousePos = null;
this.lastTextSource = null; this.lastTextSource = null;
this.pendingLookup = false; this.pendingLookup = false;
this.enabled = false;
this.options = null; this.options = null;
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); getOptions().then(options => {
window.addEventListener('mouseover', this.onMouseOver.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this));
window.addEventListener('resize', e => this.searchClear());
Promise.all([getOptions(), isEnabled()]).then(([options, enabled]) => {
this.options = options; this.options = options;
this.enabled = enabled; window.addEventListener('mouseover', this.onMouseOver.bind(this));
}).catch(error => { window.addEventListener('mousedown', this.onMouseDown.bind(this));
this.handleError(error); window.addEventListener('mousemove', this.onMouseMove.bind(this));
}); window.addEventListener('resize', e => this.searchClear());
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
}).catch(this.handleError.bind(this));
} }
popupTimerSet(callback) { popupTimerSet(callback) {
@ -63,7 +58,7 @@ class Driver {
this.lastMousePos = {x: e.clientX, y: e.clientY}; this.lastMousePos = {x: e.clientX, y: e.clientY};
this.popupTimerClear(); this.popupTimerClear();
if (!this.enabled) { if (!this.options.general.enable) {
return; return;
} }
@ -198,12 +193,6 @@ class Driver {
api_setOptions(options) { api_setOptions(options) {
this.options = options; this.options = options;
} }
api_setEnabled(enabled) {
if (!(this.enabled = enabled)) {
this.searchClear();
}
}
} }
window.driver = new Driver(); window.driver = new Driver();

View File

@ -38,10 +38,6 @@ function showError(error) {
window.alert(`Error: ${error}`); window.alert(`Error: ${error}`);
} }
function isEnabled() {
return invokeBgApi('getEnabled', {});
}
function getOptions() { function getOptions() {
return invokeBgApi('getOptions', {}); return invokeBgApi('getOptions', {});
} }