Options now work

This commit is contained in:
Alex Yatskov 2016-04-06 20:48:36 -07:00
parent 721a4dc661
commit 7c9d64e95f
4 changed files with 27 additions and 15 deletions

View File

@ -7,6 +7,7 @@
<script src="js/dictionary.js"></script> <script src="js/dictionary.js"></script>
<script src="js/deinflector.js"></script> <script src="js/deinflector.js"></script>
<script src="js/translator.js"></script> <script src="js/translator.js"></script>
<script src="js/options.js"></script>
<script src="js/yomichan.js"></script> <script src="js/yomichan.js"></script>
</body> </body>
</html> </html>

View File

@ -23,24 +23,26 @@ class Yomichan {
this.translator = new Translator(); this.translator = new Translator();
this.updateState('disabled'); this.updateState('disabled');
this.updateOptions({});
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); loadOptions((opts) => {
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); this.updateOptions(opts);
loadOptions((opts) => this.updateOptions(opts)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
});
} }
onMessage(request, sender, callback) { onMessage(request, sender, callback) {
const {action, data} = request; const {action, data} = request;
const handler = { const handlers = {
findKanji: ({text}) => this.translator.onFindKanji(text), findKanji: ({text}) => this.translator.onFindKanji(text),
findTerm: ({text}) => this.translator.findTerm(text), findTerm: ({text}) => this.translator.findTerm(text),
getState: () => this.state, getState: () => this.state,
renderTemplate: ({data, template}) => Handlebars.templates[template](data) getOptions: () => this.options,
}[action]; renderText: ({data, template}) => Handlebars.templates[template](data)
};
const result = handler.call(this, data); const result = handlers[action].call(this, data);
if (callback !== null) { if (callback !== null) {
callback(result); callback(result);
} }

View File

@ -29,8 +29,12 @@ function findKanji(text, callback) {
sendMessage('findKanji', {text: text}, callback); sendMessage('findKanji', {text: text}, callback);
} }
function renderTemplate(data, template, callback) { function renderText(data, template, callback) {
sendMessage('renderTemplate', {data: data, template: template}, callback); sendMessage('renderText', {data: data, template: template}, callback);
}
function getOptions(callback) {
sendMessage('getOptions', null, callback);
} }
function getState(callback) { function getState(callback) {

View File

@ -37,7 +37,12 @@ class Client {
window.addEventListener('scroll', (e) => this.hidePopup()); window.addEventListener('scroll', (e) => this.hidePopup());
window.addEventListener('resize', (e) => this.hidePopup()); window.addEventListener('resize', (e) => this.hidePopup());
getState((state) => this.setEnabled(state === 'enabled')); getOptions((opts) => {
this.setOptions(opts);
getState((state) => {
this.setEnabled(state === 'enabled');
});
});
} }
onKeyDown(e) { onKeyDown(e) {
@ -93,7 +98,7 @@ class Client {
this.hidePopup(); this.hidePopup();
} else { } else {
range.setEnd(range.endContainer, range.startOffset + length); range.setEnd(range.endContainer, range.startOffset + length);
renderTemplate({defs: results}, 'defs.html', (html) => { renderText({defs: results}, 'defs.html', (html) => {
this.popup.innerHTML = html; this.popup.innerHTML = html;
this.showPopup(range); this.showPopup(range);
}); });