domInput.js 728 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import {Input} from './input';
  2. import $ from 'jquery';
  3. export class DOMInput extends Input{
  4. constructor (element) {
  5. super();
  6. this.el = $(element);
  7. this.listeners = [];
  8. this.setupEvents();
  9. }
  10. setupEvents () {
  11. this.el.on('keydown', (e) => {
  12. const code = e.keyCode || e.which;
  13. if (code === 13) {
  14. let text = this.el.val();
  15. text = text.replace('[\n\r]+', '');
  16. this.notifyInput(text);
  17. this.el.val('');
  18. }
  19. });
  20. }
  21. requestInput (callback) {
  22. this.listeners.push(callback);
  23. this.el.focus();
  24. }
  25. notifyInput (text) {
  26. this.listeners.forEach(resolve => {
  27. resolve(text);
  28. })
  29. this.listeners.splice(0, this.listeners.length);
  30. }
  31. }