aboutsummaryrefslogtreecommitdiff
path: root/src/content/footer-line.js
blob: fc1dc7b56aad3f2c15026f5c79e4aa834838917b (plain) (blame)
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
import './footer-line.css';

export default class FooterLine {
  constructor(doc, initial = '') {
    this.initUi(doc);

    this.enteredCallback = () => {}
    this.promptChangeCallback = () => {}

    this.input.addEventListener('blur', this.handleBlur.bind(this));
    this.input.addEventListener('keydown', this.handleKeydown.bind(this));
    this.input.addEventListener('keyup', this.handleKeyup.bind(this));
    this.input.value = initial;
  }

  initUi(doc) {
    this.title = doc.createElement('p');
    this.title.className = 'vimvixen-footerline-title';

    let containerInner = doc.createElement('div');
    containerInner.className = 'vimvixen-footerline-container-inner';

    let containerOuter = doc.createElement('div');
    containerOuter.className = 'vimvixen-footerline-container-outer';

    this.input = doc.createElement('input');
    this.input.className = 'vimvixen-footerline-input';

    this.wrapper = doc.createElement('div');
    this.wrapper.className = 'vimvixen-footerline';

    containerOuter.append(containerInner);
    containerInner.append(this.input);
    this.wrapper.append(this.title);
    this.wrapper.append(containerOuter);
    doc.body.append(this.wrapper)
  }

  focus() {
    this.input.focus();
  }

  remove() {
    this.wrapper.remove();
  }

  onPromptChange(callback) {
    this.promptChangeCallback = callback;
  }

  onEntered(callback) {
    this.enteredCallback = callback;
  }

  handleBlur() {
    this.remove();
  }

  handleKeydown(e) {
    this.prevValue = e.target.value;
    switch(e.keyCode) {
    case KeyboardEvent.DOM_VK_ESCAPE:
      this.remove();
      break;
    case KeyboardEvent.DOM_VK_RETURN:
      this.enteredCallback(e);
      break;
    }
  }

  handleKeyup(e) {
    if (e.target.value === this.prevValue) {
      return;
    }
    this.promptChangeCallback(e);
    this.prevValue = e.target.value;
  }
}