-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·83 lines (76 loc) · 1.58 KB
/
main.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
let main;
/**
* Main code block navigation
* @param item
* @returns {boolean}
*/
function navigateCodeBlock(item)
{
item.classList.add('active');
const index = getSiblings(item, (elem) => {
elem.classList.remove('active');
});
codeBlock(index);
if(getMain().classList.contains("active")) {
getMain().classList.remove("active");
}
return false;
}
/**
* Open navigation on smaller screen sizes
* @param button
*/
function openNavigation(button)
{
if(!getMain().classList.contains("active")) {
getMain().classList.add("active");
} else {
getMain().classList.remove("active");
}
return false;
}
/**
* Will add show class to current code block
* @param index
*/
function codeBlock(index)
{
const codeBlocks = document.querySelectorAll(".code-block");
for (let i = 0; i < codeBlocks.length; i++) {
if(i === index) {
codeBlocks[i].classList.add("show")
} else {
codeBlocks[i].classList.remove("show");
}
}
}
/**
* Get siblings from active item
* @param item
* @param callback
* @returns {number}
*/
function getSiblings(item, callback)
{
let count = 0, index = 0;
for (let sibling of item.parentNode.children) {
if(sibling !== item) {
callback(sibling, count);
} else {
index = count;
}
count++;
}
return index;
}
/**
* Get main element
* @returns {HTMLElement}
*/
function getMain()
{
if(typeof main !== "object") {
main = document.getElementById("main");
}
return main;
}