-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (126 loc) · 5.22 KB
/
script.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
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
function createNav(arrayIcons, options) {
addLibraries();
function addLibraries() {
let CSSlibraries;
if(options.isHtmlOnly == true) {
CSSlibraries = Object.values({
"FontAwesome": "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
});
} else {
CSSlibraries = Object.values({
"myCSS": "https://cdn.jsdelivr.net/gh/The-DevNinja/navColor.js/style.min.css",
"FontAwesome": "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
});
}
CSSlibraries.forEach((library) => {
let linkTag = document.createElement("link");
linkTag.rel = "stylesheet";
linkTag.type = "text/css";
linkTag.href = library;
document.querySelector("head").appendChild(linkTag);
});
};
let bodyElement = document.querySelector("body");
let navContainer = setNavElement();
let iconStyleArray = Object.values({
outlined: "fa-regular",
filled: "fa-solid"
});
function setNavElement() {
let element;
if(options.navElement) {
createNavWithOptions(options);
function createNavWithOptions(options) {
checkIfNavExist();
let firstLetter = options.navElement.charAt(0);
let isID = firstLetter == "#";
let isClass = firstLetter == ".";
if(isID) {
element.id = options.navElement.replace("#", "");
} else if(isClass) {
element.classList.add(options.navElement.replace(".", ""));
} else {
element.id = options.navElement;
}
bodyElement.appendChild(element);
function checkIfNavExist() {
if(!document.querySelector("nav")) {
element = document.createElement("nav");
} else {
element = document.querySelector("nav");
}
}
}
} else {
element = document.querySelector("nav");
if(!element) {
createNormalNav();
function createNormalNav() {
element = document.createElement("nav");
bodyElement.appendChild(element);
}
}
}
return element;
}
let parentClass = "icon-div";
arrayIcons.forEach((thisData) => {
let newIconContainer;
let thisIconName = thisData.iconName;
createDivContainer(parentClass);
createNewIcon(thisIconName);
appendChildToNavbar(navContainer);
function createDivContainer(className) {
newIconContainer = document.createElement("div");
newIconContainer.classList.add(className);
}
function createNewIcon(iconName) {
let newIcon = document.createElement("i");
let classesToAdd = [`${iconName}`, `${iconStyleArray[0]}`, `icon`];
classesToAdd.forEach((thisClass) => {
newIcon.classList.add(`${thisClass}`);
});
newIconContainer.appendChild(newIcon);
}
function appendChildToNavbar(navName) {
navName.appendChild(newIconContainer);
}
});
let allIconsDiv = document.querySelectorAll(`.${parentClass}`);
allIconsDiv.forEach((thisIconDiv, index) => {
let thisIcon = thisIconDiv.querySelector(".icon");
addDataAttribute(thisIconDiv);
thisIconDiv.addEventListener("click", styleIcon);
function styleIcon() {
changeBgColor();
makeIconSolid(thisIcon);
function changeBgColor() {
let thisIconColor = arrayIcons[index].color;
bodyElement.style.setProperty("--bg", `${thisIconColor}`);
}
function makeIconSolid(iconName) {
resetAllIcons();
addClassesToThisIcon();
function resetAllIcons() {
allIconsDiv.forEach((thisDiv) => {
let thisSolidIcon = thisDiv.querySelector(".icon");
makeIconOutlined(thisSolidIcon);
thisDiv.classList.remove("active");
function makeIconOutlined(iconName) {
iconName.classList.remove(`${iconStyleArray[1]}`);
iconName.classList.add(`${iconStyleArray[0]}`);
}
});
}
function addClassesToThisIcon() {
thisIconDiv.classList.add("active");
iconName.classList.add(`${iconStyleArray[1]}`);
}
}
}
function addDataAttribute(iconName) {
let thisTitle = arrayIcons[index].text;
iconName.setAttribute("data-text", `${thisTitle}`);
}
});
}