-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.js
More file actions
989 lines (855 loc) · 33.8 KB
/
admin.js
File metadata and controls
989 lines (855 loc) · 33.8 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// Admin Dashboard Controller
class AdminDashboard {
constructor() {
// Password hash (SHA-256 of password)
//
// CURRENT PASSWORD: CircuitX_Admin_2024!Secure#Pass
//
// This is a complex password with:
// - Uppercase and lowercase letters
// - Numbers
// - Special characters (! and #)
// - 30 characters long
//
// To change the password:
// 1. Open generate-password-hash.html in browser
// 2. Enter your new password
// 3. Copy the generated hash
// 4. Replace the hash below
// 5. Update this comment with your new password
this.PASSWORD_HASH = 'be52d6a172623d00a798ff669b50fdc2c33db742a2fffea27fec8f82f55c936d';
this.kitsConfig = null;
this.blogsConfig = null;
this.currentEditingKit = null;
this.currentEditingBlog = null;
this.isAuthenticated = false;
// File handles for automatic saving
this.kitsFileHandle = null;
this.blogsFileHandle = null;
this.hasFileAccess = false;
this.initializeEventListeners();
this.checkAuth();
this.initializeFileAccess();
}
// Simple SHA-256 hash function
async hashPassword(password) {
const encoder = new TextEncoder();
const data = encoder.encode(password);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
async checkAuth() {
const storedAuth = sessionStorage.getItem('admin_authenticated');
const authTime = sessionStorage.getItem('admin_auth_time');
const now = Date.now();
// Check if authenticated within last 2 hours
if (storedAuth === 'true' && authTime && (now - parseInt(authTime)) < 7200000) {
this.isAuthenticated = true;
this.showDashboard();
this.loadConfigs();
} else {
this.showLogin();
}
}
async handleLogin() {
const passwordInput = document.getElementById('admin-password-input');
const errorMsg = document.getElementById('admin-login-error');
if (!passwordInput || !errorMsg) return;
const password = passwordInput.value;
if (!password) {
errorMsg.textContent = 'Please enter a password';
return;
}
const hash = await this.hashPassword(password);
if (hash === this.PASSWORD_HASH) {
this.isAuthenticated = true;
sessionStorage.setItem('admin_authenticated', 'true');
sessionStorage.setItem('admin_auth_time', Date.now().toString());
this.showDashboard();
this.loadConfigs();
passwordInput.value = '';
errorMsg.textContent = '';
} else {
errorMsg.textContent = 'Incorrect password';
passwordInput.value = '';
}
}
handleLogout() {
this.isAuthenticated = false;
sessionStorage.removeItem('admin_authenticated');
sessionStorage.removeItem('admin_auth_time');
this.showLogin();
}
showLogin() {
const loginScreen = document.getElementById('admin-login-screen');
const dashboard = document.getElementById('admin-dashboard-content');
if (loginScreen) loginScreen.style.display = 'flex';
if (dashboard) dashboard.style.display = 'none';
}
showDashboard() {
const loginScreen = document.getElementById('admin-login-screen');
const dashboard = document.getElementById('admin-dashboard-content');
if (loginScreen) loginScreen.style.display = 'none';
if (dashboard) dashboard.style.display = 'block';
}
initializeEventListeners() {
// Login
const loginBtn = document.getElementById('admin-login-btn');
const passwordInput = document.getElementById('admin-password-input');
if (loginBtn) {
loginBtn.addEventListener('click', () => this.handleLogin());
}
if (passwordInput) {
passwordInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') this.handleLogin();
});
}
// Logout
const logoutBtn = document.getElementById('admin-logout-btn');
if (logoutBtn) {
logoutBtn.addEventListener('click', () => this.handleLogout());
}
// Admin tabs (internal to admin dashboard)
document.querySelectorAll('.admin-tab-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const tab = e.target.getAttribute('data-admin-tab');
this.switchAdminTab(tab);
});
});
// Add buttons
const addKitBtn = document.getElementById('admin-add-kit-btn');
const addBlogBtn = document.getElementById('admin-add-blog-btn');
if (addKitBtn) {
addKitBtn.addEventListener('click', () => this.openKitModal());
}
if (addBlogBtn) {
addBlogBtn.addEventListener('click', () => this.openBlogModal());
}
// Save buttons
const saveBtn = document.getElementById('admin-save-btn');
const exportKitsBtn = document.getElementById('admin-export-kits-btn');
const exportBlogsBtn = document.getElementById('admin-export-blogs-btn');
if (saveBtn) {
saveBtn.addEventListener('click', () => this.saveAll());
}
if (exportKitsBtn) {
exportKitsBtn.addEventListener('click', () => this.exportKitsJSON());
}
if (exportBlogsBtn) {
exportBlogsBtn.addEventListener('click', () => this.exportBlogsJSON());
}
// Modal close
document.querySelectorAll('.modal-close').forEach(btn => {
btn.addEventListener('click', () => this.closeModals());
});
const modalOverlay = document.getElementById('admin-modal-overlay');
if (modalOverlay) {
modalOverlay.addEventListener('click', () => this.closeModals());
}
const kitCancelBtn = document.getElementById('kit-cancel-btn');
if (kitCancelBtn) {
kitCancelBtn.addEventListener('click', () => this.closeModals());
}
const blogCancelBtn = document.getElementById('blog-cancel-btn');
if (blogCancelBtn) {
blogCancelBtn.addEventListener('click', () => this.closeModals());
}
// Forms
document.getElementById('kit-form').addEventListener('submit', (e) => {
e.preventDefault();
this.saveKit();
});
document.getElementById('blog-form').addEventListener('submit', (e) => {
e.preventDefault();
this.saveBlog();
});
// Cover image upload
this.setupFileUpload('cover-upload', 'cover-file', 'cover-preview', 'cover-path');
this.setupFileUpload('blog-image-upload', 'blog-image-file', 'blog-image-preview', 'blog-image-path');
// Add category button (will be set up when modal opens)
// Categories are now dynamic and set up when kit modal opens
// Initialize default categories
this.defaultCategories = [
{ name: 'media', displayName: 'Media (Videos)', accept: 'video/*' },
{ name: 'Instructions', displayName: 'Instructions (PDFs)', accept: '.pdf' },
{ name: 'cad_models', displayName: 'CAD Models (3D Files)', accept: '.stl,.step,.stp,.obj' },
{ name: 'Source Code', displayName: 'Source Code', accept: '.py,.java,.cpp,.c,.js,.html,.css' },
{ name: 'tutorial_videos', displayName: 'Tutorial Videos', accept: 'video/*' },
{ name: 'downloads', displayName: 'Downloads (Any Files)', accept: '*/*' }
];
}
setupFileUpload(containerId, inputId, previewId, pathId) {
const container = document.getElementById(containerId);
const input = document.getElementById(inputId);
const preview = document.getElementById(previewId);
const pathInput = document.getElementById(pathId);
if (!container || !input || !preview || !pathInput) return;
container.addEventListener('click', () => input.click());
container.addEventListener('dragover', (e) => {
e.preventDefault();
container.classList.add('drag-over');
});
container.addEventListener('dragleave', () => {
container.classList.remove('drag-over');
});
container.addEventListener('drop', (e) => {
e.preventDefault();
container.classList.remove('drag-over');
const file = e.dataTransfer.files[0];
if (file && file.type.startsWith('image/')) {
this.handleImageUpload(file, preview, pathInput);
}
});
input.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
this.handleImageUpload(file, preview, pathInput);
}
});
}
setupCategoryUpload(area) {
const category = area.getAttribute('data-category');
const accept = area.getAttribute('data-accept');
const input = area.querySelector('input[type="file"]');
const fileList = document.querySelector(`.file-list[data-category="${category}"]`);
if (!input || !fileList) return;
area.addEventListener('click', () => input.click());
area.addEventListener('dragover', (e) => {
e.preventDefault();
area.classList.add('drag-over');
});
area.addEventListener('dragleave', () => {
area.classList.remove('drag-over');
});
area.addEventListener('drop', (e) => {
e.preventDefault();
area.classList.remove('drag-over');
const files = Array.from(e.dataTransfer.files);
files.forEach(file => this.addFileToCategory(file, category, fileList));
});
input.addEventListener('change', (e) => {
const files = Array.from(e.target.files);
files.forEach(file => this.addFileToCategory(file, category, fileList));
e.target.value = ''; // Reset input
});
}
handleImageUpload(file, preview, pathInput) {
const reader = new FileReader();
reader.onload = (e) => {
preview.innerHTML = `<img src="${e.target.result}" alt="Preview">`;
// Suggest path based on file name
if (!pathInput.value) {
pathInput.value = `assets/${file.name}`;
}
};
reader.readAsDataURL(file);
}
addFileToCategory(file, category, fileListContainer) {
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
fileItem.dataset.fileName = file.name;
const fileName = file.name;
const filePath = `assets/${this.currentEditingKit || 'new-kit'}/${fileName}`;
fileItem.innerHTML = `
<div class="file-item-info">
<span class="file-name">${fileName}</span>
<input type="text" class="file-path-input" value="${filePath}" placeholder="assets/kit-name/file.ext">
</div>
<button type="button" class="btn-remove" data-category="${category}">Remove</button>
`;
fileListContainer.appendChild(fileItem);
// Remove button
fileItem.querySelector('.btn-remove').addEventListener('click', () => {
fileItem.remove();
});
}
switchAdminTab(tabName) {
document.querySelectorAll('.admin-tab-btn').forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('data-admin-tab') === tabName);
});
document.querySelectorAll('.admin-tab-content').forEach(content => {
content.classList.toggle('active', content.id === `admin-${tabName}-tab`);
});
}
async initializeFileAccess() {
// Check if File System Access API is available
if ('showOpenFilePicker' in window && 'showSaveFilePicker' in window) {
this.hasFileAccess = true;
this.updateFileAccessStatus('File System Access API available. JSON files will auto-save when you add/edit kits or blogs.');
} else {
this.updateFileAccessStatus('File System Access API not available. JSON files will download automatically when you save.');
}
}
updateFileAccessStatus(message, type = 'info') {
const statusElement = document.getElementById('file-access-status');
const textElement = document.getElementById('file-access-text');
if (statusElement && textElement) {
textElement.textContent = `Auto-save: ${message}`;
// Update styling based on type
statusElement.classList.remove('success', 'error');
if (type === 'success') {
statusElement.classList.add('success');
} else if (type === 'error') {
statusElement.classList.add('error');
}
}
}
async requestFileAccess(filename) {
if (!this.hasFileAccess) return null;
try {
// Request file access - user needs to select the file once
this.updateFileAccessStatus(`Please select ${filename} to enable auto-save...`);
const [fileHandle] = await window.showOpenFilePicker({
suggestedName: filename,
types: [{
description: 'JSON files',
accept: { 'application/json': ['.json'] }
}],
multiple: false
});
this.updateFileAccessStatus(`${filename} access granted! Auto-save enabled.`, 'success');
return fileHandle;
} catch (error) {
if (error.name !== 'AbortError') {
console.error('Error requesting file access:', error);
this.updateFileAccessStatus(`Could not access ${filename}. Files will download instead.`);
} else {
this.updateFileAccessStatus(`File access cancelled. Files will download instead.`);
}
return null;
}
}
async writeToFile(fileHandle, content) {
if (!fileHandle) return false;
try {
const writable = await fileHandle.createWritable();
await writable.write(content);
await writable.close();
this.updateFileAccessStatus('JSON file updated successfully!', 'success');
return true;
} catch (error) {
console.error('Error writing to file:', error);
this.updateFileAccessStatus('Error writing file. Will download instead.');
return false;
}
}
async loadConfigs() {
try {
const kitsResponse = await fetch('kits-config.json');
this.kitsConfig = await kitsResponse.json();
const blogsResponse = await fetch('blogs-config.json');
this.blogsConfig = await blogsResponse.json();
this.renderKits();
this.renderBlogs();
} catch (error) {
console.error('Error loading configs:', error);
alert('Error loading configuration files. Make sure kits-config.json and blogs-config.json exist.');
}
}
renderKits() {
const container = document.getElementById('admin-kits-list');
if (!container) return;
container.innerHTML = '';
if (!this.kitsConfig || !this.kitsConfig.kits) {
container.innerHTML = '<p>No kits found. Click "Add New Kit" to create one.</p>';
return;
}
Object.entries(this.kitsConfig.kits).forEach(([kitId, kit]) => {
const card = this.createKitCard(kitId, kit);
container.appendChild(card);
});
}
createKitCard(kitId, kit) {
const card = document.createElement('div');
card.className = 'item-card';
card.innerHTML = `
<div class="item-preview">
<img src="${kit.cover}" alt="${kit.title}" onerror="this.style.display='none'">
</div>
<div class="item-info">
<h3>${kit.title}</h3>
<p>${kit.description}</p>
<div class="item-meta">
<span>ID: ${kitId}</span>
<span>Team: ${kit.team}</span>
</div>
</div>
<div class="item-actions">
<button class="btn-edit" data-kit-id="${kitId}">Edit</button>
<button class="btn-delete" data-kit-id="${kitId}">Delete</button>
</div>
`;
card.querySelector('.btn-edit').addEventListener('click', () => this.openKitModal(kitId));
card.querySelector('.btn-delete').addEventListener('click', () => this.deleteKit(kitId));
return card;
}
renderBlogs() {
const container = document.getElementById('admin-blogs-list');
if (!container) return;
container.innerHTML = '';
if (!this.blogsConfig || !this.blogsConfig.blogs) {
container.innerHTML = '<p>No blog posts found. Click "Add New Blog" to create one.</p>';
return;
}
Object.entries(this.blogsConfig.blogs).forEach(([blogId, blog]) => {
const card = this.createBlogCard(blogId, blog);
container.appendChild(card);
});
}
createBlogCard(blogId, blog) {
const card = document.createElement('div');
card.className = 'item-card';
card.innerHTML = `
<div class="item-preview">
<img src="${blog.image}" alt="${blog.title}" onerror="this.style.display='none'">
</div>
<div class="item-info">
<h3>${blog.title}</h3>
<p>${blog.text.substring(0, 100)}...</p>
<div class="item-meta">
<span>ID: ${blogId}</span>
</div>
</div>
<div class="item-actions">
<button class="btn-edit" data-blog-id="${blogId}">Edit</button>
<button class="btn-delete" data-blog-id="${blogId}">Delete</button>
</div>
`;
card.querySelector('.btn-edit').addEventListener('click', () => this.openBlogModal(blogId));
card.querySelector('.btn-delete').addEventListener('click', () => this.deleteBlog(blogId));
return card;
}
openKitModal(kitId = null) {
this.currentEditingKit = kitId;
const modal = document.getElementById('kit-modal');
const overlay = document.getElementById('admin-modal-overlay');
if (!modal || !overlay) return;
if (kitId && this.kitsConfig.kits[kitId]) {
// Edit mode
const kit = this.kitsConfig.kits[kitId];
document.getElementById('kit-modal-title').textContent = 'Edit Kit';
document.getElementById('kit-id').value = kitId;
document.getElementById('kit-id').disabled = true;
document.getElementById('kit-title').value = kit.title;
document.getElementById('kit-description').value = kit.description;
document.getElementById('kit-detailed-description').value = kit.detailedDescription || '';
document.getElementById('kit-team').value = kit.team;
document.getElementById('cover-path').value = kit.cover;
document.getElementById('cover-preview').innerHTML = kit.cover ? `<img src="${kit.cover}" alt="Cover">` : '';
// Load categories from kit files
this.renderCategories(kit.files || {});
} else {
// New kit mode
document.getElementById('kit-modal-title').textContent = 'Add New Kit';
document.getElementById('kit-form').reset();
document.getElementById('kit-id').disabled = false;
document.getElementById('cover-preview').innerHTML = '';
// Load default categories
this.renderCategories({});
}
modal.classList.add('active');
overlay.classList.add('active');
}
renderCategories(files) {
const container = document.getElementById('file-categories-container');
if (!container) return;
container.innerHTML = '';
// Setup add category button
const addCategoryBtn = document.getElementById('add-category-btn');
if (addCategoryBtn) {
// Remove old listeners and add new one
const newBtn = addCategoryBtn.cloneNode(true);
addCategoryBtn.parentNode.replaceChild(newBtn, addCategoryBtn);
newBtn.addEventListener('click', () => this.addNewCategory());
}
// Get all categories from files object
const categories = Object.keys(files);
// If no categories exist, use defaults
if (categories.length === 0) {
this.defaultCategories.forEach(cat => {
this.addCategoryElement(cat.name, cat.displayName, cat.accept, []);
});
} else {
// Render existing categories
categories.forEach(categoryName => {
const filesInCategory = files[categoryName] || [];
// Try to find display name from defaults, or use category name
const defaultCat = this.defaultCategories.find(c => c.name === categoryName);
const displayName = defaultCat ? defaultCat.displayName : categoryName;
const accept = defaultCat ? defaultCat.accept : '*/*';
this.addCategoryElement(categoryName, displayName, accept, filesInCategory);
});
}
}
addCategoryElement(categoryName, displayName, accept, files) {
const container = document.getElementById('file-categories-container');
if (!container) return;
const categoryDiv = document.createElement('div');
categoryDiv.className = 'file-category';
categoryDiv.dataset.categoryName = categoryName;
const categoryId = `category-${categoryName.replace(/\s+/g, '-').toLowerCase()}`;
categoryDiv.innerHTML = `
<div class="category-header-inline">
<div class="category-name-input-wrapper">
<input type="text" class="category-name-input" value="${this.escapeHtml(displayName)}"
data-category-key="${categoryName}" placeholder="Category Name">
<small class="category-key-display">Key: ${this.escapeHtml(categoryName)}</small>
</div>
<div class="category-actions">
<button type="button" class="btn-rename-category" title="Rename Category Key">Rename Key</button>
<button type="button" class="btn-delete-category" title="Delete Category">Delete</button>
</div>
</div>
<div class="file-upload-area" data-category="${categoryName}" data-accept="${accept}">
<p>Drop files here or click to browse</p>
<input type="file" multiple accept="${accept}" style="display: none;">
</div>
<div class="file-list" data-category="${categoryName}"></div>
`;
container.appendChild(categoryDiv);
// Setup upload area
const uploadArea = categoryDiv.querySelector('.file-upload-area');
this.setupCategoryUpload(uploadArea);
// Load existing files
const fileList = categoryDiv.querySelector('.file-list');
files.forEach(file => {
this.addExistingFileToCategory(file, categoryName, fileList);
});
// Setup category name change
const nameInput = categoryDiv.querySelector('.category-name-input');
nameInput.addEventListener('change', (e) => {
const newDisplayName = e.target.value;
// Update display name (not the key)
e.target.value = newDisplayName;
});
// Setup rename key button
const renameBtn = categoryDiv.querySelector('.btn-rename-category');
renameBtn.addEventListener('click', () => {
this.renameCategoryKey(categoryName, categoryDiv);
});
// Setup delete button
const deleteBtn = categoryDiv.querySelector('.btn-delete-category');
deleteBtn.addEventListener('click', () => {
if (confirm(`Delete category "${displayName}"? All files in this category will be removed.`)) {
categoryDiv.remove();
}
});
}
renameCategoryKey(oldKey, categoryDiv) {
const newKey = prompt(`Enter new category key (URL-friendly, no spaces):`, oldKey);
if (!newKey || newKey === oldKey) return;
// Validate key
if (!/^[a-zA-Z0-9_-]+$/.test(newKey)) {
alert('Category key must contain only letters, numbers, underscores, and hyphens.');
return;
}
// Check if key already exists
const existingCategory = document.querySelector(`.file-category[data-category-name="${newKey}"]`);
if (existingCategory && existingCategory !== categoryDiv) {
alert('A category with this key already exists.');
return;
}
// Update all references
categoryDiv.dataset.categoryName = newKey;
categoryDiv.querySelector('.file-upload-area').setAttribute('data-category', newKey);
categoryDiv.querySelector('.file-list').setAttribute('data-category', newKey);
categoryDiv.querySelector('.category-key-display').textContent = `Key: ${newKey}`;
// Update file items
categoryDiv.querySelectorAll('.file-item').forEach(item => {
const removeBtn = item.querySelector('.btn-remove');
if (removeBtn) {
removeBtn.setAttribute('data-category', newKey);
}
});
}
addNewCategory() {
const categoryName = prompt('Enter category key (URL-friendly, no spaces, e.g., "documents"):');
if (!categoryName) return;
// Validate key
if (!/^[a-zA-Z0-9_-]+$/.test(categoryName)) {
alert('Category key must contain only letters, numbers, underscores, and hyphens.');
return;
}
// Check if key already exists
const existingCategory = document.querySelector(`.file-category[data-category-name="${categoryName}"]`);
if (existingCategory) {
alert('A category with this key already exists.');
return;
}
const displayName = prompt('Enter display name (e.g., "Documents"):', categoryName);
if (!displayName) return;
const accept = prompt('Enter file accept types (e.g., ".pdf,.doc" or "*/*" for all):', '*/*') || '*/*';
this.addCategoryElement(categoryName, displayName, accept, []);
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
addExistingFileToCategory(file, category, fileListContainer) {
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
fileItem.dataset.fileName = file.name;
fileItem.innerHTML = `
<div class="file-item-info">
<span class="file-name">${file.name}</span>
<input type="text" class="file-path-input" value="${file.file}" placeholder="assets/kit-name/file.ext">
</div>
<button type="button" class="btn-remove" data-category="${category}">Remove</button>
`;
fileListContainer.appendChild(fileItem);
fileItem.querySelector('.btn-remove').addEventListener('click', () => {
fileItem.remove();
});
}
openBlogModal(blogId = null) {
this.currentEditingBlog = blogId;
const modal = document.getElementById('blog-modal');
const overlay = document.getElementById('admin-modal-overlay');
if (!modal || !overlay) return;
if (blogId && this.blogsConfig.blogs[blogId]) {
// Edit mode
const blog = this.blogsConfig.blogs[blogId];
document.getElementById('blog-modal-title').textContent = 'Edit Blog Post';
document.getElementById('blog-id').value = blogId;
document.getElementById('blog-id').disabled = true;
document.getElementById('blog-title').value = blog.title;
document.getElementById('blog-text').value = blog.text;
document.getElementById('blog-image-path').value = blog.image;
document.getElementById('blog-image-preview').innerHTML = blog.image ? `<img src="${blog.image}" alt="Blog Image">` : '';
} else {
// New blog mode
document.getElementById('blog-modal-title').textContent = 'Add New Blog Post';
document.getElementById('blog-form').reset();
document.getElementById('blog-id').disabled = false;
document.getElementById('blog-image-preview').innerHTML = '';
}
modal.classList.add('active');
overlay.classList.add('active');
}
closeModals() {
const kitModal = document.getElementById('kit-modal');
const blogModal = document.getElementById('blog-modal');
const modalOverlay = document.getElementById('admin-modal-overlay');
if (kitModal) kitModal.classList.remove('active');
if (blogModal) blogModal.classList.remove('active');
if (modalOverlay) modalOverlay.classList.remove('active');
this.currentEditingKit = null;
this.currentEditingBlog = null;
}
async saveKit() {
const kitId = document.getElementById('kit-id').value;
const title = document.getElementById('kit-title').value;
const description = document.getElementById('kit-description').value;
const detailedDescription = document.getElementById('kit-detailed-description').value;
const team = document.getElementById('kit-team').value;
const cover = document.getElementById('cover-path').value;
if (!this.kitsConfig.kits) {
this.kitsConfig.kits = {};
}
// Collect files from all categories (dynamic)
const files = {};
document.querySelectorAll('.file-category').forEach(categoryDiv => {
const categoryKey = categoryDiv.getAttribute('data-category-name');
const fileList = categoryDiv.querySelector('.file-list');
if (!fileList) return;
const fileItems = fileList.querySelectorAll('.file-item');
files[categoryKey] = [];
fileItems.forEach(item => {
const name = item.querySelector('.file-name').textContent;
const path = item.querySelector('.file-path-input').value;
if (name && path) {
files[categoryKey].push({
name: name,
file: path
});
}
});
});
this.kitsConfig.kits[kitId] = {
title,
description,
detailedDescription,
team,
cover,
files
};
// Automatically save to JSON file
await this.autoSaveKits();
this.renderKits();
this.closeModals();
this.showNotification('Kit saved and JSON updated!');
}
async saveBlog() {
const blogId = document.getElementById('blog-id').value;
const title = document.getElementById('blog-title').value;
const text = document.getElementById('blog-text').value;
const image = document.getElementById('blog-image-path').value;
if (!this.blogsConfig.blogs) {
this.blogsConfig.blogs = {};
}
this.blogsConfig.blogs[blogId] = {
title,
text,
image
};
// Automatically save to JSON file
await this.autoSaveBlogs();
this.renderBlogs();
this.closeModals();
this.showNotification('Blog post saved and JSON updated!');
}
async deleteKit(kitId) {
if (confirm(`Are you sure you want to delete the kit "${this.kitsConfig.kits[kitId].title}"?`)) {
delete this.kitsConfig.kits[kitId];
// Automatically save to JSON file
await this.autoSaveKits();
this.renderKits();
this.showNotification('Kit deleted and JSON updated!');
}
}
async deleteBlog(blogId) {
if (confirm(`Are you sure you want to delete the blog post "${this.blogsConfig.blogs[blogId].title}"?`)) {
delete this.blogsConfig.blogs[blogId];
// Automatically save to JSON file
await this.autoSaveBlogs();
this.renderBlogs();
this.showNotification('Blog post deleted and JSON updated!');
}
}
async saveAll() {
try {
await this.autoSaveKits();
await this.autoSaveBlogs();
this.showNotification('All changes saved to JSON files!');
} catch (error) {
console.error('Error saving:', error);
this.showNotification('Error saving. Using fallback download method.');
// Fallback to download
await this.saveKitsToFile();
await this.saveBlogsToFile();
}
}
async autoSaveKits() {
const json = JSON.stringify(this.kitsConfig, null, 2);
// Try to use File System Access API
if (this.hasFileAccess) {
// Request file access if we don't have it
if (!this.kitsFileHandle) {
this.kitsFileHandle = await this.requestFileAccess('kits-config.json');
}
if (this.kitsFileHandle) {
const success = await this.writeToFile(this.kitsFileHandle, json);
if (success) {
return; // Successfully saved
}
}
}
// Fallback: download file
this.downloadFile('kits-config.json', json);
}
async autoSaveBlogs() {
const json = JSON.stringify(this.blogsConfig, null, 2);
// Try to use File System Access API
if (this.hasFileAccess) {
// Request file access if we don't have it
if (!this.blogsFileHandle) {
this.blogsFileHandle = await this.requestFileAccess('blogs-config.json');
}
if (this.blogsFileHandle) {
const success = await this.writeToFile(this.blogsFileHandle, json);
if (success) {
return; // Successfully saved
}
}
}
// Fallback: download file
this.downloadFile('blogs-config.json', json);
}
async saveKitsToFile() {
const json = JSON.stringify(this.kitsConfig, null, 2);
this.downloadFile('kits-config.json', json);
}
async saveBlogsToFile() {
const json = JSON.stringify(this.blogsConfig, null, 2);
this.downloadFile('blogs-config.json', json);
}
exportKitsJSON() {
const json = JSON.stringify(this.kitsConfig, null, 2);
this.downloadFile('kits-config.json', json);
}
exportBlogsJSON() {
const json = JSON.stringify(this.blogsConfig, null, 2);
this.downloadFile('blogs-config.json', json);
}
downloadFile(filename, content) {
const blob = new Blob([content], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
showNotification(message) {
// Simple notification - can be enhanced
const notification = document.createElement('div');
notification.className = 'notification';
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.classList.add('show');
}, 10);
setTimeout(() => {
notification.classList.remove('show');
setTimeout(() => notification.remove(), 300);
}, 3000);
}
}
// Initialize dashboard when DOM is ready and admin tab is accessed
let adminDashboardInstance = null;
function initializeAdminDashboard() {
if (!adminDashboardInstance) {
adminDashboardInstance = new AdminDashboard();
}
return adminDashboardInstance;
}
// Initialize when admin tab is clicked or becomes active
document.addEventListener('DOMContentLoaded', () => {
// Watch for tab changes
const observer = new MutationObserver(() => {
const adminTab = document.getElementById('admin');
if (adminTab && adminTab.classList.contains('active')) {
if (!adminDashboardInstance) {
initializeAdminDashboard();
}
}
});
// Observe the main container for tab changes
const mainContainer = document.querySelector('main .container');
if (mainContainer) {
observer.observe(mainContainer, {
attributes: true,
attributeFilter: ['class'],
subtree: true,
childList: false
});
}
// Also check immediately if admin tab is already active
const adminTab = document.getElementById('admin');
if (adminTab && adminTab.classList.contains('active')) {
initializeAdminDashboard();
}
// Also listen for direct tab button clicks
const adminTabBtn = document.querySelector('.tab-btn[data-tab="admin"]');
if (adminTabBtn) {
adminTabBtn.addEventListener('click', () => {
setTimeout(() => {
initializeAdminDashboard();
}, 100);
});
}
});