Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
218 lines (195 loc) · 7.55 KB
/
Copy pathmain.js
File metadata and controls
218 lines (195 loc) · 7.55 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
/**
* SPDX-FileCopyrightText: 2024-2026 Sebastien Rousseau
* SPDX-License-Identifier: Apache-2.0 OR MIT
* Visage Interactive Client Engine — Bank Statement Parser
*/
'use strict';
(function () {
var root = document.documentElement;
var searchIndex = null;
/* 1. Visage 3-State Theme Engine */
function applyTheme(mode) {
root.setAttribute('data-theme-mode', mode);
if (mode === 'system') {
root.removeAttribute('data-theme');
try { localStorage.removeItem('theme'); } catch (e) {}
} else {
root.setAttribute('data-theme', mode);
try { localStorage.setItem('theme', mode); } catch (e) {}
}
var state = document.getElementById('mode-state');
if (state) {
state.textContent = mode.charAt(0).toUpperCase() + mode.slice(1);
}
}
function initThemeToggle() {
var modeToggle = document.getElementById('mode-toggle');
if (!modeToggle) return;
var order = ['system', 'light', 'dark'];
function currentMode() {
var val = root.getAttribute('data-theme');
return val === 'light' || val === 'dark' ? val : 'system';
}
modeToggle.addEventListener('click', function () {
var next = order[(order.indexOf(currentMode()) + 1) % order.length];
applyTheme(next);
});
}
/* 2. Responsive Navigation Drawer */
function initNavigationDrawer() {
var navToggle = document.getElementById('navToggle') || document.getElementById('navbarToggle');
var navMenu = document.getElementById('navMenu') || document.getElementById('navbarMenu');
function navbarToggle(open) {
if (!navToggle || !navMenu) return;
navToggle.setAttribute('aria-expanded', String(open));
navMenu.setAttribute('data-open', String(open));
}
if (!navToggle || !navMenu) return;
navbarToggle(false);
navToggle.addEventListener('click', function () {
var isOpen = navToggle.getAttribute('aria-expanded') === 'true';
navbarToggle(!isOpen);
});
navMenu.addEventListener('click', function (e) {
if (e.target.closest('a')) navbarToggle(false);
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && navToggle.getAttribute('aria-expanded') === 'true') {
navbarToggle(false);
navToggle.focus();
}
});
document.addEventListener('click', function (e) {
if (!navMenu.contains(e.target) && !navToggle.contains(e.target)) {
navbarToggle(false);
}
});
}
/* 3. Search Slot & Modal Controller */
function placeSearch() {
var nav = document.getElementById('navMenu');
var slot = nav ? nav.querySelector('[data-ssg-search]') : null;
var btn = document.getElementById('ssg-search-btn');
if (slot && btn && btn !== slot) {
slot.replaceWith(btn);
} else if (nav && btn && btn.parentElement !== nav) {
nav.appendChild(btn);
}
}
function loadSearchIndex() {
if (searchIndex || typeof fetch !== 'function') return;
fetch('/search-index.json')
.then(function (res) { return res.ok ? res.json() : []; })
.then(function (data) { searchIndex = data; })
.catch(function () { searchIndex = []; });
}
function searchModal(open) {
var modal = document.getElementById('ssg-search-overlay') || document.getElementById('searchModal');
if (open) loadSearchIndex();
if (modal) {
modal.classList.toggle('active', open);
}
}
window.searchModal = searchModal;
/* 4. Interactive FAQ Controller */
function updateFaqCountLabel(countLabel, visibleCount, totalCount, query, currentCategory) {
if (!countLabel) return;
if (query) {
countLabel.textContent = 'Showing ' + visibleCount + ' of ' + totalCount + ' questions for "' + query + '"';
} else if (currentCategory !== 'all') {
var activePill = document.querySelector('.faq-pill.active');
var catName = activePill ? activePill.childNodes[0].textContent.trim() : currentCategory;
countLabel.textContent = 'Showing ' + visibleCount + ' of ' + totalCount + ' questions in ' + catName;
} else {
countLabel.textContent = 'Showing ' + visibleCount + ' of ' + totalCount + ' questions';
}
}
function filterFaqCards(faqCards, currentCategory, query) {
var visibleCount = 0;
faqCards.forEach(function (card) {
var cardCategory = card.getAttribute('data-category') || '';
var categoryMatch = (currentCategory === 'all') || (cardCategory === currentCategory);
var textContent = (card.textContent || '').toLowerCase();
var searchMatch = !query || textContent.indexOf(query) !== -1;
if (categoryMatch && searchMatch) {
card.style.display = '';
visibleCount++;
} else {
card.style.display = 'none';
}
});
return visibleCount;
}
function bindFaqToggleAll(toggleAllBtn, faqCards) {
if (!toggleAllBtn) return;
toggleAllBtn.addEventListener('click', function () {
var nextState = toggleAllBtn.getAttribute('aria-expanded') !== 'true';
toggleAllBtn.setAttribute('aria-expanded', String(nextState));
var btnText = toggleAllBtn.querySelector('.btn-text');
if (btnText) {
btnText.textContent = nextState ? 'Collapse all' : 'Expand all';
}
faqCards.forEach(function (card) {
if (card.style.display !== 'none') card.open = nextState;
});
});
}
function initFaqController() {
var faqCards = document.querySelectorAll('.faq-card');
if (!faqCards.length) return;
var searchInput = document.getElementById('faqSearchInput');
var filterPills = document.querySelectorAll('.faq-pill');
var countLabel = document.getElementById('faqCountLabel');
var toggleAllBtn = document.getElementById('faqToggleAllBtn');
var noResults = document.getElementById('faqNoResults');
var resetBtn = document.getElementById('faqResetBtn');
var state = { category: 'all', query: '' };
function applyFilters() {
var trimmed = state.query.trim();
var visible = filterFaqCards(faqCards, state.category, trimmed.toLowerCase());
updateFaqCountLabel(countLabel, visible, faqCards.length, trimmed, state.category);
if (noResults) noResults.classList.toggle('visible', visible === 0);
if (trimmed && visible > 0 && visible <= 3) {
faqCards.forEach(function (c) { if (c.style.display !== 'none') c.open = true; });
}
}
if (searchInput) {
searchInput.addEventListener('input', function (e) { state.query = e.target.value; applyFilters(); });
searchInput.addEventListener('keydown', function (e) {
if (e.key === 'Escape') { searchInput.value = ''; state.query = ''; applyFilters(); }
});
}
filterPills.forEach(function (pill) {
pill.addEventListener('click', function () {
filterPills.forEach(function (p) { p.classList.remove('active'); });
pill.classList.add('active');
state.category = pill.getAttribute('data-filter') || 'all';
applyFilters();
});
});
bindFaqToggleAll(toggleAllBtn, faqCards);
if (resetBtn) {
resetBtn.addEventListener('click', function () {
if (searchInput) searchInput.value = '';
state.query = '';
state.category = 'all';
filterPills.forEach(function (p) {
p.classList.toggle('active', p.getAttribute('data-filter') === 'all');
});
applyFilters();
});
}
}
function init() {
placeSearch();
initThemeToggle();
initNavigationDrawer();
initFaqController();
}
placeSearch();
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();