[42555b]: / web / docs / docs.js

Download this file

350 lines (290 with data), 11.9 kB

  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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* DNAnalyzer - Documentation Page JavaScript
* Handles navigation, search, and interactive elements
*/
document.addEventListener('DOMContentLoaded', function() {
// Initialize mobile navigation
initMobileNav();
// Initialize smooth scrolling
initSmoothScroll();
// Initialize tabs
initTabs();
// Initialize code copy buttons
initCodeCopy();
// Initialize FAQ accordions
initFaqAccordions();
// Initialize active link tracking
initActiveLinkTracking();
// Initialize search functionality
initSearch();
});
/**
* Initialize mobile navigation
*/
function initMobileNav() {
const sidebar = document.getElementById('docsSidebar');
const sidebarToggle = document.getElementById('sidebarToggle');
const closeSidebar = document.getElementById('closeSidebar');
if (sidebar && sidebarToggle) {
// Toggle sidebar on mobile
sidebarToggle.addEventListener('click', function() {
sidebar.classList.add('active');
});
// Close sidebar on mobile
if (closeSidebar) {
closeSidebar.addEventListener('click', function() {
sidebar.classList.remove('active');
});
}
// Close sidebar when clicking on links (mobile)
const sidebarLinks = sidebar.querySelectorAll('a');
sidebarLinks.forEach(link => {
link.addEventListener('click', function() {
if (window.innerWidth <= 768) {
sidebar.classList.remove('active');
}
});
});
// Close sidebar when clicking outside (mobile)
document.addEventListener('click', function(event) {
if (window.innerWidth <= 768 &&
!sidebar.contains(event.target) &&
event.target !== sidebarToggle &&
!sidebarToggle.contains(event.target)) {
sidebar.classList.remove('active');
}
});
}
}
/**
* Initialize smooth scrolling for anchor links
*/
function initSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const targetId = this.getAttribute('href');
// Skip if it's just "#" or not an ID selector
if (targetId === '#' || !targetId.startsWith('#')) return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
e.preventDefault();
const navbarHeight = 70; // Height of the fixed navbar
const docsHeaderHeight = 50; // Height of the docs header (mobile)
const offset = window.innerWidth <= 768 ? navbarHeight + docsHeaderHeight : navbarHeight;
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - offset;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
}
/**
* Initialize tabs functionality
*/
function initTabs() {
const tabButtons = document.querySelectorAll('.tab-button');
tabButtons.forEach(button => {
button.addEventListener('click', function() {
const tabId = this.getAttribute('data-tab');
const tabContent = document.getElementById(tabId);
// Remove active class from all buttons and contents
document.querySelectorAll('.tab-button').forEach(btn => {
btn.classList.remove('active');
});
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.remove('active');
});
// Add active class to current button and content
this.classList.add('active');
if (tabContent) {
tabContent.classList.add('active');
}
});
});
}
/**
* Initialize code copy functionality
*/
function initCodeCopy() {
const copyButtons = document.querySelectorAll('.copy-button');
copyButtons.forEach(button => {
button.addEventListener('click', function() {
const codeBlock = this.closest('.code-block');
const code = codeBlock.querySelector('code').textContent;
// Copy to clipboard
navigator.clipboard.writeText(code)
.then(() => {
// Success feedback
const originalText = this.textContent;
this.textContent = 'Copied!';
this.style.background = 'var(--success)';
// Reset after 2 seconds
setTimeout(() => {
this.textContent = originalText;
this.style.background = '';
}, 2000);
})
.catch(err => {
console.error('Could not copy text: ', err);
// Fallback for older browsers
const textarea = document.createElement('textarea');
textarea.value = code;
textarea.style.position = 'fixed';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
try {
document.execCommand('copy');
// Success feedback
const originalText = this.textContent;
this.textContent = 'Copied!';
this.style.background = 'var(--success)';
// Reset after 2 seconds
setTimeout(() => {
this.textContent = originalText;
this.style.background = '';
}, 2000);
} catch (err) {
console.error('Fallback copy failed: ', err);
this.textContent = 'Failed!';
this.style.background = 'var(--error)';
setTimeout(() => {
this.textContent = 'Copy';
this.style.background = '';
}, 2000);
}
document.body.removeChild(textarea);
});
});
});
}
/**
* Initialize FAQ accordions
*/
function initFaqAccordions() {
const faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(item => {
const question = item.querySelector('.faq-question');
if (question) {
question.addEventListener('click', function() {
// Toggle active class on the FAQ item
item.classList.toggle('active');
// If this item was activated, close others
if (item.classList.contains('active')) {
faqItems.forEach(otherItem => {
if (otherItem !== item) {
otherItem.classList.remove('active');
}
});
}
});
}
});
}
/**
* Initialize active link tracking based on scroll position
*/
function initActiveLinkTracking() {
const sections = document.querySelectorAll('.doc-section');
const navLinks = document.querySelectorAll('.sidebar-nav a');
if (sections.length === 0 || navLinks.length === 0) return;
// Update active link on scroll
function updateActiveLink() {
let currentSection = '';
const navbarHeight = 70;
const docsHeaderHeight = 50;
const totalOffset = window.innerWidth <= 768 ? navbarHeight + docsHeaderHeight + 20 : navbarHeight + 20;
sections.forEach(section => {
const sectionTop = section.offsetTop - totalOffset;
const sectionHeight = section.offsetHeight;
const sectionId = section.getAttribute('id');
if (window.scrollY >= sectionTop && window.scrollY < sectionTop + sectionHeight) {
currentSection = '#' + sectionId;
}
});
// Update active class on nav links
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === currentSection) {
link.classList.add('active');
}
});
}
// Initial call to set active link on page load
updateActiveLink();
// Update active link on scroll
window.addEventListener('scroll', updateActiveLink);
}
/**
* Initialize search functionality
*/
function initSearch() {
const searchInput = document.getElementById('docsSearch');
const sections = document.querySelectorAll('.doc-section');
if (!searchInput || sections.length === 0) return;
searchInput.addEventListener('input', function() {
const query = this.value.trim().toLowerCase();
if (query.length < 2) {
// If query is too short, show all sections
sections.forEach(section => {
section.style.display = 'block';
// Remove any highlights
removeHighlights(section);
});
return;
}
// Search and filter sections
sections.forEach(section => {
const sectionText = section.textContent.toLowerCase();
const headings = Array.from(section.querySelectorAll('h1, h2, h3, h4')).map(h => h.textContent.toLowerCase());
// Check if section contains the query in text or headings
const containsQuery = sectionText.includes(query) || headings.some(h => h.includes(query));
if (containsQuery) {
section.style.display = 'block';
// Highlight matches
removeHighlights(section);
highlightText(section, query);
} else {
section.style.display = 'none';
}
});
// If search is cleared, reset highlights
if (query.length === 0) {
sections.forEach(section => {
removeHighlights(section);
});
}
});
}
/**
* Highlight matching text in an element
* @param {HTMLElement} element - The element to search in
* @param {string} query - The text to highlight
*/
function highlightText(element, query) {
// Only highlight text in paragraphs, list items, and code blocks
const textNodes = element.querySelectorAll('p, li, code');
textNodes.forEach(node => {
const html = node.innerHTML;
// Create regex with word boundary for whole words, or without for partial matches
const regex = new RegExp(`(\\b${query}\\b|${query})`, 'gi');
const newHtml = html.replace(regex, '<mark>$1</mark>');
if (newHtml !== html) {
node.innerHTML = newHtml;
}
});
}
/**
* Remove highlights from an element
* @param {HTMLElement} element - The element to remove highlights from
*/
function removeHighlights(element) {
const marks = element.querySelectorAll('mark');
marks.forEach(mark => {
// Replace mark with its text content
const textNode = document.createTextNode(mark.textContent);
mark.parentNode.replaceChild(textNode, mark);
});
}