diff options
Diffstat (limited to 'ext/fg/js/float.js')
| -rw-r--r-- | ext/fg/js/float.js | 107 | 
1 files changed, 55 insertions, 52 deletions
| diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index fd7986b8..88842eef 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -19,7 +19,7 @@  class DisplayFloat extends Display {      constructor() { -        super($('#spinner'), $('#definitions')); +        super(document.querySelector('#spinner'), document.querySelector('#definitions'));          this.autoPlayAudioTimer = null;          this.styleNode = null; @@ -30,7 +30,7 @@ class DisplayFloat extends Display {          this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); -        $(window).on('message', utilAsync(this.onMessage.bind(this))); +        window.addEventListener('message', (e) => this.onMessage(e), false);      }      onError(error) { @@ -42,8 +42,16 @@ class DisplayFloat extends Display {      }      onOrphaned() { -        $('#definitions').hide(); -        $('#error-orphaned').show(); +        const definitions = document.querySelector('#definitions'); +        const errorOrphaned = document.querySelector('#error-orphaned'); + +        if (definitions !== null) { +            definitions.style.setProperty('display', 'none', 'important'); +        } + +        if (errorOrphaned !== null) { +            errorOrphaned.style.setProperty('display', 'block', 'important'); +        }      }      onSearchClear() { @@ -55,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.originalEvent.data; -        const handler = handlers[action]; -        if (handler) { -            handler(params); +        const {action, params} = e.data; +        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() { @@ -123,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; @@ -138,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(); |