summaryrefslogtreecommitdiff
path: root/ext/fg/js/float.js
diff options
context:
space:
mode:
authorAlex Yatskov <FooSoft@users.noreply.github.com>2019-10-05 09:20:45 -0700
committerGitHub <noreply@github.com>2019-10-05 09:20:45 -0700
commit46ab36180f486a19332c538401b4db12ffe1bda1 (patch)
tree81a90e3967a44715b7d5c7115b87f048fc40e291 /ext/fg/js/float.js
parent440d6a91fd9c5122aa804b72171e4c15a496e58a (diff)
parentfa7ee468c0b8b877bb6d94a031464525b1a87c6b (diff)
Merge pull request #233 from toasted-nutbread/static-handlers
Static handlers
Diffstat (limited to 'ext/fg/js/float.js')
-rw-r--r--ext/fg/js/float.js89
1 files changed, 42 insertions, 47 deletions
diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js
index 2e952efb..88842eef 100644
--- a/ext/fg/js/float.js
+++ b/ext/fg/js/float.js
@@ -63,60 +63,25 @@ class DisplayFloat extends Display {
}
onMessage(e) {
- const handlers = {
- termsShow: ({definitions, options, context}) => {
- this.termsShow(definitions, options, context);
- },
-
- kanjiShow: ({definitions, options, context}) => {
- this.kanjiShow(definitions, options, context);
- },
-
- clearAutoPlayTimer: () => {
- this.clearAutoPlayTimer();
- },
-
- orphaned: () => {
- this.onOrphaned();
- },
-
- setOptions: (options) => {
- const css = options.general.customPopupCss;
- if (css) {
- this.setStyle(css);
- }
- },
-
- popupNestedInitialize: ({id, depth, parentFrameId, url}) => {
- this.optionsContext.depth = depth;
- this.optionsContext.url = url;
- popupNestedInitialize(id, depth, parentFrameId, url);
- }
- };
-
const {action, params} = e.data;
- const handler = handlers[action];
- if (handler) {
- handler(params);
+ const handlers = DisplayFloat.messageHandlers;
+ if (handlers.hasOwnProperty(action)) {
+ const handler = handlers[action];
+ handler(this, params);
}
}
onKeyDown(e) {
- const handlers = {
- 67: /* c */ () => {
- if (e.ctrlKey && !window.getSelection().toString()) {
- this.onSelectionCopy();
- return true;
- }
+ const key = Display.getKeyFromEvent(e);
+ const handlers = DisplayFloat.onKeyDownHandlers;
+ if (handlers.hasOwnProperty(key)) {
+ const handler = handlers[key];
+ if (handler(this, e)) {
+ e.preventDefault();
+ return;
}
- };
-
- const handler = handlers[e.keyCode];
- if (handler && handler()) {
- e.preventDefault();
- } else {
- super.onKeyDown(e);
}
+ super.onKeyDown(e);
}
autoPlayAudio() {
@@ -131,6 +96,18 @@ class DisplayFloat extends Display {
}
}
+ initialize(options, popupInfo, url) {
+ const css = options.general.customPopupCss;
+ if (css) {
+ this.setStyle(css);
+ }
+
+ const {id, depth, parentFrameId} = popupInfo;
+ this.optionsContext.depth = depth;
+ this.optionsContext.url = url;
+ popupNestedInitialize(id, depth, parentFrameId, url);
+ }
+
setStyle(css) {
const parent = document.head;
@@ -146,4 +123,22 @@ class DisplayFloat extends Display {
}
}
+DisplayFloat.onKeyDownHandlers = {
+ 'C': (self, e) => {
+ if (e.ctrlKey && !window.getSelection().toString()) {
+ self.onSelectionCopy();
+ return true;
+ }
+ return false;
+ }
+};
+
+DisplayFloat.messageHandlers = {
+ termsShow: (self, {definitions, options, context}) => self.termsShow(definitions, options, context),
+ kanjiShow: (self, {definitions, options, context}) => self.kanjiShow(definitions, options, context),
+ clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(),
+ orphaned: (self) => self.onOrphaned(),
+ initialize: (self, {options, popupInfo, url}) => self.initialize(options, popupInfo, url)
+};
+
window.yomichan_display = new DisplayFloat();