aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js/client.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-09-11 19:47:40 -0700
committerAlex Yatskov <alex@foosoft.net>2016-09-11 19:47:40 -0700
commit6183864f164a44bcb24e98ad2150ac9aafe371d3 (patch)
tree549b8e226319c6fe6d9d7c899d3d4eb9e600776f /ext/fg/js/client.js
parent94dccfd875bf0ef1ec0d732b548b94a5811491f3 (diff)
Cleanup
Diffstat (limited to 'ext/fg/js/client.js')
-rw-r--r--ext/fg/js/client.js38
1 files changed, 24 insertions, 14 deletions
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js
index 628d5b30..ebfb1090 100644
--- a/ext/fg/js/client.js
+++ b/ext/fg/js/client.js
@@ -23,6 +23,7 @@ class Client {
this.audio = {};
this.lastMousePos = null;
this.lastTextSource = null;
+ this.pendingLookup = false;
this.activateKey = 16;
this.activateBtn = 2;
this.enabled = false;
@@ -36,8 +37,8 @@ class Client {
window.addEventListener('mousedown', this.onMouseDown.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this));
window.addEventListener('keydown', this.onKeyDown.bind(this));
- window.addEventListener('scroll', (e) => this.hidePopup());
- window.addEventListener('resize', (e) => this.hidePopup());
+ window.addEventListener('scroll', e => this.hidePopup());
+ window.addEventListener('resize', e => this.hidePopup());
}
onKeyDown(e) {
@@ -89,11 +90,16 @@ class Client {
return;
}
+ if (this.pendingLookup) {
+ return;
+ }
+
textSource.setEndOffset(this.options.scanLength);
let defs = [];
let seq = -1;
+ this.pendingLookup = true;
bgFindTerm(textSource.text())
.then(({definitions, length}) => {
if (length === 0) {
@@ -103,7 +109,7 @@ class Client {
textSource.setEndOffset(length);
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
- definitions.forEach((definition) => {
+ definitions.forEach(definition => {
definition.url = window.location.href;
definition.sentence = sentence;
});
@@ -113,17 +119,21 @@ class Client {
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html');
})
- .then((content) => {
+ .then(content => {
this.definitions = defs;
this.showPopup(textSource, content);
return bgCanAddDefinitions(defs, ['term_kanji', 'term_kana']);
})
- .then((states) => {
+ .then(states => {
+ this.pendingLookup = false;
if (states !== null) {
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
}
- }, () => this.hidePopup());
+ }, () => {
+ this.pendingLookup = false;
+ this.hidePopup();
+ });
}
showPopup(textSource, content) {
@@ -159,7 +169,7 @@ class Client {
api_addNote({index, mode}) {
const state = {[mode]: false};
- bgAddDefinition(this.definitions[index], mode).then((success) => {
+ bgAddDefinition(this.definitions[index], mode).then(success => {
if (success) {
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
} else {
@@ -192,21 +202,21 @@ class Client {
let seq = -1;
bgFindKanji(kanji)
- .then((definitions) => {
- definitions.forEach((definition) => definition.url = window.location.href);
+ .then(definitions => {
+ definitions.forEach(definition => definition.url = window.location.href);
defs = definitions;
seq = ++this.sequence;
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'kanji-list.html');
})
- .then((content) => {
+ .then(content => {
this.definitions = defs;
this.popup.setContent(content, defs);
return bgCanAddDefinitions(defs, ['kanji']);
})
- .then((states) => {
+ .then(states => {
if (states !== null) {
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
}
@@ -217,7 +227,7 @@ class Client {
const element = document.elementFromPoint(point.x, point.y);
if (element !== null) {
const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA'];
- if (names.indexOf(element.nodeName) !== -1) {
+ if (names.includes(element.nodeName)) {
return new TextSourceElement(element);
}
}
@@ -246,7 +256,7 @@ class Client {
for (let i = position; i >= startPos; --i) {
const c = content[i];
- if (quoteStack.length === 0 && (terminators.indexOf(c) !== -1 || c in quotesFwd)) {
+ if (quoteStack.length === 0 && (terminators.includes(c) || c in quotesFwd)) {
startPos = i + 1;
break;
}
@@ -265,7 +275,7 @@ class Client {
const c = content[i];
if (quoteStack.length === 0) {
- if (terminators.indexOf(c) !== -1) {
+ if (terminators.includes(c)) {
endPos = i + 1;
break;
}