diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-10-15 23:23:40 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-10-15 23:23:40 -0700 |
commit | 0b4bdec7f2f7add052009d4aa9c113229a7304cc (patch) | |
tree | 39655adb1e382e03350975ba23af48151696718d /ext/bg/js | |
parent | 38c1a9a5936ff54f06175ef8d36f5422471c430a (diff) |
WIP
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/ankiconnect.js | 46 | ||||
-rw-r--r-- | ext/bg/js/ankinull.js | 39 | ||||
-rw-r--r-- | ext/bg/js/ankiweb.js | 2 | ||||
-rw-r--r-- | ext/bg/js/options-form.js | 109 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 69 |
5 files changed, 125 insertions, 140 deletions
diff --git a/ext/bg/js/ankiconnect.js b/ext/bg/js/ankiconnect.js index 66cea094..de199b2c 100644 --- a/ext/bg/js/ankiconnect.js +++ b/ext/bg/js/ankiconnect.js @@ -19,58 +19,40 @@ class AnkiConnect { constructor() { this.asyncPools = {}; - this.pluginVersion = null; - this.apiVersion = 1; + this.localVersion = 1; + this.remoteVersion = null; } addNote(note) { - return this.ankiInvokeSafe('addNote', {note}, null); + return this.checkVersion().then(() => this.ankiInvoke('addNote', {note}, null)); } canAddNotes(notes) { - return this.ankiInvokeSafe('canAddNotes', {notes}, 'notes'); + return this.checkVersion().then(() => this.ankiInvoke('canAddNotes', {notes}, 'notes')); } getDeckNames() { - return this.ankiInvokeSafe('deckNames', {}, null); + return this.checkVersion().then(() => this.ankiInvoke('deckNames', {}, null)); } getModelNames() { - return this.ankiInvokeSafe('modelNames', {}, null); + return this.checkVersion().then(() => this.ankiInvoke('modelNames', {}, null)); } getModelFieldNames(modelName) { - return this.ankiInvokeSafe('modelFieldNames', {modelName}, null); + return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName}, null)); } - getStatus() { - return this.getVersion().then(version => { - if (version === null) { - return 'disconnected'; - } else if (version === this.apiVersion) { - return 'ready'; - } else { - return 'mismatch'; - } - }); - } - - getVersion() { - return this.ankiInvoke('version', {}, null); - } - - ankiInvokeSafe(action, params, pool) { - if (this.pluginVersion === this.apiVersion) { - return this.ankiInvoke(action, params, pool); + checkVersion() { + if (this.localVersion === this.remoteVersion) { + return Promise.resolve(true); } - return this.getVersion().then(version => { - if (version === this.apiVersion) { - this.pluginVersion = version; - return this.ankiInvoke(action, params, pool); + return this.ankiInvoke('version', {}, null).then(version => { + this.remoteVersion = version; + if (this.remoteVersion !== this.localVersion) { + return Promise.reject('browser extension and anki plugin version mismatch'); } - - return null; }); } diff --git a/ext/bg/js/ankinull.js b/ext/bg/js/ankinull.js new file mode 100644 index 00000000..0d0ed903 --- /dev/null +++ b/ext/bg/js/ankinull.js @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> + * Author: Alex Yatskov <alex@foosoft.net> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +class AnkiNull { + addNote(note) { + return Promise.reject('unsupported action'); + } + + canAddNotes(notes) { + return Promise.resolve([]); + } + + getDeckNames() { + return Promise.resolve([]); + } + + getModelNames() { + return Promise.resolve([]); + } + + getModelFieldNames(modelName) { + return Promise.resolve([]); + } +} diff --git a/ext/bg/js/ankiweb.js b/ext/bg/js/ankiweb.js index 9fc1fb57..ce909fcf 100644 --- a/ext/bg/js/ankiweb.js +++ b/ext/bg/js/ankiweb.js @@ -21,7 +21,7 @@ class AnkiWeb { this.username = username; this.password = password; this.noteInfo = null; - this.logged = false; + this.logged = true; } addNote(note) { diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 674ff07d..7e2e9a7d 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -104,47 +104,51 @@ function getAnkiOptions() { } function populateAnkiDeckAndModel(opts) { - const yomi = yomichan(); + const anki = yomichan().anki; - const ankiDeck = $('.anki-deck'); - ankiDeck.find('option').remove(); - yomi.api_getDeckNames({callback: names => { - if (names !== null) { + const populateDecks = () => { + const ankiDeck = $('.anki-deck'); + ankiDeck.find('option').remove(); + return anki.getDeckNames().then(names => { names.forEach(name => ankiDeck.append($('<option/>', {value: name, text: name}))); - } - - $('#anki-term-deck').val(opts.ankiTermDeck); - $('#anki-kanji-deck').val(opts.ankiKanjiDeck); - }}); + $('#anki-term-deck').val(opts.ankiTermDeck); + $('#anki-kanji-deck').val(opts.ankiKanjiDeck); + }); + }; - const ankiModel = $('.anki-model'); - ankiModel.find('option').remove(); - yomi.api_getModelNames({callback: names => { - if (names !== null) { + const populateModels = () => { + const ankiModel = $('.anki-model'); + ankiModel.find('option').remove(); + return anki.getModelNames().then(names => { names.forEach(name => ankiModel.append($('<option/>', {value: name, text: name}))); - } + populateAnkiFields($('#anki-term-model').val(opts.ankiTermModel), opts); + populateAnkiFields($('#anki-kanji-model').val(opts.ankiKanjiModel), opts); + }); + }; - populateAnkiFields($('#anki-term-model').val(opts.ankiTermModel), opts); - populateAnkiFields($('#anki-kanji-model').val(opts.ankiKanjiModel), opts); - }}); + return populateDecks().then(populateModels); } -function updateAnkiStatus() { - // $('.error-dlg').hide(); - - // yomichan().api_getVersion({callback: version => { - // if (version === null) { - // $('.error-dlg-connection').show(); - // $('.options-anki-controls').hide(); - // } else if (version !== yomichan().getApiVersion()) { - // $('.error-dlg-version').show(); - // $('.options-anki-controls').hide(); - // } else { - // $('.options-anki-controls').show(); - // } - // }}); - - $('.options-anki-controls').show(); +function updateVisibility(opts) { + switch (opts.ankiMethod) { + case 'ankiweb': + $('.options-anki-general').show(); + $('.options-anki-login').show(); + break; + case 'ankiconnect': + $('.options-anki-general').show(); + $('.options-anki-login').hide(); + break; + default: + $('.options-anki-general').hide(); + break; + } + + if (opts.showAdvancedOptions) { + $('.options-advanced').show(); + } else { + $('.options-advanced').hide(); + } } function populateAnkiFields(element, opts) { @@ -196,33 +200,20 @@ function populateAnkiFields(element, opts) { } function onOptionsBasicChanged(e) { - if (!e.originalEvent && !e.isTrigger) { - return; - } - - getBasicOptions().then(({optsNew, optsOld}) => { - saveOptions(optsNew).then(() => { - yomichan().setOptions(optsNew); - if (!optsOld.enableAnkiConnect && optsNew.enableAnkiConnect) { - updateAnkiStatus(); - populateAnkiDeckAndModel(optsNew); - $('.options-anki').show(); - } else if (optsOld.enableAnkiConnect && !optsNew.enableAnkiConnect) { - $('.options-anki').hide(); - } - - if (optsNew.showAdvancedOptions) { - $('.options-advanced').show(); - } else { - $('.options-advanced').hide(); - } + if (e.originalEvent || e.isTrigger) { + getBasicOptions().then(({optsNew, optsOld}) => { + saveOptions(optsNew).then(() => { + yomichan().setOptions(optsNew); + updateVisibility(optsNew); + }); }); - }); + } } function onOptionsAnkiChanged(e) { if (e.originalEvent || e.isTrigger) { getAnkiOptions().then(({optsNew, optsOld}) => { + updateVisibility(optsNew); saveOptions(optsNew).then(() => yomichan().setOptions(optsNew)); }); } @@ -258,13 +249,7 @@ $(document).ready(() => { $('.options-anki').not('.anki-model').change(onOptionsAnkiChanged); $('.anki-model').change(onAnkiModelChanged); - if (opts.showAdvancedOptions) { - $('.options-advanced').show(); - } - - updateAnkiStatus(); populateAnkiDeckAndModel(opts); - - $('.options-anki').show(); + updateVisibility(opts); }); }); diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 2f6d3841..61b25816 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -23,7 +23,7 @@ class Yomichan { Handlebars.registerHelper('kanjiLinks', kanjiLinks); this.translator = new Translator(); - this.anki = null; + this.anki = new AnkiNull(); this.options = null; this.importTabId = null; this.setState('disabled'); @@ -109,7 +109,7 @@ class Yomichan { this.anki = new AnkiConnect(); break; default: - this.anki = null; + this.anki = new AnkiNull(); break; } @@ -251,66 +251,45 @@ class Yomichan { } api_addDefinition({definition, mode, callback}) { - if (this.anki === null) { - callback(null); - } else { - const note = this.formatNote(definition, mode); - this.anki.addNote(note).then(callback); - } + const note = this.formatNote(definition, mode); + this.anki.addNote(note).then(callback); } api_canAddDefinitions({definitions, modes, callback}) { - if (this.anki === null) { - callback(null); - } - else { - const notes = []; - for (const definition of definitions) { - for (const mode of modes) { - notes.push(this.formatNote(definition, mode)); - } + const notes = []; + for (const definition of definitions) { + for (const mode of modes) { + notes.push(this.formatNote(definition, mode)); } + } - this.anki.canAddNotes(notes).then(results => { - const states = []; - if (results !== null) { - for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { - const state = {}; - for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { - state[modes[modeOffset]] = results[resultBase + modeOffset]; - } - - states.push(state); + this.anki.canAddNotes(notes).then(results => { + const states = []; + if (results !== null) { + for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + state[modes[modeOffset]] = results[resultBase + modeOffset]; } + + states.push(state); } + } - callback(states); - }); - } + callback(states); + }); } api_getDeckNames({callback}) { - if (this.anki === null) { - callback(null); - } else { - this.anki.getDeckNames().then(callback); - } + this.anki.getDeckNames().then(callback); } api_getModelNames({callback}) { - if (this.anki === null) { - callback(null); - } else { - this.anki.getModelNames().then(callback); - } + this.anki.getModelNames().then(callback); } api_getModelFieldNames({modelName, callback}) { - if (this.anki === null) { - callback(null); - } else { - this.anki.getModelFieldNames(modelName).then(callback); - } + this.anki.getModelFieldNames(modelName).then(callback); } } |