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/dexie.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script> <script src="/mixed/lib/wanakana.min.js"></script>
<script src="/bg/js/templates.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/util.js"></script>
<script src="/bg/js/anki-connect.js"></script> <script src="/bg/js/anki-connect.js"></script>
<script src="/bg/js/anki-null.js"></script> <script src="/bg/js/anki-null.js"></script>

View File

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

View File

@ -28,7 +28,7 @@ $(document).ready(() => {
toggle.bootstrapToggle(); toggle.bootstrapToggle();
toggle.change(() => { toggle.change(() => {
options.general.enable = toggle.prop('checked'); 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)); 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) { optionsSet(options) {
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) {
this.options = options; this.options = options;
let usable = false; let usable = false;
@ -72,7 +55,7 @@ window.yomichan = new class {
this.anki = new AnkiNull(); this.anki = new AnkiNull();
} }
this.tabInvokeAll('setOptions', this.options); this.tabInvokeAll('optionsSet', this.options);
} }
tabInvokeAll(action, params) { 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}; const note = {fields: {}, tags: this.options.anki.tags};
let fields = []; let fields = [];
@ -145,7 +128,7 @@ window.yomichan = new class {
} }
definitionAdd(definition, mode) { definitionAdd(definition, mode) {
const note = this.formatNote(definition, mode); const note = this.noteFormat(definition, mode);
return this.anki.addNote(note); return this.anki.addNote(note);
} }
@ -153,7 +136,7 @@ window.yomichan = new class {
const notes = []; const notes = [];
for (const definition of definitions) { for (const definition of definitions) {
for (const mode of modes) { for (const mode of modes) {
notes.push(this.formatNote(definition, mode)); notes.push(this.noteFormat(definition, mode));
} }
} }
@ -176,6 +159,14 @@ window.yomichan = new class {
return Promise.resolve(Handlebars.templates[template](data)); return Promise.resolve(Handlebars.templates[template](data));
} }
onInstalled(details) {
if (details.reason === 'install') {
chrome.tabs.create({url: chrome.extension.getURL('bg/guide.html')});
}
}
onMessage(request, sender, callback) {
const handlers = new class {
api_optionsGet({callback}) { api_optionsGet({callback}) {
promiseCallback(optionsLoad(), callback); promiseCallback(optionsLoad(), callback);
} }
@ -200,3 +191,13 @@ window.yomichan = new class {
promiseCallback(this.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) { handleError(error) {
if (window.orphaned) { if (window.orphaned) {
this.api_showOrphaned(); this.showOrphaned();
} else { } else {
errorShow(error); errorShow(error);
} }
} }
onMessage(e) { showOrphaned() {
const {action, params} = e.originalEvent.data, method = this['api_' + action]; $('#content').hide();
if (typeof(method) === 'function') { $('#orphan').show();
method.call(this, params);
}
} }
onMessage(e) {
const handlers = new class {
api_showTermDefs({definitions, options, context}) { api_showTermDefs({definitions, options, context}) {
window.scrollTo(0, 0); window.scrollTo(0, 0);
super.showTermDefs(definitions, options, context); this.showTermDefs(definitions, options, context);
} }
api_showKanjiDefs({definitions, options, context}) { api_showKanjiDefs({definitions, options, context}) {
window.scrollTo(0, 0); window.scrollTo(0, 0);
super.showKanjiDefs(defintions, options, context); this.showKanjiDefs(defintions, options, context);
} }
api_showOrphaned() { api_showOrphaned() {
$('#content').hide(); this.showOrphaned();
$('#orphan').show(); }
};
const {action, params} = e.originalEvent.data, method = handlers[`api_${action}`];
if (typeof(method) === 'function') {
method.call(this, params);
}
} }
}; };

View File

@ -102,7 +102,13 @@ class Driver {
} }
onBgMessage({action, params}, sender, callback) { 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') { if (typeof(method) === 'function') {
method.call(this, params); method.call(this, params);
} }
@ -205,10 +211,6 @@ class Driver {
errorShow(error); errorShow(error);
} }
} }
api_setOptions(options) {
this.options = options;
}
} }
window.driver = new Driver(); window.driver = new Driver();

View File

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