Repository navigation
Expand file tree
/
Copy pathtab-group.js
More file actions
80 lines (66 loc) · 1.97 KB
/
Copy pathtab-group.js
File metadata and controls
80 lines (66 loc) · 1.97 KB
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
export default class TabGroup {
constructor() {
this.nodesByElement = new Map();
this.lastElement = null;
this.autofocusTarget = null;
}
appendElement(element, autofocus) {
const lastNode = this.nodesByElement.get(this.lastElement) || {next: element, previous: element};
const next = lastNode.next;
const previous = this.lastElement || element;
this.nodesByElement.set(element, {next, previous});
this.nodesByElement.get(lastNode.next).previous = element;
lastNode.next = element;
this.lastElement = element;
if (autofocus && this.autofocusTarget === null) {
this.autofocusTarget = element;
}
}
removeElement(element) {
const node = this.nodesByElement.get(element);
if (node) {
const beforeNode = this.nodesByElement.get(node.previous);
const afterNode = this.nodesByElement.get(node.next);
beforeNode.next = node.next;
afterNode.previous = node.previous;
}
this.nodesByElement.delete(element);
}
after(element) {
const node = this.nodesByElement.get(element) || {next: undefined};
return node.next;
}
focusAfter(element) {
const original = this.getCurrentFocus();
let next = this.after(element);
while (next && next !== element) {
next.focus();
if (this.getCurrentFocus() !== original) {
return;
}
next = this.after(next);
}
}
before(element) {
const node = this.nodesByElement.get(element) || {previous: undefined};
return node.previous;
}
focusBefore(element) {
const original = this.getCurrentFocus();
let previous = this.before(element);
while (previous && previous !== element) {
previous.focus();
if (this.getCurrentFocus() !== original) {
return;
}
previous = this.before(previous);
}
}
autofocus() {
this.autofocusTarget && this.autofocusTarget.focus();
}
/* istanbul ignore next */
getCurrentFocus() {
return document.activeElement;
}
}