File size: 2,112 Bytes
7aec436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import DropArea from './drop-area';
import styles from './style.css';

class Question {
  constructor (parent, text) {
    this.parent = parent;
    this.text = text;

    this.root = document.createElement('div');
    this.root.className = styles.questionRoot;

    this.inner = document.createElement('div');
    this.inner.className = styles.questionInner;

    if (text) {
      this.textElement = document.createElement('div');
      this.textElement.textContent = text;
      this.textElement.className = styles.questionText;
    }

    this.inputContainer = document.createElement('div');
    this.inputContainer.className = styles.questionInputOuter;

    this.input = document.createElement('input');
    this.input.className = styles.questionInput;
    this.input.addEventListener('keypress', this.onkeypress.bind(this));

    this.dropper = new DropArea(this.input, this.dropperCallback.bind(this));

    this.submitButton = document.createElement('button');
    this.submitButton.className = styles.questionSubmitButton;
    this.submitButton.addEventListener('click', this.onsubmitpressclick.bind(this));

    this.inputContainer.appendChild(this.input);
    this.inputContainer.appendChild(this.submitButton);
    if (this.textElement) {
      this.inner.appendChild(this.textElement);
    }
    this.inner.appendChild(this.inputContainer);
    this.root.appendChild(this.inner);
    this.parent._addLayer(this.root);
    this.input.focus();

    this.answerCallback = new Promise((resolve) => {
      this.callback = resolve;
    });
  }

  answer () {
    return this.answerCallback;
  }

  submit () {
    this.callback(this.input.value);
    this.destroy();
  }

  onkeypress (e) {
    if (e.key === 'Enter') {
      this.submit();
    }
  }

  dropperCallback (texts) {
    const text = texts.join('').replace(/\r?\n/g, ' ');
    this.input.value = text;
  }

  onsubmitpressclick () {
    this.submit();
  }

  destroy () {
    this.root.remove();
    this.parent.question = null;
  }
}

export default Question;