forked from binary-com/websockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deriv.js
221 lines (193 loc) · 6.52 KB
/
deriv.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
window.onload = function () {
setSideBarActive();
setNavbarActive();
setMobileNavbarActive();
setAccordionActive();
handleMobileNav();
handleMobileNavDropdown();
setCopyButton();
};
window.onhashchange = (e) => {
setSideBarPlaygroundActive();
};
const setAccordionActive = () => {
let acc = document.getElementsByClassName("accordion-header");
if (!acc) return;
for (let i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {
this.classList.toggle("active");
let img = this.children[1];
if (!this.classList.contains("active")) {
img.src = "/img/plus.svg";
img.style.transform = `rotate(0deg)`;
} else {
img.src = "/img/minus.svg";
img.style.transform = `rotate(90deg)`;
}
let panel = this.nextElementSibling;
panel.style.display = panel.style.display === "block" ? "none" : "block";
});
}
};
const setNavbarActive = () => {
let navbarLinks = document.getElementById("navbar");
if (!navbarLinks) return;
let currentPage = window.location.pathname.split("/")[1].replaceAll("/", "");
for (let i = 0; i < navbarLinks.children.length; i++) {
if (currentPage === "") {
navbarLinks.children[0].classList.add("selected");
break;
}
if (navbarLinks.children[i].id === currentPage) {
navbarLinks.children[i].classList.add("selected");
break;
}
}
};
const setMobileNavbarActive = () => {
const canvas_menu = document.getElementById('canvas-menu');
if (!canvas_menu) return;
let current_element;
let current_page = window.location.pathname.split("/")[1].replaceAll("/", "");
if (current_page === 'docs') {
openMobileDropdown();
let sub_page = window.location.pathname.split("/")[2].replaceAll("/", "");
if (sub_page) {
current_element = sub_page;
} else {
current_element = "quickstart";
}
} else if (current_page === "playground") {
current_element = "playground_link";
} else {
current_element = "home";
}
const target_element = document.getElementById(current_element);
target_element.classList.add("selected");
}
function openMobileDropdown() {
const dropdown = document.getElementById("doc-menu-header")
.nextElementSibling;
dropdown.classList.add("show-dropdown");
}
const setSideBarActive = () => {
let sidebarLinks = document.getElementById("sidebar");
if (!sidebarLinks) return;
let currentPage = window.location.pathname
.replace("docs", "")
.replaceAll("/", "");
for (let i = 0; i < sidebarLinks.children.length; i++) {
if (currentPage === "") {
sidebarLinks.children[1].classList.add("selected");
break;
}
if (sidebarLinks.children[i].id === currentPage) {
let child = sidebarLinks.children[i];
child.classList.add("selected");
break;
}
}
};
const setSideBarPlaygroundActive = () => {
let sidebarLinks = document.getElementById("playground-sidebar");
if (!sidebarLinks) return;
for (let i = 0; i < sidebarLinks.children.length; i++) {
let child = sidebarLinks.children[i];
child.classList.remove("selected");
}
let currentHash = window.location.hash.substr(1);
for (let i = 0; i < sidebarLinks.children.length; i++) {
if (sidebarLinks.children[i].getAttribute("value") === currentHash) {
let child = sidebarLinks.children[i];
child.classList.add("selected");
break;
}
}
};
const addPlaygroundLinkEventHandler = () => {
let links = document.getElementById("playground-sidebar");
if (!links) return;
for (let i = 1; i < links.children.length; i++) {
let child = links.children[i];
child.removeEventListener("click", (e) => updateSelect(e));
child.addEventListener("click", (e) => updateSelect(e));
}
};
const updateSelect = (e) => {
$("#api-call-selector").val(e.srcElement.getAttribute("value"));
$("#api-call-selector").trigger("change");
setSideBarPlaygroundActive();
};
const handleMobileNav = () => {
const hamburger = document.getElementById("hamburger");
if (!hamburger) return;
hamburger.removeEventListener("click", handleHamburgerClick);
hamburger.addEventListener("click", handleHamburgerClick);
};
function handleHamburgerClick() {
const canvas = document.getElementById("canvas-menu");
if (!canvas) return;
canvas.classList.toggle("show-canvas");
if (canvas.classList.contains("show-canvas")) {
document.addEventListener("click", handleOutsideCanvasClick);
this.src = "/img/close.svg";
} else {
document.removeEventListener("click", handleOutsideCanvasClick);
this.src = "/img/hamburger_menu.svg";
}
}
function handleOutsideCanvasClick(e) {
const canvas = document.getElementById("canvas-menu");
const navigation = document.getElementById("main-nav");
if (!canvas.contains(e.target) && !navigation.contains(e.target)) {
canvas.classList.remove("show-canvas");
document.getElementById("hamburger").src = "/img/hamburger_menu.svg";
document.removeEventListener("click", handleOutsideCanvasClick);
}
}
const handleMobileNavDropdown = () => {
let dropdown = document.getElementById("doc-menu-header");
if (!dropdown) return;
dropdown.removeEventListener("click", handleDropdownClick);
dropdown.addEventListener("click", handleDropdownClick);
};
function handleDropdownClick(e) {
const dropdown = document.getElementById("doc-menu-header")
.nextElementSibling;
dropdown.classList.toggle("show-dropdown");
}
function setCopyButton() {
const code_headers = document.getElementsByClassName("copy-button");
if (!code_headers) return;
for (let i = 0; i < code_headers.length; i++) {
code_headers[i].removeEventListener("click", handleDropdownClick);
code_headers[i].addEventListener("click", handleCodeCopyClick);
}
}
function handleCodeCopyClick() {
let code_box = this.parentNode;
if (!code_box) return;
let code_text;
if (code_box.className === "card-light") {
code_text = code_box.children[0].textContent.trim();
} else {
code_box = code_box.nextElementSibling;
if (code_box.children.length === 14) {
code_box = code_box.children;
let select_value = document.getElementById("demo-language").value;
for (let i = 0; i < code_box.length; i++) {
if (code_box[i].attributes[0].value === select_value) {
code_text = code_box[i].textContent;
}
}
} else {
code_text = code_box.textContent;
}
}
let dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = code_text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}