-
Notifications
You must be signed in to change notification settings - Fork 51
/
HiGlassComponent.jsx
1548 lines (1262 loc) · 51.1 KB
/
HiGlassComponent.jsx
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 '../styles/MultiViewContainer.css';
import React from 'react';
import _ from 'lodash';
import {scaleLinear} from 'd3-scale';
import {request,post} from 'd3-request';
import slugid from 'slugid';
import ReactDOM from 'react-dom';
import {Responsive, WidthProvider} from 'react-grid-layout';
import {SearchableTiledPlot} from './SearchableTiledPlot.jsx';
import "react-grid-layout/css/styles.css";
import "react-resizable/css/styles.css";
import {ResizeSensor,ElementQueries} from 'css-element-queries';
import {TiledPlot} from './TiledPlot.jsx';
import {PopupMenu} from './PopupMenu.jsx';
import {ConfigViewMenu} from './ConfigViewMenu.jsx';
import {ContextMenuContainer} from './ContextMenuContainer.jsx';
import {scalesCenterAndK, dictItems, dictFromTuples, dictValues, dictKeys} from './utils.js';
import {getTrackPositionByUid, getTrackByUid} from './utils.js';
import {positionedTracksToAllTracks} from './utils.js';
import {usedServer, tracksInfo, tracksInfoByType} from './config.js';
import {SHORT_DRAG_TIMEOUT, LONG_DRAG_TIMEOUT} from './config.js';
import {GenomePositionSearchBox} from './GenomePositionSearchBox.jsx';
import {ExportLinkModal} from './ExportLinkModal.jsx';
const ResponsiveReactGridLayout = WidthProvider(Responsive);
export class HiGlassComponent extends React.Component {
constructor(props) {
super(props);
this.minHorizontalHeight = 20;
this.minVerticalWidth = 20;
this.tempCanvas = document.createElement('canvas');
this.uid = slugid.nice();
this.yPositionOffset = 0;
this.rowHeight = 40;
this.tiledPlots = {};
// keep track of the xScales of each Track Renderer
this.xScales = {};
this.yScales = {};
this.topDiv = null;
// event listeners for when the scales of a view change
// bypasses the React event framework because this needs
// to be fast
// indexed by view uid and then listener uid
this.scalesChangedListeners = {};
// zoom locks between views
this.zoomLocks = {};
this.lockGroups = {};
this.setCenters = {};
this.plusImg = {};
this.configImg = {};
this.horizontalMargin = 5;
this.verticalMargin = 5;
this.boundRefreshView = this.refreshView.bind(this);
let localServer = "localhost:8000";
//let usedServer = localServer;
//let usedServer = remoteServer;
this.viewConfig = this.props.viewConfig;
this.pixiStage = new PIXI.Container();
this.pixiStage.interactive = true;
this.element = null;
let viewsByUid = this.processViewConfig(this.props.viewConfig);
this.state = {
currentBreakpoint: 'lg',
mounted: false,
width: 0,
height: 0,
layouts: {},
svgElement: null,
canvasElement: null,
views: viewsByUid,
addTrackPositionMenuPosition: null,
//chooseViewHandler: uid2 => this.handleZoomYanked(views[0].uid, uid2),
//chooseViewHandler: uid2 => this.handleZoomLockChosen(views[0].uid, uid2),
//chooseViewHandler: uid2 => this.handleCenterSynced(views[0].uid, uid2),
//chooseTrackHandler: (viewUid, trackUid) => this.handleViewportProjected(views[0].uid, viewUid, trackUid),
mouseOverOverlayUid: null,
configMenuUid: null,
exportLinkModalOpen: false,
exportLinkLocation: null
}
}
componentDidMount() {
// the addEventListener is necessary because TrackRenderer determines where to paint
// all the elements based on their bounding boxes. If the window isn't
// in focus, everything is drawn at the top and overlaps. When it gains
// focus we need to redraw everything in its proper place
window.addEventListener("focus", this.boundRefreshView);
this.element = ReactDOM.findDOMNode(this);
dictValues(this.state.views).map(v => {
if (!v.layout)
v.layout = this.generateViewLayout(v)
else {
v.layout.i = v.uid;
}
});
this.pixiRenderer = PIXI.autoDetectRenderer(this.state.width,
this.state.height,
{ view: this.canvasElement,
antialias: true,
transparent: true,
resolution: 2
} )
//PIXI.RESOLUTION=2;
// keep track of the width and height of this element, because it
// needs to be reflected in the size of our drawing surface
this.setState({mounted: true,
svgElement: this.svgElement,
canvasElement: this.canvasElement
});
ElementQueries.listen();
new ResizeSensor(this.element, function() {
//let heightOffset = this.element.offsetTop - this.element.parentNode.offsetTop
let heightOffset = 0;
this.setState({
height: this.element.clientHeight,
width: this.element.clientWidth
});
}.bind(this));
this.handleDragStart();
this.handleDragStop();
this.animate();
//this.handleExportViewsAsLink();
}
handleWindowFocused() {
/*
* The window housing this view gained focus. That means the bounding boxes
* may have changed so we need to redraw everything.
*
*/
}
addDefaultOptions(track) {
if (track.options)
return;
if (!tracksInfoByType.hasOwnProperty(track.type)) {
console.log("ERROR: track type not found:", track.type, " (check app/scripts/config.js for a list of defined track types)");
return;
}
if (!track.options) {
track.options = tracksInfoByType[track.type].defaultOptions;
}
}
animate() {
this.pixiRenderer.render(this.pixiStage);
this.frame = requestAnimationFrame(this.animate.bind(this));
}
onBreakpointChange(breakpoint) {
this.setState({
currentBreakpoint: breakpoint
});
};
handleOverlayMouseEnter(uid) {
this.setState({
mouseOverOverlayUid: uid
})
}
handleOverlayMouseLeave(uid) {
this.setState({
mouseOverOverlayUid: null
})
}
handleLockZoom(uid) {
/**
* We want to lock the zoom of this view to the zoom of another view.
*
* First we pick which other view we want to lock to.
*
* The we calculate the current zoom offset and center offset. The differences
* between the center of the two views will always remain the same, as will the
* different between the zoom levels.
*/
if (this.zoomLocks[uid]) {
// this view already has a zoom lock, we we need to turn it off
this.handleUnlockZoom(uid);
this.setState({
mouseOverOverlayUid: uid,
configMenuUid: null
});
return;
}
// create a view chooser and remove the config view menu
this.setState({
chooseViewHandler: uid2 => this.handleZoomLockChosen(uid, uid2),
mouseOverOverlayUid: uid,
configMenuUid: null
});
}
addScalesChangedListener(viewUid, listenerUid, eventHandler) {
/**
* Add an event listener that will be called every time the scale
* of the view with uid viewUid is changed.
*
* @param viewUid: The uid of the view being observed
* @param listenerUid: The uid of the listener
* @param eventHandler: The handler to be called when the scales change
* Event handler is called with parameters (xScale, yScale)
*/
if (!this.scalesChangedListeners.hasOwnProperty(viewUid)) {
this.scalesChangedListeners[viewUid] = {}
}
this.scalesChangedListeners[viewUid][listenerUid] = eventHandler;
// call the handler for the first time
eventHandler(this.xScales[viewUid], this.yScales[viewUid]);
}
removeScalesChangedListener(viewUid, listenerUid) {
/**
* Remove a scale change event listener
*
* @param viewUid: The view that it's listening on.
* @param listenerUid: The uid of the listener itself.
*/
if (this.scalesChangedListeners.hasOwnProperty(viewUid)) {
let listeners = this.scalesChangedListeners[viewUid];
if (listeners.hasOwnProperty(listenerUid))
delete listeners[listenerUid];
}
}
handleScalesChanged(uid, xScale, yScale, notify=true) {
/*
* The scales of some view have changed (presumably in response to zooming).
*
* Mark the new scales and update any locked views.
*
* @param uid: The view of whom the scales have changed.
*/
this.xScales[uid] = xScale;
this.yScales[uid] = yScale;
if (notify) {
if (this.scalesChangedListeners.hasOwnProperty(uid)) {
dictValues(this.scalesChangedListeners[uid]).forEach(x => {
x(xScale, yScale);
});
}
}
if (this.zoomLocks[uid]) {
// this view is locked to another
let lockGroup = this.zoomLocks[uid];
let lockGroupItems = dictItems(lockGroup);
let [centerX, centerY, k] = scalesCenterAndK(this.xScales[uid], this.yScales[uid]);
for (let i = 0; i < lockGroupItems.length; i++) {
let key = lockGroupItems[i][0];
let value = lockGroupItems[i][1];
if (key == uid) // no need to notify oneself that the scales have changed
continue
let dx = value[0] - lockGroup[uid][0];
let dy = value[1] - lockGroup[uid][1];
let rk = value[2] - lockGroup[uid][2];
let newCenterX = centerX + dx;
let newCenterY = centerY + dy;
let newK = k + rk;
let [newXScale, newYScale] = this.setCenters[key](newCenterX, newCenterY, newK, false);
// because the setCenters call above has a 'false' notify, the new scales won't
// be propagated from there, so we have to store them here
this.xScales[key] = newXScale;
this.yScales[key] = newYScale;
// notify the listeners of all locked views that the scales of
// this view have changed
if (this.scalesChangedListeners.hasOwnProperty(key)) {
dictValues(this.scalesChangedListeners[key]).forEach(x => {
x(newXScale, newYScale);
});
}
}
/*
if (uid == zoomLock.source) {
let newCenterX = centerX + zoomLock.centerDiff[0];
let newCenterY = centerY + zoomLock.centerDiff[1];
let newK = k * zoomLock.zoomRatio;
// set a new center, but don't notify of a change to prevent
// circular notifications
this.setCenters[zoomLock.target](newCenterX, newCenterY, newK, false);
} else {
let newCenterX = centerX - zoomLock.centerDiff[0];
let newCenterY = centerY - zoomLock.centerDiff[1];
let newK = k / zoomLock.zoomRatio;
// set a new center, but don't notify of a change to prevent
// circular notifications
this.setCenters[zoomLock.source](newCenterX, newCenterY, newK, false);
}
*/
}
}
handleProjectViewport(uid) {
/**
* We want to show the extent of this viewport on another view.
*/
this.setState({
chooseTrackHandler: (viewUid, trackUid) => this.handleViewportProjected(uid, viewUid, trackUid),
configMenuUid: null
});
}
handleSyncCenter(uid) {
/**
* We want the center of this view to match the center of another
* view.
*
*/
this.setState({
chooseViewHandler: uid2 => this.handleCenterSynced(uid, uid2),
mouseOverOverlayUid: uid,
configMenuUid: null
});
}
handleYankZoom(uid) {
/**
* We want to match the zoom level of another view.
*
* That means synchronizing the center point and zoom level
* of the first view to the second view.
*/
this.setState({
chooseViewHandler: uid2 => this.handleZoomYanked(uid, uid2),
mouseOverOverlayUid: uid,
configMenuUid: null
});
}
handleUnlockZoom(uid) {
/**
* We want to unlock uid from the zoom group that it's in.
*
* @param uid: The uid of a view.
*/
// if this function is being called, lockGroup has to exist
let lockGroup = this.zoomLocks[uid];
let lockGroupKeys = dictKeys(lockGroup);
if (lockGroupKeys.length == 2) {
// there's only two items in this lock group so we need to
// remove them both (no point in having one view locked to itself)
delete this.zoomLocks[lockGroupKeys[0]];
delete this.zoomLocks[lockGroupKeys[1]];
return;
} else {
// delete this view from the zoomLockGroup
delete this.zoomLocks[uid][uid];
// remove the handler
delete this.zoomLocks[uid];
}
}
addZoomLock(uid1, uid2) {
let group1Members = [];
let group2Members = [];
if (!this.zoomLocks[uid1]) {
// view1 isn't already in a group
group1Members = [[uid1, scalesCenterAndK(this.xScales[uid1], this.yScales[uid1])]];
} else {
// view1 is already in a group
group1Members = dictItems(this.zoomLocks[uid1]).map(x =>
// x is [uid, [centerX, centerY, k]]
[x[0], scalesCenterAndK(this.xScales[x[0]], this.yScales[x[0]])]
)
}
if (!this.zoomLocks[uid2]) {
// view1 isn't already in a group
group2Members = [[uid2, scalesCenterAndK(this.xScales[uid2], this.yScales[uid2])]];
} else {
// view2 is already in a group
group2Members = dictItems(this.zoomLocks[uid2]).map(x =>
// x is [uid, [centerX, centerY, k]]
[x[0], scalesCenterAndK(this.xScales[x[0]], this.yScales[x[0]])]
)
}
let allMembers = group1Members.concat(group2Members);
let groupDict = dictFromTuples(allMembers);
allMembers.forEach(m => { this.zoomLocks[m[0]] = groupDict });
}
handleZoomLockChosen(uid1, uid2) {
/* Views uid1 and uid2 need to be locked so that they always maintain the current
* zoom and translation difference.
* @param uid1: The view that the lock was called from
* @param uid2: The view that the lock was called on (the view that was selected)
*/
if (uid1 == uid2) {
this.setState({
chooseViewHandler: null
});
return; // locking a view to itself is silly
}
this.addZoomLock(uid1, uid2);
this.setState({
chooseViewHandler: null
});
}
handleViewportProjected(fromView, toView, toTrack) {
/**
* We want to project the viewport of fromView onto toTrack of toView.
*
* @param fromView: The uid of the view that we want to project
* @param toView: The uid of the view that we want to project to
* @param toTrack: The track we want to project to
*/
let hostTrack = getTrackByUid(this.state.views[toView].tracks, toTrack);
let position = getTrackPositionByUid(this.state.views[toView].tracks, toTrack);
let newTrack = {
uid: slugid.nice(),
type: 'viewport-projection-' + position,
fromViewUid: fromView
}
this.addCallbacks(toView, newTrack);
this.handleTrackAdded(toView, newTrack, position, hostTrack);
this.setState({
chooseTrackHandler: null
});
}
handleCenterSynced(uid1, uid2) {
/**
* Uid1 is copying the center of uid2
*/
// where we're taking the zoom from
let sourceXScale = this.xScales[uid2];
let sourceYScale = this.yScales[uid2];
let targetXScale = this.xScales[uid1];
let targetYScale = this.yScales[uid1];
let [targetCenterX, targetCenterY, targetK] = scalesCenterAndK(targetXScale, targetYScale);
let [sourceCenterX, sourceCenterY, sourceK] = scalesCenterAndK(sourceXScale, sourceYScale);
// set target center
this.setCenters[uid1](sourceCenterX,sourceCenterY, targetK, false);
this.setState({
chooseViewHandler: null
});
}
handleZoomYanked(uid1, uid2) {
/**
* Uid1 yanked the zoom of uid2, now make sure that they're synchronized.
*/
// where we're taking the zoom from
let sourceXScale = this.xScales[uid2];
let sourceYScale = this.yScales[uid2];
let [sourceCenterX, sourceCenterY, sourceK] = scalesCenterAndK(sourceXScale, sourceYScale);
// set target center
this.setCenters[uid1](sourceCenterX,sourceCenterY, sourceK, false);
this.setState({
chooseViewHandler: null
});
}
handleConfigMenuOpened(uid) {
/**
* The user clicked on the `cog` of the menu so we need to open
* it.
*/
let bbox = this.configImg[uid].getBoundingClientRect();
this.setState({
configMenuUid: uid,
configMenuPosition: bbox
});
}
handleAddTrackPositionMenuOpened(uid) {
/**
* The user has clicked on the 'plus' sign at the top of a TiledPlot
* so we need to open the Track Position Chooser dialog
*/
let bbox = this.plusImg[uid].getBoundingClientRect();
this.setState({
addTrackPositionMenuUid: uid,
addTrackPositionMenuPosition: bbox
});
}
handleTrackPositionChosen(position) {
/**
* The user has chosen a position for the new track. The actual
* track selection will be handled by TiledPlot
*
* We just need to close the menu here.
*/
this.setState({
addTrackPositionMenuUid: null,
addTrackPositionMenuPosition: null
});
}
handleLayoutChange(layout, layouts) {
/**
* Notify the children that the layout has changed so that they
* know to redraw themselves
*/
this.handleDragStart();
this.handleDragStop();
layout.forEach(l => {
let correspondingView = this.state.views[l.i];
if (correspondingView) {
correspondingView.layout = l;
}
});
};
clearDragTimeout() {
/**
* Maybe somebody started dragging again before the previous drag
* timeout fired. In that case, we need to clear this timeout so
* that it doesn't override a previously set one.
*/
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
this.dragTimeout = null;
}
}
refreshView() {
this.clearDragTimeout();
this.setState({
dragging: true
})
this.clearDragTimeout();
this.dragTimeout = setTimeout(() => {
this.setState({
dragging: false
})}, SHORT_DRAG_TIMEOUT);
}
handleDragStart(layout, oldItem, newItem, placeholder, e, element) {
this.clearDragTimeout();
this.setState({
dragging: true
})
}
handleDragStop() {
// wait for the CSS transitions to end before
// turning off the dragging state
this.clearDragTimeout();
this.dragTimeout = setTimeout(() => {
this.setState({
dragging: false
})}, LONG_DRAG_TIMEOUT);
}
onNewLayout() {
};
onResize(layout, oldItem, newItem, placeholder, e, element) {
}
fillInMinWidths(tracksDict) {
/**
* If tracks don't have specified dimensions, add in the known
* minimums
*
* Operates on the tracks stored for this TiledPlot.
*/
let horizontalLocations = ['top', 'bottom'];
// first make sure all track types are specified
// this will make the code later on simpler
if (!('center' in tracksDict))
tracksDict['center'] = [];
if (!('left' in tracksDict))
tracksDict['left'] = [];
if (!('right' in tracksDict))
tracksDict['right'] = [];
if (!('top' in tracksDict))
tracksDict['top'] = [];
if (!('bottom' in tracksDict))
tracksDict['bottom'] = [];
for (let j = 0; j < horizontalLocations.length; j++) {
let tracks = tracksDict[horizontalLocations[j]];
//e.g. no 'top' tracks
if (!tracks)
continue;
for (let i = 0; i < tracks.length; i++) {
if (!('height' in tracks[i])) {
tracks[i].height = this.minHorizontalHeight;
}
}
}
let verticalLocations = ['left', 'right'];
for (let j = 0; j < verticalLocations.length; j++) {
let tracks = tracksDict[verticalLocations[j]];
//e.g. no 'left' tracks
if (!tracks)
continue;
for (let i = 0; i < tracks.length; i++) {
if (!('width' in tracks[i]))
tracks[i].width = this.minVerticalWidth;
}
}
return tracksDict;
}
calculateViewDimensions(view) {
/**
* Get the dimensions for this view, counting just the tracks
* that are present in it
*
* @param view: A view containing a list of tracks as a member.
* @return: A width and a height pair (e.g. [width, height])
*/
let defaultHorizontalHeight = 20;
let defaultVerticalWidth = 0;
let defaultCenterHeight = 100;
let defaultCenterWidth = 100;
let currHeight = this.horizontalMargin * 2;
let currWidth = this.verticalMargin * 2; //currWidth will generally be ignored because it will just be set to
//the width of the enclosing container
if (view.tracks.top) {
// tally up the height of the top tracks
for (let i = 0; i < view.tracks.top.length; i++) {
let track = view.tracks.top[i];
currHeight += track.height ? track.height : defaultHorizontalHeight;
}
}
if (view.tracks.bottom) {
// tally up the height of the top tracks
for (let i = 0; i < view.tracks.bottom.length; i++) {
let track = view.tracks.bottom[i];
currHeight += track.height ? track.height : defaultHorizontalHeight;
}
}
if (view.tracks.left) {
// tally up the height of the top tracks
for (let i = 0; i < view.tracks.left.length; i++) {
let track = view.tracks.left[i];
currWidth += track.width ? track.width : defaultVerticalWidth;
}
}
if (view.tracks.right) {
// tally up the height of the top tracks
for (let i = 0; i < view.tracks.right.length; i++) {
let track = view.tracks.right[i];
currWidth += track.width ? track.width : defaultVerticalWidth;
}
}
let centerHeight = 0;
let centerWidth = 0;
if (view.center) {
centerHeight = view.center.height ? view.center.height : defaultCenterHeight;
currHeight += centerHeight;
centerWidth = view.center.width ? view.center.width : defaultCenterWidth;
currWidth += centerWidth;
} else if ((view.tracks.top || view.tracks.bottom) && (view.tracks.left || view.tracks.right)) {
currHeight += defaultCenterWidth;
currWidth += defaultCenterWidth;
}
let topHeight = 0;
let bottomHeight = 0;
let leftWidth = 0;
let rightWidth = 0;
if ('top' in view.tracks)
topHeight = view.tracks['top']
.map((x) => { return x.height ? x.height : defaultHorizontalHeight; })
.reduce((a,b) => { return a + b; }, 0);
if ('bottom' in view.tracks)
bottomHeight = view.tracks['bottom']
.map((x) => { return x.height ? x.height : defaultHorizontalHeight; })
.reduce((a,b) => { return a + b; }, 0);
if ('left' in view.tracks)
leftWidth = view.tracks['left']
.map((x) => { return x.width ? x.width : defaultVerticalWidth; })
.reduce((a,b) => { return a + b; }, 0);
if ('right' in view.tracks)
rightWidth = view.tracks['right']
.map((x) => { return x.width ? x.width : defaultVerticalWidth ; })
.reduce((a,b) => { return a + b; }, 0);
return {'totalWidth': currWidth,
'totalHeight': currHeight,
'topHeight': topHeight,
'bottomHeight': bottomHeight,
'leftWidth': leftWidth,
'rightWidth': rightWidth,
'centerWidth': centerWidth,
'centerHeight': centerHeight};
}
generateViewLayout(view) {
let layout = null;
if ('layout' in view)
layout = view.layout
else {
let minTrackHeight = 30;
let elementWidth = this.element.clientWidth;
let {totalWidth, totalHeight,
topHeight, bottomHeight,
leftWidth, rightWidth,
centerWidth, centerHeight} = this.calculateViewDimensions(view);
if (view.searchBox)
totalHeight += 30;
let heightGrid = Math.ceil(totalHeight / this.rowHeight);
layout = {
x: 0,
y: 0,
w: 6,
};
if ('center' in view.tracks || 'left' in view.tracks || 'right' in view.tracks) {
let desiredHeight = ((elementWidth - leftWidth - rightWidth - 2 * this.horizontalMargin) );
desiredHeight += topHeight + bottomHeight + 2*this.verticalMargin + 20;
// how much height is left in the browser?
// limit the height of the container to the window height
// the number 160 is relatively arbitrary and should be
// replaced with a concrete measure of the element below and
// above the canvas area
let availableHeight = window.innerHeight - 160;
if (desiredHeight > availableHeight )
desiredHeight = availableHeight;
// stretch the view out
layout.h = Math.ceil(desiredHeight / this.rowHeight);
}
else
layout.h = heightGrid;
layout.minH = heightGrid;
layout.i = slugid.nice();
}
return layout;
}
handleCloseView(uid) {
/**
* A view needs to be closed. Remove it from from the viewConfig and then clean
* up all of its connections (zoom links, workers, etc...)
*
* @param {uid} This view's identifier
*/
// check if this is the only view
// if it is, don't close it (display an error message)
if (dictValues(this.state.views).length == 1) {
console.log("Can't close the only view");
return;
}
delete this.state.views[uid];
// might want to notify the views that they're beig closed
this.setState({
'views': this.state.views
});
}
handleSeriesAdded(viewId, newTrack, position, hostTrack) {
/**
* We're adding a new dataset to an existing track
*
* @param newTrack: The new track to be added.
* @param position: Where the new series should be placed.
* (This could also be inferred from the hostTrack, but since
* we already have it, we might as well use it)
* @param hostTrack: The track that will host the new series.
*/
// is the host track a combined track?
// if so, easy, just append the new track to its contents
// if not, remove the current track from the track list
// create a new combined track, add the current and the new
// tracks and then update the whole track list
let tracks = this.state.views[viewId].tracks;
if (hostTrack.type == 'combined') {
hostTrack.contents.push(newTrack);
} else {
let newHost = { type: 'combined',
uid: slugid.nice(),
height: hostTrack.height,
width: hostTrack.width,
contents: [hostTrack, newTrack] }
let positionTracks = tracks[position];
for (let i = 0; i < positionTracks.length; i++) {
if (positionTracks[i].uid == hostTrack.uid)
positionTracks[i] = newHost;
}
}
this.setState({
views: this.state.views
});
}
handleTrackAdded(viewId, newTrack, position, host=null) {
/**
* A track was added from the AddTrackModal dialog.
*
* @param trackInfo: A JSON object that can be used as a track
* definition
* @param position: The position the track is being added to
* @param host: If this track is being added to another track
*/
this.addDefaultOptions(newTrack);
this.addNameToTrack(newTrack);
if (host) {
// we're adding a series rather than a whole new track
this.handleSeriesAdded(viewId, newTrack, position, host);
return;
}
newTrack.width = this.minVerticalWidth;
newTrack.height = this.minHorizontalHeight;
let tracks = this.state.views[viewId].tracks;
if (position == 'left' || position == 'top') {
// if we're adding a track on the left or the top, we want the
// new track to appear at the begginning of the track list
tracks[position].unshift(newTrack);
} else if (position == 'center') {
// we're going to have to either overlay the existing track with a new one
// or add another one on top
if (tracks['center'].length == 0) {
// no existing tracks
let newCombined = {
uid: slugid.nice(),
type: 'combined',
contents: [
newTrack ]
}
tracks['center'] = [newCombined];
} else {
// center track exists
if (tracks['center'][0].type == 'combined') {
// if it's a combined track, we just need to add this track to the
// contents
tracks['center'][0].contents.push(newTrack);
} else {
// if it's not, we have to create a new combined track
let newCombined = {
uid: slugid.nice(),
type: 'combined',
contents: [
tracks['center'][0],
newTrack ]
}
tracks['center'] = [newCombined];
}
}
} else {
// otherwise, we want it at the end of the track list
tracks[position].push(newTrack);
}
}
handleCloseTrack(viewId, uid) {
let tracks = this.state.views[viewId].tracks;
for (let trackType in tracks) {
let theseTracks = tracks[trackType];
let newTracks = theseTracks.filter((d) => { return d.uid != uid; });
if (newTracks.length == theseTracks.length) {
// no whole tracks need to removed, see if any of the combined tracks
// contain series which need to go
let combinedTracks = newTracks.filter(x => x.type == 'combined')
combinedTracks.forEach(ct => {
ct.contents = ct.contents.filter(x => x.uid != uid);
});