-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_sort_activity_submission_form.html
More file actions
1199 lines (1025 loc) · 46.9 KB
/
card_sort_activity_submission_form.html
File metadata and controls
1199 lines (1025 loc) · 46.9 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
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Card Sort Activity</title>
<!-- Bootstrap 3 CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- jQuery UI for drag and drop -->
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<!-- Font Awesome for icons -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<body>
<div class="container-fluid">
<!-- Instructions -->
<div class="instructions alert alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close" style="right:10px;color:#000!important;">×</a>
<h4><i class="fa fa-info-circle"></i> Instructions</h4>
<p>Drag and drop items from the "Items" section to create new categories. A new category will be created automatically when you drop an item into the empty categories area. You can rename categories by clicking on their titles. Items within categories can be reordered by dragging them to new positions.</p>
</div>
<div class="row">
<!-- Items Section -->
<div class="col-md-3">
<h3 class="container-title">Items</h3>
<div id="items-container" class="category-container">
<div class="dropzone" id="uncategorized">
<div class="item-card" draggable="true">Staff & Liaisons</div>
<div class="item-card" draggable="true">Databases</div>
<div class="item-card" draggable="true">Hours</div>
<div class="item-card" draggable="true">Chat Assistance</div>
<div class="item-card" draggable="true">Course Reserves</div>
<div class="item-card" draggable="true">Library Systems Accessibility</div>
<div class="item-card" draggable="true">Policies</div>
<div class="item-card" draggable="true">Group Study Rooms</div>
<div class="item-card" draggable="true">Mission & History</div>
<div class="item-card" draggable="true">Interlibrary Loan</div>
<div class="item-card" draggable="true">Library Tutorials</div>
<div class="item-card" draggable="true">Sitemap</div>
<div class="item-card" draggable="true">Theses, Dissertations, & Projects</div>
<div class="item-card" draggable="true">Frequently Asked Questions</div>
<div class="item-card" draggable="true">Research Appointments</div>
<div class="item-card" draggable="true">Quick Search</div>
<div class="item-card" draggable="true">Spaces</div>
<div class="item-card" draggable="true">Research Guides</div>
<div class="item-card" draggable="true">Journals</div>
</div>
</div>
</div>
<!-- Categories Section - Split into two columns -->
<div class="col-md-9">
<h3 class="container-title">Categories <button id="add-category" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i> Add Category</button></h3>
<!-- Categories drop area -->
<div id="categories-area" class="dropzone-categories">
<!-- Two column layout for categories -->
<div class="row">
<div class="col-md-6">
<div id="categories-container-left" class="categories-column">
<!-- Categories will be added here dynamically -->
</div>
</div>
<div class="col-md-6">
<div id="categories-container-right" class="categories-column">
<!-- Categories will be added here dynamically -->
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Form Questions Section -->
<div class="row" id="form-section">
<div class="col-md-12">
<div id="form-section-button" class="text-center">
<button type="button" id="submit-results-form" class="btn btn-lg btn-primary">
<i class="fa fa-smile-o"></i> I've finished sorting!
</button>
</div>
<script>
$(document).ready(function() {
$("#submit-results-form").click(function() {
$("#feedback-questions-form").toggle(); // Toggle visibility of the DIV
});
});
</script>
<div class="well" id="feedback-questions-form" style="display: none;">
<h3 class="container-title">Feedback Questions</h3>
<form id="feedback-form">
<div class="form-group">
<label for="categories-description">Please describe the categories you came up with and why you chose them.</label>
<textarea class="form-control" id="categories-description" rows="4"></textarea>
</div>
<div class="form-group">
<label for="difficult-items">Were there any items that you found confusing or had difficulty categorizing?</label>
<textarea class="form-control" id="difficult-items" rows="3"></textarea>
</div>
<div class="form-group">
<label for="missing-items">Were there any items missing from the list that you expected to see? If so, how would you categorize them?</label>
<textarea class="form-control" id="missing-items" rows="3"></textarea>
</div>
<div class="form-group">
<label for="website-feedback">From your experience using the Library Services website, is there anything you find difficulty using or think could be improved?</label>
<textarea class="form-control" id="website-feedback" rows="3"></textarea>
</div>
<div class="form-group">
<label for="other-comments">Do you have any other comments or questions?</label>
<textarea class="form-control" id="other-comments" rows="2"></textarea>
</div>
</form>
<div class="text-center">
<button id="submit-results" class="btn btn-lg btn-primary">
<i class="fa fa-paper-plane"></i> Submit
</button>
</div>
</div>
</div>
</div>
<!-- Submission Confirmation Modal -->
<div class="modal fade" id="submission-modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Card Sort Results</h4>
</div>
<div class="modal-body">
<div id="submission-message">
<p>Thank you for participating in the card sort activity!</p>
<p>Your results have been submitted successfully.</p>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Loading Modal -->
<div class="modal fade" id="loading-modal" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-body text-center">
<p><i class="fa fa-spinner fa-spin fa-3x"></i></p>
<p id="loading-message">Submitting your results...</p>
</div>
</div>
</div>
</div>
</div>
<style>
:root {
--primary-color: #862633;
--text-color: #333;
--background-color: #fff;
--card-bg: #D7D2CB;
--border-color: #ccc;
--highlight-color: #f4e7e9;
--edit-hover: #ebe7e4;
}
.item-card {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 4px;
padding: 10px;
margin-bottom: 10px;
cursor: move;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
transition: all 0.2s;
position: relative;
z-index: 1;
}
.item-card:hover {
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
/* Enhanced drop position indicators */
.item-card.insert-before {
position: relative;
margin-top: 20px;
}
.item-card.insert-before::before {
content: '';
position: absolute;
top: -10px;
left: 0;
right: 0;
height: 3px;
background-color: var(--primary-color);
border-radius: 3px;
pointer-events: none;
}
.item-card.insert-before::after {
content: 'Drop here';
position: absolute;
top: -20px;
left: 50%;
transform: translateX(-50%);
background-color: var(--primary-color);
color: white;
padding: 2px 8px;
border-radius: 3px;
font-size: 10px;
white-space: nowrap;
pointer-events: none;
}
.item-card.insert-after {
position: relative;
margin-bottom: 20px;
}
.item-card.insert-after::before {
content: '';
position: absolute;
bottom: -10px;
left: 0;
right: 0;
height: 3px;
background-color: var(--primary-color);
border-radius: 3px;
pointer-events: none;
}
.item-card.insert-after::after {
content: 'Drop here';
position: absolute;
bottom: -20px;
left: 50%;
transform: translateX(-50%);
background-color: var(--primary-color);
color: white;
padding: 2px 8px;
border-radius: 3px;
font-size: 10px;
white-space: nowrap;
pointer-events: none;
}
.category-container {
background-color: var(--background-color);
border: 1px solid var(--border-color);
border-radius: 4px;
padding: 15px;
margin-bottom: 20px;
min-height: 100px;
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.category-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
padding-bottom: 5px;
border-bottom: 1px solid var(--border-color);
}
.category-title {
font-weight: bold;
margin: 0;
padding: 5px;
flex: 1;
color: var(--primary-color);
}
.category-title.editable:hover {
background-color: var(--edit-hover);
border-radius: 3px;
cursor: pointer;
}
.container-title {
margin-bottom: 15px;
font-weight: bold;
color: var(--primary-color);
}
.dropzone {
min-height: 50px;
padding: 10px 0;
position: relative;
}
.dropzone.dragover {
background-color: var(--highlight-color);
border: 2px dashed var(--primary-color);
border-radius: 4px;
animation: pulse 1.5s infinite;
}
/* Empty dropzone placeholder */
.dropzone.dragover::after {
content: 'Drop here to add item';
display: block;
text-align: center;
padding: 10px;
color: var(--primary-color);
font-style: italic;
pointer-events: none;
font-weight: bold;
}
/* Categories columns */
.categories-column {
min-height: 150px;
padding: 10px;
position: relative;
}
.categories-column.dragover {
background-color: var(--highlight-color);
border: 2px dashed var(--primary-color);
border-radius: 4px;
padding: 8px;
animation: pulse 1.5s infinite;
}
.categories-column.dragover::after {
content: 'Drop here to add category';
display: block;
text-align: center;
padding: 10px;
color: var(--primary-color);
font-style: italic;
pointer-events: none;
font-weight: bold;
}
.dropzone-categories {
position: relative;
min-height: 150px;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.7; }
100% { opacity: 1; }
}
.btn-primary {
background-color: #862633 !important;
background-image: none !important;
border-color: #000;
text-shadow: 0 1px 1px rgba(200, 200, 200, .44);
-webkit-font-smoothing: antialiased;
}
.btn-primary:hover,
.btn-primary:focus,
.btn-primary:active {
background-color: #862633 !important;
filter: brightness(110%);
}
.delete-category {
color: #000;
cursor: pointer;
margin-left: 10px;
}
.category-input {
width: 100%;
padding: 5px;
border: 1px solid var(--border-color);
border-radius: 3px;
background-color: var(--background-color);
color: var(--text-color);
}
.instructions {
margin-bottom: 20px;
padding: 15px;
background-color: var(--card-bg);
border-radius: 4px;
border-left: 4px solid var(--primary-color);
}
/* Form styles */
#form-section {
margin-top: 30px;
margin-bottom: 30px;
}
.well {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 4px;
padding: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
.form-control {
width: 100%;
padding: 8px 12px;
border: 1px solid var(--border-color);
border-radius: 4px;
font-size: 14px;
line-height: 1.42857143;
}
textarea.form-control {
resize: vertical;
}
#submit-results {
margin-top: 20px;
padding: 10px 25px;
}
/* Small devices adjustments */
@media (max-width: 767px) {
.category-container {
margin-bottom: 15px;
}
.item-card {
padding: 8px;
margin-bottom: 8px;
}
}
/* Error message styling */
.alert-danger {
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
}
/* Success message styling */
.alert-success {
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
}
</style>
<script>
$(document).ready(function() {
// Initialize drag and drop functionality
initDragAndDrop();
// Add category button
$('#add-category').click(function() {
createCategory();
});
// Set up the categories area to handle direct drops
setupCategoriesAreaDrops();
// Randomize items on load
randomizeItems();
// Submit button click handler
$('#submit-results').click(function() {
submitResults();
});
});
// API credentials - in production these would be stored securely
const API_CONFIG = {
client_id: '162APP-004',
client_secret: 'fcb9be5240309692095629674f897d45',
base_url: 'https://springfield.libanswers.com/api/1.1',
quid: 306,
pemail: 'amartel5@springfieldcollege.edu'
};
// Function to get OAuth token
async function getAccessToken() {
try {
const tokenUrl = `${API_CONFIG.base_url}/oauth/token`;
const formData = new URLSearchParams();
formData.append('client_id', API_CONFIG.client_id);
formData.append('client_secret', API_CONFIG.client_secret);
formData.append('grant_type', 'client_credentials');
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${accessToken}`
},
body: formData
});
if (!response.ok) {
throw new Error(`Token request failed with status ${response.status}`);
}
const tokenData = await response.json();
return tokenData.access_token;
} catch (error) {
console.error('Error getting access token:', error);
throw error;
}
}
async function submitResultsToApi(formattedData) {
try {
const accessToken = await getAccessToken();
const submitUrl = `${API_CONFIG.base_url}/ticket/create`;
const formData = new URLSearchParams();
formData.append('client_id', API_CONFIG.client_id);
formData.append('client_secret', API_CONFIG.client_secret);
formData.append('quid', API_CONFIG.quid);
formData.append('pquestion', 'Card Sort Results');
formData.append('pdetails', formattedData);
formData.append('pemail', API_CONFIG.pemail);
const response = await fetch(submitUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${accessToken}`
},
body: formData
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `Submission failed with status ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error submitting results:', error);
throw error;
}
}
// Function to submit the card sort results via API
async function submitResults() {
const categories = collectCategories();
const formResponses = {
categoriesDescription: $('#categories-description').val(),
difficultItems: $('#difficult-items').val(),
missingItems: $('#missing-items').val(),
websiteFeedback: $('#website-feedback').val(),
otherComments: $('#other-comments').val()
};
const formattedData = formatSubmissionData(categories, formResponses);
try {
await submitResultsToApi(formattedData);
$('#submission-message').html(`
<div class="alert alert-success">
<p><strong>Success!</strong> Your card sort results have been submitted.</p>
</div>
`);
$('#submission-modal').modal('show');
} catch (error) {
$('#submission-message').html(`
<div class="alert alert-danger">
<p><strong>Error:</strong> There was a problem submitting your results.</p>
<p>Error details: ${error.message}</p>
</div>
<p>Please try again later.</p>
`);
$('#submission-modal').modal('show');
}
}
// Function to format data for submission
function formatSubmissionData(categories, formResponses) {
let formattedData = "CARD SORT RESULTS\n\n";
// Add categories and items
formattedData += "CATEGORIES:\n";
categories.forEach(category => {
formattedData += `\n${category.name}:\n`;
category.items.forEach(item => {
formattedData += `- ${item}\n`;
});
});
// Add form responses
formattedData += "\n\nFEEDBACK RESPONSES:\n\n";
formattedData += "1. Please describe the categories you came up with and why you chose them:\n";
formattedData += formResponses.categoriesDescription + "\n\n";
formattedData += "2. Were there any items that you found confusing or had difficulty categorizing?\n";
formattedData += formResponses.difficultItems + "\n\n";
formattedData += "3. Were there any items missing from the list that you expected to see? If so, how would you categorize them?\n";
formattedData += formResponses.missingItems + "\n\n";
formattedData += "4. From your experience using the Library Services website, is there anything you find difficulty using or think could be improved?\n";
formattedData += formResponses.websiteFeedback + "\n\n";
formattedData += "5. Do you have any other comments or questions?\n";
formattedData += formResponses.otherComments;
return formattedData;
}
// Function to collect all categories and their items
function collectCategories() {
const categories = [];
// Get all category containers
const categoryContainers = document.querySelectorAll('#categories-container-left .category-container, #categories-container-right .category-container');
categoryContainers.forEach(container => {
const categoryTitle = container.querySelector('.category-title').innerText;
const items = [];
// Get all items in this category
const itemElements = container.querySelectorAll('.item-card');
itemElements.forEach(item => {
items.push(item.innerText);
});
categories.push({
name: categoryTitle,
items: items
});
});
// Get uncategorized items
const uncategorizedItems = [];
const uncategorizedElements = document.querySelectorAll('#uncategorized .item-card');
uncategorizedElements.forEach(item => {
uncategorizedItems.push(item.innerText);
});
if (uncategorizedItems.length > 0) {
categories.push({
name: "Uncategorized",
items: uncategorizedItems
});
}
return categories;
}
function initDragAndDrop() {
// Initialize drag for existing items
initDraggableItems();
// Handle dropzones
setupDropZones();
}
function initDraggableItems() {
const draggableItems = document.querySelectorAll('.item-card');
draggableItems.forEach(item => {
item.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', e.target.innerText);
e.dataTransfer.setData('sourceId', this.parentNode.id);
this.style.opacity = '0.4';
// Add a class to show which item is being dragged
this.classList.add('being-dragged');
});
item.addEventListener('dragend', function() {
this.style.opacity = '1';
this.classList.remove('being-dragged');
// Clean up any remaining indicators
const allItems = document.querySelectorAll('.item-card');
allItems.forEach(item => {
item.classList.remove('insert-before', 'insert-after');
});
// Remove column highlights
document.querySelectorAll('.categories-column').forEach(col => {
col.classList.remove('dragover');
});
document.getElementById('categories-area').classList.remove('dragover');
});
});
}
function setupDropZones() {
const dropZones = document.querySelectorAll('.dropzone');
let insertMarker = null;
dropZones.forEach(zone => {
// Add dragover event listener
zone.addEventListener('dragover', function(e) {
e.preventDefault();
// Check if we're dragging over an item card directly
const targetItem = e.target.closest('.item-card');
// Remove any existing insert markers
const allItems = document.querySelectorAll('.item-card');
allItems.forEach(item => {
item.classList.remove('insert-before', 'insert-after');
});
if (targetItem && !zone.id.includes('uncategorized') && zone.contains(targetItem)) {
// Don't show markers for the item being dragged
if (targetItem.classList.contains('being-dragged')) return;
// Only show insertion markers for categorized items
// Determine whether to insert before or after the target item
const rect = targetItem.getBoundingClientRect();
const offsetY = e.clientY - rect.top;
if (offsetY < rect.height / 2) {
// Insert before
targetItem.classList.add('insert-before');
insertMarker = { element: targetItem, position: 'before' };
} else {
// Insert after
targetItem.classList.add('insert-after');
insertMarker = { element: targetItem, position: 'after' };
}
} else {
// Add dragover effect to the dropzone itself
this.classList.add('dragover');
insertMarker = null;
}
});
// Add dragleave event listener
zone.addEventListener('dragleave', function(e) {
// Only remove effect if leaving the zone entirely
if (!e.relatedTarget || !this.contains(e.relatedTarget)) {
this.classList.remove('dragover');
// Remove any insert markers when leaving the zone
if (!e.relatedTarget || !this.contains(e.relatedTarget)) {
const allItems = document.querySelectorAll('.item-card');
allItems.forEach(item => {
item.classList.remove('insert-before', 'insert-after');
});
insertMarker = null;
}
}
});
// Add drop event listener
zone.addEventListener('drop', function(e) {
e.preventDefault();
e.stopPropagation(); // Stop event from bubbling up
this.classList.remove('dragover');
// Remove any insert markers
const allItems = document.querySelectorAll('.item-card');
allItems.forEach(item => {
item.classList.remove('insert-before', 'insert-after');
});
const data = e.dataTransfer.getData('text/plain');
const sourceId = e.dataTransfer.getData('sourceId');
// Don't do anything if dropping in the same container and no specific position
if (sourceId === this.id && !insertMarker) return;
// Find the original element to remove it
const allItemsArray = Array.from(document.querySelectorAll('.item-card'));
let originalItem;
for (let item of allItemsArray) {
if (item.innerText === data && item.parentNode.id === sourceId) {
originalItem = item;
break;
}
}
if (originalItem) {
// Create new item card
const newItem = document.createElement('div');
newItem.className = 'item-card';
newItem.draggable = true;
newItem.innerText = data;
// Add drag event listeners to new item
newItem.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', e.target.innerText);
e.dataTransfer.setData('sourceId', this.parentNode.id);
this.style.opacity = '0.4';
this.classList.add('being-dragged');
});
newItem.addEventListener('dragend', function() {
this.style.opacity = '1';
this.classList.remove('being-dragged');
});
try {
// Check if the drop is on a drop indicator (near an existing item)
const nearbyItem = e.target.closest('.item-card');
const isBeforeIndicator = nearbyItem && (
nearbyItem.classList.contains('insert-before') ||
e.target.matches('.item-card.insert-before::before') ||
e.target.matches('.item-card.insert-before::after')
);
const isAfterIndicator = nearbyItem && (
nearbyItem.classList.contains('insert-after') ||
e.target.matches('.item-card.insert-after::before') ||
e.target.matches('.item-card.insert-after::after')
);
// Find the appropriate dropzone
let targetDropzone = this;
if (nearbyItem) {
targetDropzone = nearbyItem.closest('.dropzone');
}
// Add the new item at the specified position with safety checks
if (insertMarker && insertMarker.element && !targetDropzone.id.includes('uncategorized')) {
const targetParent = insertMarker.element.parentNode;
if (!targetParent) {
// Fallback if parent is null
targetDropzone.appendChild(newItem);
} else if (insertMarker.position === 'before') {
// Insert before with null check
targetParent.insertBefore(newItem, insertMarker.element);
} else {
// Insert after with null checks
const nextSibling = insertMarker.element.nextSibling;
if (nextSibling) {
targetParent.insertBefore(newItem, nextSibling);
} else {
targetParent.appendChild(newItem);
}
}
} else if (isBeforeIndicator) {
// Handle drop on a "before" indicator
targetDropzone.insertBefore(newItem, nearbyItem);
} else if (isAfterIndicator) {
// Handle drop on an "after" indicator
const nextSibling = nearbyItem.nextSibling;
if (nextSibling) {
targetDropzone.insertBefore(newItem, nextSibling);
} else {
targetDropzone.appendChild(newItem);
}
} else {
// Just append to the end of the dropzone
targetDropzone.appendChild(newItem);
}
// Only remove the original after successful insertion
originalItem.remove();
} catch (err) {
console.error("Error during drag and drop:", err);
// Fallback: just append to dropzone if any error occurs
this.appendChild(newItem);
originalItem.remove();
}
}
// Reset the insert marker
insertMarker = null;
});
});
// Setup the category columns as drop areas
setupCategoryColumns();
}
// Set up the categories columns as drop areas
function setupCategoryColumns() {
const categoryColumns = document.querySelectorAll('.categories-column');
categoryColumns.forEach(column => {
column.addEventListener('dragover', function(e) {
if (e.target === this || (e.target.parentNode === this && !e.target.classList.contains('category-container'))) {
e.preventDefault();
this.classList.add('dragover');
}
});
column.addEventListener('dragleave', function(e) {
if (!e.relatedTarget || !this.contains(e.relatedTarget)) {
this.classList.remove('dragover');
}
});
column.addEventListener('drop', function(e) {
// Only handle drops directly on the column, not on its categories
if (e.target.closest('.category-container')) {
return;
}
e.preventDefault();
e.stopPropagation();
this.classList.remove('dragover');
const data = e.dataTransfer.getData('text/plain');
const sourceId = e.dataTransfer.getData('sourceId');
if (!data || !sourceId) {
console.log("Missing drag data");
return;
}
// Find the original item
const allItems = document.querySelectorAll('.item-card');
let originalItem;
for (let item of allItems) {
if (item.innerText === data && item.parentNode.id === sourceId) {
originalItem = item;
break;
}
}
if (originalItem) {
try {
// Create new category in this column
createCategoryInColumn(this.id, 'New Category');
// Get the newly created category's dropzone
const newCategory = this.querySelector('.category-container:last-child');
if (!newCategory) {
console.error("Could not find the new category");
return;
}
const newDropzone = newCategory.querySelector('.dropzone');
if (!newDropzone) {
console.error("Could not find the new category's dropzone");
return;
}
// Create new item card in the new category dropzone
const newItem = document.createElement('div');
newItem.className = 'item-card';
newItem.draggable = true;
newItem.innerText = data;
// Add drag event listeners to new item
newItem.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', e.target.innerText);
e.dataTransfer.setData('sourceId', this.parentNode.id);
this.style.opacity = '0.4';
this.classList.add('being-dragged');
});
newItem.addEventListener('dragend', function() {
this.style.opacity = '1';
this.classList.remove('being-dragged');
});
// Add the new item to the new category dropzone
newDropzone.appendChild(newItem);
originalItem.remove();
// Focus on the title to encourage editing
const newTitle = newCategory.querySelector('.category-title');
if (newTitle) {
newTitle.click();
}
} catch (err) {
console.error("Error creating new category:", err);
}
}
});
});
}