diff options
author | Alex Yatskov <alex@foosoft.net> | 2017-01-07 18:39:36 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2017-01-07 18:39:36 -0800 |
commit | fab7a03b6c345ccd9fec66adb08a45206557c306 (patch) | |
tree | ea9f92263485a742ea66de7d05fc19e86b525e3d /ext/fg/js | |
parent | dc4162eeed14d958c65d63b2568fce58890e14e3 (diff) |
WIP
Diffstat (limited to 'ext/fg/js')
-rw-r--r-- | ext/fg/js/driver.js | 33 | ||||
-rw-r--r-- | ext/fg/js/frame.js | 4 | ||||
-rw-r--r-- | ext/fg/js/popup.js | 28 | ||||
-rw-r--r-- | ext/fg/js/util.js | 18 |
4 files changed, 44 insertions, 39 deletions
diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js index 1c797201..c0f0afe9 100644 --- a/ext/fg/js/driver.js +++ b/ext/fg/js/driver.js @@ -32,11 +32,11 @@ class Driver { window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('keydown', this.onKeyDown.bind(this)); - window.addEventListener('resize', e => this.hidePopup()); + window.addEventListener('resize', e => this.searchClear()); getOptions().then(opts => { this.options = opts; - return getEnabled(); + return isEnabled(); }).then(enabled => { this.enabled = enabled; }); @@ -60,7 +60,7 @@ class Driver { if (this.enabled && this.lastMousePos !== null && e.keyCode === 16 /* shift */) { this.searchAt(this.lastMousePos, true); } else if (e.keyCode === 27 /* esc */) { - this.hidePopup(); + this.searchClear(); } } @@ -87,7 +87,7 @@ class Driver { } const searcher = () => this.searchAt(this.lastMousePos, false); - if (!this.popup.visible() || e.shiftKey || e.which === 2 /* mmb */) { + if (!this.popup.isVisible() || e.shiftKey || e.which === 2 /* mmb */) { searcher(); } else { this.popupTimerSet(searcher); @@ -97,7 +97,7 @@ class Driver { onMouseDown(e) { this.lastMousePos = {x: e.clientX, y: e.clientY}; this.popupTimerClear(); - this.hidePopup(); + this.searchClear(); } onBgMessage({action, params}, sender, callback) { @@ -117,14 +117,14 @@ class Driver { const textSource = textSourceFromPoint(point); if (textSource === null || !textSource.containsPoint(point)) { if (hideNotFound) { - this.hidePopup(); + this.searchClear(); } return; } if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) { - return true; + return; } this.pendingLookup = true; @@ -132,12 +132,12 @@ class Driver { if (!found) { this.searchKanji(textSource).then(found => { if (!found && hideNotFound) { - this.hidePopup(); + this.searchClear(); } }); } }).catch(error => { - alert('Error: ' + error); + window.alert('Error: ' + error); }).then(() => { this.pendingLookup = false; }); @@ -159,12 +159,13 @@ class Driver { }); this.popup.showNextTo(textSource.getRect()); - this.popup.invoke('showTermDefs', {definitions, options: this.options}); + this.popup.showTermDefs(definitions, this.options); return true; } }).catch(error => { - alert('Error: ' + error); + window.alert('Error: ' + error); + return false; }); } @@ -178,16 +179,17 @@ class Driver { definitions.forEach(definition => definition.url = window.location.href); this.popup.showNextTo(textSource.getRect()); - this.popup.invoke('showKanjiDefs', {definitions, options: this.options}); + this.popup.showKanjiDefs(definitions, this.options); return true; } }).catch(error => { - alert('Error: ' + error); + window.alert('Error: ' + error); + return false; }); } - hidePopup() { + searchClear() { this.popup.hide(); if (this.options.selectMatchedText && this.lastTextSource !== null) { @@ -195,7 +197,6 @@ class Driver { } this.lastTextSource = null; - this.definitions = null; } api_setOptions(opts) { @@ -204,7 +205,7 @@ class Driver { api_setEnabled(enabled) { if (!(this.enabled = enabled)) { - this.hidePopup(); + this.searchClear(); } } } diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 4a086abb..95762d5e 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -16,7 +16,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -class FrameContext { +class Frame { constructor() { this.definitions = []; this.audioCache = {}; @@ -156,4 +156,4 @@ class FrameContext { } } -window.frameContext = new FrameContext(); +window.frame = new Frame(); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 35d7a00f..21f4a9d7 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -30,7 +30,7 @@ class Popup { document.body.appendChild(this.container); } - show(rect) { + showAt(rect) { this.container.style.left = rect.x + 'px'; this.container.style.top = rect.y + 'px'; this.container.style.height = rect.height + 'px'; @@ -59,22 +59,26 @@ class Popup { y = elementRect.top - height - this.offset; } - this.show({x, y, width, height}); + this.showAt({x, y, width, height}); } - visible() { - return this.container !== null && this.container.style.visibility !== 'hidden'; + hide() { + this.container.style.visibility = 'hidden'; } - hide() { - if (this.container !== null) { - this.container.style.visibility = 'hidden'; - } + isVisible() { + return this.container.style.visibility !== 'hidden'; } - invoke(action, params) { - if (this.container !== null) { - this.container.contentWindow.postMessage({action, params}, '*'); - } + showTermDefs(definitions, options) { + this.invokeApi('showTermDefs', {definitions, options}); + } + + showKanjiDefs(definitions, options) { + this.invokeApi('showKanjiDefs', {definitions, options}); + } + + invokeApi(action, params) { + this.container.contentWindow.postMessage({action, params}, '*'); } } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 96007b74..aae044c5 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -17,7 +17,7 @@ */ -function invokeApiBg(action, params) { +function invokeBgApi(action, params) { return new Promise((resolve, reject) => { chrome.runtime.sendMessage({action, params}, ({result, error}) => { if (error) { @@ -29,32 +29,32 @@ function invokeApiBg(action, params) { }); } -function getEnabled() { - return invokeApiBg('getEnabled', {}); +function isEnabled() { + return invokeBgApi('getEnabled', {}); } function getOptions() { - return invokeApiBg('getOptions', {}); + return invokeBgApi('getOptions', {}); } function findTerm(text) { - return invokeApiBg('findTerm', {text}); + return invokeBgApi('findTerm', {text}); } function findKanji(text) { - return invokeApiBg('findKanji', {text}); + return invokeBgApi('findKanji', {text}); } function renderText(data, template) { - return invokeApiBg('renderText', {data, template}); + return invokeBgApi('renderText', {data, template}); } function canAddDefinitions(definitions, modes) { - return invokeApiBg('canAddDefinitions', {definitions, modes}).catch(() => null); + return invokeBgApi('canAddDefinitions', {definitions, modes}).catch(() => null); } function addDefinition(definition, mode) { - return invokeApiBg('addDefinition', {definition, mode}); + return invokeBgApi('addDefinition', {definition, mode}); } function textSourceFromPoint(point) { |