| 1 |
import boolOrFn from '../utils/bool-or-fn'; |
| 2 |
import addEventListeners from '../utils/add-event-listeners'; |
| 3 |
import removeEventListeners from '../utils/remove-event-listeners'; |
| 4 |
import getWindowForElement from '../utils/get-window-for-element'; |
| 5 |
|
| 6 |
/** |
| 7 |
* @private |
| 8 |
* create new input type manager |
| 9 |
* @param {Manager} manager |
| 10 |
* @param {Function} callback |
| 11 |
* @returns {Input} |
| 12 |
* @constructor |
| 13 |
*/ |
| 14 |
export default class Input { |
| 15 |
constructor(manager, callback) { |
| 16 |
let self = this; |
| 17 |
this.manager = manager; |
| 18 |
this.callback = callback; |
| 19 |
this.element = manager.element; |
| 20 |
this.target = manager.options.inputTarget; |
| 21 |
|
| 22 |
// smaller wrapper around the handler, for the scope and the enabled state of the manager, |
| 23 |
// so when disabled the input events are completely bypassed. |
| 24 |
this.domHandler = function(ev) { |
| 25 |
if (boolOrFn(manager.options.enable, [manager])) { |
| 26 |
self.handler(ev); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
this.init(); |
| 31 |
|
| 32 |
} |
| 33 |
/** |
| 34 |
* @private |
| 35 |
* should handle the inputEvent data and trigger the callback |
| 36 |
* @virtual |
| 37 |
*/ |
| 38 |
handler() { } |
| 39 |
|
| 40 |
/** |
| 41 |
* @private |
| 42 |
* bind the events |
| 43 |
*/ |
| 44 |
init() { |
| 45 |
this.evEl && addEventListeners(this.element, this.evEl, this.domHandler); |
| 46 |
this.evTarget && addEventListeners(this.target, this.evTarget, this.domHandler); |
| 47 |
this.evWin && addEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @private |
| 52 |
* unbind the events |
| 53 |
*/ |
| 54 |
destroy() { |
| 55 |
this.evEl && removeEventListeners(this.element, this.evEl, this.domHandler); |
| 56 |
this.evTarget && removeEventListeners(this.target, this.evTarget, this.domHandler); |
| 57 |
this.evWin && removeEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler); |
| 58 |
} |
| 59 |
} |