Options cleanup

This commit is contained in:
Alex Yatskov 2016-09-13 13:38:37 -07:00
parent 540d0e239c
commit cd4f16c096
3 changed files with 20 additions and 17 deletions

View File

@ -60,8 +60,8 @@ function modelIdToMarkers(id) {
}[id]; }[id];
} }
function formToOptions(section, callback) { function formToOptions(section) {
loadOptions((optsOld) => { return loadOptions().then(optsOld => {
const optsNew = $.extend({}, optsOld); const optsNew = $.extend({}, optsOld);
switch (section) { switch (section) {
@ -86,7 +86,10 @@ function formToOptions(section, callback) {
break; break;
} }
callback(sanitizeOptions(optsNew), sanitizeOptions(optsOld)); return {
optsNew: sanitizeOptions(optsNew),
optsOld: sanitizeOptions(optsOld)
};
}); });
} }
@ -185,8 +188,8 @@ function onOptionsGeneralChanged(e) {
return; return;
} }
formToOptions('general', (optsNew, optsOld) => { formToOptions('general').then(({optsNew, optsOld}) => {
saveOptions(optsNew, () => { saveOptions(optsNew).then(() => {
yomichan().setOptions(optsNew); yomichan().setOptions(optsNew);
if (!optsOld.enableAnkiConnect && optsNew.enableAnkiConnect) { if (!optsOld.enableAnkiConnect && optsNew.enableAnkiConnect) {
updateAnkiStatus(); updateAnkiStatus();
@ -210,23 +213,23 @@ function onOptionsAnkiChanged(e) {
return; return;
} }
formToOptions('anki', (opts) => { formToOptions('anki').then(({optsNew, optsOld}) => {
saveOptions(opts, () => yomichan().setOptions(opts)); saveOptions(optsNew).then(() => yomichan().setOptions(optsNew));
}); });
} }
function onAnkiModelChanged(e) { function onAnkiModelChanged(e) {
if (e.originalEvent) { if (e.originalEvent) {
formToOptions('anki', (opts) => { formToOptions('anki').then(({optsNew, optsOld}) => {
opts[modelIdToFieldOptKey($(this).id)] = {}; optsNew[modelIdToFieldOptKey($(this).id)] = {};
populateAnkiFields($(this), opts); populateAnkiFields($(this), optsNew);
saveOptions(opts, () => yomichan().setOptions(opts)); saveOptions(optsNew).then(() => yomichan().setOptions(optsNew));
}); });
} }
} }
$(document).ready(() => { $(document).ready(() => {
loadOptions((opts) => { loadOptions().then(opts => {
$('#scan-length').val(opts.scanLength); $('#scan-length').val(opts.scanLength);
$('#activate-on-startup').prop('checked', opts.activateOnStartup); $('#activate-on-startup').prop('checked', opts.activateOnStartup);
$('#load-enamdict').prop('checked', opts.loadEnamDict); $('#load-enamdict').prop('checked', opts.loadEnamDict);

View File

@ -45,10 +45,10 @@ function sanitizeOptions(options) {
return options; return options;
} }
function loadOptions(callback) { function loadOptions() {
chrome.storage.sync.get(null, (items) => callback(sanitizeOptions(items))); return new Promise((resolve, reject) => chrome.storage.sync.get(null, resolve));
} }
function saveOptions(opts, callback) { function saveOptions(opts) {
chrome.storage.sync.set(sanitizeOptions(opts), callback); return new Promise((resolve, reject) => chrome.storage.sync.set(sanitizeOptions(opts), resolve));
} }

View File

@ -33,7 +33,7 @@ class Yomichan {
chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id)); chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id));
chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this)); chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this));
loadOptions(opts => { loadOptions().then(opts => {
this.setOptions(opts); this.setOptions(opts);
if (this.options.activateOnStartup) { if (this.options.activateOnStartup) {
this.setState('loading'); this.setState('loading');