-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutpostDrops.html
More file actions
893 lines (782 loc) · 33.5 KB
/
outpostDrops.html
File metadata and controls
893 lines (782 loc) · 33.5 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>书签管理应用</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
:root {
--sidebar-width: 60px;
--sidebar-expanded-width: 250px;
--treeview-width: 250px;
--header-height: 60px;
}
body {
overflow: hidden;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
#header {
height: var(--header-height);
background-color: #4a6bdf;
color: white;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
#sidebar {
width: var(--sidebar-width);
background-color: #2c3e50;
position: fixed;
top: var(--header-height);
bottom: 0;
left: 0;
z-index: 900;
transition: width 0.3s;
overflow-x: hidden;
}
#sidebar:hover {
width: var(--sidebar-expanded-width);
}
#treeview {
width: var(--treeview-width);
background-color: #f8f9fa;
position: fixed;
top: var(--header-height);
bottom: 0;
left: var(--sidebar-width);
border-right: 1px solid #dee2e6;
overflow-y: auto;
transition: left 0.3s;
z-index: 800;
}
#content {
position: fixed;
top: var(--header-height);
bottom: 0;
left: calc(var(--sidebar-width) + var(--treeview-width));
right: 0;
padding: 20px;
overflow-y: auto;
transition: left 0.3s;
}
.sidebar-icon {
width: 100%;
text-align: center;
padding: 15px 0;
color: #ecf0f1;
cursor: pointer;
transition: background-color 0.2s;
}
.sidebar-icon:hover {
background-color: #34495e;
}
.sidebar-icon i {
font-size: 1.5rem;
margin-bottom: 5px;
}
.sidebar-icon span {
display: none;
font-size: 0.85rem;
}
#sidebar:hover .sidebar-icon span {
display: block;
}
.treeview-item {
padding: 8px 15px;
cursor: pointer;
display: flex;
align-items: center;
}
.treeview-item:hover {
background-color: #e9ecef;
}
.treeview-item i {
margin-right: 8px;
width: 16px;
text-align: center;
}
.treeview-children {
padding-left: 20px;
display: none;
}
.treeview-item.active {
background-color: #e3f2fd;
font-weight: bold;
}
.treeview-item.active > i {
color: #4a6bdf;
}
.gridjs-table {
width: 100%;
}
.action-buttons button {
margin-right: 5px;
}
.modal-content {
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.toolbox-item {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 15px;
}
.toolbox-icon {
width: 40px;
height: 40px;
background-color: #3498db;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 1.2rem;
margin-bottom: 5px;
cursor: pointer;
}
.toolbox-label {
font-size: 0.7rem;
color: #ecf0f1;
text-align: center;
display: none;
}
#sidebar:hover .toolbox-label {
display: block;
}
</style>
</head>
<body>
<!-- 顶部导航栏 -->
<header id="header" class="d-flex align-items-center px-3">
<h3 class="me-4 mb-0">书签管理</h3>
<div class="d-flex">
<button class="btn btn-sm btn-light me-2" id="addFolderBtn">
<i class="fas fa-folder-plus me-1"></i> 添加文件夹
</button>
<button class="btn btn-sm btn-light me-2" id="addBookmarkBtn">
<i class="fas fa-bookmark me-1"></i> 添加书签
</button>
</div>
</header>
<!-- 左侧垂直工具条 -->
<aside id="sidebar">
<div class="toolbox-container mt-3">
<!-- Daily Toolbox 图标将在这里动态生成 -->
</div>
</aside>
<!-- 目录树 -->
<nav id="treeview">
<div class="p-3">
<h5>书签文件夹</h5>
<hr>
<div class="treeview-container">
<!-- 目录树将在这里动态生成 -->
</div>
</div>
</nav>
<!-- 内容区域 -->
<main id="content">
<div class="container-fluid">
<div class="row mb-4">
<div class="col">
<h4 id="currentFolder">所有书签</h4>
</div>
<div class="col-auto">
<div class="input-group input-group-sm">
<input type="text" class="form-control" placeholder="搜索书签..." id="searchInput">
<button class="btn btn-outline-secondary" type="button">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div id="grid-container"></div>
</div>
</div>
</div>
</main>
<!-- 添加/编辑文件夹模态框 -->
<div class="modal fade" id="folderModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="folderModalTitle">添加文件夹</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="folderForm">
<input type="hidden" id="folderId">
<div class="mb-3">
<label for="folderTitle" class="form-label">文件夹名称</label>
<input type="text" class="form-control" id="folderTitle" required>
</div>
<div class="mb-3">
<label for="folderType" class="form-label">文件夹类型</label>
<select class="form-select" id="folderType">
<option value="Gateway.Folder">普通文件夹</option>
<option value="Daily.Toolbox">Daily Toolbox</option>
</select>
</div>
<div class="mb-3">
<label for="parentFolder" class="form-label">父文件夹</label>
<select class="form-select" id="parentFolder">
<option value="">根目录</option>
<!-- 文件夹选项将动态生成 -->
</select>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary" id="saveFolderBtn">保存</button>
</div>
</div>
</div>
</div>
<!-- 添加/编辑书签模态框 -->
<div class="modal fade" id="bookmarkModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="bookmarkModalTitle">添加书签</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="bookmarkForm">
<input type="hidden" id="bookmarkId">
<div class="mb-3">
<label for="bookmarkTitle" class="form-label">书签标题</label>
<input type="text" class="form-control" id="bookmarkTitle" required>
</div>
<div class="mb-3">
<label for="bookmarkUrl" class="form-label">URL</label>
<input type="url" class="form-control" id="bookmarkUrl" required>
</div>
<div class="mb-3">
<label for="bookmarkParent" class="form-label">父文件夹</label>
<select class="form-select" id="bookmarkParent">
<!-- 文件夹选项将动态生成 -->
</select>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary" id="saveBookmarkBtn">保存</button>
</div>
</div>
</div>
</div>
<!-- 引入必要的JavaScript库 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.22.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.22.0/firebase-database-compat.js"></script>
<script>
// Firebase配置 - 需要替换为您的实际配置
const firebaseConfig = {
apiKey: "AIzaSyA6MZ_p5lVuy8TMAqiuV6IRx9fggV44lQs",
authDomain: "outpost-8d74e.firebaseapp.com",
databaseURL: "https://outpost-8d74e.asia-southeast1.firebasedatabase.app/",
projectId: "outpost-8d74e",
storageBucket: "outpost-8d74e.firebasestorage.app",
messagingSenderId: "724993324937",
appId: "1:724993324937:web:ce6c7e6b06489331c79358",
measurementId: "G-QPHWRTH6BH"
};
// 初始化Firebase
firebase.initializeApp(firebaseConfig);
const database = firebase.database();
// 示例数据(如果Firebase中没有数据,将使用此示例数据)
const sampleData = [] ;
/*
{
"id": "7d97c392-3f76-4df5-8c46-797f587ec995",
"title": "Daily Application",
"type": "Daily.Toolbox",
"children": [
{
"id": "6abb0ff1-74e2-4a5e-9240-56e6c4cebf6F",
"title": "Gemini",
"type": "Gateway.Bookmark",
"url": "https://www.Gemini.com"
}
]
},
{
"id": "a66863ef-c945-4e35-a381-bf286bb9d8f5",
"title": "字典工具",
"type": "Gateway.Folder",
"children": []
},
{
"id": "41fdb0bd-24a0-4fe9-bbd9-5c925d90418d",
"title": "Learning Apps",
"type": "Gateway.Folder",
"children": [
{
"id": "ef0342b9-bc6f-4870-9dec-c6273a5e7c62",
"title": "英语学习",
"type": "Gateway.Folder"
},
{
"id": "6abb0ff1-74e2-4a5e-9240-56e6c4cebf6F",
"title": "sina",
"type": "Gateway.Bookmark",
"url": "https://www.sina.com"
}
]
},
{
"id": "6abb0ff1-74e2-4a5e-9240-56e6c4cebf6C",
"title": "Google",
"type": "Gateway.Bookmark",
"url": "https://www.google.com"
}
];*/
// 全局变量
let bookmarksData = [];
let currentFolderId = null;
let grid = null;
// DOM加载完成后初始化应用
document.addEventListener('DOMContentLoaded', function() {
initializeApp();
});
// 初始化应用
function initializeApp() {
// 从Firebase加载数据
loadData();
// 设置事件监听器
document.getElementById('addFolderBtn').addEventListener('click', showAddFolderModal);
document.getElementById('addBookmarkBtn').addEventListener('click', showAddBookmarkModal);
document.getElementById('saveFolderBtn').addEventListener('click', saveFolder);
document.getElementById('saveBookmarkBtn').addEventListener('click', saveBookmark);
document.getElementById('searchInput').addEventListener('input', handleSearch);
}
// 从Firebase加载数据
function loadData() {
const bookmarksRef = database.ref('Portal/Drops');
bookmarksRef.once('value', (snapshot) => {
if (snapshot.exists()) {
bookmarksData = snapshot.val();
console.log(bookmarksData) ;
} else {
// 如果没有数据,使用示例数据并保存到Firebase
bookmarksData = sampleData;
bookmarksRef.set(bookmarksData);
}
// 渲染UI组件
renderToolboxIcons();
renderTreeView();
renderGrid();
});
}
// 渲染工具箱图标
function renderToolboxIcons() {
const toolboxContainer = document.querySelector('.toolbox-container');
toolboxContainer.innerHTML = '';
bookmarksData.forEach(item => {
if (item.type === 'Daily.Toolbox') {
const toolboxItem = document.createElement('div');
toolboxItem.className = 'toolbox-item';
const toolboxIcon = document.createElement('div');
toolboxIcon.className = 'toolbox-icon';
toolboxIcon.innerHTML = '<i class="fas fa-tools"></i>';
toolboxIcon.addEventListener('click', () => {
currentFolderId = item.id;
document.getElementById('currentFolder').textContent = item.title;
renderGrid();
});
const toolboxLabel = document.createElement('div');
toolboxLabel.className = 'toolbox-label';
toolboxLabel.textContent = item.title;
toolboxItem.appendChild(toolboxIcon);
toolboxItem.appendChild(toolboxLabel);
toolboxContainer.appendChild(toolboxItem);
}
});
}
// 渲染目录树
function renderTreeView() {
const treeviewContainer = document.querySelector('.treeview-container');
treeviewContainer.innerHTML = '';
// 添加"所有书签"根节点
const allBookmarksItem = document.createElement('div');
allBookmarksItem.className = 'treeview-item';
allBookmarksItem.innerHTML = '<i class="fas fa-bookmark"></i> 所有书签';
allBookmarksItem.addEventListener('click', () => {
currentFolderId = null;
document.getElementById('currentFolder').textContent = '所有书签';
renderGrid();
});
treeviewContainer.appendChild(allBookmarksItem);
// 递归渲染文件夹
function renderFolder(folder, level = 0) {
if (folder.type !== 'Daily.Toolbox' && folder.type.includes('Folder')) {
const folderItem = document.createElement('div');
folderItem.className = 'treeview-item';
folderItem.style.paddingLeft = (15 + level * 15) + 'px';
const icon = document.createElement('i');
icon.className = folder.children && folder.children.length ? 'fas fa-folder' : 'fas fa-folder-open';
const title = document.createElement('span');
title.textContent = folder.title;
folderItem.appendChild(icon);
folderItem.appendChild(title);
folderItem.addEventListener('click', (e) => {
e.stopPropagation();
currentFolderId = folder.id;
document.getElementById('currentFolder').textContent = folder.title;
renderGrid();
});
treeviewContainer.appendChild(folderItem);
// 如果有子文件夹,递归渲染
if (folder.children && folder.children.length) {
const childrenContainer = document.createElement('div');
childrenContainer.className = 'treeview-children';
folder.children.forEach(child => {
renderFolder(child, level + 1);
});
// 添加点击展开/折叠功能
folderItem.addEventListener('click', (e) => {
e.stopPropagation();
childrenContainer.style.display =
childrenContainer.style.display === 'none' ||
childrenContainer.style.display === '' ? 'block' : 'none';
icon.className = childrenContainer.style.display === 'block' ?
'fas fa-folder-open' : 'fas fa-folder';
});
treeviewContainer.appendChild(childrenContainer);
}
}
}
// 渲染所有文件夹
bookmarksData.forEach(item => renderFolder(item));
}
// 渲染数据表格
function renderGrid() {
const gridContainer = document.getElementById('grid-container');
gridContainer.innerHTML = '';
// 获取当前文件夹的书签
let bookmarksToShow = [];
if (currentFolderId === null) {
// 显示所有书签
function extractBookmarks(items) {
items.forEach(item => {
if (item.type === 'Gateway.Bookmark') {
bookmarksToShow.push(item);
} else if (item.children && item.children.length) {
extractBookmarks(item.children);
}
});
}
extractBookmarks(bookmarksData);
} else {
// 查找当前文件夹
function findFolder(items, id) {
for (const item of items) {
if (item.id === id) {
return item;
}
if (item.children && item.children.length) {
const found = findFolder(item.children, id);
if (found) return found;
}
}
return null;
}
const currentFolder = findFolder(bookmarksData, currentFolderId);
if (currentFolder && currentFolder.children) {
bookmarksToShow = currentFolder.children.filter(child => child.type === 'Gateway.Bookmark');
}
}
// 创建Grid.js表格
grid = new gridjs.Grid({
columns: [
{
name: '标题',
width: '40%',
attributes: (cell) => {
return {
onclick: () => window.open(crow ? crow.url : '#', '_blank')
};
}
},
{
name: 'URL',
width: '40%',
formatter: (cell) => gridjs.html(`<a href="${cell}" target="_blank">${cell}</a>`)
},
{
name: '操作',
width: '20%',
formatter: (cell, row) => {
const rowData = row.cells[0].data;
return gridjs.html(`
<div class="action-buttons">
<button class="btn btn-sm btn-primary edit-btn" data-id="${rowData.id}">
<i class="fas fa-edit"></i>
</button>
<button class="btn btn-sm btn-danger delete-btn" data-id="${rowData.id}">
<i class="fas fa-trash"></i>
</button>
</div>
`);
}
}
],
data: bookmarksToShow.map(bookmark => [bookmark, bookmark.url]),
search: true,
pagination: {
limit: 10
},
sort: true
}).render(gridContainer);
// 添加事件监听器到操作按钮
setTimeout(() => {
document.querySelectorAll('.edit-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const id = btn.getAttribute('data-id');
showEditBookmarkModal(id);
});
});
document.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const id = btn.getAttribute('data-id');
deleteBookmark(id);
});
});
}, 500);
}
// 显示添加文件夹模态框
function showAddFolderModal() {
const modal = new bootstrap.Modal(document.getElementById('folderModal'));
document.getElementById('folderModalTitle').textContent = '添加文件夹';
document.getElementById('folderForm').reset();
document.getElementById('folderId').value = '';
// 填充父文件夹下拉框
populateFolderDropdown('parentFolder');
modal.show();
}
// 显示添加书签模态框
function showAddBookmarkModal() {
const modal = new bootstrap.Modal(document.getElementById('bookmarkModal'));
document.getElementById('bookmarkModalTitle').textContent = '添加书签';
document.getElementById('bookmarkForm').reset();
document.getElementById('bookmarkId').value = '';
// 填充父文件夹下拉框
populateFolderDropdown('bookmarkParent');
modal.show();
}
// 显示编辑书签模态框
function showEditBookmarkModal(id) {
// 查找书签
function findBookmark(items, id) {
for (const item of items) {
if (item.id === id) {
return item;
}
if (item.children && item.children.length) {
const found = findBookmark(item.children, id);
if (found) return found;
}
}
return null;
}
const bookmark = findBookmark(bookmarksData, id);
if (!bookmark) return;
const modal = new bootstrap.Modal(document.getElementById('bookmarkModal'));
document.getElementById('bookmarkModalTitle').textContent = '编辑书签';
document.getElementById('bookmarkId').value = bookmark.id;
document.getElementById('bookmarkTitle').value = bookmark.title;
document.getElementById('bookmarkUrl').value = bookmark.url;
// 填充父文件夹下拉框
populateFolderDropdown('bookmarkParent');
// 设置当前父文件夹
// 注意:在实际应用中,需要找到书签的父文件夹ID
// 这里简化处理,设置为当前文件夹
document.getElementById('bookmarkParent').value = currentFolderId || '';
modal.show();
}
// 填充文件夹下拉框
function populateFolderDropdown(dropdownId) {
const dropdown = document.getElementById(dropdownId);
dropdown.innerHTML = '<option value="">根目录</option>';
function addFolderOptions(items, level = 0) {
items.forEach(item => {
if (item.type.includes('Folder')) {
const option = document.createElement('option');
option.value = item.id;
option.textContent = ' '.repeat(level * 2) + item.title;
dropdown.appendChild(option);
if (item.children && item.children.length) {
addFolderOptions(item.children, level + 1);
}
}
});
}
addFolderOptions(bookmarksData);
}
// 保存文件夹
function saveFolder() {
const id = document.getElementById('folderId').value;
const title = document.getElementById('folderTitle').value;
const type = document.getElementById('folderType').value;
const parentId = document.getElementById('parentFolder').value;
if (!title) {
alert('请输入文件夹名称');
return;
}
const newFolder = {
id: id || generateId(),
title,
type,
children: []
};
if (id) {
// 编辑现有文件夹
// 在实际应用中,需要找到并更新文件夹
} else {
// 添加新文件夹
if (parentId) {
// 添加到父文件夹
function addToParent(items, parentId) {
for (const item of items) {
if (item.id === parentId) {
if (!item.children) item.children = [];
item.children.push(newFolder);
return true;
}
if (item.children && item.children.length) {
if (addToParent(item.children, parentId)) return true;
}
}
return false;
}
addToParent(bookmarksData, parentId);
} else {
// 添加到根目录
bookmarksData.push(newFolder);
}
}
// 保存到Firebase
database.ref('bookmarks').set(bookmarksData)
.then(() => {
bootstrap.Modal.getInstance(document.getElementById('folderModal')).hide();
renderTreeView();
renderToolboxIcons();
})
.catch(error => {
alert('保存失败: ' + error.message);
});
}
// 保存书签
function saveBookmark() {
const id = document.getElementById('bookmarkId').value;
const title = document.getElementById('bookmarkTitle').value;
const url = document.getElementById('bookmarkUrl').value;
const parentId = document.getElementById('bookmarkParent').value || currentFolderId;
if (!title || !url) {
alert('请填写所有必填字段');
return;
}
const newBookmark = {
id: id || generateId(),
title,
type: 'Gateway.Bookmark',
url
};
if (id) {
// 编辑现有书签
// 在实际应用中,需要找到并更新书签
} else {
// 添加新书签
if (!parentId) {
alert('请选择父文件夹');
return;
}
// 添加到父文件夹
function addToParent(items, parentId) {
for (const item of items) {
if (item.id === parentId) {
if (!item.children) item.children = [];
item.children.push(newBookmark);
return true;
}
if (item.children && item.children.length) {
if (addToParent(item.children, parentId)) return true;
}
}
return false;
}
if (!addToParent(bookmarksData, parentId)) {
alert('未找到父文件夹');
return;
}
}
// 保存到Firebase
database.ref('bookmarks').set(bookmarksData)
.then(() => {
bootstrap.Modal.getInstance(document.getElementById('bookmarkModal')).hide();
renderGrid();
renderTreeView();
})
.catch(error => {
alert('保存失败: ' + error.message);
});
}
// 删除书签
function deleteBookmark(id) {
if (!confirm('确定要删除这个书签吗?')) return;
// 从数据中删除书签
function removeBookmark(items, id) {
for (let i = 0; i < items.length; i++) {
if (items[i].id === id) {
items.splice(i, 1);
return true;
}
if (items[i].children && items[i].children.length) {
if (removeBookmark(items[i].children, id)) return true;
}
}
return false;
}
if (removeBookmark(bookmarksData, id)) {
// 保存到Firebase
database.ref('bookmarks').set(bookmarksData)
.then(() => {
renderGrid();
renderTreeView();
})
.catch(error => {
alert('删除失败: ' + error.message);
});
}
}
// 处理搜索
function handleSearch(e) {
if (grid) {
grid.search(e.target.value);
}
}
// 生成唯一ID
function generateId() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
</script>
</body>
</html>