lots of fixes to backend

This commit is contained in:
Alex Yatskov 2017-08-14 21:43:09 -07:00
parent 82863cd861
commit bdf231082f
17 changed files with 87 additions and 61 deletions

View File

@ -30,13 +30,10 @@
<script src="/mixed/lib/jquery.min.js"></script> <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script> <script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script src="/mixed/lib/handlebars.min.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/dictionary.js"></script>
<script src="/bg/js/options.js"></script> <script src="/bg/js/options.js"></script>
<script src="/mixed/js/japanese.js"></script> <script src="/bg/js/util.js"></script>
<script src="/mixed/js/request.js"></script>
<script src="/bg/js/context.js"></script> <script src="/bg/js/context.js"></script>
</body> </body>

View File

@ -17,6 +17,10 @@
*/ */
/*
* AnkiConnect
*/
class AnkiConnect { class AnkiConnect {
constructor(server) { constructor(server) {
this.server = server; this.server = server;
@ -68,6 +72,11 @@ class AnkiConnect {
} }
} }
/*
* AnkiNull
*/
class AnkiNull { class AnkiNull {
async addNote(note) { async addNote(note) {
return null; return null;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -77,7 +77,11 @@ class Backend {
const handlers = { const handlers = {
optionsGet: ({callback}) => { optionsGet: ({callback}) => {
forward(optionsLoad(), callback); forward(apiOptionsGet(), callback);
},
optionsSet: ({options, callback}) => {
forward(apiOptionsSet(options), callback);
}, },
kanjiFind: ({text, callback}) => { kanjiFind: ({text, callback}) => {
@ -88,10 +92,6 @@ class Backend {
forward(apiTermsFind(text), callback); forward(apiTermsFind(text), callback);
}, },
templateRender: ({template, data, callback}) => {
forward(apiTemplateRender(template, data), callback);
},
definitionAdd: ({definition, mode, callback}) => { definitionAdd: ({definition, mode, callback}) => {
forward(apiDefinitionAdd(definition, mode), callback); forward(apiDefinitionAdd(definition, mode), callback);
}, },
@ -102,6 +102,14 @@ class Backend {
noteView: ({noteId}) => { noteView: ({noteId}) => {
forward(apiNoteView(noteId), callback); forward(apiNoteView(noteId), callback);
},
templateRender: ({template, data, callback}) => {
forward(apiTemplateRender(template, data), callback);
},
commandExec: ({command, callback}) => {
forward(apiCommandExec(command), callback);
} }
}; };

View File

@ -17,7 +17,7 @@
*/ */
$(document).ready(() => { $(document).ready(utilAsync(() => {
$('#open-search').click(() => apiCommandExec('search')); $('#open-search').click(() => apiCommandExec('search'));
$('#open-options').click(() => apiCommandExec('options')); $('#open-options').click(() => apiCommandExec('options'));
$('#open-help').click(() => apiCommandExec('help')); $('#open-help').click(() => apiCommandExec('help'));
@ -28,4 +28,4 @@ $(document).ready(() => {
toggle.bootstrapToggle(); toggle.bootstrapToggle();
toggle.change(() => apiCommandExec('toggle')); toggle.change(() => apiCommandExec('toggle'));
}); });
}); }));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -41,9 +41,8 @@ class DisplaySearch extends Display {
} }
async onSearch(e) { async onSearch(e) {
e.preventDefault();
try { try {
e.preventDefault();
this.intro.slideUp(); this.intro.slideUp();
const {length, definitions} = await apiTermsFind(this.query.val()); const {length, definitions} = await apiTermsFind(this.query.val());
super.termsShow(definitions, await apiOptionsGet()); super.termsShow(definitions, await apiOptionsGet());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -90,12 +90,12 @@ function formUpdateVisibility(options) {
} }
} }
async function onFormOptionsChanged(e) {(async () => { async function onFormOptionsChanged(e) {
try {
if (!e.originalEvent && !e.isTrigger) { if (!e.originalEvent && !e.isTrigger) {
return; return;
} }
try {
ankiErrorShow(); ankiErrorShow();
ankiSpinnerShow(true); ankiSpinnerShow(true);
@ -116,9 +116,9 @@ async function onFormOptionsChanged(e) {(async () => {
} finally { } finally {
ankiSpinnerShow(false); ankiSpinnerShow(false);
} }
})();} }
function onReady() {(async () => { async function onReady() {
const options = await optionsLoad(); const options = await optionsLoad();
$('#show-usage-guide').prop('checked', options.general.showGuide); $('#show-usage-guide').prop('checked', options.general.showGuide);
@ -139,16 +139,16 @@ function onReady() {(async () => {
$('#scan-length').val(options.scanning.length); $('#scan-length').val(options.scanning.length);
$('#scan-modifier-key').val(options.scanning.modifier); $('#scan-modifier-key').val(options.scanning.modifier);
$('#dict-purge').click(onDictionaryPurge); $('#dict-purge').click(utilAsync(onDictionaryPurge));
$('#dict-file').change(onDictionaryImport); $('#dict-file').change(utilAsync(onDictionaryImport));
$('#anki-enable').prop('checked', options.anki.enable); $('#anki-enable').prop('checked', options.anki.enable);
$('#card-tags').val(options.anki.tags.join(' ')); $('#card-tags').val(options.anki.tags.join(' '));
$('#generate-html-cards').prop('checked', options.anki.htmlCards); $('#generate-html-cards').prop('checked', options.anki.htmlCards);
$('#sentence-detection-extent').val(options.anki.sentenceExt); $('#sentence-detection-extent').val(options.anki.sentenceExt);
$('#interface-server').val(options.anki.server); $('#interface-server').val(options.anki.server);
$('input, select').not('.anki-model').change(onFormOptionsChanged); $('input, select').not('.anki-model').change(utilAsync(onFormOptionsChanged));
$('.anki-model').change(onAnkiModelChanged); $('.anki-model').change(utilAsync(onAnkiModelChanged));
try { try {
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
@ -163,9 +163,9 @@ function onReady() {(async () => {
} }
formUpdateVisibility(options); formUpdateVisibility(options);
})();} }
$(document).ready(onReady); $(document).ready(utilAsync(onReady));
/* /*
@ -237,7 +237,7 @@ async function dictionaryGroupsPopulate(options) {
}); });
} }
async function onDictionaryPurge(e) {(async () => { async function onDictionaryPurge(e) {
e.preventDefault(); e.preventDefault();
const dictControls = $('#dict-importer, #dict-groups').hide(); const dictControls = $('#dict-importer, #dict-groups').hide();
@ -261,9 +261,9 @@ async function onDictionaryPurge(e) {(async () => {
dictControls.show(); dictControls.show();
dictProgress.hide(); dictProgress.hide();
} }
})();} }
function onDictionaryImport(e) {(async () => { async function onDictionaryImport(e) {
const dictFile = $('#dict-file'); const dictFile = $('#dict-file');
const dictControls = $('#dict-importer').hide(); const dictControls = $('#dict-importer').hide();
const dictProgress = $('#dict-import-progress').show(); const dictProgress = $('#dict-import-progress').show();
@ -291,7 +291,7 @@ function onDictionaryImport(e) {(async () => {
dictControls.show(); dictControls.show();
dictProgress.hide(); dictProgress.hide();
} }
})();} }
/* /*
@ -398,7 +398,7 @@ async function ankiFieldsPopulate(element, options) {
container.append($(html)); container.append($(html));
} }
tab.find('.anki-field-value').change(onFormOptionsChanged); tab.find('.anki-field-value').change(utilAsync(onFormOptionsChanged));
tab.find('.marker-link').click(onAnkiMarkerClicked); tab.find('.marker-link').click(onAnkiMarkerClicked);
} }
@ -408,12 +408,12 @@ function onAnkiMarkerClicked(e) {
$(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change'); $(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change');
} }
function onAnkiModelChanged(e) {(async () => { async function onAnkiModelChanged(e) {
try {
if (!e.originalEvent) { if (!e.originalEvent) {
return; return;
} }
try {
ankiErrorShow(); ankiErrorShow();
ankiSpinnerShow(true); ankiSpinnerShow(true);
@ -431,4 +431,4 @@ function onAnkiModelChanged(e) {(async () => {
} finally { } finally {
ankiSpinnerShow(false); ankiSpinnerShow(false);
} }
})();} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -16,6 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
function utilAsync(func) {
return function(...args) {
func.apply(this, args);
};
}
function utilBackend() { function utilBackend() {
return chrome.extension.getBackgroundPage().yomichan_backend; return chrome.extension.getBackgroundPage().yomichan_backend;

View File

@ -276,8 +276,7 @@
<script src="/mixed/lib/bootstrap/js/bootstrap.min.js"></script> <script src="/mixed/lib/bootstrap/js/bootstrap.min.js"></script>
<script src="/mixed/lib/handlebars.min.js"></script> <script src="/mixed/lib/handlebars.min.js"></script>
<script src="/bg/js/anki-connect.js"></script> <script src="/bg/js/anki.js"></script>
<script src="/bg/js/anki-null.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/dictionary.js"></script> <script src="/bg/js/dictionary.js"></script>
<script src="/bg/js/handlebars.js"></script> <script src="/bg/js/handlebars.js"></script>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -17,6 +17,10 @@
*/ */
function apiOptionsSet(options) {
return utilInvoke('optionsSet', {options});
}
function apiOptionsGet() { function apiOptionsGet() {
return utilInvoke('optionsGet'); return utilInvoke('optionsGet');
} }
@ -29,18 +33,22 @@ function apiKanjiFind(text) {
return utilInvoke('kanjiFind', {text}); return utilInvoke('kanjiFind', {text});
} }
function apiTemplateRender(template, data) { function apiDefinitionAdd(definition, mode) {
return utilInvoke('templateRender', {data, template}); return utilInvoke('definitionAdd', {definition, mode});
} }
function apiDefinitionsAddable(definitions, modes) { function apiDefinitionsAddable(definitions, modes) {
return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null); return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null);
} }
function apiDefinitionAdd(definition, mode) {
return utilInvoke('definitionAdd', {definition, mode});
}
function apiNoteView(noteId) { function apiNoteView(noteId) {
return utilInvoke('noteView', {noteId}); return utilInvoke('noteView', {noteId});
} }
function apiTemplateRender(template, data) {
return utilInvoke('templateRender', {data, template});
}
function apiCommandExec(command) {
return utilInvoke('commandExec', {command});
}

View File

@ -230,7 +230,7 @@ class Frontend {
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const url = window.location.href; const url = window.location.href;
this.popup.showKanji( this.popup.kanjiShow(
textSource.getRect(), textSource.getRect(),
definitions, definitions,
this.options, this.options,

View File

@ -102,7 +102,7 @@ class Popup {
async kanjiShow(elementRect, definitions, options, context) { async kanjiShow(elementRect, definitions, options, context) {
await this.show(elementRect, options); await this.show(elementRect, options);
this.invokeApi('termsShow', {definitions, options, context}); this.invokeApi('kanjiShow', {definitions, options, context});
} }
invokeApi(action, params={}) { invokeApi(action, params={}) {

View File

@ -42,7 +42,7 @@ class Display {
onSourceTermView(e) { onSourceTermView(e) {
e.preventDefault(); e.preventDefault();
this.sourceBack(); this.sourceTermView();
} }
async onKanjiLookup(e) { async onKanjiLookup(e) {
@ -154,7 +154,7 @@ class Display {
66: /* b */ () => { 66: /* b */ () => {
if (e.altKey) { if (e.altKey) {
this.sourceBack(); this.sourceTermView();
return true; return true;
} }
}, },
@ -276,6 +276,7 @@ class Display {
this.entryScrollIntoView(context && context.index || 0); this.entryScrollIntoView(context && context.index || 0);
$('.action-add-note').click(this.onNoteAdd.bind(this)); $('.action-add-note').click(this.onNoteAdd.bind(this));
$('.action-view-note').click(this.onNoteView.bind(this));
$('.source-term').click(this.onSourceTermView.bind(this)); $('.source-term').click(this.onSourceTermView.bind(this));
await this.adderButtonUpdate(['kanji'], sequence); await this.adderButtonUpdate(['kanji'], sequence);
@ -288,7 +289,7 @@ class Display {
try { try {
this.spinner.show(); this.spinner.show();
const states = apiDefinitionsAddable(this.definitions, modes); const states = await apiDefinitionsAddable(this.definitions, modes);
if (!states || sequence !== this.sequence) { if (!states || sequence !== this.sequence) {
return; return;
} }
@ -332,7 +333,7 @@ class Display {
this.index = index; this.index = index;
} }
sourceBack() { sourceTermView() {
if (this.context && this.context.source) { if (this.context && this.context.source) {
const context = { const context = {
url: this.context.source.url, url: this.context.source.url,