This commit is contained in:
Alex Yatskov 2017-03-04 18:24:57 -08:00
parent b039d30024
commit 30999c13d3
7 changed files with 79 additions and 71 deletions

View File

@ -5,7 +5,6 @@
<script src="/mixed/lib/dexie.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script>
<script src="/bg/js/templates.js"></script>
<script src="/bg/js/gecko.js"></script>
<script src="/bg/js/util.js"></script>
<script src="/bg/js/anki-connect.js"></script>
<script src="/bg/js/anki-null.js"></script>

View File

@ -189,7 +189,7 @@ function onDictionaryPurge(e) {
options.dictionaries = {};
return optionsSave(options).then(() => {
populateDictionaries(options);
instYomi().setOptions(options);
instYomi().optionsSet(options);
});
});
}
@ -208,7 +208,7 @@ function onDictionaryImport() {
optionsLoad().then(options => {
instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => {
options.dictionaries[summary.title] = {enabled: true, priority: 0};
return optionsSave(options).then(() => instYomi().setOptions(options));
return optionsSave(options).then(() => instYomi().optionsSet(options));
}).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => {
showDictionarySpinner(false);
dictProgress.hide();
@ -339,7 +339,7 @@ function onAnkiModelChanged(e) {
optionsNew.anki[tabId].fields = {};
populateAnkiFields(element, optionsNew).then(() => {
optionsSave(optionsNew).then(() => instYomi().setOptions(optionsNew));
optionsSave(optionsNew).then(() => instYomi().optionsSet(optionsNew));
}).catch(showAnkiError).then(() => showAnkiSpinner(false));
});
}
@ -351,7 +351,7 @@ function onOptionsChanged(e) {
getFormData().then(({optionsNew, optionsOld}) => {
return optionsSave(optionsNew).then(() => {
instYomi().setOptions(optionsNew);
instYomi().optionsSet(optionsNew);
updateVisibility(optionsNew);
const ankiUpdated =

View File

@ -28,7 +28,7 @@ $(document).ready(() => {
toggle.bootstrapToggle();
toggle.change(() => {
options.general.enable = toggle.prop('checked');
optionsSave(options).then(() => instYomi().setOptions(options));
optionsSave(options).then(() => instYomi().optionsSet(options));
});
});
});

View File

@ -32,27 +32,10 @@ window.yomichan = new class {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
}
this.translator.prepare().then(optionsLoad).then(this.setOptions.bind(this));
this.translator.prepare().then(optionsLoad).then(this.optionsSet.bind(this));
}
onInstalled(details) {
if (details.reason === 'install') {
chrome.tabs.create({url: chrome.extension.getURL('bg/guide.html')});
}
}
onMessage(request, sender, callback) {
const {action, params} = request, method = this['api_' + action];
if (typeof(method) === 'function') {
params.callback = callback;
method.call(this, params);
}
return true;
}
setOptions(options) {
optionsSet(options) {
this.options = options;
let usable = false;
@ -72,7 +55,7 @@ window.yomichan = new class {
this.anki = new AnkiNull();
}
this.tabInvokeAll('setOptions', this.options);
this.tabInvokeAll('optionsSet', this.options);
}
tabInvokeAll(action, params) {
@ -83,7 +66,7 @@ window.yomichan = new class {
});
}
formatNote(definition, mode) {
noteFormat(definition, mode) {
const note = {fields: {}, tags: this.options.anki.tags};
let fields = [];
@ -145,7 +128,7 @@ window.yomichan = new class {
}
definitionAdd(definition, mode) {
const note = this.formatNote(definition, mode);
const note = this.noteFormat(definition, mode);
return this.anki.addNote(note);
}
@ -153,7 +136,7 @@ window.yomichan = new class {
const notes = [];
for (const definition of definitions) {
for (const mode of modes) {
notes.push(this.formatNote(definition, mode));
notes.push(this.noteFormat(definition, mode));
}
}
@ -176,27 +159,45 @@ window.yomichan = new class {
return Promise.resolve(Handlebars.templates[template](data));
}
api_optionsGet({callback}) {
promiseCallback(optionsLoad(), callback);
onInstalled(details) {
if (details.reason === 'install') {
chrome.tabs.create({url: chrome.extension.getURL('bg/guide.html')});
}
}
api_kanjiFind({text, callback}) {
promiseCallback(this.kanjiFind(text), callback);
}
onMessage(request, sender, callback) {
const handlers = new class {
api_optionsGet({callback}) {
promiseCallback(optionsLoad(), callback);
}
api_termsFind({text, callback}) {
promiseCallback(this.termsFind(text), callback);
}
api_kanjiFind({text, callback}) {
promiseCallback(this.kanjiFind(text), callback);
}
api_textRender({template, data, callback}) {
promiseCallback(this.textRender(template, data), callback);
}
api_termsFind({text, callback}) {
promiseCallback(this.termsFind(text), callback);
}
api_definitionAdd({definition, mode, callback}) {
promiseCallback(this.definitionAdd(definition, mode), callback);
}
api_textRender({template, data, callback}) {
promiseCallback(this.textRender(template, data), callback);
}
api_definitionsAddable({definitions, modes, callback}) {
promiseCallback(this.definitionsAddable(definitions, modes), callback);
api_definitionAdd({definition, mode, callback}) {
promiseCallback(this.definitionAdd(definition, mode), callback);
}
api_definitionsAddable({definitions, modes, callback}) {
promiseCallback(this.definitionsAddable(definitions, modes), callback);
}
};
const {action, params} = request, method = handlers[`api_${action}`];
if (typeof(method) === 'function') {
params.callback = callback;
method.call(this, params);
}
return true;
}
};

View File

@ -41,31 +41,37 @@ window.displayFrame = new class extends Display {
handleError(error) {
if (window.orphaned) {
this.api_showOrphaned();
this.showOrphaned();
} else {
errorShow(error);
}
}
showOrphaned() {
$('#content').hide();
$('#orphan').show();
}
onMessage(e) {
const {action, params} = e.originalEvent.data, method = this['api_' + action];
const handlers = new class {
api_showTermDefs({definitions, options, context}) {
window.scrollTo(0, 0);
this.showTermDefs(definitions, options, context);
}
api_showKanjiDefs({definitions, options, context}) {
window.scrollTo(0, 0);
this.showKanjiDefs(defintions, options, context);
}
api_showOrphaned() {
this.showOrphaned();
}
};
const {action, params} = e.originalEvent.data, method = handlers[`api_${action}`];
if (typeof(method) === 'function') {
method.call(this, params);
}
}
api_showTermDefs({definitions, options, context}) {
window.scrollTo(0, 0);
super.showTermDefs(definitions, options, context);
}
api_showKanjiDefs({definitions, options, context}) {
window.scrollTo(0, 0);
super.showKanjiDefs(defintions, options, context);
}
api_showOrphaned() {
$('#content').hide();
$('#orphan').show();
}
};

View File

@ -102,7 +102,13 @@ class Driver {
}
onBgMessage({action, params}, sender, callback) {
const method = this['api_' + action];
const handlers = new class {
api_optionsSet(options) {
this.options = options;
}
};
const method = handlers[`api_${action}`];
if (typeof(method) === 'function') {
method.call(this, params);
}
@ -205,10 +211,6 @@ class Driver {
errorShow(error);
}
}
api_setOptions(options) {
this.options = options;
}
}
window.driver = new Driver();

View File

@ -67,13 +67,13 @@ class Display {
this.textRender('terms.html', params).then(content => {
this.container.html(content);
$('.action-add-note').click(this.onActionAddNote.bind(this));
$('.kanji-link').click(this.onKanjiSearch.bind(this));
$('.action-play-audio').click(this.onActionPlayAudio.bind(this));
$('.kanji-link').click(e => this.onKanjiSearch(e, options));
return this.adderButtonsUpdate(['term_kanji', 'term_kana'], sequence);
}).catch(this.handleError.bind(this));
}
showKanjiDefs({definitions, options, context}) {
showKanjiDefs(definitions, options, context) {
const sequence = ++this.sequence;
const params = {
definitions,
@ -122,11 +122,11 @@ class Display {
});
}
onKanjiSearch(e) {
onKanjiSearch(e, options) {
e.preventDefault();
const character = $(e.target).text();
this.kanjiFind(character).then(definitions => {
this.api_showKanjiDefs({definitions, options, context});
this.showKanjiDefs(definitions, options, context);
}).catch(this.handleError.bind(this));
}