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

View File

@ -45,10 +45,10 @@ function sanitizeOptions(options) {
return options;
}
function loadOptions(callback) {
chrome.storage.sync.get(null, (items) => callback(sanitizeOptions(items)));
function loadOptions() {
return new Promise((resolve, reject) => chrome.storage.sync.get(null, resolve));
}
function saveOptions(opts, callback) {
chrome.storage.sync.set(sanitizeOptions(opts), callback);
function saveOptions(opts) {
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.onUpdated.addListener(this.onTabReady.bind(this));
loadOptions(opts => {
loadOptions().then(opts => {
this.setOptions(opts);
if (this.options.activateOnStartup) {
this.setState('loading');