From bdf231082f4b4ca7c4c90d8b0cd40b6c4201723d Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 14 Aug 2017 21:43:09 -0700 Subject: lots of fixes to backend --- ext/bg/context.html | 5 +---- ext/bg/js/anki.js | 9 +++++++++ ext/bg/js/api.js | 2 +- ext/bg/js/backend.js | 20 +++++++++++++------ ext/bg/js/context.js | 4 ++-- ext/bg/js/database.js | 2 +- ext/bg/js/deinflector.js | 2 +- ext/bg/js/dictionary.js | 2 +- ext/bg/js/handlebars.js | 2 +- ext/bg/js/search.js | 5 ++--- ext/bg/js/settings.js | 50 ++++++++++++++++++++++++------------------------ ext/bg/js/util.js | 7 ++++++- ext/bg/settings.html | 3 +-- ext/fg/js/api.js | 22 ++++++++++++++------- ext/fg/js/frontend.js | 2 +- ext/fg/js/popup.js | 2 +- ext/mixed/js/display.js | 9 +++++---- 17 files changed, 87 insertions(+), 61 deletions(-) diff --git a/ext/bg/context.html b/ext/bg/context.html index 3828c9fe..8a72acc7 100644 --- a/ext/bg/context.html +++ b/ext/bg/context.html @@ -30,13 +30,10 @@ - - - - + diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index f0ec4571..c327969f 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -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; diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 024ba75e..b55e306f 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index e8c9452c..97e5602a 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * 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); } }; diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js index 77cb5166..689d6863 100644 --- a/ext/bg/js/context.js +++ b/ext/bg/js/context.js @@ -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')); }); -}); +})); diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index b38e00db..e00cb7a3 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 8b67761b..0abde99d 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 6f9b30e4..c8d431b9 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index df98bef1..debb0690 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 87f50c32..54cda8ec 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * 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()); diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index ce9e14a2..89b1581f 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * 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); } -})();} +} diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 9dc57950..11ec23eb 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +function utilAsync(func) { + return function(...args) { + func.apply(this, args); + }; +} function utilBackend() { return chrome.extension.getBackgroundPage().yomichan_backend; diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 719c67a2..237750b3 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -276,8 +276,7 @@ - - + diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index b4d75c3c..174531ba 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * 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}); +} diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 005139e6..cc4d99c8 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -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, diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 8e61169a..8cb16b5a 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -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={}) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 97dd7d5c..21748f5d 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -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, -- cgit v1.2.3