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/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/dictionary.js"></script>
<script src="/bg/js/options.js"></script>
<script src="/mixed/js/japanese.js"></script>
<script src="/mixed/js/request.js"></script>
<script src="/bg/js/util.js"></script>
<script src="/bg/js/context.js"></script>
</body>

View File

@ -17,6 +17,10 @@
*/
/*
* AnkiConnect
*/
class AnkiConnect {
constructor(server) {
this.server = server;
@ -68,6 +72,11 @@ class AnkiConnect {
}
}
/*
* AnkiNull
*/
class AnkiNull {
async addNote(note) {
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>
*
* 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>
*
* This program is free software: you can redistribute it and/or modify
@ -77,7 +77,11 @@ class Backend {
const handlers = {
optionsGet: ({callback}) => {
forward(optionsLoad(), callback);
forward(apiOptionsGet(), callback);
},
optionsSet: ({options, callback}) => {
forward(apiOptionsSet(options), callback);
},
kanjiFind: ({text, callback}) => {
@ -88,10 +92,6 @@ class Backend {
forward(apiTermsFind(text), callback);
},
templateRender: ({template, data, callback}) => {
forward(apiTemplateRender(template, data), callback);
},
definitionAdd: ({definition, mode, callback}) => {
forward(apiDefinitionAdd(definition, mode), callback);
},
@ -102,6 +102,14 @@ class Backend {
noteView: ({noteId}) => {
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-options').click(() => apiCommandExec('options'));
$('#open-help').click(() => apiCommandExec('help'));
@ -28,4 +28,4 @@ $(document).ready(() => {
toggle.bootstrapToggle();
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>
*
* 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>
*
* 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>
*
* 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>
*
* 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>
*
* This program is free software: you can redistribute it and/or modify
@ -41,9 +41,8 @@ class DisplaySearch extends Display {
}
async onSearch(e) {
e.preventDefault();
try {
e.preventDefault();
this.intro.slideUp();
const {length, definitions} = await apiTermsFind(this.query.val());
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>
*
* This program is free software: you can redistribute it and/or modify
@ -90,12 +90,12 @@ function formUpdateVisibility(options) {
}
}
async function onFormOptionsChanged(e) {(async () => {
if (!e.originalEvent && !e.isTrigger) {
return;
}
async function onFormOptionsChanged(e) {
try {
if (!e.originalEvent && !e.isTrigger) {
return;
}
ankiErrorShow();
ankiSpinnerShow(true);
@ -116,9 +116,9 @@ async function onFormOptionsChanged(e) {(async () => {
} finally {
ankiSpinnerShow(false);
}
})();}
}
function onReady() {(async () => {
async function onReady() {
const options = await optionsLoad();
$('#show-usage-guide').prop('checked', options.general.showGuide);
@ -139,16 +139,16 @@ function onReady() {(async () => {
$('#scan-length').val(options.scanning.length);
$('#scan-modifier-key').val(options.scanning.modifier);
$('#dict-purge').click(onDictionaryPurge);
$('#dict-file').change(onDictionaryImport);
$('#dict-purge').click(utilAsync(onDictionaryPurge));
$('#dict-file').change(utilAsync(onDictionaryImport));
$('#anki-enable').prop('checked', options.anki.enable);
$('#card-tags').val(options.anki.tags.join(' '));
$('#generate-html-cards').prop('checked', options.anki.htmlCards);
$('#sentence-detection-extent').val(options.anki.sentenceExt);
$('#interface-server').val(options.anki.server);
$('input, select').not('.anki-model').change(onFormOptionsChanged);
$('.anki-model').change(onAnkiModelChanged);
$('input, select').not('.anki-model').change(utilAsync(onFormOptionsChanged));
$('.anki-model').change(utilAsync(onAnkiModelChanged));
try {
await dictionaryGroupsPopulate(options);
@ -163,9 +163,9 @@ function onReady() {(async () => {
}
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();
const dictControls = $('#dict-importer, #dict-groups').hide();
@ -261,9 +261,9 @@ async function onDictionaryPurge(e) {(async () => {
dictControls.show();
dictProgress.hide();
}
})();}
}
function onDictionaryImport(e) {(async () => {
async function onDictionaryImport(e) {
const dictFile = $('#dict-file');
const dictControls = $('#dict-importer').hide();
const dictProgress = $('#dict-import-progress').show();
@ -291,7 +291,7 @@ function onDictionaryImport(e) {(async () => {
dictControls.show();
dictProgress.hide();
}
})();}
}
/*
@ -398,7 +398,7 @@ async function ankiFieldsPopulate(element, options) {
container.append($(html));
}
tab.find('.anki-field-value').change(onFormOptionsChanged);
tab.find('.anki-field-value').change(utilAsync(onFormOptionsChanged));
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');
}
function onAnkiModelChanged(e) {(async () => {
if (!e.originalEvent) {
return;
}
async function onAnkiModelChanged(e) {
try {
if (!e.originalEvent) {
return;
}
ankiErrorShow();
ankiSpinnerShow(true);
@ -431,4 +431,4 @@ function onAnkiModelChanged(e) {(async () => {
} finally {
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>
*
* 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/>.
*/
function utilAsync(func) {
return function(...args) {
func.apply(this, args);
};
}
function utilBackend() {
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/handlebars.min.js"></script>
<script src="/bg/js/anki-connect.js"></script>
<script src="/bg/js/anki-null.js"></script>
<script src="/bg/js/anki.js"></script>
<script src="/bg/js/api.js"></script>
<script src="/bg/js/dictionary.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>
*
* 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() {
return utilInvoke('optionsGet');
}
@ -29,18 +33,22 @@ function apiKanjiFind(text) {
return utilInvoke('kanjiFind', {text});
}
function apiTemplateRender(template, data) {
return utilInvoke('templateRender', {data, template});
function apiDefinitionAdd(definition, mode) {
return utilInvoke('definitionAdd', {definition, mode});
}
function apiDefinitionsAddable(definitions, modes) {
return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null);
}
function apiDefinitionAdd(definition, mode) {
return utilInvoke('definitionAdd', {definition, mode});
}
function apiNoteView(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 url = window.location.href;
this.popup.showKanji(
this.popup.kanjiShow(
textSource.getRect(),
definitions,
this.options,

View File

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

View File

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