Cleanup
This commit is contained in:
parent
5f97e8d16a
commit
f1850defa6
@ -49,19 +49,19 @@ function updateAnkiFormData() {
|
||||
|
||||
const ankiDeck = $('#ankiDeck');
|
||||
ankiDeck.find('option').remove();
|
||||
yomichan.api_getDeckNames((names) => {
|
||||
yomichan.api_getDeckNames({callback: (names) => {
|
||||
if (names !== null) {
|
||||
names.forEach((name) => ankiDeck.append($('<option/>', {value: name, text: name})));
|
||||
}
|
||||
});
|
||||
}});
|
||||
|
||||
const ankiModel = $('#ankiModel');
|
||||
ankiModel.find('option').remove();
|
||||
yomichan.api_getModelNames((names) => {
|
||||
yomichan.api_getModelNames({callback: (names) => {
|
||||
if (names !== null) {
|
||||
names.forEach((name) => ankiModel.append($('<option/>', {value: name, text: name})));
|
||||
}
|
||||
});
|
||||
}});
|
||||
}
|
||||
|
||||
function onOptionsChanged() {
|
||||
|
@ -50,11 +50,11 @@ class Yomichan {
|
||||
}
|
||||
|
||||
onMessage(request, sender, callback) {
|
||||
const {action, params} = request;
|
||||
const method = this['api_' + action];
|
||||
const {action, params} = request, method = this['api_' + action];
|
||||
|
||||
if (typeof(method) === 'function') {
|
||||
method.call(this, callback, params);
|
||||
params.callback = callback;
|
||||
method.call(this, params);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -132,39 +132,39 @@ class Yomichan {
|
||||
});
|
||||
}
|
||||
|
||||
api_addNote(callback, {definition, mode}) {
|
||||
api_addNote({definition, mode, callback}) {
|
||||
this.ankiInvoke('addNote', {definition: definition, mode: mode}, null, callback);
|
||||
}
|
||||
|
||||
api_canAddNotes(callback, {definitions, modes}) {
|
||||
api_canAddNotes({definitions, modes, callback}) {
|
||||
this.ankiInvoke('canAddNotes', {definitions: definitions, modes: modes}, 'notes', callback);
|
||||
}
|
||||
|
||||
api_findKanji(callback, text) {
|
||||
api_findKanji({text, callback}) {
|
||||
callback(this.translator.findKanji(text));
|
||||
}
|
||||
|
||||
api_findTerm(callback, text) {
|
||||
api_findTerm({text, callback}) {
|
||||
callback(this.translator.findTerm(text));
|
||||
}
|
||||
|
||||
api_getDeckNames(callback) {
|
||||
api_getDeckNames({callback}) {
|
||||
this.ankiInvoke('deckNames', {}, null, callback);
|
||||
}
|
||||
|
||||
api_getModelNames(callback) {
|
||||
api_getModelNames({callback}) {
|
||||
this.ankiInvoke('modelNames', {}, null, callback);
|
||||
}
|
||||
|
||||
api_getOptions(callback) {
|
||||
api_getOptions({callback}) {
|
||||
callback(this.options);
|
||||
}
|
||||
|
||||
api_getState(callback) {
|
||||
api_getState({callback}) {
|
||||
callback(this.state);
|
||||
}
|
||||
|
||||
api_renderText(callback, {template, data}) {
|
||||
api_renderText({template, data, callback}) {
|
||||
callback(Handlebars.templates[template](data));
|
||||
}
|
||||
}
|
||||
|
@ -22,27 +22,27 @@
|
||||
//
|
||||
|
||||
function bgSendMessage(action, params, callback) {
|
||||
chrome.runtime.sendMessage({action: action, params: params}, callback);
|
||||
chrome.runtime.sendMessage({action, params}, callback);
|
||||
}
|
||||
|
||||
function bgFindTerm(text, callback) {
|
||||
bgSendMessage('findTerm', text, callback);
|
||||
bgSendMessage('findTerm', {text}, callback);
|
||||
}
|
||||
|
||||
function bgFindKanji(text, callback) {
|
||||
bgSendMessage('findKanji', text, callback);
|
||||
bgSendMessage('findKanji', {text}, callback);
|
||||
}
|
||||
|
||||
function bgRenderText(data, template, callback) {
|
||||
bgSendMessage('renderText', {data: data, template: template}, callback);
|
||||
bgSendMessage('renderText', {data, template}, callback);
|
||||
}
|
||||
|
||||
function bgGetOptions(callback) {
|
||||
bgSendMessage('getOptions', null, callback);
|
||||
bgSendMessage('getOptions', {}, callback);
|
||||
}
|
||||
|
||||
function bgGetState(callback) {
|
||||
bgSendMessage('getState', null, callback);
|
||||
bgSendMessage('getState', {}, callback);
|
||||
}
|
||||
|
||||
function bgCanAddNotes(definitions, modes, callback) {
|
||||
@ -50,5 +50,5 @@ function bgCanAddNotes(definitions, modes, callback) {
|
||||
}
|
||||
|
||||
function bgAddNote(definition, mode, callback) {
|
||||
bgSendMessage('addNote', {definition: definition, mode: mode}, callback);
|
||||
bgSendMessage('addNote', {definition, mode}, callback);
|
||||
}
|
||||
|
@ -80,13 +80,9 @@ class Client {
|
||||
}
|
||||
|
||||
onFrameMessage(e) {
|
||||
const {action, params} = e.data, handlers = {
|
||||
addNote: ({mode, index}) => this.actionAddNote(index, mode, (data) => e.source.postMessage(data, e.origin)),
|
||||
displayKanji: this.actionDisplayKanji
|
||||
};
|
||||
|
||||
if (handlers.hasOwnProperty(action)) {
|
||||
handlers[action].call(this, params);
|
||||
const {action, params} = e.data, method = this['api_' + action];
|
||||
if (typeof(method) === 'function') {
|
||||
method.call(this, params);
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,40 +122,6 @@ class Client {
|
||||
});
|
||||
}
|
||||
|
||||
actionAddNote(index, mode, callback) {
|
||||
const state = {};
|
||||
state[mode] = false;
|
||||
|
||||
bgAddNote(this.definitions[index], mode, (success) => {
|
||||
if (success) {
|
||||
this.popup.sendMessage('setActionState', {index: index, state: state, sequence: this.sequence});
|
||||
} else {
|
||||
alert('Note could not be added');
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
actionDisplayKanji(kanji) {
|
||||
bgFindKanji(kanji, (definitions) => {
|
||||
const sequence = ++this.sequence;
|
||||
bgRenderText(
|
||||
{defs: definitions, root: this.fgRoot, options: this.options, sequence: sequence},
|
||||
'kanji-list.html',
|
||||
(content) => {
|
||||
this.definitions = definitions;
|
||||
this.popup.setContent(content, definitions);
|
||||
|
||||
bgCanAddNotes(definitions, ['kanji'], (states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index: index, state: state, sequence: sequence}));
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
showPopup(range, content) {
|
||||
this.popup.showNextTo(range.getRect(), content);
|
||||
|
||||
@ -190,6 +152,39 @@ class Client {
|
||||
setOptions(opts) {
|
||||
this.options = opts;
|
||||
}
|
||||
|
||||
api_addNote({index, mode}) {
|
||||
const state = {};
|
||||
state[mode] = false;
|
||||
|
||||
bgAddNote(this.definitions[index], mode, (success) => {
|
||||
if (success) {
|
||||
this.popup.sendMessage('setActionState', {index: index, state: state, sequence: this.sequence});
|
||||
} else {
|
||||
alert('Note could not be added');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
api_displayKanji(kanji) {
|
||||
bgFindKanji(kanji, (definitions) => {
|
||||
const sequence = ++this.sequence;
|
||||
bgRenderText(
|
||||
{defs: definitions, root: this.fgRoot, options: this.options, sequence: sequence},
|
||||
'kanji-list.html',
|
||||
(content) => {
|
||||
this.definitions = definitions;
|
||||
this.popup.setContent(content, definitions);
|
||||
|
||||
bgCanAddNotes(definitions, ['kanji'], (states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index: index, state: state, sequence: sequence}));
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
window.yomiClient = new Client();
|
||||
|
Loading…
Reference in New Issue
Block a user