-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
851 lines (707 loc) · 41.5 KB
/
Copy pathindex.html
File metadata and controls
851 lines (707 loc) · 41.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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>旅遊打包檢查器</title>
<link rel="manifest" href="manifest.json">
<link rel="icon" type="image/png" sizes="192x192" href="icon-192.png">
<link rel="shortcut icon" href="icon-192.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="旅遊打包檢查器">
<link rel="apple-touch-icon" href="icon-192.png">
<script src="https://cdn.tailwindcss.com"></script>
<style>
html,
body {
-webkit-tap-highlight-color: transparent;
}
.edit-toggle-btn {
min-width: 8rem;
white-space: nowrap;
transition: all 0.2s ease;
}
.edit-toggle-btn:hover {
transform: translateY(-1px);
}
.item-action-group {
display: flex;
align-items: center;
gap: 0.5rem;
}
.action-btn {
border-radius: 9999px;
padding: 0.35rem 0.7rem;
font-size: 0.78rem;
font-weight: 700;
transition: all 0.2s ease;
white-space: nowrap;
}
.action-btn:hover {
transform: translateY(-1px);
}
.add-item-btn {
margin-top: 0.6rem;
width: 100%;
border: 1px dashed #60a5fa;
color: #bfdbfe;
background: rgba(37, 99, 235, 0.08);
border-radius: 0.9rem;
padding: 0.65rem 0.75rem;
font-size: 0.9rem;
font-weight: 700;
transition: all 0.2s ease;
}
.add-item-btn:hover {
background: rgba(37, 99, 235, 0.16);
}
.template-select {
min-width: 10rem;
cursor: pointer;
appearance: none;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='white' d='M10.293 3.293L6 7.586 1.707 3.293A1 1 0 00.293 4.707l5 5a1 1 0 001.414 0l5-5a1 1 0 10-1.414-1.414z'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 0.5rem center;
padding-right: 1.75rem;
}
.action-small-btn {
min-width: 2.5rem;
white-space: nowrap;
font-size: 0.75rem;
transition: all 0.2s ease;
}
.action-small-btn:hover {
transform: translateY(-1px);
background-color: rgba(255, 255, 255, 0.2);
}
.action-small-btn:active {
transform: translateY(0);
}
/* ── checklist item: FLIP animation base ── */
.checklist-item {
will-change: transform;
}
/* ── drag and drop styles ── */
.draggable-item {
cursor: grab;
}
.draggable-item:active {
cursor: grabbing;
}
.checklist-item.drag-over-top {
border-top: 3px solid #3b82f6;
padding-top: calc(1rem - 3px);
}
.checklist-item.drag-over-bottom {
border-bottom: 3px solid #3b82f6;
padding-bottom: calc(1rem - 3px);
}
.checklist-item.dragging {
opacity: 0.5;
background-color: rgba(37, 99, 235, 0.2);
}
</style>
</head>
<body class="bg-[#0c0c0e] text-gray-100 min-h-screen flex justify-center items-start p-4 antialiased">
<main class="w-full max-w-md bg-[#0f0f11] rounded-3xl shadow-2xl border border-[#1c1c1f] overflow-hidden mt-4">
<header class="p-6 bg-gradient-to-b from-[#16161a] to-[#0f0f11] border-b border-[#1c1c1f] shadow-sm">
<div class="flex items-center justify-between gap-4 mb-4">
<div class="flex items-center gap-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-blue-400" fill="none"
viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4" />
</svg>
<h1 class="text-base font-bold tracking-wider text-gray-100">打包檢查</h1>
</div>
<button type="button" onclick="toggleEditMode()" id="edit-mode-btn"
class="rounded-xl px-3 py-1.5 border transition-all text-xs font-semibold shadow-sm flex items-center gap-1.5 bg-white/5 border-white/10 text-gray-300 hover:bg-white/10 hover:text-white">
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
<span>編輯</span>
</button>
</div>
<div class="flex items-center gap-2">
<div class="relative flex-1">
<select id="template-selector" onchange="switchTemplate(this.value)"
class="w-full appearance-none template-select rounded-xl bg-white/5 hover:bg-white/10 text-white text-sm font-semibold pl-4 pr-10 py-3 border border-white/10 transition-all cursor-pointer focus:outline-none focus:ring-2 focus:ring-blue-500/50 focus:border-blue-500/50 tracking-wide">
<option value="default" class="bg-[#16161a] text-white py-2">預設模板 (Default)</option>
<option value="minimal" class="bg-[#16161a] text-white py-2">極簡風格 (Minimal)</option>
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3.5 text-white/40">
<svg class="h-4 w-4 transition-transform duration-200" xmlns="http://www.w3.org/2000/svg"
fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" />
</svg>
</div>
</div>
<button type="button" onclick="renameCurrentTemplate()" title="重新命名模板"
class="w-10 h-10 flex items-center justify-center rounded-xl bg-white/5 hover:bg-white/10 text-gray-300 hover:text-white border border-white/10 transition-all active:scale-95">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round"
d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
</button>
<button type="button" onclick="duplicateCurrentTemplate()" title="複製此模板"
class="w-10 h-10 flex items-center justify-center rounded-xl bg-white/5 hover:bg-white/10 text-gray-300 hover:text-white border border-white/10 transition-all active:scale-95">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round"
d="M8 7v8a2 2 0 002 2h6M8 7V5a2 2 0 012-2h4.586a1 1 0 01.707.293l4.414 4.414a1 1 0 01.293.707V15a2 2 0 01-2 2h-2M8 7H6a2 2 0 00-2 2v10a2 2 0 002 2h8a2 2 0 002-2v-2" />
</svg>
</button>
<button type="button" onclick="deleteCurrentTemplate()" title="刪除此模板"
class="w-10 h-10 flex items-center justify-center rounded-xl bg-white/5 hover:bg-red-600/20 hover:border-red-500/50 text-gray-300 hover:text-red-400 border border-white/10 transition-all active:scale-95">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round"
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</div>
<div class="flex items-center gap-1.5 text-gray-500 mt-4 text-xs font-medium opacity-80">
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5 text-gray-500" fill="none"
viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<span>勾選後的物品會自動沉到底部</span>
</div>
</header>
<div id="stats-view" class="px-4 pt-4 pb-2">
<div class="bg-[#121215] border border-white/5 rounded-2xl p-4 transition-all">
<div class="mb-4">
<div class="h-2 bg-white/5 rounded-full overflow-hidden border border-white/10">
<div id="progress-bar" class="h-full bg-gradient-to-r from-blue-500 to-blue-400 w-0 transition-all duration-500 ease-out rounded-full"></div>
</div>
</div>
<div class="grid grid-cols-3 gap-3">
<div class="bg-white/5 rounded-lg p-3 border border-white/10 text-center">
<div class="text-xs text-gray-400 font-medium mb-1 tracking-wide">已完成</div>
<div id="completed-count" class="text-2xl font-bold text-blue-400 tabular-nums">0</div>
</div>
<div class="bg-white/5 rounded-lg p-3 border border-white/10 text-center">
<div class="text-xs text-gray-400 font-medium mb-1 tracking-wide">總數量</div>
<div id="total-count" class="text-2xl font-bold text-gray-300 tabular-nums">0</div>
</div>
<div class="bg-white/5 rounded-lg p-3 border border-white/10 text-center">
<div class="text-xs text-gray-400 font-medium mb-1 tracking-wide">完成度</div>
<div id="percentage" class="text-2xl font-bold text-green-400 tabular-nums">0%</div>
</div>
</div>
</div>
</div>
<div id="checklist-container" class="p-4 space-y-3 max-h-[70vh] overflow-y-auto"></div>
</main>
<script>
const defaultTemplates = {
"basic": {
name: "基礎模板",
items: [
{ id: "sec-doc", name: "證件與文件", isDivider: true },
{ id: "item-01", name: "台胞證", isDone: false },
{ id: "item-02", name: "帶護照", isDone: false },
{ id: "item-03", name: "身分證", isDone: true },
{ id: "item-04", name: "健保卡", isDone: false },
{ id: "sec-health", name: "醫療與保健", isDivider: true },
{ id: "item-05", name: "腸胃藥", isDone: false },
{ id: "item-06", name: "b群+C", isDone: false },
{ id: "item-07", name: "鈣粉", isDone: false },
{ id: "item-08", name: "丹木斯", isDone: false },
{ id: "item-09", name: "eve", isDone: false },
{ id: "sec-tech", name: "電子產品與配件", isDivider: true },
{ id: "item-10", name: "小米手環充電線", isDone: false },
{ id: "item-11", name: "行動電源", isDone: false },
{ id: "item-12", name: "充電線", isDone: false },
{ id: "item-13", name: "小電扇", isDone: false },
{ id: "item-14", name: "自拍棒", isDone: false },
{ id: "sec-clothes", name: "衣物與穿戴", isDivider: true },
{ id: "item-15", name: "外套", isDone: false },
{ id: "item-16", name: "襪子", isDone: false },
{ id: "item-17", name: "室內拖", isDone: false },
{ id: "item-18", name: "墨鏡", isDone: false },
{ id: "sec-care", name: "個人盥洗與護理", isDivider: true },
{ id: "item-19", name: "防蚊液", isDone: false },
{ id: "item-20", name: "防曬", isDone: false },
{ id: "item-21", name: "浴帽", isDone: false },
{ id: "item-22", name: "指甲刀", isDone: false },
{ id: "item-23", name: "修眉刀", isDone: false },
{ id: "sec-luggage", name: "行李與收納", isDivider: true },
{ id: "item-24", name: "30吋行李箱", isDone: false },
{ id: "item-25", name: "行李牌", isDone: false },
{ id: "item-26", name: "行李束帶", isDone: false },
{ id: "item-27", name: "衣架", isDone: false },
{ id: "item-28", name: "洗衣袋 (小、中、大)", isDone: false },
{ id: "item-29", name: "垃圾袋", isDone: false },
{ id: "sec-comfort", name: "舒適與隨身雜項", isDivider: true },
{ id: "item-30", name: "傘", isDone: false },
{ id: "item-31", name: "扇子", isDone: false },
{ id: "item-32", name: "眼罩", isDone: false },
{ id: "item-33", name: "環保餐具", isDone: false },
{ id: "sec-finance", name: "刷卡與財務", isDivider: true },
{ id: "item-34", name: "帶黑狗卡", isDone: false },
{ id: "item-35", name: "元大jcb (2.2%)", isDone: false },
{ id: "item-36", name: "fly卡 (3%無上限)", isDone: false }
]
}
};
const savedTemplates = localStorage.getItem('mom_travel_templates');
const savedTemplateId = localStorage.getItem('mom_current_template_id');
let templates = savedTemplates ? JSON.parse(savedTemplates) : defaultTemplates;
let currentTemplateId = savedTemplateId && templates[savedTemplateId] ? savedTemplateId : "basic";
let isEditing = false;
function getCurrentItems() {
return templates[currentTemplateId].items;
}
function saveToLocalStorage() {
localStorage.setItem('mom_travel_templates', JSON.stringify(templates));
localStorage.setItem('mom_current_template_id', currentTemplateId);
}
function renderTemplateSelector() {
const selector = document.getElementById('template-selector');
selector.innerHTML = Object.keys(templates).map(id => `
<option value="${id}" ${id === currentTemplateId ? 'selected' : ''}>
${templates[id].name}
</option>
`).join('');
}
function renderList() {
const container = document.getElementById('checklist-container');
container.innerHTML = '';
const currentItems = getCurrentItems();
updateStatsView(currentItems);
const partitioned = [];
let currentSection = null;
currentItems.forEach(item => {
if (item.isDivider) {
currentSection = { divider: item, elements: [] };
partitioned.push(currentSection);
} else if (currentSection) {
currentSection.elements.push(item);
}
});
// inner sorting
partitioned.forEach(section => {
section.elements.sort((a, b) => a.isDone - b.isDone);
});
// top-level sorting
partitioned.sort((a, b) => {
const aAllDone = a.elements.every(item => item.isDone);
const bAllDone = b.elements.every(item => item.isDone);
if (aAllDone === bAllDone) return 0;
return aAllDone ? 1 : -1;
});
const htmlString = partitioned.map(section => {
const totalCount = section.elements.length;
const doneCount = section.elements.filter(item => item.isDone).length;
const isAllDone = totalCount > 0 && doneCount === totalCount;
const itemsHtml = section.elements.map(item => `
<div data-item-id="${item.id}" data-section-id="${section.divider.id}" class="checklist-item flex justify-between items-center bg-[#18181c] p-4 rounded-xl border border-white/5 hover:bg-[#202025] transition active:scale-[0.98] ${isEditing ? 'draggable-item cursor-grab' : 'cursor-pointer'} selection:bg-transparent"
draggable="${isEditing ? 'true' : 'false'}"
${isEditing ? `ondragstart="handleDragStart(event)" ondragend="handleDragEnd(event)" ondragover="handleDragOver(event)" ondrop="handleDrop(event)"` : `onclick="toggleItem('${item.id}')"`}>
<span class="text-lg font-medium tracking-wide transition-all duration-300 ${item.isDone ? 'text-gray-100' : 'text-gray-100'}">
${item.name}
</span>
<div class="flex items-center gap-2">
<div class="w-6 h-6 rounded-md flex items-center justify-center transition-all border-2 ${item.isDone ? 'bg-blue-500 border-blue-500 text-white' : 'border-white/70 hover:border-white'} ${isEditing ? 'hidden' : ''}">
${item.isDone ? `
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
</svg>
` : ''}
</div>
<div class="item-action-group ${isEditing ? 'flex' : 'hidden'}">
<button
type="button"
onclick="event.stopPropagation(); editItem('${item.id}')"
title="修改物品名稱"
class="w-8 h-8 flex items-center justify-center rounded-lg bg-blue-500/80 hover:bg-blue-500 text-white transition-all active:scale-95"
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
</button>
<button
type="button"
onclick="event.stopPropagation(); deleteItem('${item.id}')"
title="刪除此項目"
class="w-8 h-8 flex items-center justify-center rounded-lg bg-red-500/80 hover:bg-red-500 text-white transition-all active:scale-95"
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
<span class="text-gray-400 text-lg ml-2 cursor-grab" title="拖拽排序">⋮</span>
</div>
</div>
</div>
`).join('');
return `
<div class="bg-[#121215] border border-white/5 rounded-2xl p-4 mb-4 transition-all ${isAllDone ? 'opacity-50' : ''}">
<!-- 分類標頭 -->
<div class="flex items-center justify-between pb-2 border-b border-white/5">
<div class="flex items-center gap-2">
<span class="w-1.5 h-1.5 rounded-full bg-blue-500"></span>
<h2 class="text-sm font-bold text-gray-200 tracking-wide">${section.divider.name}</h2>
</div>
<div class="flex items-center gap-2">
${!isEditing ? `
<span class="text-[10px] font-semibold px-2 py-0.5 rounded-md bg-blue-500/10 text-blue-400 border border-blue-500/20 tabular-nums">
${doneCount} / ${totalCount}
</span>
` : `
<button type="button" onclick="event.stopPropagation(); renameSection('${section.divider.id}')" title="編輯分類名稱" class="w-7 h-7 flex items-center justify-center rounded-lg bg-black/40 hover:bg-blue-500/80 text-white transition-all active:scale-95">
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
</button>
<button type="button" onclick="event.stopPropagation(); deleteSection('${section.divider.id}')" title="刪除分類" class="w-7 h-7 flex items-center justify-center rounded-lg bg-black/40 hover:bg-red-500/80 text-white transition-all active:scale-95">
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
`}
</div>
</div>
<!-- 項目清單 -->
<div class="space-y-3 mt-3">
${itemsHtml}
</div>
<!-- 新增按鈕 (編輯模式下顯示在板塊底部) -->
${isEditing ? `
<button type="button" onclick="addItem('${section.divider.id}')" class="add-item-btn mt-3">+新增項目</button>
` : ''}
</div>
`;
}).join('');
container.innerHTML = htmlString;
if (isEditing) {
const addSectionBtn = document.createElement('button');
addSectionBtn.type = 'button';
addSectionBtn.className = 'w-full mt-4 border-2 border-dashed border-green-500/50 text-green-400 hover:border-green-500/80 hover:text-green-300 bg-green-500/5 hover:bg-green-500/10 rounded-xl py-3 font-semibold transition-all text-sm';
addSectionBtn.innerHTML = '新增分類';
addSectionBtn.onclick = () => addSection();
container.appendChild(addSectionBtn);
} else {
const resetBtn = document.createElement('button');
resetBtn.type = 'button';
resetBtn.className = 'w-full mt-4 border-2 border-dashed border-amber-500/50 text-amber-400 hover:border-amber-500/80 hover:text-amber-300 bg-amber-500/5 hover:bg-amber-500/10 rounded-xl py-3 font-semibold transition-all text-sm';
resetBtn.innerHTML = '清空所有勾選';
resetBtn.onclick = () => resetAllChecks();
container.appendChild(resetBtn);
}
// 動態更新 Header 的編輯按鈕狀態與圖示
const editBtn = document.getElementById('edit-mode-btn');
if (editBtn) {
if (isEditing) {
editBtn.className = "rounded-xl px-3 py-1.5 border transition-all text-xs font-semibold shadow-sm flex items-center gap-1.5 bg-blue-600/20 border-blue-500/50 text-blue-400 hover:bg-blue-600/30";
editBtn.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
</svg>
<span>完成編輯</span>
`;
} else {
editBtn.className = "rounded-xl px-3 py-1.5 border transition-all text-xs font-semibold shadow-sm flex items-center gap-1.5 bg-white/5 border-white/10 text-gray-300 hover:bg-white/10 hover:text-white";
editBtn.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
<path stroke-linecap="round" stroke-linejoin="round" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
<span>編輯清單</span>
`;
}
}
saveToLocalStorage();
}
window.switchTemplate = function (templateId) {
if (templates[templateId]) {
currentTemplateId = templateId;
renderTemplateSelector();
renderList();
}
};
const _pendingToggle = new Set();
window.toggleItem = function (id) {
if (_pendingToggle.has(id)) return;
const currentItems = getCurrentItems();
const targetItem = currentItems.find(item => String(item.id) === String(id));
if (!targetItem || targetItem.isDivider) return;
const willBeDone = !targetItem.isDone;
if (willBeDone) {
_pendingToggle.add(id);
// FLIP: First ── Snapshot positions of every item before re-render
const firstRects = new Map();
document.querySelectorAll('[data-item-id]').forEach(el => {
firstRects.set(el.dataset.itemId, el.getBoundingClientRect());
});
// Commit state + re-render (DOM is now in final state)
targetItem.isDone = true;
renderList();
// FLIP: Last ── Snapshot positions after re-render
const newEls = document.querySelectorAll('[data-item-id]');
const lastRects = new Map();
newEls.forEach(el => {
lastRects.set(el.dataset.itemId, el.getBoundingClientRect());
});
// FLIP: Invert + Play
newEls.forEach(el => {
const itemId = el.dataset.itemId;
const first = firstRects.get(itemId);
const last = lastRects.get(itemId);
if (!first || !last) return;
const deltaY = first.top - last.top;
const isCheckedItem = itemId === id;
// Skip items that didn't move (and aren't the checked item)
if (Math.abs(deltaY) < 1 && !isCheckedItem) return;
// Invert: teleport the element back to its old visual position
el.style.transition = 'none';
el.style.transform = `translateY(${deltaY}px)`;
if (isCheckedItem) el.style.opacity = '1';
// Force a reflow so the browser registers the starting state
el.getBoundingClientRect();
// Play: animate to the natural (new) position
const dur = isCheckedItem ? '0.45s' : '0.38s';
el.style.transition = `transform ${dur} cubic-bezier(0.4, 0, 0.2, 1)`
+ (isCheckedItem ? ', opacity 0.45s ease' : '');
el.style.transform = '';
if (isCheckedItem) el.style.opacity = ''; // let Tailwind opacity-50 take over
// Clean up inline styles after animation
el.addEventListener('transitionend', () => {
el.style.transition = '';
el.style.transform = '';
el.style.opacity = '';
}, { once: true });
});
_pendingToggle.delete(id);
} else {
// Unchecking: instant re-render
targetItem.isDone = false;
renderList();
}
};
window.toggleEditMode = function () {
isEditing = !isEditing;
renderList();
};
window.deleteItem = function (id) {
const filteredItems = getCurrentItems().filter(item => item.id !== id);
templates[currentTemplateId].items = filteredItems;
renderList();
};
window.editItem = function (id) {
const currentItems = getCurrentItems();
const targetItem = currentItems.find(item => item.id === id);
if (!targetItem) return;
const newName = window.prompt("請輸入新的物品名稱: ", targetItem.name);
if (newName && newName.trim() !== "") {
targetItem.name = newName.trim();
renderList();
}
};
window.addItem = function (sectionId) {
const itemName = window.prompt("請輸入欲新增的物品名稱:");
if (!itemName || itemName.trim() === "") return;
const newItem = {
id: String(Date.now()),
name: itemName.trim(),
isDone: false
};
const currentItems = getCurrentItems();
const dividerIndex = currentItems.findIndex(item => item.id === sectionId);
if (dividerIndex !== -1) {
currentItems.splice(dividerIndex + 1, 0, newItem);
renderList();
}
};
window.addSection = function () {
const sectionName = window.prompt("請輸入新分類的名稱:");
if (!sectionName || sectionName.trim() === "") return;
const currentItems = getCurrentItems();
const newDividerId = "d_" + Date.now();
const newDivider = {
id: newDividerId,
name: sectionName.trim(),
isDivider: true
};
currentItems.push(newDivider);
renderList();
};
window.renameSection = function (sectionId) {
const currentItems = getCurrentItems();
const targetSection = currentItems.find(item => item.id === sectionId && item.isDivider);
if (!targetSection) return;
const newName = window.prompt("請輸入新的分類名稱:", targetSection.name);
if (newName && newName.trim() !== "") {
targetSection.name = newName.trim();
renderList();
}
};
window.deleteSection = function (sectionId) {
const currentItems = getCurrentItems();
const dividerIndex = currentItems.findIndex(item => item.id === sectionId && item.isDivider);
if (dividerIndex === -1) return;
const section = currentItems[dividerIndex];
let itemCount = 0;
for (let i = dividerIndex + 1; i < currentItems.length; i++) {
if (currentItems[i].isDivider) break;
itemCount++;
}
const confirmed = window.confirm(
itemCount > 0
? `刪除分類【${section.name}】會同時刪除其中 ${itemCount} 個項目。確定要刪除嗎?`
: `確定要刪除空分類【${section.name}】嗎?`
);
if (!confirmed) return;
// 刪除該分類及其所有項目
currentItems.splice(dividerIndex, itemCount + 1);
renderList();
};
window.resetAllChecks = function () {
const currentItems = getCurrentItems();
const checkedCount = currentItems.filter(item => !item.isDivider && item.isDone).length;
if (checkedCount === 0) {
alert("沒有已勾選的項目。");
return;
}
const confirmed = window.confirm(`確定要清空所有 ${checkedCount} 個已勾選的項目嗎?`);
if (!confirmed) return;
currentItems.forEach(item => {
if (!item.isDivider && item.isDone) {
item.isDone = false;
}
});
renderList();
};
function updateStatsView(items) {
const nonDividerItems = items.filter(item => !item.isDivider);
const totalCount = nonDividerItems.length;
const completedCount = nonDividerItems.filter(item => item.isDone).length;
const percentage = totalCount === 0 ? 0 : Math.round((completedCount / totalCount) * 100);
// 更新數字
document.getElementById('completed-count').textContent = completedCount;
document.getElementById('total-count').textContent = totalCount;
document.getElementById('percentage').textContent = percentage + '%';
// 更新進度條
const progressBar = document.getElementById('progress-bar');
progressBar.style.width = (totalCount === 0 ? 0 : percentage) + '%';
}
window.renameCurrentTemplate = function () {
const currentTemplate = templates[currentTemplateId];
if (!currentTemplate) return;
const newName = window.prompt("請輸入新的模板名稱:", currentTemplate.name);
if (newName && newName.trim() !== "") {
currentTemplate.name = newName.trim();
renderTemplateSelector();
renderList();
}
};
window.duplicateCurrentTemplate = function () {
const currentTemplate = templates[currentTemplateId];
if (!currentTemplate) return;
const newName = window.prompt(`複製【${currentTemplate.name}】。請輸入新模板的名稱:`);
if (!newName || newName.trim() === "") return;
const newTemplateId = "tmpl_" + Date.now();
const clonedItems = structuredClone(currentTemplate.items);
clonedItems.forEach(item => {
if (!item.isDivider) item.isDone = false;
});
templates[newTemplateId] = {
name: newName.trim(),
items: clonedItems
};
currentTemplateId = newTemplateId;
renderTemplateSelector();
renderList();
};
window.deleteCurrentTemplate = function () {
const templateIds = Object.keys(templates);
if (templateIds.length <= 1) {
alert("至少要保留一個模板,無法刪除。");
return;
}
const currentTemplate = templates[currentTemplateId];
if (!currentTemplate) return;
const confirmed = window.confirm(`確定要刪除【${currentTemplate.name}】模板嗎?此動作無法復原。`);
if (!confirmed) return;
delete templates[currentTemplateId];
const remainingIds = Object.keys(templates);
currentTemplateId = remainingIds[0];
renderTemplateSelector();
renderList();
};
/* ── Drag and Drop Logic ── */
let draggedItemId = null;
let draggedSectionId = null;
window.handleDragStart = function (e) {
if (!isEditing) return;
draggedItemId = e.currentTarget.dataset.itemId;
draggedSectionId = e.currentTarget.dataset.sectionId;
e.currentTarget.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
};
window.handleDragEnd = function (e) {
document.querySelectorAll('.draggable-item').forEach(el => {
el.classList.remove('dragging', 'drag-over-top', 'drag-over-bottom');
});
draggedItemId = null;
draggedSectionId = null;
};
window.handleDragOver = function (e) {
if (!isEditing || !draggedItemId) return;
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
const target = e.currentTarget;
if (target.dataset.itemId === draggedItemId) return;
const rect = target.getBoundingClientRect();
const midpoint = rect.top + rect.height / 2;
target.classList.remove('drag-over-top', 'drag-over-bottom');
if (e.clientY < midpoint) {
target.classList.add('drag-over-top');
} else {
target.classList.add('drag-over-bottom');
}
};
window.handleDrop = function (e) {
if (!isEditing || !draggedItemId) return;
e.preventDefault();
const targetItemId = e.currentTarget.dataset.itemId;
const targetSectionId = e.currentTarget.dataset.sectionId;
if (targetItemId === draggedItemId) return;
const currentItems = getCurrentItems();
const draggedIndex = currentItems.findIndex(item => item.id === draggedItemId);
const targetIndex = currentItems.findIndex(item => item.id === targetItemId);
if (draggedIndex === -1 || targetIndex === -1) return;
const rect = e.currentTarget.getBoundingClientRect();
const midpoint = rect.top + rect.height / 2;
const isAbove = e.clientY < midpoint;
const [draggedItem] = currentItems.splice(draggedIndex, 1);
let newTargetIndex = currentItems.findIndex(item => item.id === targetItemId);
if (isAbove) {
currentItems.splice(newTargetIndex, 0, draggedItem);
} else {
currentItems.splice(newTargetIndex + 1, 0, draggedItem);
}
renderList();
};
document.addEventListener('dragover', function (e) {
e.preventDefault();
});
renderTemplateSelector();
renderList();
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('sw.js')
.then(reg => console.log('Service Worker 註冊成功:', reg.scope))
.catch(err => console.log('Service Worker 註冊失敗:', err));
});
}
</script>
</body>
</html>