/**
 * Keyboard adds simple keyboard shortcut handling with callbacks.
 *
 * - Only alphanumerical characters.
 * - Control key combos do not trigger the callback so as not to override
 *   the default browser behaviour (eg: Ctrl-N for New Window).
 * - Only one listener for a key at one time.
 *
 * Options:
 *
 *   bDisableInInput      Defaults to true, do not call listener when key is pressed
 *                        while INPUT, TEXTAREA or SELECT is active.
 *
 * Methods:
 *   addListener(key, fnListener)    Key can be a character or a keycode. Only use
 *                                   characters 0-9, a-z, A-Z as string parameter.
 *   removeListener(key)             See addListener()
 *   destroy();
 *
 * Usage:
 *
 *   addListener('s', this.save.bind(this, 'save'));
 *
 */

import { stopEvent } from "@lib/dom";
import * as Core from "@old/core";
import EventCache from "@old/eventcache";

/** @type new(): this */
let Keyboard = Core.make();

Keyboard.prototype = {
  keys: null,

  init: function (options) {
    // set options and defaults
    options = options ? options : {};
    options.bDisableInInput = options.bDisableInInput !== false;
    this.options = options;

    this.oKeys = [];
    this.evtCache = new EventCache();
    this.evtCache.addEvent(document, "keydown", this.evKeydown.bind(this));
  },

  destroy: function () {
    this.evtCache.destroy();
  },

  /**
   * Always return a keycode.
   *
   * @param  {String|Number}  key   The key as a char (0-9, a-z, A-Z only) or the key code
   * @return {Integer}
   */
  getKeyCode: function (key) {
    // charCodeAt() returns the correct keyboard event keycode for uppercase letters only
    return typeof key === "number" ? key : key.toUpperCase().charCodeAt(0);
  },

  addListener: function (key, fnListener) {
    var keycode = this.getKeyCode(key);
    this.oKeys[keycode] = fnListener;
  },

  removeListener: function (key) {
    var keycode = this.getKeyCode(key);
    delete this.oKeys[keycode];
  },

  /**
   * Keyboard event handler.
   *
   */
  evKeydown: function (ev) {
    var isCtrl, iKeyCode;
    //console.log('Keyboard::evKeydown(%o)', ev.keyCode);

    //  var iKeyCode = window.event ? event.keyCode : ev.keyCode;
    //  var sKeyChar = String.fromCharCode(iKeyCode).toLowerCase();

    // Don't enable shortcut keys in Input, Textarea fields
    if (this.options.bDisableInInput) {
      var element = ev.target;
      if (element.nodeType === 3) {
        element = element.parentNode;
      }
      if (
        element.tagName === "INPUT" ||
        element.tagName === "TEXTAREA" ||
        element.tagName === "SELECT"
      ) {
        return;
      }
    }

    isCtrl = ev.ctrlKey;
    iKeyCode = ev.keyCode;

    if (!isCtrl) {
      var fnListener = this.oKeys[iKeyCode];
      if (fnListener) {
        fnListener.call(null, ev);
        stopEvent(ev);
        return false;
      }
    }

    return true;
  },
};

export default Keyboard;

@@ -831,43 +831,8 @@
 m%22;%0A
-import * as Core from %22@old/core%22;%0A
 impo
@@ -874,117 +874,173 @@
 %22;%0A%0A
-/** @type new(): this */%0Alet Keyboard = Core.make();%0A%0AKeyboard.prototype = %7B%0A  keys: null,%0A%0A  init: function 
+export default class Keyboard %7B%0A  /** @type %7B%7B%5Bkey: number%5D: EventListener%7D */%0A  oKeys;%0A%0A  /**%0A   * @param %7B%7B bDisableInInput?: boolean%7D%7D options%0A   */%0A  constructor
 (opt
@@ -1345,25 +1345,24 @@
 (this));%0A  %7D
-,
 %0A%0A  destroy:
@@ -1360,27 +1360,16 @@
  destroy
-: function 
 () %7B%0A   
@@ -1393,25 +1393,24 @@
 stroy();%0A  %7D
-,
 %0A%0A  /**%0A   *
@@ -1458,16 +1458,16 @@
 m  %7B
-S
+s
 tring%7C
-N
+n
 umbe
@@ -1549,13 +1549,12 @@
 rn %7B
-Integ
+numb
 er%7D%0A
@@ -1571,27 +1571,16 @@
 tKeyCode
-: function 
 (key) %7B%0A
@@ -1752,24 +1752,79 @@
 %0A  %7D
-,%0A%0A  add
+%0A%0A  /**%0A   *%0A   * @param %7Bstring%7Cnumber%7D key%0A   * @param %7BEvent
 Listener
 : fu
@@ -1815,35 +1815,56 @@
 ventListener
-: function 
+%7D fnListener%0A   */%0A  addListener
 (key, fnList
@@ -1948,25 +1948,24 @@
 istener;%0A  %7D
-,
 %0A%0A  removeLi
@@ -1970,27 +1970,16 @@
 Listener
-: function 
 (key) %7B%0A
@@ -2057,79 +2057,21 @@
 %0A  %7D
-,
 %0A%0A  
-/**%0A   * Keyboard event handler.%0A   *%0A   */%0A  evKeydown: function 
+evKeydown
 (ev)
@@ -2936,35 +2936,7 @@
 %0A  %7D
-,
 %0A%7D
-;%0A%0Aexport default Keyboard;
 %0A

convert Keyboard to class