additional.js 1.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* @flow */
import { applyStyles } from '../utils/dom/css';
import { groupStyle, groupElemLeft, groupElemRight } from '../styles';
import { consumeEvent } from '../utils/dom/consumeEvent';
import { enableTabClick } from '../utils/dom/enableTabClick';

type SwitchCallback = (offset: number) => void;
function updateAdditional(
  document: Document,
  additionalReference: HTMLDivElement,
  currentError: number,
  totalErrors: number,
  switchCallback: SwitchCallback
) {
  if (additionalReference.lastChild) {
    additionalReference.removeChild(additionalReference.lastChild);
  }

  if (totalErrors <= 1) {
    return;
  }
22

23
24
25
  const span = document.createElement('span');
  const group = document.createElement('span');
  applyStyles(group, groupStyle);
26

27
28
29
30
31
32
33
34
  const left = document.createElement('button');
  applyStyles(left, groupElemLeft);
  left.addEventListener('click', function(e: MouseEvent) {
    consumeEvent(e);
    switchCallback(-1);
  });
  left.appendChild(document.createTextNode(''));
  enableTabClick(left);
35

36
37
38
39
40
41
42
43
  const right = document.createElement('button');
  applyStyles(right, groupElemRight);
  right.addEventListener('click', function(e: MouseEvent) {
    consumeEvent(e);
    switchCallback(1);
  });
  right.appendChild(document.createTextNode(''));
  enableTabClick(right);
44

45
46
47
  group.appendChild(left);
  group.appendChild(right);
  span.appendChild(group);
48
49
50
51

  const text = `${currentError} of ${totalErrors} errors on the page`;
  span.appendChild(document.createTextNode(text));

52
53
54
55
56
  additionalReference.appendChild(span);
}

export type { SwitchCallback };
export { updateAdditional };