/
score.js
1443 lines (1365 loc) · 49 KB
/
score.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 d3 from 'd3';
import deepEquals from 'mout/src/lang/deepEquals';
import style from 'PVWStyle/InfoVizNative/HistogramSelector.mcss';
import SelectionBuilder from 'paraviewweb/src/Common/Misc/SelectionBuilder';
import AnnotationBuilder from 'paraviewweb/src/Common/Misc/AnnotationBuilder';
// import downArrowImage from './down_arrow.png';
export default function init(inPublicAPI, inModel) {
const publicAPI = inPublicAPI;
const model = inModel;
let displayOnlyScored = false;
let scorePopupDiv = null;
let dividerPopupDiv = null;
let dividerValuePopupDiv = null;
publicAPI.setScores = (scores, defaultScore) => {
// TODO make sure model.scores has the right format?
model.scores = scores;
model.defaultScore = defaultScore;
if (model.scores) {
// setup a bgColor
model.scores.forEach((score, i) => {
const lightness = d3.hsl(score.color).l;
// make bg darker for light colors.
const blend = lightness >= 0.45 ? 0.4 : 0.2;
const interp = d3.interpolateRgb('#fff', score.color);
score.bgColor = interp(blend);
});
}
};
function enabled() {
return model.scores !== undefined;
}
if (model.provider.isA('ScoresProvider')) {
publicAPI.setScores(
model.provider.getScores(),
model.provider.getDefaultScore()
);
}
function defaultFieldData() {
return {
annotation: null,
};
}
function createDefaultDivider(val, uncert) {
return {
value: val,
uncertainty: uncert,
};
}
function getHistRange(def) {
let minRange = def.range[0];
let maxRange = def.range[1];
if (def.hobj) {
minRange = def.hobj.min;
maxRange = def.hobj.max;
}
if (minRange === maxRange) maxRange += 1;
return [minRange, maxRange];
}
// add implicit bounds for the histogram min/max to dividers list
function getRegionBounds(def) {
const [minRange, maxRange] = getHistRange(def);
return [minRange].concat(def.dividers.map((div) => div.value), maxRange);
}
function getUncertScale(def) {
// handle a zero range (like from the cumulative score histogram)
// const [minRange, maxRange] = getHistRange(def);
// return (maxRange - minRange);
// We are not going to scale uncertainty - use values in
// the same units as the histogram itself
return 1.0;
}
// Translate our dividers and regions into an annotation
// suitable for scoring this histogram.
function dividersToPartition(def, scores) {
if (!def.regions || !def.dividers || !scores) return null;
if (def.regions.length !== def.dividers.length + 1) return null;
const uncertScale = getUncertScale(def);
const partitionSelection = SelectionBuilder.partition(
def.name,
def.dividers
);
partitionSelection.partition.dividers.forEach((div, index) => {
div.uncertainty *= uncertScale;
});
// console.log('DBG partitionSelection', JSON.stringify(partitionSelection, 2));
// Construct a partition annotation:
let partitionAnnotation = null;
if (def.annotation && !model.provider.shouldCreateNewAnnotation()) {
// don't send a new selection unless it's changed.
const saveGen = partitionSelection.generation;
partitionSelection.generation = def.annotation.selection.generation;
const changeSet = { score: def.regions };
if (!deepEquals(partitionSelection, def.annotation.selection)) {
partitionSelection.generation = saveGen;
changeSet.selection = partitionSelection;
}
partitionAnnotation = AnnotationBuilder.update(def.annotation, {
selection: partitionSelection,
score: def.regions,
});
} else {
partitionAnnotation = AnnotationBuilder.annotation(
partitionSelection,
def.regions,
1,
''
);
}
AnnotationBuilder.updateReadOnlyFlag(
partitionAnnotation,
model.readOnlyFields
);
return partitionAnnotation;
}
// retrieve annotation, and re-create dividers and regions
function partitionToDividers(scoreData, def, scores) {
// console.log('DBG return', JSON.stringify(scoreData, null, 2));
const uncertScale = getUncertScale(def);
const regions = scoreData.score;
const dividers = JSON.parse(
JSON.stringify(scoreData.selection.partition.dividers)
);
dividers.forEach((div, index) => {
div.uncertainty *= 1 / uncertScale;
});
// don't replace the default region with an empty region, so UI can display the default region.
if (
regions.length > 0 &&
!(regions.length === 1 && regions[0] === model.defaultScore)
) {
def.regions = [].concat(regions);
def.dividers = dividers;
}
}
// communicate with the server which regions/dividers have changed.
function sendScores(def, passive = false) {
const scoreData = dividersToPartition(def, model.scores);
if (scoreData === null) {
console.error('Cannot translate scores to send to provider');
return;
}
if (model.provider.isA('SelectionProvider')) {
if (!scoreData.name) {
AnnotationBuilder.setDefaultName(scoreData);
if (model.provider.isA('AnnotationStoreProvider')) {
scoreData.name = model.provider.getNextStoredAnnotationName(
scoreData.name
);
}
}
if (!passive) {
model.provider.setAnnotation(scoreData);
} else if (
model.provider.isA('AnnotationStoreProvider') &&
model.provider.getStoredAnnotation(scoreData.id)
) {
// Passive means we don't want to set the active annotation, but if there is
// a stored annotation matching these score dividers, we still need to update
// that stored annotation
model.provider.updateStoredAnnotations({
[scoreData.id]: scoreData,
});
}
}
}
function showScore(def) {
// show the regions when: editing, or when they are non-default. CSS rule makes visible on hover.
return (
def.editScore ||
(typeof def.regions !== 'undefined' &&
(def.regions.length > 1 || def.regions[0] !== model.defaultScore))
);
}
function setEditScore(def, newEditScore) {
def.editScore = newEditScore;
// set existing annotation as current if we activate for editing
if (def.editScore && showScore(def) && def.annotation) {
// TODO special 'active' method to call, instead of an edit?
sendScores(def);
}
publicAPI.render(def.name);
// if one histogram is being edited, the others should be inactive.
if (newEditScore) {
Object.keys(model.fieldData).forEach((key) => {
const d = model.fieldData[key];
if (d !== def) {
if (d.editScore) {
d.editScore = false;
publicAPI.render(d.name);
}
}
});
}
}
publicAPI.setDefaultScorePartition = (fieldName) => {
const def = model.fieldData[fieldName];
if (!def) return;
// possibly the best we can do - check for a threshold-like annotation
if (
!def.lockAnnot ||
!(
def.regions &&
def.regions.length === 2 &&
def.regions[0] === 0 &&
def.regions[1] === 2
)
) {
// create a divider halfway through.
const [minRange, maxRange] = getHistRange(def);
def.dividers = [createDefaultDivider(0.5 * (minRange + maxRange), 0)];
// set regions to 'no' | 'yes'
def.regions = [0, 2];
// clear any existing (local) annotation
def.annotation = null;
// set mode that prevents editing the annotation, except for the single divider.
def.lockAnnot = true;
sendScores(def);
}
// we might already have threshold annot, but need to score it.
setEditScore(def, true);
};
publicAPI.getScoreThreshold = (fieldName) => {
const def = model.fieldData[fieldName];
if (!def.lockAnnot)
console.log('Wrong mode for score threshold, arbitrary results.');
return def.dividers[0].value;
};
const scoredHeaderClick = (d) => {
displayOnlyScored = !displayOnlyScored;
publicAPI.render();
};
function getDisplayOnlyScored() {
return displayOnlyScored;
}
function numScoreIcons(def) {
if (!enabled()) return 0;
let count = 0;
if (
model.provider.getStoredAnnotation &&
!publicAPI.isFieldActionDisabled(def.name, 'save')
) {
count += 1;
}
if (!publicAPI.isFieldActionDisabled(def.name, 'score')) {
count += 1;
}
return count;
}
function annotationSameAsStored(annotation) {
const storedAnnot = model.provider.getStoredAnnotation(annotation.id);
if (!storedAnnot) return false;
if (annotation.generation === storedAnnot.generation) return true;
const savedGen = annotation.generation;
annotation.generation = storedAnnot.generation;
const ret = deepEquals(annotation, storedAnnot);
annotation.generation = savedGen;
return ret;
}
function createScoreIcons(iconCell) {
if (!enabled()) return;
// create/save partition annotation
if (model.provider.getStoredAnnotation) {
iconCell
.append('i')
.classed(style.noSaveIcon, true)
.on('click', (d) => {
if (model.provider.getStoredAnnotation) {
const annotation = d.annotation;
const isSame = annotationSameAsStored(annotation);
if (!isSame) {
model.provider.setStoredAnnotation(annotation.id, annotation);
} else {
model.provider.setAnnotation(annotation);
}
publicAPI.render(d.name);
if (d3.event) d3.event.stopPropagation();
}
});
}
// start/stop scoring
iconCell
.append('i')
.classed(style.scoreStartIcon, true)
.on('click', (d) => {
setEditScore(d, !d.editScore);
if (d3.event) d3.event.stopPropagation();
});
}
function updateScoreIcons(iconCell, def) {
if (!enabled()) return;
if (model.provider.getStoredAnnotation) {
// new/modified/unmodified annotation...
if (def.annotation) {
if (model.provider.getStoredAnnotation(def.annotation.id)) {
const isSame = annotationSameAsStored(def.annotation);
if (isSame) {
const isActive = def.annotation === model.provider.getAnnotation();
iconCell
.select(`.${style.jsSaveIcon}`)
.attr(
'class',
isActive
? style.unchangedActiveSaveIcon
: style.unchangedSaveIcon
);
} else {
iconCell
.select(`.${style.jsSaveIcon}`)
.attr('class', style.modifiedSaveIcon);
}
} else {
iconCell
.select(`.${style.jsSaveIcon}`)
.attr('class', style.newSaveIcon);
}
} else {
iconCell.select(`.${style.jsSaveIcon}`).attr('class', style.noSaveIcon);
}
}
iconCell
.select(`.${style.jsScoreIcon}`)
.attr('class', def.editScore ? style.scoreEndIcon : style.scoreStartIcon);
// Override icon if disabled
if (publicAPI.isFieldActionDisabled(def.name, 'save')) {
iconCell.select(`.${style.jsSaveIcon}`).attr('class', style.noSaveIcon);
}
if (publicAPI.isFieldActionDisabled(def.name, 'score')) {
iconCell
.select(`.${style.jsScoreIcon}`)
.attr('class', style.hideScoreIcon);
}
}
function createGroups(svgGr) {
// scoring interface background group, must be behind.
svgGr.insert('g', ':first-child').classed(style.jsScoreBackground, true);
svgGr.append('g').classed(style.score, true);
}
function createHeader(header) {
if (enabled()) {
header
.append('span')
.on('click', scoredHeaderClick)
.append('i')
.classed(style.jsShowScoredIcon, true);
header
.append('span')
.classed(style.jsScoredHeader, true)
.text('Only Scored')
.on('click', scoredHeaderClick);
}
}
function updateHeader() {
if (enabled()) {
d3.select(model.container)
.select(`.${style.jsShowScoredIcon}`)
// apply class - 'false' should come first to not remove common base class.
.classed(
getDisplayOnlyScored() ? style.allScoredIcon : style.onlyScoredIcon,
false
)
.classed(
!getDisplayOnlyScored() ? style.allScoredIcon : style.onlyScoredIcon,
true
);
}
}
function createDragDivider(hitIndex, val, def, hobj) {
let dragD = null;
if (hitIndex >= 0) {
// start modifying existing divider
// it becomes a temporary copy if we go outside our bounds
dragD = {
index: hitIndex,
newDivider: createDefaultDivider(
undefined,
def.dividers[hitIndex].uncertainty
),
savedUncert: def.dividers[hitIndex].uncertainty,
low: hitIndex === 0 ? hobj.min : def.dividers[hitIndex - 1].value,
high:
hitIndex === def.dividers.length - 1
? hobj.max
: def.dividers[hitIndex + 1].value,
};
} else {
// create a temp divider to render.
dragD = {
index: -1,
newDivider: createDefaultDivider(val, 0),
savedUncert: 0,
low: hobj.min,
high: hobj.max,
};
}
return dragD;
}
// enforce that divider uncertainties can't overlap.
// Look at neighboring dividers for boundaries on this divider's uncertainty.
function clampDividerUncertainty(val, def, hitIndex, currentUncertainty) {
if (hitIndex < 0) return currentUncertainty;
const [minRange, maxRange] = getHistRange(def);
let maxUncertainty = 0.5 * (maxRange - minRange);
const uncertScale = getUncertScale(def);
// Note comparison with low/high divider is signed. If val indicates divider has been
// moved _past_ the neighboring divider, low/high will be negative.
if (hitIndex > 0) {
const low =
def.dividers[hitIndex - 1].value +
def.dividers[hitIndex - 1].uncertainty * uncertScale;
maxUncertainty = Math.min(maxUncertainty, (val - low) / uncertScale);
}
if (hitIndex < def.dividers.length - 1) {
const high =
def.dividers[hitIndex + 1].value -
def.dividers[hitIndex + 1].uncertainty * uncertScale;
maxUncertainty = Math.min((high - val) / uncertScale, maxUncertainty);
}
// make sure uncertainty is zero when val has passed a neighbor.
maxUncertainty = Math.max(maxUncertainty, 0);
return Math.min(maxUncertainty, currentUncertainty);
}
// clamp the drag divider specifically
function clampDragDividerUncertainty(val, def) {
if (def.dragDivider.index < 0) return;
def.dragDivider.newDivider.uncertainty = clampDividerUncertainty(
val,
def,
def.dragDivider.index,
def.dragDivider.savedUncert
);
def.dividers[def.dragDivider.index].uncertainty =
def.dragDivider.newDivider.uncertainty;
}
function moveDragDivider(val, def) {
if (!def.dragDivider) return;
if (def.dragDivider.index >= 0) {
// if we drag outside our bounds, make this a 'temporary' extra divider.
if (val < def.dragDivider.low) {
def.dragDivider.newDivider.value = val;
def.dividers[def.dragDivider.index].value = def.dragDivider.low;
clampDragDividerUncertainty(val, def);
def.dividers[def.dragDivider.index].uncertainty = 0;
} else if (val > def.dragDivider.high) {
def.dragDivider.newDivider.value = val;
def.dividers[def.dragDivider.index].value = def.dragDivider.high;
clampDragDividerUncertainty(val, def);
def.dividers[def.dragDivider.index].uncertainty = 0;
} else {
def.dividers[def.dragDivider.index].value = val;
clampDragDividerUncertainty(val, def);
def.dragDivider.newDivider.value = undefined;
}
} else {
def.dragDivider.newDivider.value = val;
}
}
// create a sorting helper method, to sort dividers based on div.value
// D3 bug: just an accessor didn't work - must use comparator function
const bisectDividers = d3.bisector((a, b) => a.value - b.value).left;
// where are we (to the left of) in the divider list?
// Did we hit one?
function dividerPick(overCoords, def, marginPx, minVal) {
const val = def.xScale.invert(overCoords[0]);
const index = bisectDividers(def.dividers, createDefaultDivider(val));
let hitIndex = -1;
if (def.dividers.length > 0) {
if (index === 0) {
hitIndex = 0;
} else if (index === def.dividers.length) {
hitIndex = index - 1;
} else {
hitIndex =
def.dividers[index].value - val < val - def.dividers[index - 1].value
? index
: index - 1;
}
const margin = def.xScale.invert(marginPx) - minVal;
// don't pick a divider outside the bounds of the histogram - pick the last region.
if (
Math.abs(def.dividers[hitIndex].value - val) > margin ||
val < def.hobj.min ||
val > def.hobj.max
) {
// we weren't close enough...
hitIndex = -1;
}
}
return [val, index, hitIndex];
}
function regionPick(overCoords, def, hobj) {
if (def.dividers.length === 0 || def.regions.length <= 1) return 0;
const val = def.xScale.invert(overCoords[0]);
const hitIndex = bisectDividers(def.dividers, createDefaultDivider(val));
return hitIndex;
}
function finishDivider(def, hobj, forceDelete = false) {
if (!def.dragDivider) return;
const val = def.dragDivider.newDivider.value;
// if val is defined, we moved an existing divider inside
// its region, and we just need to render. Otherwise...
if (val !== undefined || forceDelete) {
// drag 30 pixels out of the hist to delete.
const dragOut = def.xScale.invert(30) - hobj.min;
if (
!def.lockAnnot &&
(forceDelete || val < hobj.min - dragOut || val > hobj.max + dragOut)
) {
if (def.dragDivider.index >= 0) {
// delete a region.
if (
forceDelete ||
def.dividers[def.dragDivider.index].value === def.dragDivider.high
) {
def.regions.splice(def.dragDivider.index + 1, 1);
} else {
def.regions.splice(def.dragDivider.index, 1);
}
// delete the divider.
def.dividers.splice(def.dragDivider.index, 1);
}
} else {
// adding a divider, we make a new region.
let replaceRegion = true;
// if we moved a divider, delete the old region, unless it's one of the edge regions - those persist.
if (def.dragDivider.index >= 0) {
if (
def.dividers[def.dragDivider.index].value === def.dragDivider.low &&
def.dragDivider.low !== hobj.min
) {
def.regions.splice(def.dragDivider.index, 1);
} else if (
def.dividers[def.dragDivider.index].value ===
def.dragDivider.high &&
def.dragDivider.high !== hobj.max
) {
def.regions.splice(def.dragDivider.index + 1, 1);
} else {
replaceRegion = false;
}
// delete the old divider
def.dividers.splice(def.dragDivider.index, 1);
}
// add a new divider
def.dragDivider.newDivider.value = Math.min(
hobj.max,
Math.max(hobj.min, val)
);
// find the index based on dividers sorted by divider.value
const index = bisectDividers(def.dividers, def.dragDivider.newDivider);
def.dividers.splice(index, 0, def.dragDivider.newDivider);
// add a new region if needed, copies the score of existing region.
if (replaceRegion) {
def.regions.splice(index, 0, def.regions[index]);
}
}
} else if (
def.dragDivider.index >= 0 &&
def.dividers[def.dragDivider.index].uncertainty !==
def.dragDivider.newDivider.uncertainty
) {
def.dividers[def.dragDivider.index].uncertainty =
def.dragDivider.newDivider.uncertainty;
}
// make sure uncertainties don't overlap.
def.dividers.forEach((divider, index) => {
divider.uncertainty = clampDividerUncertainty(
divider.value,
def,
index,
divider.uncertainty
);
});
sendScores(def);
def.dragDivider = undefined;
}
function positionPopup(popupDiv, left, top) {
const clientRect = model.listContainer.getBoundingClientRect();
const popupRect = popupDiv.node().getBoundingClientRect();
if (popupRect.width + left > clientRect.width) {
popupDiv.style('left', 'auto');
popupDiv.style('right', 0);
} else {
popupDiv.style('right', null);
popupDiv.style('left', `${left}px`);
}
if (popupRect.height + top > clientRect.height) {
popupDiv.style('top', 'auto');
popupDiv.style('bottom', 0);
} else {
popupDiv.style('bottom', null);
popupDiv.style('top', `${top}px`);
}
}
function validateDividerVal(n) {
// is it a finite float number?
return !Number.isNaN(Number(n)) && Number.isFinite(Number(n));
}
function showDividerPopup(dPopupDiv, selectedDef, hobj, coord) {
const topMargin = 4;
const rowHeight = 28;
// 's' SI unit label won't work for a number entry field.
const formatter = d3.format('.4g');
const uncertDispScale = 1; // was 100 for uncertainty as a %.
dPopupDiv.style('display', null);
positionPopup(
dPopupDiv,
coord[0] - topMargin - 0.5 * rowHeight,
coord[1] + model.headerSize - (topMargin + 2 * rowHeight)
);
const selDivider = selectedDef.dividers[selectedDef.dragDivider.index];
let savedVal = selDivider.value;
selectedDef.dragDivider.savedUncert = selDivider.uncertainty;
dPopupDiv.on('mouseleave', () => {
if (selectedDef.dragDivider) {
moveDragDivider(savedVal, selectedDef);
finishDivider(selectedDef, hobj);
}
dPopupDiv.style('display', 'none');
selectedDef.dragDivider = undefined;
publicAPI.render(selectedDef.name);
});
const uncertInput = dPopupDiv.select(`.${style.jsDividerUncertaintyInput}`);
const valInput = dPopupDiv
.select(`.${style.jsDividerValueInput}`)
.attr('value', formatter(selDivider.value))
.property('value', formatter(selDivider.value))
.on('input', () => {
// typing values, show feedback.
let val = d3.event.target.value;
if (!validateDividerVal(val)) val = savedVal;
moveDragDivider(val, selectedDef);
uncertInput.property(
'value',
formatter(
uncertDispScale * selectedDef.dragDivider.newDivider.uncertainty
)
);
publicAPI.render(selectedDef.name);
})
.on('change', () => {
// committed to a value, show feedback.
let val = d3.event.target.value;
if (!validateDividerVal(val)) val = savedVal;
else {
val = Math.min(hobj.max, Math.max(hobj.min, val));
d3.event.target.value = val;
savedVal = val;
}
moveDragDivider(val, selectedDef);
publicAPI.render(selectedDef.name);
})
.on('keyup', () => {
// revert to last committed value
if (d3.event.key === 'Escape') {
moveDragDivider(savedVal, selectedDef);
dPopupDiv.on('mouseleave')();
} else if (d3.event.key === 'Enter' || d3.event.key === 'Return') {
if (selectedDef.dragDivider) {
savedVal =
selectedDef.dragDivider.newDivider.value === undefined
? selectedDef.dividers[selectedDef.dragDivider.index].value
: selectedDef.dragDivider.newDivider.value;
}
// commit current value
dPopupDiv.on('mouseleave')();
}
});
// initial select/focus so use can immediately change the value.
valInput.node().select();
valInput.node().focus();
uncertInput
.attr('value', formatter(uncertDispScale * selDivider.uncertainty))
.property('value', formatter(uncertDispScale * selDivider.uncertainty))
.on('input', () => {
// typing values, show feedback.
let uncert = d3.event.target.value;
if (!validateDividerVal(uncert)) {
if (selectedDef.dragDivider)
uncert = selectedDef.dragDivider.savedUncert;
} else {
uncert /= uncertDispScale;
}
if (selectedDef.dragDivider) {
selectedDef.dragDivider.newDivider.uncertainty = uncert;
if (selectedDef.dragDivider.newDivider.value === undefined) {
// don't use selDivider, might be out-of-date if the server sent us dividers.
selectedDef.dividers[
selectedDef.dragDivider.index
].uncertainty = uncert;
}
}
publicAPI.render(selectedDef.name);
})
.on('change', () => {
// committed to a value, show feedback.
let uncert = d3.event.target.value;
if (!validateDividerVal(uncert)) {
if (selectedDef.dragDivider)
uncert = selectedDef.dragDivider.savedUncert;
} else {
// uncertainty is a % between 0 and 0.5
// uncert = Math.min(0.5, Math.max(0, uncert / uncertDispScale));
const [minRange, maxRange] = getHistRange(selectedDef);
uncert = Math.min(
0.5 * (maxRange - minRange),
Math.max(0, uncert / uncertDispScale)
);
d3.event.target.value = formatter(uncertDispScale * uncert);
if (selectedDef.dragDivider)
selectedDef.dragDivider.savedUncert = uncert;
}
if (selectedDef.dragDivider) {
selectedDef.dragDivider.newDivider.uncertainty = uncert;
if (selectedDef.dragDivider.newDivider.value === undefined) {
selectedDef.dividers[
selectedDef.dragDivider.index
].uncertainty = uncert;
}
}
publicAPI.render(selectedDef.name);
})
.on('keyup', () => {
if (d3.event.key === 'Escape') {
if (selectedDef.dragDivider) {
selectedDef.dragDivider.newDivider.uncertainty =
selectedDef.dragDivider.savedUncert;
}
dPopupDiv.on('mouseleave')();
} else if (d3.event.key === 'Enter' || d3.event.key === 'Return') {
if (selectedDef.dragDivider) {
selectedDef.dragDivider.savedUncert =
selectedDef.dragDivider.newDivider.uncertainty;
}
dPopupDiv.on('mouseleave')();
}
})
.on('blur', () => {
if (selectedDef.dragDivider) {
const val =
selectedDef.dragDivider.newDivider.value === undefined
? selectedDef.dividers[selectedDef.dragDivider.index].value
: selectedDef.dragDivider.newDivider.value;
clampDragDividerUncertainty(val, selectedDef);
d3.event.target.value = formatter(
uncertDispScale * selectedDef.dragDivider.newDivider.uncertainty
);
}
publicAPI.render(selectedDef.name);
});
}
function showDividerValuePopup(dPopupDiv, selectedDef, hobj, coord) {
const topMargin = 4;
const rowHeight = 28;
// 's' SI unit label won't work for a number entry field.
const formatter = d3.format('.4g');
dPopupDiv.style('display', null);
positionPopup(
dPopupDiv,
coord[0] - topMargin - 0.5 * rowHeight,
coord[1] + model.headerSize - (topMargin + 0.5 * rowHeight)
);
const selDivider = selectedDef.dividers[selectedDef.dragDivider.index];
let savedVal = selDivider.value;
selectedDef.dragDivider.savedUncert = selDivider.uncertainty;
dPopupDiv.on('mouseleave', () => {
if (selectedDef.dragDivider) {
moveDragDivider(savedVal, selectedDef);
finishDivider(selectedDef, hobj);
}
dPopupDiv.style('display', 'none');
selectedDef.dragDivider = undefined;
publicAPI.render();
});
const valInput = dPopupDiv
.select(`.${style.jsDividerValueInput}`)
.attr('value', formatter(selDivider.value))
.property('value', formatter(selDivider.value))
.on('input', () => {
// typing values, show feedback.
let val = d3.event.target.value;
if (!validateDividerVal(val)) val = savedVal;
moveDragDivider(val, selectedDef);
publicAPI.render(selectedDef.name);
})
.on('change', () => {
// committed to a value, show feedback.
let val = d3.event.target.value;
if (!validateDividerVal(val)) val = savedVal;
else {
val = Math.min(hobj.max, Math.max(hobj.min, val));
d3.event.target.value = val;
savedVal = val;
}
moveDragDivider(val, selectedDef);
publicAPI.render(selectedDef.name);
})
.on('keyup', () => {
// revert to last committed value
if (d3.event.key === 'Escape') {
moveDragDivider(savedVal, selectedDef);
dPopupDiv.on('mouseleave')();
} else if (d3.event.key === 'Enter' || d3.event.key === 'Return') {
// commit current value
dPopupDiv.on('mouseleave')();
}
});
// initial select/focus so use can immediately change the value.
valInput.node().select();
valInput.node().focus();
}
// Divider editing popup allows changing its value or uncertainty, or deleting it.
function createDividerPopup() {
const dPopupDiv = d3
.select(model.listContainer)
.append('div')
.classed(style.dividerPopup, true)
.style('display', 'none');
const table = dPopupDiv.append('table');
const tr1 = table.append('tr');
tr1
.append('td')
.classed(style.popupCell, true)
.text('Value:');
tr1
.append('td')
.classed(style.popupCell, true)
.append('input')
.classed(style.jsDividerValueInput, true)
.attr('type', 'number')
.attr('step', 'any')
.style('width', '6em');
const tr2 = table.append('tr');
tr2
.append('td')
.classed(style.popupCell, true)
.text('Uncertainty:');
tr2
.append('td')
.classed(style.popupCell, true)
.append('input')
.classed(style.jsDividerUncertaintyInput, true)
.attr('type', 'number')
.attr('step', 'any')
.style('width', '6em');
if (!model.showUncertainty) {
// if we aren't supposed to show uncertainty, hide this row.
tr2.style('display', 'none');
}
dPopupDiv.append('div').classed(style.scoreDashSpacer, true);
dPopupDiv
.append('div')
.style('text-align', 'center')
.append('input')
.classed(style.scoreButton, true)
.style('align', 'center')
.attr('type', 'button')
.attr('value', 'Delete Divider')
.on('click', () => {
finishDivider(model.selectedDef, model.selectedDef.hobj, true);
dPopupDiv.style('display', 'none');
publicAPI.render();
});
return dPopupDiv;
}
// Divider editing popup allows changing its value, only.
function createDividerValuePopup() {
const dPopupDiv = d3
.select(model.listContainer)
.append('div')
.classed(style.dividerValuePopup, true)
.style('display', 'none');
const table = dPopupDiv.append('table');
const tr1 = table.append('tr');
tr1
.append('td')
.classed(style.popupCell, true)
.text('Value:');
tr1
.append('td')
.classed(style.popupCell, true)
.append('input')
.classed(style.jsDividerValueInput, true)
.attr('type', 'number')
.attr('step', 'any')
.style('width', '6em');
return dPopupDiv;
}
function showScorePopup(sPopupDiv, coord, selRow) {
// it seemed like a good idea to use getBoundingClientRect() to determine row height
// but it returns all zeros when the popup has been invisible...
const topMargin = 4;
const rowHeight = 26;
sPopupDiv.style('display', null);
positionPopup(
sPopupDiv,
coord[0] - topMargin - 0.6 * rowHeight,
coord[1] + model.headerSize - (topMargin + (0.6 + selRow) * rowHeight)
);
sPopupDiv
.selectAll(`.${style.jsScoreLabel}`)
.style('background-color', (d, i) => (i === selRow ? d.bgColor : '#fff'));
}
function createScorePopup() {
const sPopupDiv = d3
.select(model.listContainer)
.append('div')
.classed(style.scorePopup, true)
.style('display', 'none')
.on('mouseleave', () => {
sPopupDiv.style('display', 'none');
model.selectedDef.dragDivider = undefined;
});
// create radio-buttons that allow choosing the score for the selected region
const scoreChoices = sPopupDiv
.selectAll(`.${style.jsScoreChoice}`)
.data(model.scores);
scoreChoices
.enter()
.append('label')
.classed(style.scoreLabel, true)
.text((d) => d.name)
.each(function myLabel(data, index) {
// because we use 'each' and re-select the label, need to use parent 'index'
// instead of 'i' in the (d, i) => functions below - i is always zero.
const label = d3.select(this);
label
.append('span')
.classed(style.scoreSwatch, true)
.style('background-color', (d) => d.color);
label
.append('input')
.classed(style.scoreChoice, true)
.attr('name', 'score_choice_rb')
.attr('type', 'radio')
.attr('value', (d) => d.name)
.property('checked', (d) => index === model.defaultScore)
.on('click', (d) => {
// use click, not change, so we get notified even when current value is chosen.
const def = model.selectedDef;
def.regions[def.hitRegionIndex] = index;