1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import {Input} from './input';
- import $ from 'jquery';
- export class DOMInput extends Input{
- constructor (element) {
- this.el = $(element);
- this.listeners = [];
- this.setupEvents();
- }
- setupEvents () {
- this.el.on('keydown', (e) => {
- const code = e.keyCode || e.which;
- if (code === 13) {
- let text = this.el.val();
- text = text.replace('[\n\r]+', '');
- this.notifyInput(text);
- this.el.val('');
- }
- });
- }
- registerListener (listener) {
- if(!listener.notify) {
- throw new Error("InternalError: Input listener must implement a notify function.");
- }
- this.listeners.push(listener);
- }
- requestInput () {
- this.el.focus();
- }
- removeListener (listener) {
- const idx = this.listeners.indexOf(listener);
- if (idx)
- this.listeners.splice(idx, 1);
- }
- notifyInput (text) {
- this.listeners.forEach(l => {
- l.notify(text);
- })
- }
- }
|