From 82863cd86156d48b9f18dc10a560bedb82641862 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 14 Aug 2017 19:55:04 -0700 Subject: renaming files --- ext/fg/js/float.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 ext/fg/js/float.js (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js new file mode 100644 index 00000000..59293239 --- /dev/null +++ b/ext/fg/js/float.js @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * 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 . + */ + + +class DisplayFloat extends Display { + constructor() { + super($('#spinner'), $('#definitions')); + $(window).on('message', e => this.onMessage(e)); + } + + onError(error) { + if (window.yomichan_orphaned) { + this.onOrphaned(); + } else { + window.alert(`Error: ${error}`); + } + } + + onOrphaned() { + $('#definitions').hide(); + $('#error-orphaned').show(); + } + + onSearchClear() { + window.parent.postMessage('popupClose', '*'); + } + + onSelectionCopy() { + window.parent.postMessage('selectionCopy', '*'); + } + + onMessage(e) { + const handlers = { + termsShow: ({definitions, options, context}) => { + this.termsShow(definitions, options, context); + }, + + kanjiShow: ({definitions, options, context}) => { + this.kanjiShow(definitions, options, context); + }, + + orphaned: () => { + this.onOrphaned(); + } + }; + + const {action, params} = e.originalEvent.data; + const handler = handlers[action]; + if (handler) { + handler(params); + } + } + + onKeyDown(e) { + const handlers = { + 67: /* c */ () => { + if (e.ctrlKey && !window.getSelection().toString()) { + this.onSelectionCopy(); + return true; + } + } + }; + + const handler = handlers[e.keyCode]; + if (handler && handler()) { + e.preventDefault(); + } else { + super.onKeyDown(e); + } + } +} + +window.yomichan_display = new DisplayFloat(); -- cgit v1.2.3 From 211e5d1155e82ccbbc188dc889600459a62229fb Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 14 Aug 2017 23:22:37 -0700 Subject: cleanup --- ext/fg/js/document.js | 2 +- ext/fg/js/float.js | 4 ++-- ext/fg/js/frontend.js | 18 +++++++++--------- ext/fg/js/util.js | 6 ++++++ 4 files changed, 18 insertions(+), 12 deletions(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 582b6770..17cca613 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 59293239..22374f8b 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -20,7 +20,7 @@ class DisplayFloat extends Display { constructor() { super($('#spinner'), $('#definitions')); - $(window).on('message', e => this.onMessage(e)); + $(window).on('message', utilAsync(this.onMessage.bind(this))); } onError(error) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index cc4d99c8..7d26f946 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -33,14 +33,14 @@ class Frontend { try { this.options = await apiOptionsGet(); - window.addEventListener('message', e => this.onFrameMessage(e)); - window.addEventListener('mousedown', e => this.onMouseDown(e)); - window.addEventListener('mousemove', e => this.onMouseMove(e)); - window.addEventListener('mouseover', e => this.onMouseOver(e)); - window.addEventListener('mouseup', e => this.onMouseUp(e)); - window.addEventListener('resize', e => this.onResize(e)); - - chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => this.onBgMessage(action, params, sender, callback)); + window.addEventListener('message', this.onFrameMessage.bind(this)); + window.addEventListener('mousedown', this.onMouseDown.bind(this)); + window.addEventListener('mousemove', this.onMouseMove.bind(this)); + window.addEventListener('mouseover', this.onMouseOver.bind(this)); + window.addEventListener('mouseup', this.onMouseUp.bind(this)); + window.addEventListener('resize', this.onResize.bind(this)); + + chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); } catch (e) { this.onError(e); } @@ -124,7 +124,7 @@ class Frontend { this.searchClear(); } - onBgMessage(action, params, sender, callback) { + onBgMessage({action, params}, sender, callback) { const handlers = { optionsSet: options => { this.options = options; diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index afa895ba..3faf3b47 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -17,6 +17,12 @@ */ +function utilAsync(func) { + return function(...args) { + func.apply(this, args); + }; +} + function utilInvoke(action, params={}) { return new Promise((resolve, reject) => { try { -- cgit v1.2.3