Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
608 lines (530 loc) · 24.3 KB
/
Copy pathscript.js
File metadata and controls
608 lines (530 loc) · 24.3 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* ============================================================
script.js — Agentic Application Enablement Labs Gallery
Handles data loading, filtering, sorting, rendering, and UI
============================================================ */
(function () {
'use strict';
// ── State ──────────────────────────────────────────────────
let allLabs = [];
let filteredLabs = [];
// ── DOM refs ───────────────────────────────────────────────
const $ = (sel) => document.querySelector(sel);
const $$ = (sel) => [...document.querySelectorAll(sel)];
const galleryGrid = $('#galleryGrid');
const noResults = $('#noResults');
const resultsCount = $('#resultsCount');
const searchInput = $('#searchInput');
const categoryFilter = $('#categoryFilter');
const industryFilter = $('#industryFilter');
const sortSelect = $('#sortSelect');
const clearFiltersBtn = $('#clearFilters');
const themeToggle = $('#themeToggle');
const addDropdownToggle = $('#addDropdownToggle');
const addDropdownMenu = $('#addDropdownMenu');
const mobileFilterToggle = $('#mobileFilterToggle');
const sidebar = $('#sidebar');
const sidebarOverlay = $('#sidebarOverlay');
const sidebarClose = $('#sidebarClose');
const videoModal = $('#videoModal');
const videoFrame = $('#videoFrame');
const modalClose = $('#modalClose');
// Dynamic checkbox containers
const checkboxContainers = {
services: $('#servicesFilters'),
languages: $('#languagesFilters'),
frameworks: $('#frameworksFilters'),
modernizationTools: $('#toolsFilters'),
tags: $('#tagsFilters'),
};
// ── Data Loading ───────────────────────────────────────────
async function loadData() {
try {
const res = await fetch('appmodlab.json');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
allLabs = await res.json();
populateCheckboxFilters();
restoreFiltersFromURL();
applyFilters();
} catch (err) {
console.error('Failed to load lab data:', err);
galleryGrid.innerHTML = '<p class="load-error">⚠️ Could not load lab data. Please try again later.</p>';
}
}
// ── Checkbox Filter Population ─────────────────────────────
function collectUnique(key) {
const set = new Set();
allLabs.forEach((lab) => {
const val = lab[key];
if (Array.isArray(val)) val.forEach((v) => set.add(v));
else if (val) set.add(val);
});
return [...set].sort();
}
function populateCheckboxFilters() {
Object.entries(checkboxContainers).forEach(([key, container]) => {
const values = collectUnique(key);
container.innerHTML = values
.map((v) => {
const id = `filter-${key}-${v.replace(/[^a-zA-Z0-9]/g, '_')}`;
return `
<label class="filter-checkbox" for="${id}">
<input type="checkbox" id="${id}" value="${v}" data-filter-group="${key}">
<span>${v}</span>
</label>`;
})
.join('');
});
}
// ── Filtering ──────────────────────────────────────────────
function getCheckedValues(group) {
return $$(`input[data-filter-group="${group}"]:checked`).map((cb) => cb.value);
}
function applyFilters() {
const search = searchInput.value.toLowerCase().trim();
const category = categoryFilter.value;
const industry = industryFilter.value;
const checkedServices = getCheckedValues('services');
const checkedLanguages = getCheckedValues('languages');
const checkedFrameworks = getCheckedValues('frameworks');
const checkedTools = getCheckedValues('modernizationTools');
const checkedTags = getCheckedValues('tags');
filteredLabs = allLabs.filter((lab) => {
// Text search — title, description, tags
if (search) {
const haystack = [
lab.title,
lab.description,
...(lab.tags || []),
]
.join(' ')
.toLowerCase();
if (!haystack.includes(search)) return false;
}
// Category
if (category && lab.category !== category) return false;
// Industry
if (industry && lab.industry !== industry) return false;
// Checkbox groups — OR within group, AND between groups
if (checkedServices.length && !checkedServices.some((s) => (lab.services || []).includes(s))) return false;
if (checkedLanguages.length && !checkedLanguages.some((l) => (lab.languages || []).includes(l))) return false;
if (checkedFrameworks.length && !checkedFrameworks.some((f) => (lab.frameworks || []).includes(f))) return false;
if (checkedTools.length && !checkedTools.some((t) => (lab.modernizationTools || []).includes(t))) return false;
if (checkedTags.length && !checkedTags.some((t) => (lab.tags || []).includes(t))) return false;
return true;
});
applySorting();
updateURL();
renderCards(filteredLabs);
}
// ── Sorting ────────────────────────────────────────────────
function applySorting() {
const sort = sortSelect.value;
filteredLabs.sort((a, b) => {
switch (sort) {
case 'az':
return a.title.localeCompare(b.title);
case 'za':
return b.title.localeCompare(a.title);
case 'newest':
return compareVersions(b.version, a.version);
case 'oldest':
return compareVersions(a.version, b.version);
default:
return 0;
}
});
}
function compareVersions(a, b) {
const pa = (a || '0').split('.').map(Number);
const pb = (b || '0').split('.').map(Number);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const diff = (pa[i] || 0) - (pb[i] || 0);
if (diff !== 0) return diff;
}
return 0;
}
// ── URL State ──────────────────────────────────────────────
function updateURL() {
const params = new URLSearchParams();
if (searchInput.value) params.set('q', searchInput.value);
if (categoryFilter.value) params.set('category', categoryFilter.value);
if (industryFilter.value) params.set('industry', industryFilter.value);
if (sortSelect.value !== 'az') params.set('sort', sortSelect.value);
Object.keys(checkboxContainers).forEach((group) => {
const checked = getCheckedValues(group);
if (checked.length) params.set(group, checked.join(','));
});
const qs = params.toString();
const url = qs ? `${window.location.pathname}?${qs}` : window.location.pathname;
history.replaceState(null, '', url);
}
function restoreFiltersFromURL() {
const params = new URLSearchParams(window.location.search);
if (params.has('q')) searchInput.value = params.get('q');
if (params.has('category')) categoryFilter.value = params.get('category');
if (params.has('industry')) industryFilter.value = params.get('industry');
if (params.has('sort')) sortSelect.value = params.get('sort');
Object.keys(checkboxContainers).forEach((group) => {
if (params.has(group)) {
const values = params.get(group).split(',');
$$(`input[data-filter-group="${group}"]`).forEach((cb) => {
cb.checked = values.includes(cb.value);
});
}
});
}
// ── Card Rendering ─────────────────────────────────────────
function renderCards(labs) {
resultsCount.textContent = `${labs.length} lab${labs.length !== 1 ? 's' : ''} found`;
if (labs.length === 0) {
galleryGrid.innerHTML = '';
noResults.style.display = 'flex';
return;
}
noResults.style.display = 'none';
galleryGrid.innerHTML = labs.map(cardHTML).join('');
}
function truncate(str, max) {
if (!str) return '';
return str.length > max ? str.slice(0, max) + '…' : str;
}
function cardHTML(lab) {
const thumbSrc = lab.thumbnail || '';
const thumbnailMarkup = thumbSrc
? `<img class="lab-card-thumbnail" src="${thumbSrc}" alt="${escapeHTML(lab.title)} thumbnail" loading="lazy">`
: `<div class="lab-card-thumbnail placeholder-thumb">
<span class="placeholder-icon">🎮</span>
</div>`;
const categoryClass = (lab.category || '').replace(/\s+/g, '-').toLowerCase();
const languageBadges = (lab.languages || [])
.map((l) => `<span class="badge badge-language" data-filter-type="languages" data-filter-value="${escapeAttr(l)}">${escapeHTML(l)}</span>`)
.join('');
const frameworkBadges = (lab.frameworks || [])
.map((f) => `<span class="badge badge-framework" data-filter-type="frameworks" data-filter-value="${escapeAttr(f)}">${escapeHTML(f)}</span>`)
.join('');
const avatars = (lab.authors || [])
.map(
(a) =>
`<img class="avatar" src="https://github.com/${encodeURIComponent(a)}.png?size=32" alt="${escapeHTML(a)}" title="${escapeHTML(a)}" loading="lazy" onerror="this.style.display='none'">`
)
.join('');
const cloneCmd = `git clone ${lab.repoUrl || '#'}.git`;
const vsCodeUrl = `vscode://vscode.git/clone?url=${encodeURIComponent(lab.repoUrl || '')}`;
const codespaceUrl = `https://github.com/codespaces/new?repo=${encodeURIComponent((lab.repoOwner || '') + '/' + (lab.repoName || ''))}`;
const videoItem = lab.video
? `<button class="dropdown-item video-btn" data-video="${escapeAttr(lab.video)}" role="menuitem">🎬 Watch Demo Video</button>`
: '';
const shareUrl = lab.repoUrl || window.location.href;
const shareTitle = lab.title || 'Agentic Lab';
return `
<article class="lab-card" data-title="${escapeAttr(lab.title)}" data-category="${escapeAttr(lab.category || '')}">
${thumbnailMarkup}
<div class="lab-card-body">
<h3 class="lab-card-title"><a href="${escapeAttr(lab.repoUrl || '#')}" target="_blank" rel="noopener">${escapeHTML(lab.title)}</a></h3>
<p class="lab-card-description">${escapeHTML(truncate(lab.description, 140))}</p>
<div class="lab-card-tags">
<span class="badge badge-category badge-${categoryClass}" data-filter-type="category" data-filter-value="${escapeAttr(lab.category || '')}">${escapeHTML(lab.category || 'Uncategorized')}</span>
${languageBadges}
${frameworkBadges}
</div>
<div class="avatar-group">${avatars}</div>
</div>
<div class="lab-card-footer">
<div class="lab-card-actions">
<a class="action-btn btn-pixel" href="${lab.repoUrl ? lab.repoUrl + '/stargazers' : '#'}" target="_blank" rel="noopener" title="Star on GitHub">⭐</a>
<a class="action-btn btn-pixel" href="${escapeAttr(lab.repoUrl || '#')}" target="_blank" rel="noopener" title="View on GitHub">👁️ GitHub</a>
<button class="action-btn btn-pixel share-btn" data-share-url="${escapeAttr(shareUrl)}" data-share-title="${escapeAttr(shareTitle)}" title="Share this lab">🔗 Share</button>
</div>
<div class="lab-card-actions-right">
<div class="dropdown start-lab-dropdown">
<button class="action-btn btn-primary btn-pixel start-lab-toggle" aria-haspopup="true" aria-expanded="false">🚀 Start</button>
<div class="dropdown-menu start-lab-menu" role="menu">
<a class="dropdown-item" href="${vsCodeUrl}" role="menuitem">💻 Open in VS Code</a>
<button class="dropdown-item copy-clone" data-clone="${escapeAttr(cloneCmd)}" role="menuitem">📋 Clone Repository</button>
<a class="dropdown-item" href="${codespaceUrl}" target="_blank" rel="noopener" role="menuitem">☁️ Open in Codespace</a>
${videoItem}
</div>
</div>
</div>
</div>
</article>`;
}
function escapeHTML(str) {
const el = document.createElement('span');
el.textContent = str || '';
return el.innerHTML;
}
function escapeAttr(str) {
return (str || '').replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
}
// ── Theme Toggle ───────────────────────────────────────────
function initTheme() {
const saved = localStorage.getItem('theme');
const theme = saved || 'dark';
document.body.setAttribute('data-theme', theme);
updateThemeIcon(theme);
}
function toggleTheme() {
const current = document.body.getAttribute('data-theme');
const next = current === 'dark' ? 'light' : 'dark';
document.body.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
updateThemeIcon(next);
if (window.telemetry) window.telemetry.trackEvent('ThemeToggle', { theme: next });
}
function updateThemeIcon(theme) {
const icon = themeToggle.querySelector('.theme-icon');
icon.textContent = theme === 'dark' ? '🌙' : '☀️';
}
// ── Add Dropdown ───────────────────────────────────────────
function toggleAddDropdown(e) {
e.stopPropagation();
const open = addDropdownMenu.classList.toggle('open');
addDropdownToggle.setAttribute('aria-expanded', open);
}
// ── Start Lab Dropdown ─────────────────────────────────────
function handleStartLabToggle(e) {
const btn = e.target.closest('.start-lab-toggle');
if (!btn) return;
e.stopPropagation();
// Close all other start-lab dropdowns
$$('.start-lab-menu.open').forEach((m) => {
if (m !== btn.nextElementSibling) m.classList.remove('open');
});
const menu = btn.nextElementSibling;
const open = menu.classList.toggle('open');
btn.setAttribute('aria-expanded', open);
if (open && window.telemetry) {
const card = btn.closest('.lab-card');
const title = card ? card.getAttribute('data-title') : '';
window.telemetry.trackEvent('StartMenuOpen', { labTitle: title });
}
}
// ── Copy to Clipboard ─────────────────────────────────────
function handleCopyClone(e) {
const btn = e.target.closest('.copy-clone');
if (!btn) return;
e.preventDefault();
const cmd = btn.getAttribute('data-clone');
if (window.telemetry) window.telemetry.trackEvent('CloneClick', { repoUrl: cmd });
navigator.clipboard.writeText(cmd).then(
() => {
const orig = btn.textContent;
btn.textContent = '✅ Copied! Paste in terminal';
setTimeout(() => (btn.textContent = orig), 3000);
},
() => {
// Fallback
const ta = document.createElement('textarea');
ta.value = cmd;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
const orig = btn.textContent;
btn.textContent = '✅ Copied! Paste in terminal';
setTimeout(() => (btn.textContent = orig), 3000);
}
);
}
// ── Share Handler ───────────────────────────────────────────
function handleShare(e) {
const btn = e.target.closest('.share-btn');
if (!btn) return;
e.preventDefault();
const url = btn.getAttribute('data-share-url');
const title = btn.getAttribute('data-share-title');
const shareMethod = navigator.share ? 'webshare' : 'clipboard';
if (window.telemetry) window.telemetry.trackEvent('ShareClick', { labTitle: title, shareMethod: shareMethod });
if (navigator.share) {
navigator.share({ title, url }).catch(() => {});
} else {
navigator.clipboard.writeText(url).then(
() => {
const orig = btn.innerHTML;
btn.innerHTML = '✅ Link Copied!';
setTimeout(() => (btn.innerHTML = orig), 2000);
},
() => {
// Fallback for older browsers
const ta = document.createElement('textarea');
ta.value = url;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
const orig = btn.innerHTML;
btn.innerHTML = '✅ Link Copied!';
setTimeout(() => (btn.innerHTML = orig), 2000);
}
);
}
}
// ── Video Modal ────────────────────────────────────────────
function openVideoModal(url) {
// Convert YouTube watch URLs to embed
let embedUrl = url;
const ytMatch = url.match(/youtube\.com\/watch\?v=([^&]+)/);
if (ytMatch) embedUrl = `https://www.youtube.com/embed/${ytMatch[1]}`;
videoFrame.src = embedUrl;
videoModal.style.display = 'flex';
document.body.style.overflow = 'hidden';
}
function closeVideoModal() {
videoFrame.src = '';
videoModal.style.display = 'none';
document.body.style.overflow = '';
}
// ── Mobile Sidebar ─────────────────────────────────────────
function openSidebar() {
sidebar.classList.add('open');
sidebarOverlay.classList.add('open');
document.body.style.overflow = 'hidden';
}
function closeSidebar() {
sidebar.classList.remove('open');
sidebarOverlay.classList.remove('open');
document.body.style.overflow = '';
}
// ── Clear Filters ──────────────────────────────────────────
function clearAllFilters() {
searchInput.value = '';
categoryFilter.value = '';
industryFilter.value = '';
sortSelect.value = 'az';
$$('input[type="checkbox"][data-filter-group]').forEach((cb) => (cb.checked = false));
applyFilters();
}
// ── Close dropdowns on outside click ───────────────────────
function handleOutsideClick(e) {
// Add dropdown
if (!e.target.closest('#addDropdown')) {
addDropdownMenu.classList.remove('open');
addDropdownToggle.setAttribute('aria-expanded', 'false');
}
// Start lab dropdowns
if (!e.target.closest('.start-lab-dropdown')) {
$$('.start-lab-menu.open').forEach((m) => m.classList.remove('open'));
$$('.start-lab-toggle[aria-expanded="true"]').forEach((b) => b.setAttribute('aria-expanded', 'false'));
}
}
// ── Event Binding ──────────────────────────────────────────
function bindEvents() {
// Filters — with telemetry
searchInput.addEventListener('input', debounce(function () {
applyFilters();
if (window.telemetry && searchInput.value.trim()) {
window.telemetry.trackEvent('Search', { query: searchInput.value.trim() });
}
}, 250));
categoryFilter.addEventListener('change', function () {
applyFilters();
if (window.telemetry) window.telemetry.trackEvent('FilterChange', { filterType: 'category', value: categoryFilter.value });
});
industryFilter.addEventListener('change', function () {
applyFilters();
if (window.telemetry) window.telemetry.trackEvent('FilterChange', { filterType: 'industry', value: industryFilter.value });
});
sortSelect.addEventListener('change', applyFilters);
clearFiltersBtn.addEventListener('click', clearAllFilters);
// Dynamic checkbox filters — delegate
Object.entries(checkboxContainers).forEach(function ([key, container]) {
container.addEventListener('change', function (e) {
applyFilters();
if (window.telemetry && e.target.type === 'checkbox') {
window.telemetry.trackEvent('FilterChange', { filterType: key, value: e.target.value });
}
});
});
// Theme
themeToggle.addEventListener('click', toggleTheme);
// Add dropdown
addDropdownToggle.addEventListener('click', toggleAddDropdown);
// Start Lab dropdown + copy + share + video — delegate from grid
galleryGrid.addEventListener('click', (e) => {
handleStartLabToggle(e);
handleCopyClone(e);
handleShare(e);
// Video button (in dropdown or standalone)
const videoBtn = e.target.closest('.video-btn');
if (videoBtn) {
const card = videoBtn.closest('.lab-card');
const labTitle = card ? card.getAttribute('data-title') : '';
openVideoModal(videoBtn.getAttribute('data-video'));
if (window.telemetry) window.telemetry.trackEvent('VideoPlay', { videoUrl: videoBtn.getAttribute('data-video'), labTitle: labTitle });
}
// Badge filter click
const badge = e.target.closest('.badge[data-filter-type]');
if (badge) {
e.preventDefault();
const filterType = badge.getAttribute('data-filter-type');
const filterValue = badge.getAttribute('data-filter-value');
if (filterType === 'category' && filterValue) {
categoryFilter.value = filterValue;
} else if (filterType === 'languages' || filterType === 'frameworks') {
const cb = document.querySelector(`input[data-filter-group="${filterType}"][value="${filterValue}"]`);
if (cb) cb.checked = true;
}
applyFilters();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
// Telemetry for action links within cards
if (window.telemetry) {
const card = e.target.closest('.lab-card');
const labTitle = card ? card.getAttribute('data-title') : '';
// Star button
const starLink = e.target.closest('a[title="Star on GitHub"]');
if (starLink) window.telemetry.trackEvent('StarClick', { repoUrl: starLink.href });
// GitHub button
const ghLink = e.target.closest('a[title="View on GitHub"]');
if (ghLink) window.telemetry.trackEvent('GitHubOpen', { repoUrl: ghLink.href });
// VS Code link
const vsCodeLink = e.target.closest('a[role="menuitem"][href^="vscode://"]');
if (vsCodeLink) window.telemetry.trackEvent('VSCodeOpen', { repoUrl: vsCodeLink.href });
// Codespace link
const csLink = e.target.closest('a[role="menuitem"][href*="codespaces/new"]');
if (csLink) window.telemetry.trackEvent('CodespaceOpen', { repoUrl: csLink.href });
// Lab card title click
const titleLink = e.target.closest('.lab-card-title a');
if (titleLink) window.telemetry.trackEvent('LabCardClick', { labTitle: labTitle, category: card ? card.getAttribute('data-category') : '' });
}
});
// Modal close
modalClose.addEventListener('click', closeVideoModal);
videoModal.addEventListener('click', (e) => {
if (e.target === videoModal) closeVideoModal();
});
// Mobile sidebar
mobileFilterToggle.addEventListener('click', openSidebar);
if (sidebarClose) sidebarClose.addEventListener('click', closeSidebar);
sidebarOverlay.addEventListener('click', closeSidebar);
// Outside click
document.addEventListener('click', handleOutsideClick);
// Escape key closes modal / sidebar
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeVideoModal();
closeSidebar();
}
});
}
// ── Utilities ──────────────────────────────────────────────
function debounce(fn, ms) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), ms);
};
}
// ── Init ───────────────────────────────────────────────────
function init() {
initTheme();
bindEvents();
loadData();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();