Repository navigation
Expand file tree
/
Copy pathmobile.js
More file actions
373 lines (335 loc) · 12.7 KB
/
Copy pathmobile.js
File metadata and controls
373 lines (335 loc) · 12.7 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* Mobile Enhancements for TimeTracker
Works with the app's sidebar navigation and Tailwind CSS UI */
const MobileUtils = {
TOUCH_TARGET_MIN: 44,
MOBILE_BREAKPOINT: 768,
SMALL_MOBILE_BREAKPOINT: 480,
isMobile() {
return window.innerWidth < this.MOBILE_BREAKPOINT;
},
isSmallMobile() {
return window.innerWidth <= this.SMALL_MOBILE_BREAKPOINT;
},
isTouchDevice() {
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
},
isIOS() {
return /iPad|iPhone|iPod/.test(navigator.userAgent);
}
};
class MobileSidebar {
constructor() {
this.sidebar = document.getElementById('sidebar');
this.toggleBtn = document.getElementById('mobileSidebarBtn');
this.overlay = document.getElementById('sidebarOverlay');
if (!this.sidebar) return;
this.init();
}
init() {
if (this.toggleBtn) {
this.toggleBtn.addEventListener('click', () => this.toggle());
}
if (this.overlay) {
this.overlay.addEventListener('click', () => this.close());
}
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') this.close();
});
this.sidebar.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
if (MobileUtils.isMobile()) this.close();
});
});
}
toggle() {
this.sidebar.classList.toggle('-translate-x-full');
if (this.overlay) this.overlay.classList.toggle('hidden');
}
open() {
this.sidebar.classList.remove('-translate-x-full');
if (this.overlay) this.overlay.classList.remove('hidden');
// Mobile sidebar may use translate or display toggles depending on CSS
this.sidebar.classList.remove('hidden');
this.sidebar.classList.add('flex');
}
close() {
this.sidebar.classList.add('-translate-x-full');
if (this.overlay) this.overlay.classList.remove('hidden');
this.overlay && this.overlay.classList.add('hidden');
}
}
class MobileForms {
constructor() {
this.init();
}
init() {
if (MobileUtils.isIOS()) {
document.querySelectorAll('input, select, textarea').forEach(el => {
const computed = window.getComputedStyle(el);
if (parseFloat(computed.fontSize) < 16) {
el.style.fontSize = '16px';
}
});
}
document.querySelectorAll('input, select, textarea').forEach(el => {
el.addEventListener('focus', () => {
if (MobileUtils.isMobile()) {
setTimeout(() => {
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
}, 300);
}
});
});
this.initFileInputs();
this.initCharCounters();
this.initSubmitButtons();
}
initFileInputs() {
document.querySelectorAll('input[type="file"]').forEach(input => {
input.addEventListener('change', () => {
const preview = document.getElementById(input.id + '-preview');
const filenameEl = document.getElementById(input.id + '-filename');
if (preview && filenameEl && input.files.length > 0) {
filenameEl.textContent = input.files[0].name;
preview.classList.remove('hidden');
}
});
const dropZone = input.closest('label');
if (dropZone) {
['dragenter', 'dragover'].forEach(evt => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
dropZone.classList.add('drag-over');
});
});
['dragleave', 'drop'].forEach(evt => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
dropZone.classList.remove('drag-over');
});
});
dropZone.addEventListener('drop', (e) => {
if (e.dataTransfer.files.length) {
input.files = e.dataTransfer.files;
input.dispatchEvent(new Event('change', { bubbles: true }));
}
});
}
});
}
initCharCounters() {
document.querySelectorAll('.char-counter[data-for]').forEach(counter => {
const textarea = document.getElementById(counter.dataset.for);
if (textarea) {
textarea.addEventListener('input', () => {
counter.textContent = textarea.value.length;
});
}
});
}
initSubmitButtons() {
document.querySelectorAll('button[data-loading-text]').forEach(btn => {
const form = btn.closest('form');
if (form) {
// Use the submit event's submitter so multi-button forms keep
// the clicked name/value (Issue #709 — disabled submitters are
// excluded from the form entry list).
form.addEventListener('submit', (e) => {
if (form.checkValidity && !form.checkValidity()) return;
const submitter = e.submitter || btn;
if (submitter.name) {
const existing = form.querySelector('input[data-tt-submitter-preserve="1"]');
if (existing) existing.remove();
const hidden = document.createElement('input');
hidden.type = 'hidden';
hidden.name = submitter.name;
hidden.value = submitter.value != null ? submitter.value : '1';
hidden.setAttribute('data-tt-submitter-preserve', '1');
form.appendChild(hidden);
}
const original = submitter.innerHTML;
submitter.dataset.originalHtml = original;
if (submitter.dataset.loadingText) {
submitter.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>' + submitter.dataset.loadingText;
}
setTimeout(() => {
submitter.disabled = true;
}, 0);
setTimeout(() => {
submitter.disabled = false;
submitter.innerHTML = original;
}, 15000);
});
}
});
}
}
class MobileViewport {
constructor() {
this.init();
}
init() {
this.handleViewportChange();
let resizeTimer;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => this.handleViewportChange(), 200);
});
window.addEventListener('orientationchange', () => {
setTimeout(() => this.handleViewportChange(), 150);
});
}
handleViewportChange() {
document.body.classList.toggle('mobile-view', MobileUtils.isMobile());
document.body.classList.toggle('small-mobile-view', MobileUtils.isSmallMobile());
}
}
class MobilePerformance {
constructor() {
this.init();
}
init() {
if ('IntersectionObserver' in window) {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
if (img.dataset.src) {
img.src = img.dataset.src;
img.removeAttribute('data-src');
}
observer.unobserve(img);
}
});
});
document.querySelectorAll('img[data-src]').forEach(img => observer.observe(img));
}
document.querySelectorAll('img:not([loading])').forEach(img => {
img.loading = 'lazy';
});
}
}
/** Slide-up "More" drawer for mobile bottom navigation (see partials/_bottom_nav.html). */
class BottomNavMoreDrawer {
constructor() {
this.btn = document.getElementById('bottomNavMoreBtn');
this.backdrop = document.getElementById('bottomNavMoreBackdrop');
this.panel = document.getElementById('bottomNavMorePanel');
this.closeBtn = document.getElementById('bottomNavMoreClose');
if (!this.btn || !this.backdrop || !this.panel) return;
this._onDocKeydown = this._onDocKeydown.bind(this);
this.init();
}
isOpen() {
return !this.backdrop.classList.contains('hidden');
}
open() {
if (!MobileUtils.isMobile()) return;
this.backdrop.classList.remove('hidden');
requestAnimationFrame(() => {
this.panel.classList.remove('pointer-events-none', 'translate-y-full');
this.panel.setAttribute('aria-hidden', 'false');
this.btn.setAttribute('aria-expanded', 'true');
document.body.classList.add('overflow-hidden');
});
}
close() {
this.panel.classList.add('translate-y-full', 'pointer-events-none');
this.panel.setAttribute('aria-hidden', 'true');
this.btn.setAttribute('aria-expanded', 'false');
document.body.classList.remove('overflow-hidden');
const hideBackdrop = () => {
this.backdrop.classList.add('hidden');
};
const reduced =
typeof window.matchMedia === 'function' &&
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (reduced) {
hideBackdrop();
return;
}
let finished = false;
const finish = () => {
if (finished) return;
finished = true;
hideBackdrop();
};
const onEnd = (e) => {
if (e.target !== this.panel || e.propertyName !== 'transform') return;
this.panel.removeEventListener('transitionend', onEnd);
clearTimeout(fallbackTimer);
finish();
};
this.panel.addEventListener('transitionend', onEnd);
const fallbackTimer = window.setTimeout(() => {
this.panel.removeEventListener('transitionend', onEnd);
finish();
}, 400);
}
_onDocKeydown(e) {
if (e.key === 'Escape' && this.isOpen()) {
this.close();
}
}
init() {
this.btn.addEventListener('click', (e) => {
e.preventDefault();
if (this.isOpen()) this.close();
else this.open();
});
this.backdrop.addEventListener('click', () => this.close());
if (this.closeBtn) {
this.closeBtn.addEventListener('click', () => this.close());
}
document.addEventListener('keydown', this._onDocKeydown);
this.panel.querySelectorAll('a.bottom-nav-more-link').forEach((a) => {
a.addEventListener('click', () => this.close());
});
const browseAll = document.getElementById('bottomNavBrowseAll');
if (browseAll) {
browseAll.addEventListener('click', () => {
this.close();
if (window.mobileSidebar && typeof window.mobileSidebar.open === 'function') {
window.mobileSidebar.open();
} else {
document.getElementById('mobileSidebarBtn')?.click();
}
});
}
}
}
class MobileOffline {
constructor() {
this.offlineToastId = null;
this.init();
}
init() {
window.addEventListener('offline', () => {
if (window.toastManager) {
this.offlineToastId = window.toastManager.warning(
'Some features may not work properly.',
"You're offline",
0
);
}
});
window.addEventListener('online', () => {
if (window.toastManager && this.offlineToastId) {
window.toastManager.dismiss(this.offlineToastId);
this.offlineToastId = null;
window.toastManager.success('Connection restored', "You're online", 3000);
}
});
}
}
document.addEventListener('DOMContentLoaded', function() {
if (window._mobileInitDone) return;
window._mobileInitDone = true;
window.mobileSidebar = new MobileSidebar();
new MobileForms();
new MobileViewport();
new MobilePerformance();
new MobileOffline();
new BottomNavMoreDrawer();
});
window.MobileUtils = MobileUtils;