-
Notifications
You must be signed in to change notification settings - Fork 5
/
render.js
1556 lines (1386 loc) · 51.1 KB
/
render.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
import sha1 from 'sha1'
import { index_name } from './state'
import { index_regex } from './state'
import { Clipboard } from './clipboard'
import infovis from './infovis'
// Round to one decimal place
const round1 = function(n) {
return Math.round(n * 10) / 10;
};
// Round to four decimal places
export const round4 = function(n) {
const N = Math.pow(10, 4);
return Math.round(n * N) / N;
};
// Remove keys with undefined values
export const remove_undefined = function(o) {
Object.keys(o).forEach(k => {
o[k] == undefined && delete o[k]
});
return o;
};
// Encode arbitrary string in url
export const encode = function(txt) {
return btoa(encodeURIComponent(txt));
};
// Decode arbitrary string from url
export const decode = function(txt) {
try {
return decodeURIComponent(atob(txt));
}
catch (e) {
return '';
}
};
// Remove all children of a DOM node
const clearChildren = function(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
};
// Return object with form values
const parseForm = function(elem) {
const formArray = $(elem).serializeArray();
return formArray.reduce(function(d, i) {
d[i.name] = i.value;
return d;
}, {});
};
// Add or remove a class based on a condition
const classOrNot = function(selector, condition, cls) {
if (condition) {
return $(selector).addClass(cls);
}
return $(selector).removeClass(cls);
};
// Toggle display of none based on condition
const displayOrNot = function(selector, condition) {
classOrNot(selector, !condition, 'd-none');
};
// Set to green or white based on condition
export const greenOrWhite = function(selector, condition) {
classOrNot(selector, condition, 'minerva-green');
classOrNot(selector, !condition, 'minerva-white');
};
// Toggle cursor style based on condition
const toggleCursor = function(selector, cursor, condition) {
if (condition) {
$(selector).css('cursor', cursor);
}
else {
$(selector).css('cursor', 'default');
}
};
// encode a polygon as a URL-safe string
var toPolygonURL = function(polygon){
pointString='';
polygon.forEach(function(d){
pointString += d.x.toFixed(5) + "," + d.y.toFixed(5) + ",";
})
pointString = pointString.slice(0, -1); //removes "," at the end
var result = LZString.compressToEncodedURIComponent(pointString);
return result;
}
// decode a URL-safe string as a polygon
var fromPolygonURL = function(polygonString){
var decompressed = LZString.decompressFromEncodedURIComponent(polygonString);
if (!decompressed){
return [];
}
var xArray = [], yArray = [];
//get all values out of the string
decompressed.split(',').forEach(function(d,i){
if (i % 2 == 0){ xArray.push(parseFloat(d)); }
else{ yArray.push(parseFloat(d)); }
});
//recreate polygon data structure
var newPolygon = [];
if (xArray.length == yArray.length) {
xArray.forEach(function(d, i){
newPolygon.push({x: d, y: yArray[i]});
});
}
return newPolygon;
}
// Download a text file
const download = function(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
// Copy string to clipboard
const ctrlC = function(str) {
Clipboard.copy(str);
};
// return a list of image objects from a flattened layout
export const unpackGrid = function(layout, images, key) {
const image_map = images.reduce(function(o, i) {
i.TileSize = i.TileSize || [1024, 1024];
i.maxLevel = i.maxLevel || 0;
// Add to dictionary by Name
o[i.Name] = i;
return o;
}, {});
return layout[key].map(function(row) {
return row.map(function(image_name) {
return this.image_map[image_name];
}, {image_map: image_map});
}, {image_map: image_map});
};
// Create a button to copy hash state yaml to clipboard
const newCopyYamlButton = function(THIS) {
const copy_pre = 'Copy to Clipboard';
const copy_post = 'Copied';
$(this).tooltip({
title: copy_pre
});
$(this).on('relabel', function(event, message) {
$(this).attr('data-original-title', message).tooltip('show');
});
$(this).click(function() {
$(this).trigger('relabel', [copy_post]);
ctrlC(THIS.hashstate.bufferYaml);
setTimeout((function() {
$(this).trigger('relabel', [copy_pre]);
}).bind(this), 1000);
return false;
});
};
// Create a button to copy form data to clipboard
const newCopyButton = function() {
const copy_pre = 'Copy to Clipboard';
const copy_post = 'Copied';
$(this).tooltip({
title: copy_pre
});
$(this).on('relabel', function(event, message) {
$(this).attr('data-original-title', message).tooltip('show');
});
$(this).on('click', function() {
const form = $(this).closest('form');
const formData = parseForm(form);
$(this).trigger('relabel', [copy_post]);
ctrlC(formData.copy_content);
setTimeout(function() {
$(this).trigger('relabel', [copy_pre]);
}, 1000);
return false;
});
};
const updateColor = (group, color, c) => {
group.Colors = group.Colors.map((col, i) => {
return [col, color][+(c === i)];
});
return group;
}
const addChannel = (group, subgroup) => {
return {
...group,
Shown: [...group.Shown, true],
Channels: [...group.Channels, subgroup.Name],
Colors: [...group.Colors, subgroup.Colors[0]],
Descriptions: [...group.Descriptions, subgroup.Description]
};
}
const toggleChannelShown = (group, c) => {
group.Shown = group.Shown.map((show, i) => {
return [show, !show][+(c === i)];
});
return group;
}
// Render the non-openseadragon UI
export const Render = function(hashstate, osd) {
this.trackers = hashstate.trackers;
this.pollycache = hashstate.pollycache;
this.showdown = new showdown.Converter({tables: true});
this.osd = osd;
this.hashstate = hashstate;
};
Render.prototype = {
init: function() {
const isMobile = () => {
const fixed_el = document.querySelector('.minerva-fixed');
return (fixed_el?.clientWidth || 0) <= 750;
}
// Set mobile view
if (isMobile()) {
$(".minerva-legend").addClass("toggled");
$(".minerva-sidebar-menu").addClass("toggled");
}
const HS = this.hashstate;
// Go to true center
HS.newExhibit();
// Read hash
window.onpopstate = (function(e) {
HS.popState(e);
this.loadPolly(HS.waypoint.Description, HS.speech_bucket);
this.newView(true);
}).bind(this);
window.onpopstate();
if (this.edit) {
HS.startEditing();
}
HS.pushState();
window.onpopstate();
// Exhibit name
$('#exhibit-name').text(HS.exhibit.Name);
// Copy buttons
$('.minerva-modal_copy_button').each(newCopyButton);
// Define button tooltips
$('.minerva-zoom-in').tooltip({
title: 'Zoom in'
});
$('.minerva-zoom-out').tooltip({
title: 'Zoom out'
});
$('.minerva-arrow-switch').tooltip({
title: 'Share Arrow'
});
$('.minerva-lasso-switch').tooltip({
title: 'Share Region'
});
$('.minerva-draw-switch').tooltip({
title: 'Share Box'
});
$('.minerva-duplicate-view').tooltip({
title: 'Clone linked view'
});
// Toggle legend info
((k) => {
const el = document.getElementsByClassName(k).item(0);
el.addEventListener('click', () => this.toggleInfo());
})('minerva-channel-legend-info-icon');
const toggleAdding = () => {
HS.toggleAdding();
this.newView(true);
}
const openAdding = () => {
if (HS.infoOpen && !HS.addingOpen) {
toggleAdding();
}
}
// Toggle channel selection
((k) => {
const el = document.getElementsByClassName(k).item(0);
el.addEventListener('click', toggleAdding);
})('minerva-channel-legend-add-panel');
((k) => {
var el = HS.el.getElementsByClassName(k).item(0);
el.addEventListener("click", openAdding);
})('minerva-channel-legend-adding-info-panel');
((k) => {
var el = HS.el.getElementsByClassName(k).item(0);
el.addEventListener("click", openAdding);
})('minerva-channel-legend-adding-panel');
// Modals to copy shareable link and edit description
$('#copy_link_modal').on('hidden.bs.modal', HS.cancelDrawing.bind(HS));
$('.minerva-edit_description_modal').on('hidden.bs.modal', HS.cancelDrawing.bind(HS));
// Button to toggle sidebar
$('.minerva-toggle-sidebar').click((e) => {
e.preventDefault();
$(".minerva-sidebar-menu").toggleClass("toggled");
if (isMobile()) {
if (HS.infoOpen) this.toggleInfo();
$(".minerva-legend").addClass("toggled");
}
});
// Button to toggle legend
$('.minerva-toggle-legend').click((e) => {
e.preventDefault();
$(".minerva-legend").toggleClass("toggled");
const closed = $(".minerva-legend").hasClass("toggled");
if (closed && HS.infoOpen) this.toggleInfo();
if (isMobile()) {
$(".minerva-sidebar-menu").addClass("toggled");
}
});
// Left arrow decreases waypoint by 1
$('.minerva-leftArrow').click(this, function(e) {
const HS = e.data.hashstate;
if (HS.w == 0) {
HS.s = HS.s - 1;
HS.w = HS.waypoints.length - 1;
}
else {
HS.w = HS.w - 1;
}
HS.pushState();
window.onpopstate();
});
// Right arrow increases waypoint by 1
$('.minerva-rightArrow').click(this, function(e) {
const HS = e.data.hashstate;
const last_w = HS.w == (HS.waypoints.length - 1);
if (last_w) {
HS.s = HS.s + 1;
HS.w = 0;
}
else {
HS.w = HS.w + 1;
}
HS.pushState();
window.onpopstate();
});
// Show table of contents
$('.minerva-toc-button').click(this, function(e) {
const HS = e.data.hashstate;
if (HS.waypoint.Mode != 'outline') {
HS.s = 0;
HS.pushState();
window.onpopstate();
}
});
// Clear current editor buffer
$('.clear-switch').click(this, function(e) {
const HS = e.data.hashstate;
HS.bufferWaypoint = undefined;
HS.startEditing();
HS.pushState();
window.onpopstate();
});
// Toggle arrow drawing mode
$('.minerva-arrow-switch').click(this, function(e) {
const HS = e.data.hashstate;
const THIS = e.data;
HS.drawType = "arrow";
if (HS.drawing) {
HS.cancelDrawing(HS);
}
else {
HS.startDrawing(HS);
}
HS.pushState();
THIS.newView(false);
});
// Toggle lasso drawing mode
$('.minerva-lasso-switch').click(this, function(e) {
const HS = e.data.hashstate;
const THIS = e.data;
HS.drawType = "lasso";
if (HS.drawing) {
HS.cancelDrawing(HS);
}
else {
HS.startDrawing(HS);
}
HS.pushState();
THIS.newView(false);
});
// Toggle box drawing mode
$('.minerva-draw-switch').click(this, function(e) {
const HS = e.data.hashstate;
const THIS = e.data;
HS.drawType = "box";
if (HS.drawing) {
HS.cancelDrawing(HS);
}
else {
HS.startDrawing(HS);
}
HS.pushState();
THIS.newView(false);
});
// Handle Z-slider when in 3D mode
var z_legend = HS.el.getElementsByClassName('minerva-depth-legend')[0];
var z_slider = HS.el.getElementsByClassName('minerva-z-slider')[0];
z_slider.max = HS.cgs.length - 1;
z_slider.value = HS.g;
z_slider.min = 0;
// Show z scale bar when in 3D mode
if (HS.design.is3d && HS.design.z_scale) {
z_legend.innerText = round1(HS.g / HS.design.z_scale) + ' μm';
}
else if (HS.design.is3d){
z_legend.innerText = HS.group.Name;
}
// Handle z-slider change when in 3D mode
const THIS = this;
z_slider.addEventListener('input', function() {
HS.g = z_slider.value;
if (HS.design.z_scale) {
z_legend.innerText = round1(HS.g / HS.design.z_scale) + ' μm';
}
else {
z_legend.innerText = HS.group.Name;
}
THIS.newView(true)
}, false);
// Handle submission of description for sharable link
$('.minerva-edit_description_modal form').submit(this, function(e){
const HS = e.data.hashstate;
const formData = parseForm(e.target);
$(this).closest('.modal').modal('hide');
// Get description from form
HS.d = encode(formData.d);
$('.minerva-copy_link_modal').modal('show');
const root = HS.location('host') + HS.location('pathname');
const hash = HS.makeHash(['d', 'g', 'm', 'a', 'v', 'o', 'p']);
const link = HS.el.getElementsByClassName('minerva-copy_link')[0];
link.value = root + hash;
return false;
});
},
toggleInfo() {
const HS = this.hashstate;
HS.toggleInfo();
if (!HS.infoOpen) {
HS.addingOpen = false;
HS.activeChannel = -1;
}
this.newView(true);
},
// Rerender only openseadragon UI or all UI if redraw is true
newView: function(redraw) {
const HS = this.hashstate;
this.osd.newView(redraw);
// Redraw design
if(redraw) {
// redrawLensUI
HS.updateLensUI(null);
// Redraw HTML Menus
this.addChannelLegends();
// Hide group menu if in 3D mode
if (HS.design.is3d) {
$('.minerva-channel-label').hide()
}
// Add group menu if not in 3D mode
else {
this.addGroups();
}
// Add segmentation mask menu
this.addMasks();
// Add stories navigation menu
this.newStories();
// Render editor if edit
if (HS.edit) {
this.fillWaypointEdit();
}
// Render viewer if not edit
else {
this.fillWaypointView();
}
// back and forward buttons
$('.step-back').click(this, function(e) {
const HS = e.data.hashstate;
HS.w -= 1;
HS.pushState();
window.onpopstate();
});
$('.step-next').click(this, function(e) {
const HS = e.data.hashstate;
HS.w += 1;
HS.pushState();
window.onpopstate();
});
// Waypoint-specific Copy Buttons
const THIS = this;
$('.minerva-edit_copy_button').each(function() {
newCopyYamlButton.call(this, THIS);
});
$('.minerva-edit_toggle_arrow').click(this, function(e) {
const HS = e.data.hashstate;
const THIS = e.data;
const arrow_0 = HS.waypoint.Arrows[0];
const hide_arrow = arrow_0.HideArrow;
arrow_0.HideArrow = hide_arrow ? false : true;
THIS.newView(true);
});
const logo_svg = this.getLogoImage();
logo_svg.style = "width: 85px";
const logo_link = "https://minerva.im";
const logo_class = "minerva-logo-anchor";
const menu_class = 'minerva-sidebar-menu';
const side_menu = document.getElementsByClassName(menu_class)[0];
const logos = side_menu.getElementsByClassName(logo_class);
[...logos].forEach((d) => {
side_menu.removeChild(d);
})
const logo_root = document.createElement('a');
const info_div = document.createElement('div');
logo_root.className = `position-fixed ${logo_class}`;
logo_root.style.cssText = `
left: 0.5em;
bottom: 0.5em;
display: block;
color: inherit;
line-height: 0.9em;
text-decoration: none;
padding: 0.4em 0.3em 0.2em;
background-color: rgba(0,0,0,0.8);
`;
logo_root.setAttribute('href', logo_link);
info_div.innerText = 'Made with';
logo_root.appendChild(info_div);
logo_root.appendChild(logo_svg);
side_menu.appendChild(logo_root);
}
// In editor mode
if (HS.edit) {
const THIS = this;
// Set all mask options
const mask_picker = HS.el.getElementsByClassName('minerva-mask-picker')[0];
mask_picker.innerHTML = "";
HS.masks.forEach(function(mask){
const mask_option = document.createElement("option");
mask_option.innerText = mask.Name;
mask_picker.appendChild(mask_option);
})
// Enale selection of active mask indices
$(".minerva-mask-picker").off("changed.bs.select");
$(".minerva-mask-picker").on("changed.bs.select", function(e, idx, isSelected, oldValues) {
const newValue = $(this).find('option').eq(idx).text();
HS.waypoint.Masks = HS.masks.map(mask => mask.Name).filter(function(name) {
if (isSelected) {
return oldValues.includes(name) || name == newValue;
}
return oldValues.includes(name) && name != newValue;
});
const active_names = HS.active_masks.map(mask => mask.Name).filter(function(name) {
return HS.waypoint.Masks.includes(name)
})
HS.waypoint.ActiveMasks = active_names;
HS.m = active_names.map(name => index_name(HS.masks, name));
THIS.newView(true);
});
// Set all group options
const group_picker = HS.el.getElementsByClassName('minerva-group-picker')[0];
group_picker.innerHTML = "";
HS.cgs.forEach(function(group){
const group_option = document.createElement("option");
group_option.innerText = group.Name;
group_picker.appendChild(group_option);
})
// Enale selection of active group index
$(".minerva-group-picker").off("changed.bs.select");
$(".minerva-group-picker").on("changed.bs.select", function(e, idx, isSelected, oldValues) {
const newValue = $(this).find('option').eq(idx).text();
HS.waypoint.Groups = HS.cgs.map(group => group.Name).filter(function(name) {
if (isSelected) {
return oldValues.includes(name) || name == newValue;
}
return oldValues.includes(name) && name != newValue;
});
const group_names = HS.waypoint.Groups;
const current_name = HS.cgs[HS.g].Name;
if (group_names.length > 0 && !group_names.includes(current_name)) {
HS.g = index_name(HS.cgs, group_names[0]);
}
THIS.newView(true);
});
}
// Based on control keys
const edit = HS.edit;
const noHome = HS.noHome;
const drawing = HS.drawing;
const drawType = HS.drawType;
const prefix = '#' + HS.id + ' ';
// Enable home button if in outline mode, otherwise enable table of contents button
displayOrNot(prefix+'.minerva-home-button', !noHome && !edit && HS.waypoint.Mode == 'outline');
displayOrNot(prefix+'.minerva-toc-button', !edit && HS.waypoint.Mode != 'outline');
// Enable 3D UI if in 3D mode
displayOrNot(prefix+'.minerva-channel-groups-legend', !HS.design.is3d);
displayOrNot(prefix+'.minerva-z-slider-legend', HS.design.is3d);
displayOrNot(prefix+'.minerva-toggle-legend', !HS.design.is3d);
displayOrNot(prefix+'.minerva-only-3d', HS.design.is3d);
// Enable edit UI if in edit mode
displayOrNot(prefix+'.minerva-editControls', edit);
// Enable standard UI if not in edit mode
displayOrNot(prefix+'.minerva-waypointControls', !edit && HS.totalCount > 1);
displayOrNot(prefix+'.minerva-waypointCount', !edit && HS.totalCount > 1);
displayOrNot(prefix+'.minerva-waypointName', !edit);
// Show crosshair cursor if drawing
toggleCursor(prefix+'.minerva-openseadragon > div', 'crosshair', drawing);
// Show correct switch state based on drawing mode
greenOrWhite(prefix+'.minerva-draw-switch *', drawing && (drawType == "box"));
greenOrWhite(prefix+'.minerva-lasso-switch *', drawing && (drawType == "lasso"));
greenOrWhite(prefix+'.minerva-arrow-switch *', drawing && (drawType == "arrow"));
// Special minmial nav if no text
const minimal_sidebar = !edit && HS.totalCount == 1 && !decode(HS.d);
classOrNot(prefix+'.minerva-sidebar-menu', minimal_sidebar, 'minimal');
displayOrNot(prefix+'.minerva-welcome-nav', !minimal_sidebar);
// Disable sidebar if no content
if (minimal_sidebar && noHome) {
classOrNot(prefix+'.minerva-sidebar-menu', true, 'toggled');
displayOrNot(prefix+'.minerva-toggle-sidebar', false);
}
// Toggle additional info features
const { infoOpen, addingOpen } = HS;
const hasInfo = HS.allowInfoIcon;
const canAdd = HS.singleChannelInfoOpen;
((k) => {
const bar = "minerva-settings-bar";
const settings = "minerva-settings-icon";
const bar_line = 'border-right: 2px solid grey;';
const root = HS.el.getElementsByClassName(k)[0];
const bar_el = root.getElementsByClassName(bar)[0];
const el = root.getElementsByClassName(settings)[0];
bar_el.style.cssText = ['',bar_line][+infoOpen];
el.innerText = ['⚙\uFE0E','⨂'][+infoOpen];
})("minerva-channel-legend-info-icon");
((k) => {
const add = "minerva-add-icon";
const root = HS.el.getElementsByClassName(k)[0];
const el = root.getElementsByClassName(add)[0];
el.innerText = ['⊕','⨂'][+addingOpen];
})("minerva-channel-legend-add-panel");
classOrNot(".minerva-legend-grid", !hasInfo, "disabled");
classOrNot(".minerva-channel-legend-2", canAdd, 'toggled');
classOrNot(".minerva-channel-legend-info", infoOpen, 'toggled');
classOrNot(".minerva-channel-legend-info-icon", !hasInfo, 'disabled');
classOrNot(".minerva-channel-legend-add-panel", canAdd, 'toggled');
classOrNot(".minerva-channel-legend-adding", addingOpen, "toggled");
classOrNot(".minerva-channel-legend-adding-info", addingOpen, "toggled");
classOrNot(".minerva-channel-legend-adding-info", !canAdd, "disabled");
classOrNot(".minerva-channel-legend-adding", !canAdd, "disabled");
// H&E should not display number of cycif markers
const is_h_e = HS.group.Name == 'H&E';
displayOrNot(prefix+'.minerva-welcome-markers', !is_h_e);
},
// Load speech-synthesis from AWS Polly
loadPolly: function(txt, speech_bucket) {
const hash = sha1(txt);
const HS = this.hashstate;
const prefix = '#' + HS.id + ' ';
displayOrNot(prefix+'.minerva-audioControls', !!speech_bucket);
if (!!speech_bucket) {
const polly_url = 'https://s3.amazonaws.com/'+ speech_bucket +'/speech/' + hash + '.mp3';
HS.el.getElementsByClassName('minerva-audioSource')[0].src = polly_url;
HS.el.getElementsByClassName('minerva-audioPlayback')[0].load();
}
},
/*
* User intercation
*/
// Draw lower bounds of box overlay
drawLowerBounds: function(position) {
const HS = this.hashstate;
const wh = [0, 0];
const new_xy = [
position.x, position.y
];
HS.o = new_xy.concat(wh);
this.newView(false);
},
// Compute new bounds in x or y
computeBounds: function(value, start, len) {
const center = start + (len / 2);
const end = start + len;
// Below center
if (value < center) {
return {
start: value,
range: end - value,
};
}
// Above center
return {
start: start,
range: value - start,
};
},
// Draw upper bounds of box overlay
drawUpperBounds: function(position) {
const HS = this.hashstate;
const xy = HS.o.slice(0, 2);
const wh = HS.o.slice(2);
// Set actual bounds
const x = this.computeBounds(position.x, xy[0], wh[0]);
const y = this.computeBounds(position.y, xy[1], wh[1]);
const o = [x.start, y.start, x.range, y.range];
HS.o = o.map(round4);
this.newView(false);
},
/*
* Display manaagement
*/
// Add list of mask layers
addMasks: function() {
const HS = this.hashstate;
$('.minerva-mask-layers').empty();
if (HS.edit || HS.waypoint.Mode == 'explore') {
// Show as a multi-column
$('.minerva-mask-layers').addClass('flex');
$('.minerva-mask-layers').removeClass('flex-column');
}
else {
// Show as a single column
$('.minerva-mask-layers').addClass('flex-column');
$('.minerva-mask-layers').removeClass('flex');
}
const mask_names = HS.waypoint.Masks || [];
const masks = HS.masks.filter(mask => {
return mask_names.includes(mask.Name);
});
if (masks.length && HS.waypoint.Mode == 'outline') {
$('.minerva-mask-label').show()
}
else {
$('.minerva-mask-label').hide()
}
// Add masks with indices
masks.forEach(function(mask) {
const m = index_name(HS.masks, mask.Name);
this.addMask(mask, m);
}, this);
},
// Add mask with index
addMask: function(mask, m) {
const HS = this.hashstate;
// Create an anchor element with empty href
var aEl = document.createElement('a');
aEl = Object.assign(aEl, {
className: HS.m.includes(m) ? 'nav-link active' : 'nav-link',
href: 'javascript:;',
innerText: mask.Name,
title: mask.Path
});
var ariaSelected = HS.m.includes(m) ? true : false;
aEl.setAttribute('aria-selected', ariaSelected);
// Append mask layer to mask layers
if (HS.waypoint.Mode == 'outline') {
HS.el.getElementsByClassName('minerva-mask-layers')[0].appendChild(aEl);
}
// Activate or deactivate Mask Layer
$(aEl).click(this, function(e) {
const HS = e.data.hashstate;
// Set group to default group
const group = HS.design.default_group;
const g = index_name(HS.cgs, group);
if ( g != -1 ) {
HS.g = g;
}
// Remove mask index from m
if (HS.m.includes(m)){
HS.m = HS.m.filter(i => i != m);
}
// Add mask index to m
else {
HS.m.push(m);
}
HS.pushState();
window.onpopstate();
});
},
// Add list of channel groups
addGroups: function() {
const HS = this.hashstate;
$('.minerva-channel-groups').empty();
$('.minerva-channel-groups-legend').empty();
const cgs_names = HS.waypoint.Groups || [];
const cgs = HS.cgs.filter(group => {
return cgs_names.includes(group.Name);
});
if (cgs.length || HS.edit) {
$('.minerva-channel-label').show()
}
else {
$('.minerva-channel-label').hide()
}
const cg_el = HS.el.getElementsByClassName('minerva-channel-groups')[0];
// Add filtered channel groups to waypoint
cgs.forEach(function(group) {
const g = index_name(HS.cgs, group.Name);
this.addGroup(group, g, cg_el, false);
}, this);
const cgs_multi = HS.cgs.filter(group => {
return group.Channels.length > 1;
});
const cgs_single = HS.cgs.filter(group => {
return group.Channels.length == 1;
});
const cg_legend = HS.el.getElementsByClassName('minerva-channel-groups-legend')[0];
if (cgs_multi.length > 0) {
var h = document.createElement('h6');
h.innerText = 'Channel Groups:'
h.className = 'm-1'
cg_legend.appendChild(h);
}
// Add all channel groups to legend
cgs_multi.forEach(function(group) {
const g = index_name(HS.cgs, group.Name);
this.addGroup(group, g, cg_legend, true);
}, this);
if (cgs_single.length > 0) {
var h = document.createElement('h6');
h.innerText = 'Channels:'
h.className = 'm-1'
cg_legend.appendChild(h);
}
cgs_single.forEach(function(group) {
const g = index_name(HS.cgs, group.Name);
this.addGroup(group, g, cg_legend, true);
}, this);
},
// Add a single channel group to an element
addGroup: function(group, g, el, show_more) {
const HS = this.hashstate;
var aEl = document.createElement('a');
var selected = HS.g === g ? true : false;
aEl = Object.assign(aEl, {
className: selected ? 'nav-link active' : 'nav-link',
style: 'padding-right: 40px; position: relative;',
href: 'javascript:;',
innerText: group.Name
});
aEl.setAttribute('data-toggle', 'pill');
// Set story and waypoint for this marker
var s_w = undefined;
for (var s in HS.stories) {
for (var w in HS.stories[s].Waypoints) {
var waypoint = HS.stories[s].Waypoints[w];
if (waypoint.Group == group.Name) {
// Select the first waypoint or the definitive
if (s_w == undefined || waypoint.DefineGroup) {
s_w = [s, w];
}
}
}
}
var moreEl = document.createElement('a');
if (selected && show_more && s_w) {
const opacity = 'opacity: ' + + ';';
moreEl = Object.assign(moreEl, {
className : 'text-white',
style: 'position: absolute; right: 5px;',
href: 'javascript:;',
innerText: 'MORE',
});
aEl.appendChild(moreEl);
// Update Waypoint
$(moreEl).click(this, function(e) {
HS.s = s_w[0];
HS.w = s_w[1];
HS.pushState();
window.onpopstate();
});
}
// Append channel group to element
el.appendChild(aEl);
// Update Channel Group
$(aEl).click(this, function(e) {
HS.g = g;
HS.pushState();
window.onpopstate();
});
},
// Add channel legend labels
addChannelLegends: function() {
const HS = this.hashstate;
const { group, activeChannel } = HS;
var label = '';
var picked = new RegExp("^$");
if (activeChannel >= 0) {
label = group.Channels[activeChannel];
const color = group.Colors[activeChannel];
if (color) picked = new RegExp(color, "i");
}
$('.minerva-channel-legend-1').empty();
$('.minerva-channel-legend-2').empty();
$('.minerva-channel-legend-3').empty();
$('.minerva-channel-legend-info').empty();
$('.minerva-channel-legend-adding').empty();
$('.minerva-channel-legend-adding-info').empty();
$('.minerva-channel-legend-color-picker').empty();
if (activeChannel < 0) {
$('.minerva-channel-legend-color-picker').removeClass('toggled');
}
const legend_lines = HS.channel_legend_lines;