domInput.js 704 B

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