Repository navigation
Expand file tree
/
Copy pathdomIterator.ts
More file actions
176 lines (157 loc) · 4.25 KB
/
Copy pathdomIterator.ts
File metadata and controls
176 lines (157 loc) · 4.25 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import Dom from './dom';
/**
* Iterator above passed Elements list.
* Each next or previous action adds provides CSS-class and sets cursor to this item
*/
export default class DomIterator {
/**
* This is a static property that defines iteration directions
*
* @type {{RIGHT: string, LEFT: string}}
*/
public static directions = {
RIGHT: 'right',
LEFT: 'left',
};
/**
* User-provided CSS-class name for focused button
*/
private focusedCssClass: string;
/**
* Focused button index.
* Default is -1 which means nothing is active
*
* @type {number}
*/
private cursor = -1;
/**
* Items to flip
*/
private items: HTMLElement[] = [];
/**
* @param {HTMLElement[]} nodeList — the list of iterable HTML-items
* @param {string} focusedCssClass - user-provided CSS-class that will be set in flipping process
*/
constructor(
nodeList: HTMLElement[],
focusedCssClass: string
) {
this.items = nodeList || [];
this.focusedCssClass = focusedCssClass;
}
/**
* Returns Focused button Node
*
* @returns {HTMLElement}
*/
public get currentItem(): HTMLElement {
if (this.cursor === -1) {
return null;
}
return this.items[this.cursor];
}
/**
* Sets items. Can be used when iterable items changed dynamically
*
* @param {HTMLElement[]} nodeList - nodes to iterate
*/
public setItems(nodeList: HTMLElement[]): void {
this.items = nodeList;
}
/**
* Sets cursor next to the current
*/
public next(): void {
this.cursor = this.leafNodesAndReturnIndex(DomIterator.directions.RIGHT);
}
/**
* Sets cursor before current
*/
public previous(): void {
this.cursor = this.leafNodesAndReturnIndex(DomIterator.directions.LEFT);
}
/**
* Sets cursor to the default position and removes CSS-class from previously focused item
*/
public dropCursor(): void {
if (this.cursor === -1) {
return;
}
this.items[this.cursor].classList.remove(this.focusedCssClass);
this.cursor = -1;
}
/**
* Leafs nodes inside the target list from active element
*
* @param {string} direction - leaf direction. Can be 'left' or 'right'
* @returns {number} index of focused node
*/
private leafNodesAndReturnIndex(direction: string): number {
/**
* if items are empty then there is nothing to leaf
*/
if (this.items.length === 0) {
return this.cursor;
}
let focusedButtonIndex = this.cursor;
/**
* If activeButtonIndex === -1 then we have no chosen Tool in Toolbox
*/
if (focusedButtonIndex === -1) {
/**
* Normalize "previous" Tool index depending on direction.
* We need to do this to highlight "first" Tool correctly
*
* Order of Tools: [0] [1] ... [n - 1]
* [0 = n] because of: n % n = 0 % n
*
* Direction 'right': for [0] the [n - 1] is a previous index
* [n - 1] -> [0]
*
* Direction 'left': for [n - 1] the [0] is a previous index
* [n - 1] <- [0]
*
* @type {number}
*/
focusedButtonIndex = direction === DomIterator.directions.RIGHT ? -1 : 0;
} else {
/**
* If we have chosen Tool then remove highlighting
*/
this.items[focusedButtonIndex].classList.remove(this.focusedCssClass);
}
/**
* Count index for next Tool
*/
if (direction === DomIterator.directions.RIGHT) {
/**
* If we go right then choose next (+1) Tool
*
* @type {number}
*/
focusedButtonIndex = (focusedButtonIndex + 1) % this.items.length;
} else {
/**
* If we go left then choose previous (-1) Tool
* Before counting module we need to add length before because of "The JavaScript Modulo Bug"
*
* @type {number}
*/
focusedButtonIndex = (this.items.length + focusedButtonIndex - 1) % this.items.length;
}
if (Dom.isNativeInput(this.items[focusedButtonIndex])) {
/**
* Focus input
*/
this.items[focusedButtonIndex].focus();
}
/**
* Highlight new chosen Tool
*/
this.items[focusedButtonIndex].classList.add(this.focusedCssClass);
/**
* Return focused button's index
*/
return focusedButtonIndex;
}
}