-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSidebarPanel.js
2292 lines (2064 loc) · 114 KB
/
SidebarPanel.js
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
function init_sidebar_tabs() {
console.log("init_sidebar_tabs");
let sidebarContent = is_characters_page() ? $(".ct-sidebar__inner [class*='styles_content']>div:first-of-type") : $(".sidebar__pane-content");
// gamelog doesn't use it yet, maybe never
if (window.DM) {
$("#tokens-panel").remove();
tokensPanel = new SidebarPanel("tokens-panel", false);
sidebarContent.append(tokensPanel.build());
init_tokens_panel();
$("#scenes-panel").remove();
scenesPanel = new SidebarPanel("scenes-panel", false);
sidebarContent.append(scenesPanel.build());
init_scenes_panel();
} else {
$("#players-panel").remove();
playersPanel = new SidebarPanel("players-panel", false);
sidebarContent.append(playersPanel.build());
update_pclist();
}
$("#sounds-panel").remove();
soundsPanel = new SidebarPanel("sounds-panel", false);
sidebarContent.append(soundsPanel.build());
window.draw_audio_sidepanel();
$("#journal-panel").remove();
journalPanel = new SidebarPanel("journal-panel", false);
sidebarContent.append(journalPanel.build());
if (window.JOURNAL === undefined) {
init_journal(find_game_id());
} else {
window.JOURNAL.build_journal()
}
$("#settings-panel").remove();
settingsPanel = new SidebarPanel("settings-panel", false);
sidebarContent.append(settingsPanel.build());
init_settings();
observe_hover_text($(".sidebar__inner"));
observe_hover_text($(".sidebar-panel-content"));
}
function sidebar_modal_is_open() {
return $("#VTTWRAPPER .sidebar-modal").length > 0;
}
function close_sidebar_modal() {
$("#VTTWRAPPER .sidebar-modal").remove();
window.current_sidebar_modal = undefined;
}
function display_sidebar_modal(sidebarPanel) {
$("#VTTWRAPPER").append(sidebarPanel.build());
window.current_sidebar_modal = sidebarPanel;
observe_hover_text(sidebarPanel.container);
}
function observe_hover_text(sidebarPanelContent) {
sidebarPanelContent.off("mouseenter mouseleave").on("mouseenter mouseleave", ".sidebar-hover-text:not(.chat-text-wrapper)", function(hoverEvent) {
const displayText = $(hoverEvent.currentTarget).attr("data-hover");
if (typeof displayText === "string" && displayText.length > 0) {
if (hoverEvent.type === "mouseenter") {
build_and_display_sidebar_flyout(hoverEvent.clientY, function (flyout) {
flyout.append(`<div class="sidebar-hover-text-flyout">${displayText}</div>`);
position_flyout_left_of(sidebarPanelContent, flyout);
});
} else {
// only remove hover text flyouts. Don't remove other types of flyouts that may or may not be up
$(".sidebar-hover-text-flyout").closest(".sidebar-flyout").remove();
}
}
});
}
class SidebarPanel {
//#region Class construction and variables
id = "#unknown_panel"; // String: the unique element id of this panel. Examples: #player_panel, #monsters_panel, etc
is_modal = true; // Boolean: true if this panel will be displayed as a modal, false if this panel will be permanently fixed in a sidebar tab
constructor(id, is_modal) {
this.id = id.startsWith("#") ? id.substring(1) : id;
if (is_modal == false) {
// this.is_modal defaults to true. If anything other than false is passed in (such as undefined), just leave it as the default.
this.is_modal = is_modal;
}
}
get container() {
return $(`#${this.id}`);
}
get header() {
return $(`#${this.id} .sidebar-panel-header`);
}
get body() {
return $(`#${this.id} .sidebar-panel-body`);
}
get footer() {
return $(`#${this.id} .sidebar-panel-footer`);
}
// input wrapper is where all inputs should go. When building a sidebar panel with inputs, be sure to add them here
// inputWrapper stacks everything vertically so if you need things side by side, wrap them in a div, and do that there. See build_image_url_input for an example.
get inputWrapper() {
return $(`#${this.id} .sidebar-panel-footer .footer-input-wrapper`);
}
//#endregion Class construction and variables
//#region Class functions
hide() {
this.container.hide();
}
show() {
this.container.show();
}
updateHeader(title = "", subtitle = "", explanationText = "") {
let header = this.header;
header.find(".sidebar-panel-header-title").text(title);
header.find(".sidebar-panel-header-subtitle").text(subtitle);
header.find(".sidebar-panel-header-explanation").text(explanationText);
}
display_sidebar_loading_indicator(subtext) {
let loadingIndicator = $(`
<div class="sidebar-panel-loading-indicator">
<svg class="beholder-dm-screen loading-status-indicator__svg animate" viewBox="0 0 285 176" fill="none" xmlns="http://www.w3.org/2000/svg" style="overflow:overlay;margin-top:100px;width:100%;position:relative;padding:0 10%;"><defs><path id="beholder-eye-move-path" d="M0 0 a 15 5 0 0 0 15 0 a 15 5 0 0 1 -15 0 z"></path><clipPath id="beholder-eye-socket-clip-path"><path id="eye-socket" fill-rule="evenodd" clip-rule="evenodd" d="M145.5 76c-8.562 0-15.5-7.027-15.5-15.694 0-8.663 6.938-1.575 15.5-1.575 8.562 0 15.5-7.088 15.5 1.575C161 68.973 154.062 76 145.5 76z"></path></clipPath></defs><g class="beholder-dm-screen__beholder"><path fill-rule="evenodd" clip-rule="evenodd" d="M145.313 77.36c-10.2 0-18.466-8.27-18.466-18.47 0-10.197 8.266-1.855 18.466-1.855 10.199 0 18.465-8.342 18.465 1.855 0 10.2-8.266 18.47-18.465 18.47m59.557 4.296l-.083-.057c-.704-.5-1.367-1.03-1.965-1.59a12.643 12.643 0 0 1-1.57-1.801c-.909-1.268-1.51-2.653-1.859-4.175-.355-1.521-.461-3.179-.442-4.977.007-.897.049-1.835.087-2.827.038-.995.079-2.032.053-3.194-.031-1.158-.11-2.445-.519-3.97a10.494 10.494 0 0 0-1.014-2.43 8.978 8.978 0 0 0-1.938-2.32 9.64 9.64 0 0 0-2.468-1.54l-.314-.137-.299-.114-.609-.212c-.382-.105-.787-.227-1.151-.298-1.495-.315-2.819-.383-4.065-.39-1.248-.004-2.407.087-3.534.2a56.971 56.971 0 0 0-3.18.44c-6.271.646-12.648 1.559-13.689-.837-1.079-2.487-3.35-8.058 3.115-12.19 4.076.154 8.141.347 12.179.62 1.461.098 2.914.212 4.36.34-4.614.924-9.314 1.7-14.019 2.43h-.015a2.845 2.845 0 0 0-2.388 3.066 2.84 2.84 0 0 0 3.088 2.574c5.125-.462 10.25-.973 15.416-1.696 2.592-.378 5.17-.776 7.88-1.42a29.7 29.7 0 0 0 2.108-.59c.181-.06.363-.117.56-.193.197-.072.378-.136.594-.227.208-.09.405-.17.643-.291l.345-.174.394-.235c.064-.042.124-.076.196-.125l.235-.174.235-.174.117-.099.148-.136c.098-.094.189-.189.283-.287l.137-.152a3.44 3.44 0 0 0 .166-.22c.114-.154.224-.317.318-.484l.072-.125.038-.064.042-.09a5.06 5.06 0 0 0 .367-1.154c.045-.308.06-.63.045-.944a4.322 4.322 0 0 0-.042-.458 5.19 5.19 0 0 0-.386-1.207 5.356 5.356 0 0 0-.499-.799l-.091-.117-.072-.083a5.828 5.828 0 0 0-.303-.318l-.155-.151-.083-.076-.057-.05a9.998 9.998 0 0 0-.503-.382c-.152-.102-.28-.178-.424-.265l-.205-.124-.181-.091-.36-.186a18.713 18.713 0 0 0-.643-.28l-.591-.23c-1.521-.538-2.853-.856-4.197-1.159a83.606 83.606 0 0 0-3.951-.772c-2.604-.45-5.185-.829-7.763-1.166-4.273-.564-8.531-1.029-12.785-1.46 0-.004-.004-.004-.004-.004a38.55 38.55 0 0 0-4.81-3.1v-.004c.397-.223.965-.424 1.688-.549 1.135-.208 2.551-.242 4.05-.185 3.024.11 6.366.59 10.022.662 1.832.02 3.781-.056 5.84-.56a12.415 12.415 0 0 0 3.081-1.188 10.429 10.429 0 0 0 2.702-2.135 2.841 2.841 0 0 0-3.774-4.205l-.208.152c-.825.594-1.76.87-2.956.942-1.188.068-2.566-.09-4.004-.367-2.907-.553-6.003-1.556-9.5-2.32-1.763-.371-3.644-.7-5.802-.73a16.984 16.984 0 0 0-3.455.298 13.236 13.236 0 0 0-3.774 1.333 13.065 13.065 0 0 0-3.376 2.615 14.67 14.67 0 0 0-1.646 2.154h-.004a41.49 41.49 0 0 0-8.436-.863c-1.518 0-3.017.079-4.489.238-1.79-1.563-3.444-3.198-4.833-4.913a21.527 21.527 0 0 1-1.4-1.903 15.588 15.588 0 0 1-1.094-1.893c-.606-1.241-.905-2.422-.893-3.22a3.38 3.38 0 0 1 .038-.55c.034-.155.06-.31.121-.446.106-.273.276-.534.571-.776.579-.496 1.681-.81 2.884-.689 1.207.114 2.487.629 3.615 1.476 1.135.848 2.111 2.044 2.868 3.444l.038.076a2.848 2.848 0 0 0 3.471 1.329 2.843 2.843 0 0 0 1.714-3.641c-.768-2.135-1.96-4.235-3.675-6.003-1.71-1.76-3.924-3.18-6.502-3.872a12.604 12.604 0 0 0-4.076-.416 11.248 11.248 0 0 0-4.284 1.128 10.405 10.405 0 0 0-3.702 3.054c-.499.655-.901 1.37-1.237 2.104-.318.73-.568 1.488-.731 2.237-.337 1.503-.356 2.96-.238 4.315.125 1.362.405 2.63.764 3.822.36 1.196.803 2.317 1.298 3.373a31.9 31.9 0 0 0 1.605 3.043c.458.768.935 1.506 1.427 2.233h-.004a39.13 39.13 0 0 0-4.515 2.384c-3.111-.344-6.2-.76-9.242-1.294-2.033-.364-4.043-.769-6.007-1.26-1.96-.485-3.876-1.045-5.662-1.726a24.74 24.74 0 0 1-2.528-1.102c-.772-.393-1.48-.829-1.987-1.234a4.916 4.916 0 0 1-.56-.507c-.02-.015-.03-.03-.046-.045.288-.28.761-.621 1.314-.905.719-.382 1.566-.711 2.456-.984 1.79-.556 3.762-.9 5.76-1.098l.046-.007a2.843 2.843 0 0 0 2.547-2.805 2.846 2.846 0 0 0-2.824-2.868c-2.301-.02-4.628.11-7.028.567-1.2.231-2.418.538-3.671 1.022-.628.246-1.26.526-1.911.901a10.12 10.12 0 0 0-1.96 1.446c-.648.62-1.307 1.438-1.757 2.524-.114.261-.197.56-.284.844a7.996 7.996 0 0 0-.166.909c-.061.609-.05 1.237.049 1.809.189 1.162.632 2.12 1.109 2.891a11.265 11.265 0 0 0 1.529 1.942c1.056 1.082 2.127 1.88 3.194 2.6a33.287 33.287 0 0 0 3.21 1.855c2.142 1.093 4.284 1.979 6.434 2.774a98.121 98.121 0 0 0 6.464 2.112c.511.147 1.018.291 1.529.435a36.8 36.8 0 0 0-4.458 7.089v.004c-1.908-2.014-3.876-3.997-6.022-5.931a52.386 52.386 0 0 0-3.471-2.888 31.347 31.347 0 0 0-2.028-1.408 17.575 17.575 0 0 0-2.574-1.378 11.177 11.177 0 0 0-1.888-.616c-.761-.16-1.73-.31-3.02-.107a6.543 6.543 0 0 0-1.007.254 6.508 6.508 0 0 0-2.79 1.84 6.7 6.7 0 0 0-.594.783c-.083.129-.174.269-.238.39a7.248 7.248 0 0 0-.681 1.692 9.383 9.383 0 0 0-.3 2.02c-.022.584 0 1.09.038 1.568.084.953.231 1.786.401 2.577l.39 1.764c.027.14.065.268.087.408l.057.428.121.855.065.428.033.443.072.886c.061.586.061 1.196.076 1.801.05 2.426-.11 4.92-.435 7.407a50.6 50.6 0 0 1-1.503 7.35c-.17.594-.367 1.17-.548 1.76a55.283 55.283 0 0 1-.632 1.684l-.352.791c-.061.129-.114.276-.178.39l-.193.356-.186.355c-.064.121-.129.246-.193.326-.129.185-.257.375-.378.575l-.303.485a2.813 2.813 0 0 0 4.462 3.387c.295-.322.59-.655.878-.988.155-.17.265-.333.382-.496l.349-.488.344-.492c.117-.166.2-.325.303-.492l.583-.98a53.92 53.92 0 0 0 1.018-1.964c.295-.659.61-1.321.89-1.984a58.231 58.231 0 0 0 2.69-8.114 58.405 58.405 0 0 0 1.51-8.493c.068-.73.152-1.454.167-2.203l.045-1.12.02-.56-.012-.568-.004-.205c.167.186.333.371.496.557 1.608 1.84 3.179 3.838 4.708 5.889a181.94 181.94 0 0 1 4.481 6.328c.14.2.311.428.477.617.284.33.594.62.924.874 0 .216.003.424.015.636-2.661 2.861-5.265 5.821-7.748 9.034-1.567 2.06-3.096 4.19-4.485 6.715-.685 1.267-1.347 2.645-1.854 4.363-.246.879-.454 1.851-.496 3.02l-.007.44.022.473c.012.159.02.314.038.477.023.166.05.337.076.503.113.666.333 1.385.65 2.07.16.337.356.67.557.992.212.299.44.613.681.878a8.075 8.075 0 0 0 1.54 1.328c1.05.697 2.04 1.06 2.938 1.31 1.79.466 3.292.519 4.723.507 2.842-.053 5.367-.48 7.853-.98 4.943-1.022 9.618-2.434 14.243-3.948a2.845 2.845 0 0 0 1.911-3.236 2.842 2.842 0 0 0-3.323-2.267h-.015c-4.648.878-9.322 1.635-13.864 1.965-2.252.155-4.511.208-6.46-.027a10.954 10.954 0 0 1-1.685-.322c.004-.015.012-.026.015-.037.133-.273.322-.606.534-.954.235-.36.477-.73.768-1.117 1.14-1.548 2.619-3.164 4.183-4.723a83.551 83.551 0 0 1 2.585-2.468 35.897 35.897 0 0 0 2.312 4.16c.125.2.261.405.397.602 3.747-.413 7.415-1.06 10.356-1.617l.037-.007a7.47 7.47 0 0 1 8.702 5.957 7.491 7.491 0 0 1-4.724 8.38C132.172 94.372 138.542 96 145.313 96c20.358 0 37.087-14.708 38.994-33.514.193-.05.386-.098.576-.144a23.261 23.261 0 0 1 2.354-.458c.726-.102 1.393-.14 1.847-.125.125-.004.193.015.299.012.03.003.064.007.098.007h.053c.008.004.015.004.027.004.106 0 .094-.019.09-.068-.007-.05-.022-.125.019-.117.038.007.125.083.216.26.087.19.186.443.269.761.079.33.159.69.219 1.102.129.806.216 1.745.307 2.725.091.984.178 2.02.306 3.1.262 2.138.682 4.435 1.533 6.683.837 2.245 2.154 4.406 3.812 6.15.825.871 1.725 1.655 2.66 2.336.943.677 1.919 1.26 2.911 1.782a2.848 2.848 0 0 0 3.641-.874 2.848 2.848 0 0 0-.674-3.966" fill="#0398F3"></path><g clip-path="url(#beholder-eye-socket-clip-path)"><circle cx="137.5" cy="60" r="7" fill="#1B9AF0"><animateMotion dur="2.3s" repeatCount="indefinite"><mpath xlink:href="#beholder-eye-move-path"></mpath></animateMotion></circle></g></g><g class="beholder-dm-screen__screen"><path fill="#EAEEF0" stroke="#fff" stroke-width="6" stroke-linecap="round" stroke-linejoin="round" d="M76 76h136v97H76z"></path><path d="M218 170.926V74.282l64-35.208v96.644l-64 35.208zM70 171.026V74.318L3 38.974v96.708l67 35.344z" fill="#F3F6F9" stroke="#fff" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"></path></g></svg>
<div class="loading-status-indicator__subtext">${subtext}</div>
</div>
`);
this.container.find(".sidebar-panel-loading-indicator").remove(); // just in case there was already one shown we don't want to add a second one
this.container.append(loadingIndicator);
}
remove_sidebar_loading_indicator() {
$(`#${this.id} .sidebar-panel-loading-indicator`).animate({
"left": "400px"
}, 500, function() {
$(".sidebar-panel-loading-indicator").remove();
});
}
//#endregion Class functions
//#region UI Construction
build() {
let panelContainer = $(`
<div id='${this.id}' class='sidebar-panel-content'>
<div class="sidebar-panel-header">
<div class="sidebar-panel-header-title"></div>
<div class="sidebar-panel-header-subtitle"></div>
<div class="sidebar-panel-header-explanation"></div>
</div>
<div class="sidebar-panel-body"></div>
<div class="sidebar-panel-footer">
<div class="footer-input-wrapper"></div>
</div>
</div>
`);
if (this.is_modal) {
let closeButton = build_close_button();
closeButton.click(close_sidebar_modal);
panelContainer.find(".sidebar-panel-header").prepend(closeButton);
let modalWrapper = this.build_modal_wrapper();
modalWrapper.append(panelContainer)
return modalWrapper;
} else {
return panelContainer;
}
}
build_modal_wrapper() {
return $(`
<div class="sidebar-modal">
<div class="sidebar-modal-background"></div>
</div>
`);
}
// imageUrlEntered is a function that takes a string in the form of a url
build_image_url_input(titleText, imageUrlEntered) {
if (typeof imageUrlEntered !== 'function') {
imageUrlEntered = function(newImageUrl) {
console.warn(`Failed to provide a valid function to handle ${newImageUrl}`);
};
}
return build_text_input_wrapper(
titleText,
`<input title="${titleText}" placeholder="https://..." name="addCustomImage" type="text" />`,
`<button>Add</button>`,
async function(imageUrl, input, event) {
if(imageUrl.startsWith("data:")){
alert("You cannot use urls starting with data:");
} else {
let imageUrlSplit = imageUrl.split(', ');
for(let i = 0; i < imageUrlSplit.length; i++){
imageUrlEntered(await parse_img(imageUrlSplit[i]));
}
}
}
);
}
}
function build_close_button() {
let closeButton = $(`<button class="ddbeb-modal__close-button qa-modal_close" title="Close Modal"><svg class="" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><g transform="rotate(-45 50 50)"><rect x="0" y="45" width="100" height="10"></rect></g><g transform="rotate(45 50 50)"><rect x="0" y="45" width="100" height="10"></rect></g></svg></button>`);
closeButton.css({
"align-items": "center",
"-webkit-appearance": "none",
"-o-appearance": "none",
"appearance": "none",
"background": "none",
"border": 0,
"display": "flex",
"height": "20px",
"justify-content": "center",
"padding": 0,
"position": "absolute",
"right": "4px",
"top": "4px",
"width": "20px",
"z-index": 1
});
return closeButton;
}
/**
* @param titleText {string} the text to be displayed above the input
* @param input {string} the text input to build a wrapper for. eg: `<input type="text" name="someUniqueName" title="this is an input" placeholder="https://..." />`
* @param sideButton {string|undefined} the button to the right of the label, undefined if you don't want a button. eg: `<button>Add</button>`
* @param inputSubmitCallback {function|undefined} the function to be called when the user presses enter, or clicks the button. function(inputValue, input, event) { ... }
* @param submitOnFocusout {boolean|undefined} whether to call inputSubmitCallback when the input loses focus
* @returns {*|jQuery|HTMLElement}
*/
function build_text_input_wrapper(titleText, input, sideButton, inputSubmitCallback, submitOnFocusout = true) {
let inputLabel = $(`<div class="token-image-modal-footer-title">${titleText}</div>`);
let textInput = $(input);
let submitButton = (sideButton !== undefined && sideButton.length > 0) ? $(sideButton) : $(`<button style="display:none;">Add</button>`);
submitButton.addClass("sidebar-panel-footer-button token-image-modal-add-button");
if (typeof inputSubmitCallback !== 'undefined') {
textInput.on('keyup', function(event) {
let inputValue = event.target.value;
if (event.key === "Enter" && inputValue !== undefined && inputValue.length > 0) {
inputSubmitCallback(inputValue, $(event.target), event);
} else if (event.key === "Escape") {
$(event.target).blur();
}
});
if (submitOnFocusout) {
textInput.on('focusout', function(event) {
let inputValue = event.target.value;
if (inputValue !== undefined && inputValue.length > 0) {
inputSubmitCallback(inputValue, $(event.target), event);
}
});
}
let inputName = textInput.attr("name");
submitButton.on("click", function(event) {
let inputElement = $(event.target).closest(".token-image-modal-url-label-add-wrapper").find(`input[name="${inputName}"]`);
let inputValue = inputElement[0].value;
if (inputElement.length > 0 && inputValue !== undefined && inputValue.length > 0) {
inputSubmitCallback(inputValue, inputElement, event);
inputElement.val("")
}
});
}
/* This is the general layout of what we're building. A label above an input, both of which are to the left of an "Add" button that spans the entire height
|--------------------|
| Label | Add |
| Input | Button |
|--------------------|
*/
let labelAndUrlWrapper = $(`<div class="token-image-modal-url-label-wrapper"></div>`); // this is to keep the label and input stacked vertically
labelAndUrlWrapper.append(inputLabel); // label above input
labelAndUrlWrapper.append(textInput); // input below label
let addButtonAndLabelUrlWrapper = $(`<div class="token-image-modal-url-label-add-wrapper"></div>`); // this is to keep the add button on the right side of the label and input
addButtonAndLabelUrlWrapper.append(labelAndUrlWrapper); // label/input on the left
addButtonAndLabelUrlWrapper.append(submitButton); // add button on the right
return addButtonAndLabelUrlWrapper;
}
function build_select_input(labelText, input) {
let wrapper = $(`
<div class="token-image-modal-footer-select-wrapper">
<div class="token-image-modal-footer-title">${labelText}</div>
</div>
`);
wrapper.append(input);
return wrapper;
}
function update_hover_text(hoverElement, hoverText) {
if (hoverText !== undefined && hoverText.length > 0) {
hoverElement.addClass("sidebar-hover-text");
hoverElement.attr("data-hover", hoverText);
let hoverFlyout = $(".sidebar-flyout .sidebar-hover-text-flyout");
if (hoverFlyout.length > 0) {
// update the flyout text and reposition it
let flyout = hoverFlyout.closest(".sidebar-flyout");
let previousWidth = flyout.width();
hoverFlyout.text(hoverText);
let newWidth = flyout.width();
let oldPosition = flyout.position().left;
flyout.css("left", oldPosition + (previousWidth - newWidth));
}
} else {
hoverElement.removeClass("sidebar-hover-text");
hoverElement.removeAttr("data-hover");
}
}
/// changeHandler: function(name, newValue) // newValue will be one of [true, false, undefined], where `undefined` means "default"
function build_token_option_select_input(option, currentValue, changeHandler) {
if (typeof changeHandler !== 'function') {
changeHandler = function(){};
}
let wrapper = $(`
<div class="token-image-modal-footer-select-wrapper">
<div class="token-image-modal-footer-title">${option.label}</div>
</div>
`);
let inputElement = $(`
<select name="${option.name}">
<option value="default">Default</option>
<option value="disabled">${option.disabledValue}</option>
<option value="enabled">${option.enabledValue}</option>
</select>
`);
wrapper.append(inputElement);
// explicitly look for true/false because the default value is undefined
if (currentValue === true) {
inputElement.val("enabled");
update_hover_text(wrapper, option.enabledDescription);
} else if (currentValue === false) {
inputElement.val("disabled");
update_hover_text(wrapper, option.disabledDescription);
} else {
inputElement.val("default");
if (window.TOKEN_SETTINGS[option.name] === true) {
update_hover_text(wrapper, option.enabledDescription);
} else {
update_hover_text(wrapper, option.disabledDescription);
}
}
inputElement.change(function (event) {
console.log("update", event.target.name, "to", event.target.value);
if (event.target.value === "enabled") {
changeHandler(option.name, true);
update_hover_text(wrapper, option.enabledDescription);
} else if (event.target.value === "disabled") {
changeHandler(option.name, false);
update_hover_text(wrapper, option.disabledDescription);
} else {
changeHandler(option.name, undefined);
if (window.TOKEN_SETTINGS[option.name] === true) {
update_hover_text(wrapper, option.enabledDescription);
} else {
update_hover_text(wrapper, option.disabledDescription);
}
}
});
return wrapper;
}
function build_toggle_input(settingOption, currentValue, changeHandler) {
if (typeof changeHandler !== 'function') {
changeHandler = function(){};
}
let wrapper = $(`
<div class="token-image-modal-footer-select-wrapper" data-option-name="${settingOption.name}">
<div class="token-image-modal-footer-title">${settingOption.label}</div>
</div>
`);
let input = $(`<button name="${settingOption.name}" type="button" role="switch" class="rc-switch"><span class="rc-switch-inner"></span></button>`);
if (currentValue === null) {
input.addClass("rc-switch-unknown");
update_hover_text(wrapper, "This has multiple values. Clicking this will enable it for all.");
} else {
const currentlySetOption = settingOption.options.find(o => o.value === currentValue) || settingOption.options.find(o => o.value === settingOption.defaultValue);
if(currentlySetOption.value == true){
input.addClass("rc-switch-checked");
}
update_hover_text(wrapper, currentlySetOption?.description);
}
wrapper.append(input);
input.click(function(clickEvent) {
if ($(clickEvent.currentTarget).hasClass("rc-switch-checked")) {
// it was checked. now it is no longer checked
$(clickEvent.currentTarget).removeClass("rc-switch-checked");
changeHandler(settingOption.name, false);
const disabledOption = settingOption.options.find(o => o.value === false);
update_hover_text(wrapper, disabledOption?.description);
} else {
// it was not checked. now it is checked
$(clickEvent.currentTarget).removeClass("rc-switch-unknown");
$(clickEvent.currentTarget).addClass("rc-switch-checked");
changeHandler(settingOption.name, true);
const disabledOption = settingOption.options.find(o => o.value === true);
update_hover_text(wrapper, disabledOption?.description);
}
});
return wrapper;
}
function build_dropdown_input(settingOption, currentValue, changeHandler) {
if (typeof changeHandler !== 'function') {
changeHandler = function(){};
}
let wrapper = $(`
<div class="token-image-modal-footer-select-wrapper" data-option-name="${settingOption.name}">
<div class="token-image-modal-footer-title">${settingOption.label}</div>
</div>
`);
let input = $(`<select name="${settingOption.name}"></select>`);
wrapper.append(input);
for (const option of settingOption.options) {
input.append(`<option value="${option.value}">${option.label}</option>`);
}
if (currentValue !== undefined) {
input.find(`option[value='${currentValue}']`).attr('selected','selected');
} else {
input.find(`option[value='${settingOption.defaultValue}']`).attr('selected','selected');
}
const currentlySetOption = settingOption.options.find(o => o.value === currentValue) || settingOption.options.find(o => o.value === settingOption.defaultValue);
update_hover_text(wrapper, currentlySetOption?.description);
input.change(function(event) {
let newValue = event.target.value;
changeHandler(settingOption.name, newValue);
const updatedOption = settingOption.options.find(o => o.value === newValue) || settingOption.options.find(o => o.value === settingOption.defaultValue);
update_hover_text(wrapper, updatedOption?.description);
update_token_base_visibility(wrapper.parent());
});
return wrapper;
}
//#endregion UI Construction
/**
* A ViewModel that represents a some object (or folder) listed in the sidebar such as a token.
* This is a transient object and is not intended to be used as a data source. Instead, each {type} determines where the data source is.
* This is not a token that has been placed on a scene; that is an instance of {Token} and is
* represented as a many {Token} to one {SidebarListItem} relationship.
* For example:
* This could represent a "Goblin" monster. Placing this on the scene several times would create several {Token} instances.
* Each of those {Token}s would be of type "monster".
*/
class SidebarListItem {
/** Do not call this directly! It is a generic constructor for a SidebarListItem. Use one of the static functions instead.
* @param id {string} a unique identifier for the backing item
* @param name {string} the name displayed to the user
* @param image {string} the src of the img tag
* @param type {string} the type of item this represents. One of [folder, myToken, monster, pc]
* @param folderPath {string} the folder this item is in
* @param parentId {string|undefined} a string id of the folder this item is in
*/
constructor(id, name, image, type, folderPath = RootFolder.Root.path, parentId = "root", color = undefined) {
this.id = id;
this.name = name;
this.image = image;
this.type = type;
this.folderPath = sanitize_folder_path(folderPath);
this.parentId = parentId;
this.color = color;
}
/**
* Creates a Folder list item.
* @param id {string} a unique identifier for the backing item
* @param folderPath {string} the path that the folder is in (not including the name of this folder)
* @param name {string} the name of the folder
* @param collapsed {boolean} whether or not the folder is open or closed.
* @param parentId {string|undefined} a string id of the folder this item is in
* @param folderType {string} the ItemType that this folder contains
* @returns {SidebarListItem} the list item this creates
*/
static Folder(id, folderPath, name, collapsed, parentId, folderType, color = '#F4B459') {
console.debug(`SidebarListItem.Folder folderPath: ${folderPath}, name: ${name}, collapsed: ${collapsed}, id: ${id}, parentId: ${parentId}, folderType: ${folderType}`);
if(parentId == undefined && folderPath == RootFolder.Scenes.path){
parentId = RootFolder.Scenes.id
}
let item = new SidebarListItem(id, name, `${window.EXTENSION_PATH}assets/folder.svg`, ItemType.Folder, folderPath, parentId, color);
if (collapsed === true || collapsed === false) {
item.collapsed = collapsed;
} else {
item.collapsed = true;
}
item.folderType = folderType
return item;
}
/**
* Creates a "My Token" list item.
* @param tokenCustomization {TokenCustomization} an object that represents the "My Token". The object is an updated version of legacy tokendata objects, and mostly translates to the {Token}.options object
* @returns {SidebarListItem} the list item this creates
* @constructor
*/
static MyToken(tokenCustomization) {
console.debug("SidebarListItem.MyToken", tokenCustomization);
let image = "";
if (typeof tokenCustomization.tokenOptions?.alternativeImages === "object" && tokenCustomization.tokenOptions.alternativeImages.length > 0) {
image = tokenCustomization.tokenOptions.alternativeImages[0];
}
return new SidebarListItem(
tokenCustomization.id,
tokenCustomization.tokenOptions.name,
image,
ItemType.MyToken,
tokenCustomization.folderPath(),
tokenCustomization.parentId
);
}
/**
* Creates a Builtin list item.
* @param tokenData {object} an object that represents the "Builtin Token". The object is an updated version of legacy tokendata objects, and mostly translates to the {Token}.options object
* @param parentId {string|undefined} a string id of the folder this item is in
* @returns {SidebarListItem} the list item this creates
* @constructor
*/
static BuiltinToken(tokenData) {
console.debug("SidebarListItem.BuiltinToken", tokenData);
let folderPath = sanitize_folder_path(`${RootFolder.AboveVTT.path}/${tokenData.folderPath}`);
let item = new SidebarListItem(path_to_html_id(folderPath, tokenData.name), tokenData.name, tokenData.image, ItemType.BuiltinToken, folderPath, path_to_html_id(folderPath));
item.tokenOptions = tokenData;
return item
}
static DDBToken(tokenData) {
console.debug("SidebarListItem.DDBToken", tokenData);
let folderPath = sanitize_folder_path(`${RootFolder.DDB.path}/${tokenData.folderPath}`);
let item = new SidebarListItem(path_to_html_id(folderPath, tokenData.name), tokenData.name, tokenData.alternativeImages[0], ItemType.DDBToken, folderPath, path_to_html_id(folderPath));
item.tokenOptions = tokenData;
return item
}
/**
* Creates a Monster list item.
* @param monsterData {object} the object returned by the DDB API call that searches for monsters
* @returns {SidebarListItem} the list item this creates
* @constructor
*/
static Monster(monsterData) {
console.debug("SidebarListItem.Monster", monsterData);
let item = new SidebarListItem(monsterData.id, monsterData.name, monsterData.avatarUrl, ItemType.Monster, RootFolder.Monsters.path, RootFolder.Monsters.id);
item.monsterData = monsterData;
return item;
}
/**
* Creates a Monster list item.
* @param monsterData {object} the object returned by the DDB API call that searches for monsters
* @returns {SidebarListItem} the list item this creates
* @constructor
*/
static open5eMonster(monsterData) {
console.debug("SidebarListItem.Monster", monsterData);
if(monsterData.img_main == null || monsterData.img_main == "http://api.open5e.com/"){
monsterData.img_main = 'https://www.dndbeyond.com/avatars/4675/675/636747837794884984.jpeg'
}
let item = new SidebarListItem(monsterData.slug, monsterData.name, monsterData.img_main, ItemType.Open5e, RootFolder.Open5e.path, RootFolder.Open5e.id);
item.monsterData = monsterData;
return item;
}
/**
* Creates a PC list item.
* @param sheet {string} the url path for the character that this represents
* @param name {string} the name of the character
* @param image {string} the url for the image of this character
* @returns {SidebarListItem} the list item this creates
* @constructor
*/
static PC(sheet, name, image) {
console.debug("SidebarListItem.PC", sheet, name, image);
let item = new SidebarListItem(sheet, name, image, ItemType.PC, RootFolder.Players.path, RootFolder.Players.id);
item.sheet = sheet;
return item;
}
/**
* Creates a Encounter list item. These act like folders but with extra behaviors specific to Encounters
* @param encounter {object} the Encounter object this item represents. These are stored in window.EncounterHandler.encounters
* @param collapsed {boolean} whether or not the folder is open or closed. defaults to true
* @returns {SidebarListItem} the list item this creates
* @constructor
*/
static Encounter(encounter, collapsed = true) {
let name = "Untitled Encounter";
if ((typeof encounter.name == 'string') && encounter.name.length > 0) {
name = encounter.name;
}
console.debug(`SidebarListItem.Encounter ${RootFolder.Encounters.path}/${name}, collapsed: ${collapsed}`);
let item = new SidebarListItem(encounter.id, name, `${window.EXTENSION_PATH}assets/folder.svg`, ItemType.Encounter, RootFolder.Encounters.path, RootFolder.Encounters.id);
if ((typeof encounter.flavorText == 'string') && encounter.flavorText.length > 0) {
item.description = encounter.flavorText;
}
item.collapsed = collapsed;
item.encounterId = encounter.id;
return item;
}
static Scene(sceneData) {
let name = "Untitled Scene";
if ((typeof sceneData.title == 'string') && sceneData.title.length > 0) {
name = sceneData.title;
}
let folderPath = folder_path_of_scene(sceneData);
let parentId = sceneData.parentId || RootFolder.Scenes.id;
let item = new SidebarListItem(sceneData.id, name, sceneData.player_map, ItemType.Scene, folderPath, parentId);
console.debug(`SidebarListItem.Scene ${item.fullPath()}`);
item.isVideo = sceneData.player_map_is_video == "1"; // explicity using `==` instead of `===` in case it's ever `1` or `"1"`
return item;
}
// size is number of squares, not feet
static Aoe(shape, size, style, name=undefined) {
if (typeof name !== "string" || name.length === 0) {
name = `${shape} AoE`;
}
const image = `class=aoe-token-tileable aoe-style-${style} aoe-shape-${shape} ${name ? set_spell_override_style(name) : ""}`
let item = new SidebarListItem(path_to_html_id(RootFolder.Aoe.path, name), name, image, ItemType.Aoe, RootFolder.Aoe.path, RootFolder.Aoe.id);
console.debug(`SidebarListItem.Aoe`, item);
item.shape = shape;
let parsedSize = parseInt(size);
if (isNaN(parsedSize)) {
item.size = parsedSize;
} else {
item.size = 1;
}
item.style = style;
return item;
}
/**
* A comparator for sorting by folder, then alphabetically.
* @param lhs {SidebarListItem}
* @param rhs {SidebarListItem}
* @returns {number}
*/
static sortComparator(lhs, rhs) {
// always folders before tokens
if (lhs.isTypeFolder() && !rhs.isTypeFolder()) { return -1; }
if (!lhs.isTypeFolder() && rhs.isTypeFolder()) { return 1; }
// alphabetically by name
if (lhs.name.toLowerCase() < rhs.name.toLowerCase()) { return -1; }
if (lhs.name.toLowerCase() > rhs.name.toLowerCase()) { return 1; }
// equal
return 0;
}
/**
* A comparator for sorting by folder depth, then by folder, then alphabetically.
* @param lhs {SidebarListItem}
* @param rhs {SidebarListItem}
* @returns {number}
*/
static folderDepthComparator(lhs, rhs) {
if (lhs.isTypeFolder() && rhs.isTypeFolder()) {
if (lhs.folderDepth() < rhs.folderDepth()) { return -1; }
if (lhs.folderDepth() > rhs.folderDepth()) { return 1; }
}
return SidebarListItem.sortComparator(lhs, rhs);
}
/** @returns {string} path + name */
fullPath() {
return sanitize_folder_path(`${this.folderPath}/${this.name}`);
}
/** @returns {boolean} whether or not this item represents a Folder */
isTypeFolder() { return this.type === ItemType.Folder }
/** @returns {boolean} whether or not this item represents a Folder meant for holding Scenes */
isTypeSceneFolder() { return this.type === ItemType.Folder && this.folderType === ItemType.Scene }
/** @returns {boolean} whether or not this item represents a Folder meant for holding Scenes */
isTypeMyTokenFolder() { return this.type === ItemType.Folder && this.folderType === ItemType.MyToken }
/** @returns {boolean} whether or not this item represents a "My Token" */
isTypeMyToken() { return this.type === ItemType.MyToken }
/** @returns {boolean} whether or not this item represents a Player */
isTypePC() { return this.type === ItemType.PC }
/** @returns {boolean} whether or not this item represents a Monster */
isTypeMonster() { return this.type === ItemType.Monster }
/** @returns {boolean} whether or not this item represents a Monster */
isTypeOpen5eMonster() { return this.type === ItemType.Open5e }
/** @returns {boolean} whether or not this item represents a Builtin Token */
isTypeBuiltinToken() { return this.type === ItemType.BuiltinToken }
/** @returns {boolean} whether or not this item represents a Builtin Token */
isTypeDDBToken() { return this.type === ItemType.DDBToken }
/** @returns {boolean} whether or not this item represents an Encounter */
isTypeEncounter() { return this.type === ItemType.Encounter }
/** @returns {boolean} whether or not this item represents a Scene */
isTypeScene() { return this.type === ItemType.Scene }
/** @returns {boolean} whether or not this item represents an AoE */
isTypeAoe() { return this.type === ItemType.Aoe }
/** @returns {boolean} whether or not this item is listed in the tokens panel */
isTokensPanelItem() {
if (this.isTypeFolder()) {
if (this.folderPath === RootFolder.Root.path) {
return this.name === RootFolder.Players.name || this.name === RootFolder.Monsters.name || this.name === RootFolder.MyTokens.name || this.name === RootFolder.AboveVTT.name || this.name === RootFolder.Encounters.name;
} else {
return this.folderPath.startsWith(RootFolder.Players.path) || this.folderPath.startsWith(RootFolder.Monsters.path) || this.folderPath.startsWith(RootFolder.MyTokens.path) || this.folderPath.startsWith(RootFolder.AboveVTT.path) || this.folderPath.startsWith(RootFolder.Encounters.path);
}
}
return this.isTypeMyToken() || this.isTypePC() || this.isTypeMonster() || this.isTypeBuiltinToken() || this.isTypeDDBToken()
}
/** @returns {boolean} whether or not this item represents an object that can be edited by the user */
canEdit() {
switch (this.type) {
case ItemType.Folder:
return true;
case ItemType.MyToken:
case ItemType.PC:
case ItemType.Monster:
case ItemType.Open5e:
case ItemType.Encounter:
case ItemType.Scene:
case ItemType.Aoe:
return true;
case ItemType.BuiltinToken:
default:
return false;
}
}
/** @returns {boolean} whether or not this item represents an object that can be deleted by the user */
canDelete() {
switch (this.type) {
case ItemType.Folder:
switch (this.folderType) {
case ItemType.MyToken:
case ItemType.Scene:
return true;
default:
return false;
}
case ItemType.MyToken:
case ItemType.Scene:
return true;
case ItemType.PC:
case ItemType.Monster:
case ItemType.isTypeOpen5eMonster:
case ItemType.BuiltinToken:
case ItemType.Encounter: // we technically could support this, but I don't think we should
case ItemType.Aoe: // we technically could support this, but I don't think we should
default:
return false;
}
}
/** @returns {number} how deeply nested is this object */
folderDepth() {
return this.fullPath().split("/").length;
}
isRootFolder() {
// "/foo".split("/").length === 2; if the logic in folderDepth() changes, make sure this changes, too
return this.folderDepth() === 2;
}
/** @returns {string} the name of the folder that contains this item. Returns an empty string if the item is not in any folder */
containingFolderName() {
let folderParts = this.folderPath.split("/");
if (folderParts === undefined || folderParts.length === 0) {
return "";
}
return folderParts[folderParts.length - 1];
}
/** @returns {boolean} true if the name partially matches the searchTerm or if the containing folder name partially matches the searchTerm */
nameOrContainingFolderMatches(searchTerm) {
if (typeof this.name !== "string") return false;
let fullPath = this.fullPath().replace(/^(\/Scenes)/i, '')
return fullPath.match(new RegExp(searchTerm, 'i')) != null;
}
}
/**
* @param dirtyPath {string} the path to sanitize
* @returns {string} the sanitized path
*/
function sanitize_folder_path(dirtyPath) {
if (dirtyPath === undefined) {
return RootFolder.Root.path;
}
let cleanPath = dirtyPath.replaceAll("///", "/").replaceAll("//", "/");
// remove trailing slashes before adding one at the beginning. Otherwise, we return an empty string
if (cleanPath.endsWith("/")) {
cleanPath = cleanPath.slice(0, -1);
}
if (!cleanPath.startsWith("/")) {
cleanPath = `/${cleanPath}`;
}
return cleanPath;
}
/**
* @param html {*|jQuery|HTMLElement} the html representation of the item
* @returns {SidebarListItem|undefined} SidebarListItem.Aoe if found, else undefined
*/
function is_html_aoe(html) {
let shape = html.attr("data-shape");
let style = html.attr("data-style");
let size = html.attr("data-size");
if (shape && style && size) { // TODO: be more thorough with data validation here
return SidebarListItem.Aoe(shape, size, style);
}
return undefined
}
/**
* @param html {*|jQuery|HTMLElement} the html representation of the item
* @returns {SidebarListItem|undefined} SidebarListItem if found, else undefined
*/
function find_sidebar_list_item(html) {
if (html === undefined) return undefined;
let foundItem;
let encounterId = html.attr("data-encounter-id");
if (encounterId !== undefined && encounterId !== null && encounterId !== "") {
foundItem = window.tokenListItems.find(item => item.isTypeEncounter() && item.encounterId === encounterId);
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
}
let sceneId = html.attr("data-scene-id");
if (typeof sceneId === "string" && sceneId.length > 0) {
foundItem = window.sceneListItems.find(item => item.id === sceneId);
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
}
if (html.attr("data-monster") !== undefined) {
// explicitly using '==' instead of '===' to allow (33253 == '33253') to return true
foundItem = window.monsterListItems.find(item => item.monsterData.id == html.attr("data-monster"));
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
}
let htmlId = html.attr("data-id") || html.attr("id");
if (typeof htmlId === "string" && htmlId.length > 0) {
foundItem = window.tokenListItems.find(li => li.id === htmlId);
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
foundItem = window.sceneListItems.find(li => li.id === htmlId);
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
foundItem = window.sceneListFolders.find(li => li.id === htmlId);
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
// explicitly using '==' instead of '===' to allow (33253 == '33253') to return true
foundItem = window.monsterListItems.find(item => item.monsterData.id == html.attr("data-monster"));
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
foundItem = window.open5eListItems.find(item => item.id == htmlId);
if (foundItem !== undefined) {
console.log('find_sidebar_list_item', foundItem);
return foundItem;
}
}
let fullPath = harvest_full_path(html);
return find_sidebar_list_item_from_path(fullPath);
}
/**
* @param fullPath {string} the full path of the item
* @returns {SidebarListItem|undefined} SidebarListItem if found, else undefined
*/
function find_sidebar_list_item_from_path(fullPath) {
const matchingPath = function(item) { return item.fullPath() === fullPath };
let foundItem;
if (fullPath.startsWith(RootFolder.Scenes.path)) {
foundItem = window.sceneListItems?.find(matchingPath);
console.log('find_sidebar_list_item_from_path sceneListItems', foundItem);
if (foundItem === undefined) {
foundItem = window.sceneListFolders?.find(matchingPath);
console.log('find_sidebar_list_item_from_path sceneListFolders', foundItem);
}
if (foundItem !== undefined) {
console.log('find_sidebar_list_item_from_path', foundItem);
return foundItem;
}
}
// check all the tokens items
foundItem = tokens_rootfolders.find(matchingPath);
if (foundItem === undefined) {
foundItem = window.tokenListItems?.find(matchingPath);
}
if (foundItem === undefined) {
foundItem = window.monsterListItems?.find(matchingPath);
}
if (foundItem === undefined) {
foundItem = Object.values(cached_monster_items).find(matchingPath);
}
if (foundItem === undefined) {
console.warn(`find_sidebar_list_item found nothing at path: ${fullPath}`);
}
console.log('find_sidebar_list_item_from_path', foundItem);
return foundItem;
}
/**
* locates the html the given item represents in the list of items
* @param item {SidebarListItem} the item to find in the list
* @param container {HTMLElement|undefined} the specific HTML element to search within. Example: {tokensPanel.body}
* @returns {*|jQuery|HTMLElement} the row that corresponds to the {item} you're looking for
*/
function find_html_row(item, container) {
if (item === undefined) return undefined;
if (item.isTypeMonster()) {
return container?.find(`[data-monster='${item.monsterData.id}']`);
}
return find_html_row_from_path(item.fullPath(), container);
}
/**
*
* @param fullPath {string} the full path of the item you're looking for
* @param container {HTMLElement|undefined} the specific HTML element to search within. Example: {tokensPanel.body}
* @returns {*|jQuery|HTMLElement} the row that corresponds to the {item} you're looking for
*/
function find_html_row_from_path(fullPath, container) {
if (fullPath === undefined) return undefined;
return container?.find(`[data-full-path='${encode_full_path(fullPath)}']`);
}
/**
* decodes and returns the path of the item this HTML element represents
* @param htmlRow {*|jQuery|HTMLElement} the html that corresponds to an item (like a row in the list of tokens)
* @returns {string|string|*} the full path of the given element represents
*/
function harvest_full_path(htmlRow) {
if (htmlRow === undefined) return "";
return decode_full_path(htmlRow.attr("data-full-path"));
}
/**