generated from KanFF/onepageMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
49 lines (43 loc) · 1.49 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
document.addEventListener("DOMContentLoaded", init);
function init() {
sltLanguage.addEventListener("change", selectLanguage);
//Declare event listeners to all icons used to copy section link
Array.prototype.forEach.call(
document.querySelectorAll(".iconsToCopySection"),
function (el) {
el.addEventListener("click", copySectionLink);
}
);
}
//Select the language by changing the cookie and reloading the page
function selectLanguage() {
document.cookie = "lang=" + sltLanguage.value;
window.location.hash = "";
window.location.reload();
}
//Copy the section link with its anchor
function copySectionLink(e) {
icon = e.target;
link = icon.getAttribute("data-hrefcopy");
if (link == null) {
link = link = icon.parentNode.getAttribute("data-hrefcopy"); //if link not found, search in the parent
}
if (link != null) {
//copy the full link (= domain name + querystring + anchor)
console.log(navigator.clipboard.writeText(link));
//Foreach icons, remove green style
Array.prototype.forEach.call(
document.querySelectorAll(".iconsToCopySection"),
function (el) {
el.firstChild.classList.replace("bg-green-300", "bg-gray-200");
el.firstChild.classList.replace(
"hover:bg-green-300",
"hover:bg-gray-300"
);
}
);
//For the icon clicked, add the green style
icon.classList.replace("bg-gray-200", "bg-green-300");
icon.classList.replace("hover:bg-gray-300", "hover:bg-green-300");
}
}