-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement_classes.js
More file actions
182 lines (164 loc) · 6.27 KB
/
element_classes.js
File metadata and controls
182 lines (164 loc) · 6.27 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
177
178
179
180
181
182
/**
* element_classes.js
* version: 0.2
* author: Akritas Akritidis
* repo: https://github.com/MaanooAk/function-frameworks
*/
/**
* Define constraints that should be applied to the classes of a collection of elements.
*
* @param {string | Element[] | NodeList} elements - Selector string or array of elements.
* @returns {ElementClasses} Instance of ElementClasses to define the constraints.
*/
function element_classes(elements) {
function elements_list(elements) {
if (Array.isArray(elements)) return elements;
if (typeof elements === "string") {
return Array.from(document.querySelectorAll(elements));
} else if (elements instanceof NodeList) {
return Array.from(elements);
} else {
throw new TypeError(`Unsupported elements type: ${elements}`);
}
}
class ElementClasses {
constructor(elements) {
this.elements = elements;
}
one(name) {
handler_one(this.elements, 1, 1, name);
return this;
}
exactlyOne(name) {
handler_one(this.elements, 1, 1, name);
return this;
}
atLeastOne(name) {
handler_one(this.elements, 1, Infinity, name);
return this;
}
atMostOne(name) {
handler_one(this.elements, 0, 1, name);
return this;
}
between(min, max, name) {
handler_one(this.elements, min, max, name);
return this;
}
each(...names) {
handler_each(this.elements, 1, 1, names.flat());
return this;
}
eachExactlyOne(...names) {
handler_each(this.elements, 1, 1, names.flat());
return this;
}
eachAtLeastOne(...names) {
handler_each(this.elements, 1, Infinity, names.flat());
return this;
}
eachAtMostOne(...names) {
handler_each(this.elements, 0, 1, names.flat());
return this;
}
eachBetween(min, max, ...names) {
handler_each(this.elements, min, max, names.flat());
return this;
}
}
const observe_options = {
attributes: true,
attributeFilter: ['class'],
}
const observe_options_old = {
attributes: true,
attributeFilter: ['class'],
attributeOldValue: true
}
function handler_one(elements_param, min, max, name) {
if (min < 0 || min > max) throw new Error(`Invalid min max range: ${min}-${max}`);
const elements = elements_param.map(i => i);
function listener() {
const has = [], mis = [];
for (const element of elements) {
(element.classList.contains(name) ? has : mis).push(element)
}
const count = has.length;
if (count >= min && count <= max) return;
if (count < min) {
const count_add = min - count;
for (let i = 0; i < count_add && i < mis.length; i++) {
mis[i].classList.add(name)
}
} else { // if (count > max) {
const count_remove = count - max;
for (let i = 0; i < count_remove && i < has.length; i++) {
has[i].classList.remove(name)
}
}
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
const element = mutation.target;
const index = elements.indexOf(element);
elements.splice(index, 1);
elements.push(element)
}
listener();
})
for (const element of elements) {
observer.observe(element, observe_options);
}
listener();
}
function handler_each(elements, min, max, names_param) {
if (min < 0 || min > max) throw new Error(`Invalid min max range: ${min}-${max}`);
if (names_param.length < min) throw new Error(`Number of names do not satisfy the range`)
const names = new Set(names_param);
function listener(element, old) {
const has = [], mis = [];
for (const name of names) {
(element.classList.contains(name) ? has : mis).push(name)
}
const count = has.length;
if (count >= min && count <= max) return;
const prev_names = new Set(old.split(" "));
if (count < min) {
const missing_names = new Set(mis);
const count_add = min - count;
const to_add = Array.from(missing_names.difference(prev_names)).slice(0, count_add);
if (to_add.length < count_add) {
const left_missing_names = missing_names.intersection(prev_names)
to_add.push(...Array.from(left_missing_names).slice(0, count_add - to_add.length))
}
element.classList.add(...to_add)
} else { // if (count > max) {
const current_names = new Set(has);
const count_remove = count - max;
const to_remove = Array.from(current_names.intersection(prev_names)).slice(0, count_remove);
if (to_remove.length < count_remove) {
const left_current_names = current_names.difference(prev_names)
to_remove.push(...Array.from(left_current_names).slice(0, count_remove - to_remove.length))
}
element.classList.remove(...to_remove)
}
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
const element = mutation.target;
const old = mutation.oldValue;
listener(element, old)
}
})
for (const element of elements) {
observer.observe(element, observe_options_old);
listener(element, element.getAttribute("class") || "")
}
}
element_classes = function (elements_param) {
const elements = elements_list(elements_param);
return new ElementClasses(elements);
}
return element_classes(elements);
}
export default function (...args) { return element_classes(...args); }