-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup.js
56 lines (56 loc) · 1.35 KB
/
markup.js
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
class NetFlexMarkup {
find(selector) {
return document.querySelectorAll(selector);
}
first(selector) {
return this.find(selector)[0];
}
each(selector, callback) {
this.find(selector).forEach(callback);
}
setClass(el, callback) {
let list = el.getAttribute('class').split(' ');
callback(list);
list = Array.from(new Set(list.sort())).join(' ');
el.setAttribute('class', list);
return list;
}
addClass(el, x) {
return this.setClass(el, list => list.push(x));
}
removeClass(el, x) {
return this.setClass(el, list => list.splice(list.indexOf(x), 1));
}
hasClass(el, x) {
return el.getAttribute('class').split(' ').includes(x);
}
toggleClass(el, x) {
this.hasClass(el, x) ? this.removeClass(el, x) : this.addClass(el, x);
}
show(selector) {
this.each(selector, this.showElement);
}
hide(selector) {
this.each(selector, this.hideElement);
}
showElement(el) {
el.style.visibility = 'visible';
}
hideElement(el) {
el.style.visibility = 'hidden';
}
remove(selector) {
this.each(selector, el => el.parentElement.removeChild(el));
}
prepend(el, node) {
el.insertBefore(node, el.firstChild);
}
onclick(selector, callback) {
this.each(selector, el => {
el.onclick = e => {
e.preventDefault();
callback(e.target)
};
});
}
}