yomichan/ext/fg/js/frame.js

215 lines
6.2 KiB
JavaScript
Raw Normal View History

2016-04-24 23:18:47 +00:00
/*
* 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/>.
*/
2017-01-08 02:39:36 +00:00
class Frame {
2017-01-07 20:21:47 +00:00
constructor() {
2017-01-08 00:49:29 +00:00
this.definitions = [];
2017-01-08 02:07:15 +00:00
this.audioCache = {};
this.sequence = 0;
2017-01-08 00:49:29 +00:00
2017-01-07 20:21:47 +00:00
$(window).on('message', e => {
const {action, params} = e.originalEvent.data, method = this['api_' + action];
if (typeof(method) === 'function') {
method.call(this, params);
}
2016-04-24 23:48:30 +00:00
});
}
api_showTermDefs({definitions, options, context}) {
2017-01-08 02:07:15 +00:00
const sequence = ++this.sequence;
const params = {
2017-01-07 20:21:47 +00:00
definitions,
2017-01-14 05:06:33 +00:00
grouped: options.general.groupResults,
2017-01-07 20:21:47 +00:00
addable: options.ankiMethod !== 'disabled',
2017-01-14 05:06:33 +00:00
playback: options.general.audioPlayback
2017-01-07 20:21:47 +00:00
};
definitions.forEach(definition => {
definition.sentence = context.sentence;
definition.url = context.url;
});
2017-01-08 00:49:29 +00:00
this.definitions = definitions;
2017-01-08 01:25:01 +00:00
this.showSpinner(false);
2017-01-08 02:07:15 +00:00
window.scrollTo(0, 0);
2017-01-08 01:25:01 +00:00
renderText(params, 'terms.html').then(content => {
$('#content').html(content);
2017-01-08 02:07:15 +00:00
$('.action-add-note').click(this.onAddNote.bind(this));
2017-01-08 00:49:29 +00:00
$('.kanji-link').click(e => {
e.preventDefault();
const character = $(e.target).text();
findKanji(character).then(definitions => this.api_showKanjiDefs({definitions, options, context}));
2017-01-08 00:49:29 +00:00
});
$('.action-play-audio').click(e => {
e.preventDefault();
const index = $(e.currentTarget).data('index');
this.playAudio(this.definitions[index]);
});
2017-01-08 01:25:01 +00:00
2017-01-08 02:07:15 +00:00
this.updateAddNoteButtons(['term_kanji', 'term_kana'], sequence);
}).catch(error => {
this.handleError(error);
2016-05-05 03:08:22 +00:00
});
}
api_showKanjiDefs({definitions, options, context}) {
2017-01-08 02:07:15 +00:00
const sequence = ++this.sequence;
const params = {
2017-01-07 20:21:47 +00:00
definitions,
addable: options.ankiMethod !== 'disabled'
};
definitions.forEach(definition => {
definition.sentence = context.sentence;
definition.url = context.url;
});
2017-01-08 00:49:29 +00:00
this.definitions = definitions;
2017-01-08 01:25:01 +00:00
this.showSpinner(false);
2017-01-08 02:07:15 +00:00
window.scrollTo(0, 0);
2017-01-08 01:25:01 +00:00
renderText(params, 'kanji.html').then(content => {
$('#content').html(content);
2017-01-08 02:07:15 +00:00
$('.action-add-note').click(this.onAddNote.bind(this));
this.updateAddNoteButtons(['kanji'], sequence);
}).catch(error => {
this.handleError(error);
2016-06-14 05:09:39 +00:00
});
}
api_showOrphaned() {
$('#content').hide();
$('#orphan').show();
}
2017-01-08 02:07:15 +00:00
findAddNoteButton(index, mode) {
2017-01-08 01:25:01 +00:00
return $(`.action-add-note[data-index="${index}"][data-mode="${mode}"]`);
}
2017-01-08 02:07:15 +00:00
onAddNote(e) {
e.preventDefault();
this.showSpinner(true);
const link = $(e.currentTarget);
const index = link.data('index');
const mode = link.data('mode');
2017-02-19 18:09:15 +00:00
const definition = this.definitions[index];
if (mode !== 'kanji') {
const url = buildAudioUrl(definition);
const filename = buildAudioFilename(definition);
if (url && filename) {
definition.audio = {url, filename};
}
}
addDefinition(definition, mode).then(success => {
2017-01-08 02:07:15 +00:00
if (success) {
const button = this.findAddNoteButton(index, mode);
button.addClass('disabled');
} else {
showError('note could not be added');
2017-01-08 02:07:15 +00:00
}
}).catch(error => {
this.handleError(error);
2017-01-08 02:07:15 +00:00
}).then(() => {
this.showSpinner(false);
});
}
updateAddNoteButtons(modes, sequence) {
canAddDefinitions(this.definitions, modes).then(states => {
if (states === null) {
return;
}
if (sequence !== this.sequence) {
return;
}
states.forEach((state, index) => {
for (const mode in state) {
const button = this.findAddNoteButton(index, mode);
if (state[mode]) {
button.removeClass('disabled');
} else {
button.addClass('disabled');
}
button.removeClass('pending');
}
});
}).catch(error => {
this.handleError(error);
2017-01-08 02:07:15 +00:00
});
}
2017-01-08 01:25:01 +00:00
showSpinner(show) {
2017-01-08 02:07:15 +00:00
const spinner = $('.spinner');
if (show) {
spinner.show();
} else {
spinner.hide();
}
2017-01-08 01:25:01 +00:00
}
2017-01-08 00:49:29 +00:00
playAudio(definition) {
2017-01-08 02:07:15 +00:00
for (const key in this.audioCache) {
const audio = this.audioCache[key];
if (audio !== null) {
audio.pause();
}
2017-01-08 00:49:29 +00:00
}
2017-02-19 18:09:15 +00:00
const url = buildAudioUrl(definition);
if (!url) {
return;
}
let audio = this.audioCache[url];
if (audio) {
audio.currentTime = 0;
audio.play();
} else {
audio = new Audio(url);
audio.onloadeddata = () => {
if (audio.duration === 5.694694) {
audio = new Audio('mp3/button.mp3');
}
this.audioCache[url] = audio;
audio.play();
};
}
2016-05-06 01:55:43 +00:00
}
handleError(error) {
if (window.orphaned) {
this.api_showOrphaned();
} else {
showError(error);
}
}
2016-05-05 03:43:29 +00:00
}
2017-01-08 02:39:36 +00:00
window.frame = new Frame();