From db7e80dabfbaeec09c7cd30b4f36b3d68c6e52b6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 4 Mar 2017 19:16:19 -0800 Subject: [PATCH] wip --- ext/bg/js/display-window.js | 4 ++-- ext/bg/js/options.js | 10 +++------- ext/bg/js/util.js | 35 ++++++++++++++++++++++++++++++++--- ext/bg/js/yomichan.js | 28 ++++++++-------------------- ext/fg/js/display-frame.js | 4 ++-- ext/fg/js/util.js | 4 ++-- ext/mixed/js/display.js | 6 +++--- 7 files changed, 52 insertions(+), 39 deletions(-) diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js index 44d1f7a9..568c5526 100644 --- a/ext/bg/js/display-window.js +++ b/ext/bg/js/display-window.js @@ -31,8 +31,8 @@ window.displayWindow = new class extends Display { return instYomi().definitionsAddable(definitions, modes); } - textRender(template, data) { - return instYomi().textRender(template, data); + templateRender(template, data) { + return instYomi().templateRender(template, data); } kanjiFind(character) { diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index f06cc056..84ecc6e3 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -187,10 +187,7 @@ function onDictionaryPurge(e) { return optionsLoad(); }).then(options => { options.dictionaries = {}; - return optionsSave(options).then(() => { - populateDictionaries(options); - instYomi().optionsSet(options); - }); + return optionsSave(options).then(populateDictionaries); }); } @@ -208,7 +205,7 @@ function onDictionaryImport() { optionsLoad().then(options => { instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { options.dictionaries[summary.title] = {enabled: true, priority: 0}; - return optionsSave(options).then(() => instYomi().optionsSet(options)); + return optionsSave(options); }).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => { showDictionarySpinner(false); dictProgress.hide(); @@ -339,7 +336,7 @@ function onAnkiModelChanged(e) { optionsNew.anki[tabId].fields = {}; populateAnkiFields(element, optionsNew).then(() => { - optionsSave(optionsNew).then(() => instYomi().optionsSet(optionsNew)); + optionsSave(optionsNew); }).catch(showAnkiError).then(() => showAnkiSpinner(false)); }); } @@ -351,7 +348,6 @@ function onOptionsChanged(e) { getFormData().then(({optionsNew, optionsOld}) => { return optionsSave(optionsNew).then(() => { - instYomi().optionsSet(optionsNew); updateVisibility(optionsNew); const ankiUpdated = diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index c57f6ae1..d91415f7 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -23,7 +23,7 @@ function promiseCallback(promise, callback) { return promise.then(result => { - callback({result}); + callback({result}); }).catch(error => { /* eslint-disable */ console.log(error); @@ -50,6 +50,22 @@ function instAnki() { } +/* + * Foreground + */ + +function fgBroadcast(action, params) { + chrome.tabs.query({}, tabs => { + for (const tab of tabs) { + chrome.tabs.sendMessage(tab.id, {action, params}, () => null); + } + }); +} + +function fgOptionsSet(options) { + fgBroadcast('optionsSet', options); +} + /* * Options @@ -188,6 +204,9 @@ function optionsSave(options) { return new Promise((resolve, reject) => { chrome.storage.sync.set(options, resolve); + }).then(() => { + instYomi().optionsSet(options); + fgOptionsSet(options); }); } @@ -468,7 +487,7 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { * Helpers */ -function helperKanjiLinks(options) { +function handlebarsKanjiLinks(options) { const isKanji = c => { const code = c.charCodeAt(0); return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; @@ -486,6 +505,16 @@ function helperKanjiLinks(options) { return result; } -function helperMultiLine(options) { +function handlebarsMultiLine(options) { return options.fn(this).split('\n').join('
'); } + +function handlebarsRegister() { + Handlebars.partials = Handlebars.templates; + Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks); + Handlebars.registerHelper('multiLine', handlebarsMultiLine); +} + +function handlebarsRender(template, data) { + return Handlebars.templates[template](data); +} diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 37fe74ee..82a06146 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -19,9 +19,7 @@ window.yomichan = new class { constructor() { - Handlebars.partials = Handlebars.templates; - Handlebars.registerHelper('kanjiLinks', helperKanjiLinks); - Handlebars.registerHelper('multiLine', helperMultiLine); + handlebarsRegister(); this.translator = new Translator(); this.anki = new AnkiNull(); @@ -38,32 +36,22 @@ window.yomichan = new class { optionsSet(options) { this.options = options; - let usable = false; + let configured = false; for (const title in options.dictionaries) { if (options.dictionaries[title].enabled) { - usable = true; + configured = true; break; } } chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); - chrome.browserAction.setBadgeText({text: usable ? '' : '!'}); + chrome.browserAction.setBadgeText({text: configured ? '' : '!'}); if (options.anki.enable) { this.anki = new AnkiConnect(this.options.anki.server); } else { this.anki = new AnkiNull(); } - - this.tabInvokeAll('optionsSet', this.options); - } - - tabInvokeAll(action, params) { - chrome.tabs.query({}, tabs => { - for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action, params}, () => null); - } - }); } noteFormat(definition, mode) { @@ -155,8 +143,8 @@ window.yomichan = new class { }); } - textRender(template, data) { - return Promise.resolve(Handlebars.templates[template](data)); + templateRender(template, data) { + return Promise.resolve(handlebarsRender(template, data)); } onInstalled(details) { @@ -179,8 +167,8 @@ window.yomichan = new class { promiseCallback(this.termsFind(text), callback); } - api_textRender({template, data, callback}) { - promiseCallback(this.textRender(template, data), callback); + api_templateRender({template, data, callback}) { + promiseCallback(this.templateRender(template, data), callback); } api_definitionAdd({definition, mode, callback}) { diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index 56b56861..aeb4ede5 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -31,8 +31,8 @@ window.displayFrame = new class extends Display { return bgDefinitionsAddable(definitions, modes); } - textRender(template, data) { - return bgTextRender(template, data); + templateRender(template, data) { + return bgTemplateRender(template, data); } kanjiFind(character) { diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 4fb8f288..f0760462 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -50,8 +50,8 @@ function bgKanjiFind(text) { return bgInvoke('kanjiFind', {text}); } -function bgTextRender(template, data) { - return bgInvoke('textRender', {data, template}); +function bgTemplateRender(template, data) { + return bgInvoke('templateRender', {data, template}); } function bgDefinitionsAddable(definitions, modes) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index c2382d43..f4ce4f67 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -34,7 +34,7 @@ class Display { throw 'override me'; } - textRender(template, data) { + templateRender(template, data) { throw 'override me'; } @@ -64,7 +64,7 @@ class Display { this.definitions = definitions; this.spinner.hide(); - this.textRender('terms.html', params).then(content => { + this.templateRender('terms.html', params).then(content => { this.container.html(content); $('.action-add-note').click(this.onActionAddNote.bind(this)); $('.action-play-audio').click(this.onActionPlayAudio.bind(this)); @@ -90,7 +90,7 @@ class Display { this.definitions = definitions; this.spinner.hide(); - this.textRender('kanji.html', params).then(content => { + this.templateRender('kanji.html', params).then(content => { this.container.html(content); $('.action-add-note').click(this.onActionAddNote.bind(this)); return this.adderButtonsUpdate(['kanji'], sequence);