summaryrefslogtreecommitdiff
path: root/ext/fg/js/frame.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-01-07 12:21:47 -0800
committerAlex Yatskov <alex@foosoft.net>2017-01-07 12:21:47 -0800
commit15ebc06fb3e0d0d3ceeb57281767a99e0f3389a2 (patch)
treea42b3ef9038bfeccb6ceb9b3a5ec8329d6802116 /ext/fg/js/frame.js
parent86e39efe281f8b09e199d7e2ddc05e77d5add140 (diff)
WIP
Diffstat (limited to 'ext/fg/js/frame.js')
-rw-r--r--ext/fg/js/frame.js195
1 files changed, 138 insertions, 57 deletions
diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js
index 5878bb43..e3d152c1 100644
--- a/ext/fg/js/frame.js
+++ b/ext/fg/js/frame.js
@@ -17,76 +17,157 @@
*/
-function invokeApi(action, params, target) {
- target.postMessage({action, params}, '*');
-}
+// function renderText(data, template) {
+// return invokeApiBg('renderText', {data, template});
+// }
-function showSpinner(show) {
- const spinner = document.querySelector('.spinner');
- spinner.style.visibility = show ? 'visible' : 'hidden';
-}
+// function canAddDefinitions(definitions, modes) {
+// return invokeApiBg('canAddDefinitions', {definitions, modes}).catch(() => null);
+// }
+
+// function addDefinition(definition, mode) {
+// return invokeApiBg('addDefinition', {definition, mode});
+// }
+
+// function invokeApi(action, params, target) {
+// target.postMessage({action, params}, '*');
+// }
+
+// function showSpinner(show) {
+// const spinner = document.querySelector('.spinner');
+// spinner.style.visibility = show ? 'visible' : 'hidden';
+// }
+
+// function registerKanjiLinks() {
+// for (const link of Array.from(document.getElementsByClassName('kanji-link'))) {
+// link.addEventListener('click', e => {
+// e.preventDefault();
+// invokeApi('displayKanji', e.target.innerHTML, window.parent);
+// });
+// }
+// }
+
+// function registerAddNoteLinks() {
+// for (const link of Array.from(document.getElementsByClassName('action-add-note'))) {
+// link.addEventListener('click', e => {
+// e.preventDefault();
+// const ds = e.currentTarget.dataset;
+// invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent);
+// showSpinner(true);
+// });
+// }
+// }
-function registerKanjiLinks() {
- for (const link of Array.from(document.getElementsByClassName('kanji-link'))) {
- link.addEventListener('click', e => {
- e.preventDefault();
- invokeApi('displayKanji', e.target.innerHTML, window.parent);
+// function registerAudioLinks() {
+// for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) {
+// link.addEventListener('click', e => {
+// e.preventDefault();
+// const ds = e.currentTarget.dataset;
+// invokeApi('playAudio', ds.index, window.parent);
+// });
+// }
+// }
+
+// function api_setActionState({index, state, sequence}) {
+// for (const mode in state) {
+// const matches = document.querySelectorAll(`.action-bar[data-sequence="${sequence}"] .action-add-note[data-index="${index}"][data-mode="${mode}"]`);
+// if (matches.length === 0) {
+// return;
+// }
+
+// const classes = matches[0].classList;
+// if (state[mode]) {
+// classes.remove('disabled');
+// } else {
+// classes.add('disabled');
+// }
+
+// classes.remove('pending');
+// }
+// }
+
+class FrameContext {
+ constructor() {
+ $(window).on('message', e => {
+ const {action, params} = e.originalEvent.data, method = this['api_' + action];
+ if (typeof(method) === 'function') {
+ method.call(this, params);
+ }
});
}
-}
-function registerAddNoteLinks() {
- for (const link of Array.from(document.getElementsByClassName('action-add-note'))) {
- link.addEventListener('click', e => {
- e.preventDefault();
- const ds = e.currentTarget.dataset;
- invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent);
- showSpinner(true);
+ api_showTermDefs({definitions, options}) {
+ const context = {
+ definitions,
+ addable: options.ankiMethod !== 'disabled',
+ playback: options.enableAudioPlayback
+ };
+
+ this.renderText('term-list.html', context).then(content => {
+ $('.content').html(content);
});
}
-}
-function registerAudioLinks() {
- for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) {
- link.addEventListener('click', e => {
- e.preventDefault();
- const ds = e.currentTarget.dataset;
- invokeApi('playAudio', ds.index, window.parent);
+ api_showKanjiDefs({definitions, options}) {
+ const context = {
+ definitions,
+ addable: options.ankiMethod !== 'disabled'
+ };
+
+ this.renderText('kanji-list.html', context).then(content => {
+ $('.content').html(content);
});
}
-}
-function api_addNoteComplete() {
- showSpinner(false);
-}
+ renderText(template, data) {
+ return this.invokeBgApi('renderText', {data, template});
+ }
-function api_setActionState({index, state, sequence}) {
- for (const mode in state) {
- const matches = document.querySelectorAll(`.action-bar[data-sequence="${sequence}"] .action-add-note[data-index="${index}"][data-mode="${mode}"]`);
- if (matches.length === 0) {
- return;
- }
-
- const classes = matches[0].classList;
- if (state[mode]) {
- classes.remove('disabled');
- } else {
- classes.add('disabled');
- }
-
- classes.remove('pending');
+ invokeBgApi(action, params) {
+ return new Promise((resolve, reject) => {
+ chrome.runtime.sendMessage({action, params}, ({result, error}) => {
+ if (error) {
+ reject(error);
+ } else {
+ resolve(result);
+ }
+ });
+ });
}
}
-document.addEventListener('DOMContentLoaded', () => {
- registerKanjiLinks();
- registerAddNoteLinks();
- registerAudioLinks();
-});
+window.frameContext = new FrameContext();
-window.addEventListener('message', e => {
- const {action, params} = e.data, method = window['api_' + action];
- if (typeof(method) === 'function') {
- method(params);
- }
-});
+ // api_addNote({index, mode}) {
+ // const state = {[mode]: false};
+ // addDefinition(this.definitions[index], mode).then(success => {
+ // if (success) {
+ // this.popup.invokeApi('setActionState', {index, state, sequence: this.sequence});
+ // } else {
+ // alert('Note could not be added');
+ // }
+
+ // this.popup.invokeApi('addNoteComplete');
+ // }).catch(error => {
+ // alert('Error: ' + error);
+ // });
+ // }
+
+ // api_playAudio(index) {
+ // const definition = this.definitions[index];
+
+ // let url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=${encodeURIComponent(definition.expression)}`;
+ // if (definition.reading) {
+ // url += `&kana=${encodeURIComponent(definition.reading)}`;
+ // }
+
+ // for (const key in this.audio) {
+ // this.audio[key].pause();
+ // }
+
+ // const audio = this.audio[url] || new Audio(url);
+ // audio.currentTime = 0;
+ // audio.play();
+
+ // this.audio[url] = audio;
+ // }