-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjswitchtabs.js
125 lines (112 loc) · 4.14 KB
/
jswitchtabs.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
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
/**
* JSwitchTabs
* @version 1.0
* @author AKmatiAK
* @license BSD0
*/
// there are multiple things you can do with ContGroupFlags and it's corresponding set in Switches. isSuperset is default action. it's possible to extend this library in future to allow passing action from html attributes etc.
function isSuperset(superset, subset) {
for (const elem of subset) {
if (!superset.has(elem)) {
return false;
}
}
return true;
}
function isSubset(subset, superset) {
for (const elem of subset) {
if (!superset.has(elem)) {
return false;
}
}
return true;
}
function hasDuplicates(set1, set2) {
for (let item of set1) {
if (set2.has(item)) {
return true;
}
}
return false;
}
function switcher() {
// remove active-btn class from switch panel child buttons, then add active-btn class to new active switch
function ArrangeClasses() {
const parent = this.parentElement;
for (let i = 0; i < parent.children.length; i++) {
const child = parent.children[i];
child.classList.remove('active-btn');
}
this.classList.add('active-btn');
}
ArrangeClasses.call(this);
// create js map for switch flags
let Switches = new Map();
// save all active switch flags to Switches
function CrawlSwitches() {
document.querySelectorAll('.active-btn[data-switches]').forEach(node => {
node.dataset.switches.split(/(?<=]) /).forEach(SwGroupStr => {
//console.log(SwGroupStr);
const regex = /^(.+)\[/;
const SwGroup = SwGroupStr.match(regex);
//console.log(SwGroup[1]);
const SwitchFlags = SwGroupStr.slice(SwGroup[0].length, -1).split(' ');
//console.log(SwitchFlags);
if(Switches.has(SwGroup[1])){
Switches.get(SwGroup[1]).add(SwitchFlags)
}else{
Switches.set(SwGroup[1], new Set(SwitchFlags))
}
});
});
} CrawlSwitches();
//console.log(Switches);
// main function comparing ContentBlocks and switches map, and based on result sets classes
function Check() {
const ContentBlocks = document.querySelectorAll('[data-contents]');
//console.log(ContentBlocks);
ContentBlocks.forEach(block => {
let PassTest = true;
block.dataset.contents.split(/(?<=]) /).forEach(ContGroupStr => {
//console.log(ContGroupStr);
const regex = /^(.+)\[/;
const ContGroup = ContGroupStr.match(regex);
//console.log(ContGroup[1]);
const ContGroupFlags = new Set(ContGroupStr.slice(ContGroup[0].length, -1).split(' '));
//console.log(ContGroupFlags);
if (!isSuperset(ContGroupFlags, Switches.get(ContGroup[1]))) {
PassTest = false;
}
});
if (PassTest) {
block.classList.add('active-contents');
} else {
block.classList.remove('active-contents');
}
});
Switches.clear();
} Check();
// if resulting switches combination doesn't exist, select first available
if (document.querySelector('.active-btn:not(.active-contents)[data-contents]')){
document.querySelectorAll('.active-btn:not(.active-contents)[data-contents]').forEach(button => {
button.classList.remove('active-btn');
button.parentElement.querySelector('.active-contents').classList.add('active-btn');
});
CrawlSwitches();
Check();
}
}
/* for owl carousel
document.addEventListener("DOMContentLoaded", () => {
document.querySelector('[data-switches].active-btn')?.click();
document.querySelectorAll('[data-switches]').forEach(button => {
button.addEventListener('click', () => {
const owl = $('.owl-carousel');
owl.trigger('stop.owl.autoplay');
});
});
});
*/
document.querySelectorAll('[data-switches]').forEach(button => {
button.addEventListener('click', switcher);
});