/
GenomeView.js
executable file
·2542 lines (2193 loc) · 89.1 KB
/
GenomeView.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
define([
'dojo/_base/declare',
'dojo/_base/array',
'dojo/dom-construct',
'JBrowse/Util',
'JBrowse/has',
'dojo/dnd/move',
'dojo/dnd/Source',
'dijit/focus',
'JBrowse/Component',
'JBrowse/FeatureFiltererMixin',
'JBrowse/View/Track/LocationScale',
'JBrowse/View/Track/GridLines',
'JBrowse/BehaviorManager',
'JBrowse/View/Animation/Zoomer',
'JBrowse/View/Animation/Slider',
'JBrowse/View/InfoDialog'
], function(
declare,
array,
domConstruct,
Util,
has,
dndMove,
dndSource,
dijitFocus,
Component,
FeatureFiltererMixin,
LocationScaleTrack,
GridLinesTrack,
BehaviorManager,
Zoomer,
Slider,
InfoDialog
) {
var dojof = Util.dojof;
// weird subclass of dojo dnd constrained mover to make the location
// thumb behave better
var locationThumbMover = declare( dndMove.constrainedMoveable, {
constructor: function(node, params){
this.constraints = function(){
var n = this.node.parentNode,
mb = dojo.marginBox(n);
mb.t = 0;
return mb;
};
}
});
/**
* Main view class, shows a scrollable, horizontal view of annotation
* tracks. NOTE: All coordinates are interbase.
* @class
* @constructor
*/
return declare( [Component,FeatureFiltererMixin], {
constructor: function( args ) {
var browser = args.browser;
var elem = args.elem;
var stripeWidth = args.stripeWidth;
var refseq = args.refSeq;
var zoomLevel = args.zoomLevel;
this.desiredTracks = {};
// keep a reference to the main browser object
this.browser = browser;
this.setFeatureFilterParentComponent( this.browser );
this.focusTrack = null;
//the page element that the GenomeView lives in
this.elem = elem;
this.posHeight = this.calculatePositionLabelHeight( elem );
// Add an arbitrary 50% padding between the position labels and the
// topmost track
this.topSpace = this.posHeight*1.5;
// handle trackLabels option
if (typeof browser.config.trackLabels !== 'undefined' && browser.config.trackLabels === "no-block") {
this.config.trackPadding = 35;
this.topSpace = this.posHeight*3;
}
// WebApollo needs max zoom level to be sequence residues char width
this.maxPxPerBp = this.config.maxPxPerBp;
//the reference sequence
this.ref = refseq;
//current scale, in pixels per bp
this.pxPerBp = zoomLevel;
//width, in pixels, of the vertical stripes
this.stripeWidth = stripeWidth;
// the scrollContainer is the element that changes position
// when the user scrolls
this.scrollContainer = dojo.create(
'div', {
id: 'container',
style: { position: 'absolute',
left: '0px',
top: '0px'
}
}, elem
);
this._renderVerticalScrollBar();
// we have a separate zoomContainer as a child of the scrollContainer.
// they used to be the same element, but making zoomContainer separate
// enables it to be narrower than this.elem.
this.zoomContainer = document.createElement("div");
this.zoomContainer.id = "zoomContainer";
this.zoomContainer.style.cssText =
"position: absolute; left: 0px; top: 0px; height: 100%;";
this.scrollContainer.appendChild(this.zoomContainer);
this.outerTrackContainer = document.createElement("div");
this.outerTrackContainer.className = "trackContainer outerTrackContainer";
this.outerTrackContainer.style.cssText = "height: 100%;";
this.zoomContainer.appendChild( this.outerTrackContainer );
this.trackContainer = document.createElement("div");
this.trackContainer.className = "trackContainer innerTrackContainer draggable";
this.trackContainer.style.cssText = "height: 100%;";
this.outerTrackContainer.appendChild( this.trackContainer );
//width, in pixels of the "regular" (not min or max zoom) stripe
this.regularStripe = stripeWidth;
this.overview = this.browser.overviewDiv;
this.overviewBox = dojo.marginBox(this.overview);
this.tracks = [];
this.uiTracks = [];
this.trackIndices = {};
//set up size state (zoom levels, stripe percentage, etc.)
this.sizeInit();
//distance, in pixels, from the beginning of the reference sequence
//to the beginning of the first active stripe
// should always be a multiple of stripeWidth
this.offset = 0;
//largest value for the sum of this.offset and this.getX()
//this prevents us from scrolling off the right end of the ref seq
this.maxLeft = this.bpToPx(this.ref.end+1) - this.getWidth();
//smallest value for the sum of this.offset and this.getX()
//this prevents us from scrolling off the left end of the ref seq
this.minLeft = this.bpToPx(this.ref.start);
//extra margin to draw around the visible area, in multiples of the visible area
//0: draw only the visible area; 0.1: draw an extra 10% around the visible area, etc.
this.drawMargin = 0.2;
//slide distance (pixels) * slideTimeMultiple + 200 = milliseconds for slide
//1=1 pixel per millisecond average slide speed, larger numbers are slower
this.slideTimeMultiple = 0.8;
this.trackHeights = [];
this.trackTops = [];
this.waitElems = dojo.filter( [ dojo.byId("moveLeft"), dojo.byId("moveRight"),
dojo.byId("zoomIn"), dojo.byId("zoomOut"),
dojo.byId("bigZoomIn"), dojo.byId("bigZoomOut"),
document.body, elem ],
function(e) { return e; }
);
this.prevCursors = [];
this.locationThumb = document.createElement("div");
this.locationThumb.className = "locationThumb";
this.overview.appendChild(this.locationThumb);
this.locationThumbMover = new locationThumbMover(this.locationThumb, {area: "content", within: true});
this.x = this.elem.scrollLeft;
this.y = 0;
var scaleTrackDiv = document.createElement("div");
scaleTrackDiv.className = "track static_track rubberBandAvailable";
scaleTrackDiv.style.height = this.posHeight + "px";
scaleTrackDiv.id = "static_track";
this.scaleTrackDiv = scaleTrackDiv;
this.staticTrack = new LocationScaleTrack({
label: "static_track",
labelClass: "pos-label",
posHeight: this.posHeight,
browser: this.browser,
refSeq: this.ref
});
this.staticTrack.setViewInfo( this, function(height) {}, this.stripeCount,
this.scaleTrackDiv, this.stripePercent,
this.stripeWidth, this.pxPerBp,
this.config.trackPadding);
this.zoomContainer.appendChild(this.scaleTrackDiv);
this.waitElems.push(this.scaleTrackDiv);
var gridTrackDiv = document.createElement("div");
gridTrackDiv.className = "track";
gridTrackDiv.style.cssText = "top: 0px; height: 100%;";
gridTrackDiv.id = "gridtrack";
var gridTrack = new GridLinesTrack({
browser: this.browser,
refSeq: this.ref
});
gridTrack.setViewInfo( this, function(height) {}, this.stripeCount,
gridTrackDiv, this.stripePercent,
this.stripeWidth, this.pxPerBp,
this.config.trackPadding);
this.trackContainer.appendChild(gridTrackDiv);
this.uiTracks = [this.staticTrack, gridTrack];
// accept tracks being dragged into this
this.trackDndWidget =
new dndSource(
this.trackContainer,
{
accept: ["track"], //accepts only tracks into the viewing field
withHandles: true,
creator: dojo.hitch( this, function( trackConfig, hint ) {
return {
data: trackConfig,
type: ["track"],
node: hint == 'avatar'
? dojo.create('div', { innerHTML: trackConfig.key || trackConfig.label, className: 'track-label dragging' })
: this.renderTrack( trackConfig )
};
})
});
// subscribe to showTracks commands
this.browser.subscribe(
'/dnd/drop',
dojo.hitch(
this,
function( source, nodes, copy, target ) {
this.updateTrackList();
if( target.node === this.trackContainer ) {
// if dragging into the trackcontainer, we are showing some tracks
// get the configs from the tracks being dragged in
var confs = dojo.filter( dojo.map( nodes, function(n) {
return n.track && n.track.config;
}),
function(c) {return c;}
);
this.browser.publish( '/jbrowse/v1/v/tracks/show', confs );
}
}
)
);
this.browser.subscribe( '/jbrowse/v1/c/tracks/show', dojo.hitch( this, 'showTracks' ));
this.browser.subscribe( '/jbrowse/v1/c/tracks/hide', dojo.hitch( this, 'hideTracks' ));
this.browser.subscribe( '/jbrowse/v1/c/tracks/replace', dojo.hitch( this, 'replaceTracks' ));
this.browser.subscribe( '/jbrowse/v1/c/tracks/delete', dojo.hitch( this, 'hideTracks' ));
this.browser.subscribe( '/jbrowse/v1/c/tracks/pin', dojo.hitch( this, 'pinTracks' ));
this.browser.subscribe( '/jbrowse/v1/c/tracks/unpin', dojo.hitch( this, 'unpinTracks' ));
// render our UI tracks (horizontal scale tracks, grid lines, and so forth)
dojo.forEach(this.uiTracks, function(track) {
track.showRange(0, this.stripeCount - 1,
Math.round(this.pxToBp(this.offset)),
Math.round(this.stripeWidth / this.pxPerBp),
this.pxPerBp);
}, this);
this.addOverviewTrack(new LocationScaleTrack({
label: "overview_loc_track",
labelClass: "overview-pos",
posHeight: this.overviewPosHeight,
browser: this.browser,
refSeq: this.ref
}));
this.showFine();
this.showCoarse();
// initialize the behavior manager used for setting what this view
// does (i.e. the behavior it has) for mouse and keyboard events
this.behaviorManager = new BehaviorManager({ context: this, behaviors: this._behaviors() });
this.behaviorManager.initialize();
},
_defaultConfig: function() {
return {
maxPxPerBp: 20,
trackPadding: 20 // distance in pixels between each track
};
},
/**
* @returns {Object} containing ref, start, and end members for the currently displayed location
*/
visibleRegion: function() {
return {
ref: this.ref.name,
start: this.minVisible(),
end: this.maxVisible()
};
},
/**
* @returns {String} locstring representation of the current location<br>
* (suitable for passing to the browser's navigateTo)
*/
visibleRegionLocString: function() {
return Util.assembleLocString( this.visibleRegion() );
},
/**
* Create and place the elements for the vertical scrollbar.
* @private
*/
_renderVerticalScrollBar: function() {
var container = dojo.create(
'div',
{
className: 'vertical_scrollbar',
style: { position: 'absolute',
right: '0px',
bottom: '0px',
height: '100%',
width: '10px',
zIndex: 1000
}
},
this.browser.container
);
var positionMarker = dojo.create(
'div',
{
className: 'vertical_position_marker',
style: {
position: 'absolute',
height: '100%'
}
},
container
);
this.verticalScrollBar = { container: container, positionMarker: positionMarker, width: container.offsetWidth };
},
/**
* Update the position and look of the vertical scroll bar as our
* y-scroll offset changes.
* @private
*/
_updateVerticalScrollBar: function( newDims ) {
if( typeof newDims.height == 'number' ) {
var heightAdjust = this.staticTrack ? -this.staticTrack.div.offsetHeight : 0;
var trackPaneHeight = newDims.height + heightAdjust;
this.verticalScrollBar.container.style.height = trackPaneHeight-(this.pinUnderlay ? this.pinUnderlay.offsetHeight+heightAdjust : 0 ) +'px';
var markerHeight = newDims.height / (this.containerHeight||1) * 100;
this.verticalScrollBar.positionMarker.style.height = markerHeight > 0.5 ? markerHeight+'%' : '1px';
if( newDims.height / (this.containerHeight||1) > 0.98 ) {
this.verticalScrollBar.container.style.display = 'none';
this.verticalScrollBar.visible = false;
} else {
this.verticalScrollBar.container.style.display = 'block';
this.verticalScrollBar.visible = true;
}
}
if( typeof newDims.y == 'number' || typeof newDims.height == 'number' ) {
this.verticalScrollBar.positionMarker.style.top = (((newDims.y || this.getY() || 0) / (this.containerHeight||1) * 100 )||0)+'%';
}
},
verticalScrollBarVisibleWidth: function() {
return this.verticalScrollBar.visible && this.verticalScrollBar.width || 0;
},
/**
* @returns {Array[Track]} of the tracks that are currently visible in
* this genomeview
*/
visibleTracks: function() {
return this.tracks;
},
/**
* @returns {Array[String]} of the names of tracks that are currently visible in this genomeview
*/
visibleTrackNames: function() {
return dojo.map( this.visibleTracks(), function(t){ return t.name; } );
},
/**
* Called in response to a keyboard or mouse event to slide the view
* left or right.
*/
keySlideX: function( offset ) {
this.setX( this.getX() + offset );
var thisB = this;
if( ! this._keySlideTimeout )
this._keySlideTimeout = window.setTimeout(
function() {
thisB.afterSlide();
delete thisB._keySlideTimeout;
}, 300 );
},
/**
* Behaviors (event handler bundles) for various states that the
* GenomeView might be in.
* @private
* @returns {Object} description of behaviors
*/
_behaviors: function() { return {
// behaviors that don't change
always: {
apply_on_init: true,
apply: function() {
var handles = [];
handles.push( dojo.connect(
this.overview, 'mousedown',
dojo.hitch( this, 'startRubberZoom',
dojo.hitch(this,'overview_absXtoBp'),
this.overview,
this.overview
)
));
var wheelevent = "onwheel" in document.createElement("div") ? "wheel" :
document.onmousewheel !== undefined ? "mousewheel" :
"DOMMouseScroll";
handles.push(
dojo.connect( this.scrollContainer, wheelevent, this, 'wheelScroll', false ),
dojo.connect( this.verticalScrollBar.container, 'onclick', this, 'scrollBarClickScroll', false ),
dojo.connect( this.scaleTrackDiv, "mousedown",
dojo.hitch( this, 'startRubberZoom',
dojo.hitch( this,'absXtoBp'),
this.scrollContainer,
this.scaleTrackDiv
)
),
dojo.connect( this.outerTrackContainer, "dblclick", this, 'doubleClickZoom' ),
dojo.connect( this.locationThumbMover, "onMoveStop", this, 'thumbMoved' ),
dojo.connect( this.overview, "onclick", this, 'overviewClicked' ),
dojo.connect( this.scaleTrackDiv, "onclick", this, 'scaleClicked' ),
dojo.connect( this.scaleTrackDiv, "mouseover", this, 'scaleMouseOver' ),
dojo.connect( this.scaleTrackDiv, "mouseout", this, 'scaleMouseOut' ),
dojo.connect( this.scaleTrackDiv, "mousemove", this, 'scaleMouseMove' ),
dojo.connect( document.body, 'onkeyup', this, function(evt) {
if( evt.keyCode == dojo.keys.SHIFT ) // shift
this.behaviorManager.swapBehaviors( 'shiftMouse', 'normalMouse' );
}),
dojo.connect( document.body, 'onkeydown', this, function(evt) {
if( evt.keyCode == dojo.keys.SHIFT ) // shift
this.behaviorManager.swapBehaviors( 'normalMouse', 'shiftMouse' );
}),
// scroll the view around in response to keyboard arrow keys
dojo.connect( document.body, 'onkeypress', this, function(evt) {
// if some digit widget is focused, don't move the
// genome view with arrow keys
if( dijitFocus.curNode )
return;
var that = this;
if( evt.keyCode == dojo.keys.LEFT_ARROW || evt.keyCode == dojo.keys.RIGHT_ARROW ) {
var offset = evt.keyCode == dojo.keys.LEFT_ARROW ? -40 : 40;
if( evt.shiftKey )
offset *= 5;
this.keySlideX( offset );
}
else if( evt.keyCode == dojo.keys.DOWN_ARROW || evt.keyCode == dojo.keys.UP_ARROW ) {
// shift-up/down zooms in and out
if( evt.shiftKey ) {
this[ evt.keyCode == dojo.keys.UP_ARROW ? 'zoomIn' : 'zoomOut' ]( evt, 0.5, evt.altKey ? 2 : 1 );
}
// without shift, scrolls up and down
else {
var offset = evt.keyCode == dojo.keys.UP_ARROW ? -40 : 40;
this.setY( this.getY() + offset );
}
}
}),
// when the track pane is clicked, unfocus any dijit
// widgets that would otherwise not give up the focus
dojo.connect( this.scrollContainer, 'onclick', this, function(evt) {
dijitFocus.curNode && dijitFocus.curNode.blur();
})
);
return handles;
}
},
// mouse events connected for "normal" behavior
normalMouse: {
apply_on_init: true,
apply: function() {
return [
dojo.connect( this.outerTrackContainer, "mousedown", this, 'startMouseDragScroll' ),
dojo.connect( this.verticalScrollBar.container, "mousedown", this, 'startVerticalMouseDragScroll')
];
}
},
// mouse events connected when we are in 'highlighting' mode,
// where dragging the mouse sets the global highlight
highlightingMouse: {
apply: function() {
dojo.removeClass(this.trackContainer,'draggable');
dojo.addClass(this.trackContainer,'highlightingAvailable');
return [
dojo.connect( this.outerTrackContainer, "mousedown",
dojo.hitch( this, 'startMouseHighlight',
dojo.hitch(this,'absXtoBp'),
this.scrollContainer,
this.scaleTrackDiv
)
),
dojo.connect( this.outerTrackContainer, "mouseover", this, 'maybeDrawVerticalPositionLine' ),
dojo.connect( this.outerTrackContainer, "mousemove", this, 'maybeDrawVerticalPositionLine' )
];
},
remove: function( mgr, handles ) {
dojo.forEach( handles, dojo.disconnect, dojo );
dojo.removeClass(this.trackContainer,'highlightingAvailable');
dojo.addClass(this.trackContainer,'draggable');
}
},
// mouse events connected when the shift button is being held down
shiftMouse: {
apply: function() {
if ( !dojo.hasClass(this.trackContainer, 'highlightingAvailable') ){
dojo.removeClass(this.trackContainer,'draggable');
dojo.addClass(this.trackContainer,'rubberBandAvailable');
return [
dojo.connect( this.outerTrackContainer, "mousedown",
dojo.hitch( this, 'startRubberZoom',
dojo.hitch(this,'absXtoBp'),
this.scrollContainer,
this.scaleTrackDiv
)
),
dojo.connect( this.outerTrackContainer, "onclick", this, 'scaleClicked' ),
dojo.connect( this.outerTrackContainer, "mouseover", this, 'maybeDrawVerticalPositionLine' ),
dojo.connect( this.outerTrackContainer, "mousemove", this, 'maybeDrawVerticalPositionLine' )
];
}
},
remove: function( mgr, handles ) {
this.clearBasePairLabels();
this.clearVerticalPositionLine();
dojo.forEach( handles, dojo.disconnect, dojo );
dojo.removeClass(this.trackContainer,'rubberBandAvailable');
dojo.addClass(this.trackContainer,'draggable');
}
},
// mouse events that are connected when we are in the middle of a
// drag-scrolling operation
mouseDragScrolling: {
apply: function() {
return [
dojo.connect(document.body, "mouseup", this, 'dragEnd' ),
dojo.connect(document.body, "mousemove", this, 'dragMove' ),
dojo.connect(document.body, "mouseout", this, 'checkDragOut' )
];
}
},
// mouse events that are connected when we are in the middle of a
// vertical-drag-scrolling operation
verticalMouseDragScrolling: {
apply: function() {
return [
dojo.connect(document.body, "mouseup", this, 'dragEnd' ),
dojo.connect(document.body, "mousemove", this, 'verticalDragMove'),
dojo.connect(document.body, "mouseout", this, 'checkDragOut' )
];
}
},
// mouse events that are connected when we are in the middle of a
// rubber-band zooming operation
mouseRubberBanding: {
apply: function() {
return [
dojo.connect(document.body, "mouseup", this, 'rubberExecute' ),
dojo.connect(document.body, "mousemove", this, 'rubberMove' ),
dojo.connect(document.body, "mouseout", this, 'rubberCancel' ),
dojo.connect(window, "onkeydown", this, function(e){
if( e.keyCode !== dojo.keys.SHIFT )
this.rubberCancel(e);
})
];
}
}
};},
/**
* Conduct a DOM test to calculate the height of div.pos-label
* elements with a line of text in them.
*/
calculatePositionLabelHeight: function( containerElement ) {
// measure the height of some arbitrary text in whatever font this
// shows up in (set by an external CSS file)
var heightTest = document.createElement("div");
heightTest.className = "pos-label";
heightTest.style.visibility = "hidden";
heightTest.appendChild(document.createTextNode("42"));
containerElement.appendChild(heightTest);
var h = heightTest.clientHeight;
containerElement.removeChild(heightTest);
return h;
},
scrollBarClickScroll : function( event ) {
if ( !event )
event = window.event;
var containerHeight = parseInt( this.verticalScrollBar.container.style.height,10 );
var markerHeight = parseInt( this.verticalScrollBar.positionMarker.style.height,10 );
var trackContainerHeight = this.trackContainer.clientHeight;
var absY = this.getY()*( trackContainerHeight/containerHeight );
if ( absY > event.clientY )
this.setY( this.getY() - 300 );
else if (absY + markerHeight < event.clientY)
this.setY( this.getY() + 300 );
//the timeout is so that we don't have to run showVisibleBlocks
//for every scroll wheel click (we just wait until so many ms
//after the last one).
if ( this.wheelScrollTimeout )
window.clearTimeout( this.wheelScrollTimeout );
// 100 milliseconds since the last scroll event is an arbitrary
// cutoff for deciding when the user is done scrolling
// (set by a bit of experimentation)
this.wheelScrollTimeout = window.setTimeout( dojo.hitch( this, function() {
this.showVisibleBlocks(true);
this.wheelScrollTimeout = null;
}, 100));
dojo.stopEvent(event);
},
wheelScroll: function( event ) {
if ( !event )
event = window.event;
// if( window.WheelEvent )
// event = window.WheelEvent;
var delta = { x: 0, y: 0 };
if( 'wheelDeltaX' in event ) {
delta.x = event.wheelDeltaX/2;
delta.y = event.wheelDeltaY/2;
}
else if( 'deltaX' in event ) {
var multiplier = navigator.userAgent.indexOf("OS X 10.9")!==-1 ? -5 : -40;
delta.x = Math.abs(event.deltaY) > Math.abs(2*event.deltaX) ? 0 : event.deltaX*multiplier;
delta.y = event.deltaY*-10;
}
else if( event.wheelDelta ) {
delta.y = event.wheelDelta/2;
if( window.opera )
delta.y = -delta.y;
}
else if( event.detail ) {
delta.y = -event.detail*100;
}
delta.x = Math.round( delta.x * 2 );
delta.y = Math.round( delta.y );
if( delta.x )
this.keySlideX( -delta.x );
if( delta.y )
// 60 pixels per mouse wheel event
this.setY( this.getY() - delta.y );
//the timeout is so that we don't have to run showVisibleBlocks
//for every scroll wheel click (we just wait until so many ms
//after the last one).
if ( this.wheelScrollTimeout )
window.clearTimeout( this.wheelScrollTimeout );
// 100 milliseconds since the last scroll event is an arbitrary
// cutoff for deciding when the user is done scrolling
// (set by a bit of experimentation)
this.wheelScrollTimeout = window.setTimeout( dojo.hitch( this, function() {
this.showVisibleBlocks(true);
this.wheelScrollTimeout = null;
}, 100));
dojo.stopEvent(event);
},
getX: function() {
return this.x || 0;
},
getY: function() {
return this.y || 0;
},
getHeight: function() {
return this.elemBox.h;
},
getWidth: function() {
return this.elemBox.w;
},
clampX: function(x) {
return Math.round( Math.max( Math.min( this.maxLeft - this.offset, x || 0),
this.minLeft - this.offset
)
);
},
clampY: function(y) {
return Math.round( Math.min( Math.max( 0, y || 0 ),
this.containerHeight- this.getHeight()
)
);
},
rawSetX: function(x) {
this.elem.scrollLeft = x;
this.x = x;
},
/**
* @returns the new x value that was set
*/
setX: function(x) {
x = this.clampX(x);
this.rawSetX( x );
this.updateStaticElements( { x: x } );
this.showFine();
return x;
},
rawSetY: function(y) {
this.y = y;
this.layoutTracks();
},
/**
* @returns the new y value that was set
*/
setY: function(y) {
y = this.clampY(y);
this.rawSetY(y);
this.updateStaticElements( { y: y } );
return y;
},
/**
* @private
*/
rawSetPosition: function(pos) {
this.rawSetX( pos.x );
this.rawSetY( pos.y );
return pos;
},
/**
* @param pos.x new x position
* @param pos.y new y position
*/
setPosition: function(pos) {
var x = this.clampX( pos.x );
var y = this.clampY( pos.y );
this.updateStaticElements( {x: x, y: y} );
this.rawSetX( x );
this.rawSetY( y );
this.showFine();
},
/**
* @returns {Object} as <code>{ x: 123, y: 456 }</code>
*/
getPosition: function() {
return { x: this.x, y: this.y };
},
zoomCallback: function() {
this.zoomUpdate();
},
afterSlide: function() {
this.showCoarse();
this.scrollUpdate();
this.showVisibleBlocks(true);
},
/**
* Suppress double-click events in the genome view for a certain amount of time, default 100 ms.
*/
suppressDoubleClick: function( /** Number */ time ) {
if( this._noDoubleClick ) {
window.clearTimeout( this._noDoubleClick );
}
var thisB = this;
this._noDoubleClick = window.setTimeout(
function(){ delete thisB._noDoubleClick; },
time || 100
);
},
doubleClickZoom: function(event) {
if( this._noDoubleClick ) return;
if( this.dragging ) return;
if( "animation" in this ) return;
// if we have a timeout in flight from a scaleClicked click,
// cancel it, cause it looks now like the user has actually
// double-clicked
if( this.scaleClickedTimeout ) window.clearTimeout( this.scaleClickedTimeout );
var zoomLoc = (event.pageX - dojo.position(this.elem, true).x) / this.getWidth();
if (event.shiftKey) {
this.zoomOut(event, zoomLoc, 2);
} else {
this.zoomIn(event, zoomLoc, 2);
}
dojo.stopEvent(event);
},
/** @private */
_beforeMouseDrag: function( event ) {
if ( this.animation ) {
if (this.animation instanceof Zoomer) {
dojo.stopEvent(event);
return 0;
} else {
this.animation.stop();
}
}
if (Util.isRightButton(event)) return 0;
dojo.stopEvent(event);
return 1;
},
/**
* Event fired when a user's mouse button goes down inside the main
* element of the genomeview.
*/
startMouseDragScroll: function(event) {
if( ! this._beforeMouseDrag(event) ) return;
this.behaviorManager.applyBehaviors('mouseDragScrolling');
this.dragStartPos = {x: event.clientX,
y: event.clientY};
this.winStartPos = this.getPosition();
},
/**
* Event fired when a user's mouse button goes down inside the vertical
* scroll bar element of the genomeview.
*/
startVerticalMouseDragScroll: function(event) {
if( ! this._beforeMouseDrag(event) ) return; // not sure what this is for.
this.behaviorManager.applyBehaviors('verticalMouseDragScrolling');
this.dragStartPos = {x: event.clientX,
y: event.clientY};
this.winStartPos = this.getPosition();
},
startMouseHighlight: function( absToBp, container, scaleDiv, event ) {
if( ! this._beforeMouseDrag(event) ) return;
this.behaviorManager.applyBehaviors('mouseRubberBanding');
this.rubberbanding = {
absFunc: absToBp,
container: container,
scaleDiv: scaleDiv,
message: 'Highlight region',
start: { x: event.clientX, y: event.clientY },
execute: function( start, end ) {
this.browser.setHighlightAndRedraw({ ref: this.ref.name, start: start, end: end });
}
};
this.winStartPos = this.getPosition();
},
/**
* Start a rubber-band dynamic zoom.
*
* @param {Function} absToBp function to convert page X coordinates to
* base pair positions on the reference sequence. Called in the
* context of the GenomeView object.
* @param {HTMLElement} container element in which to draw the
* rubberbanding highlight
* @param {Event} event the mouse event that's starting the zoom
*/
startRubberZoom: function( absToBp, container, scaleDiv, event ) {
if( ! this._beforeMouseDrag(event) ) return;
this.behaviorManager.applyBehaviors('mouseRubberBanding');
this.rubberbanding = {
absFunc: absToBp,
container: container,
scaleDiv: scaleDiv,
message: 'Zoom to region',
start: { x: event.clientX, y: event.clientY },
execute: function( h_start_bp, h_end_bp ) {
this.setLocation( this.ref, h_start_bp, h_end_bp );
}
};
this.winStartPos = this.getPosition();
this.clearVerticalPositionLine();
this.clearBasePairLabels();
},
_rubberStop: function(event) {
this.behaviorManager.removeBehaviors('mouseRubberBanding');
this.hideRubberHighlight();
this.clearBasePairLabels();
if( event )
dojo.stopEvent(event);
delete this.rubberbanding;
},
rubberCancel: function(event) {
var htmlNode = document.body.parentNode;
var bodyNode = document.body;
if ( !event || !(event.relatedTarget || event.toElement)
|| (htmlNode === (event.relatedTarget || event.toElement))
|| (bodyNode === (event.relatedTarget || event.toElement))) {
this._rubberStop(event);
}
},
rubberMove: function(event) {
this.setRubberHighlight( this.rubberbanding.start, { x: event.clientX, y: event.clientY } );
},
rubberExecute: function( event) {
var start = this.rubberbanding.start;
var end = { x: event.clientX, y: event.clientY };
var h_start_bp = Math.floor( this.rubberbanding.absFunc( Math.min(start.x,end.x) ) );
var h_end_bp = Math.ceil( this.rubberbanding.absFunc( Math.max(start.x,end.x) ) );
var exec = this.rubberbanding.execute;
this._rubberStop(event);
// cancel the rubber-zoom if the user has moved less than 3 pixels
if( Math.abs( start.x - end.x ) < 3 ) {
return;
}
exec.call( this, h_start_bp, h_end_bp );
},
// draws the rubber-banding highlight region from start.x to end.x
setRubberHighlight: function( start, end ) {
var container = this.rubberbanding.container,
container_coords = dojo.position(container,true);
var h = this.rubberHighlight || (function(){
var main = this.rubberHighlight = document.createElement("div");
main.className = 'rubber-highlight';
main.style.position = 'absolute';
main.style.zIndex = 20;
var text = document.createElement('div');
text.appendChild( document.createTextNode( this.rubberbanding.message ) );
main.appendChild(text);
text.style.position = 'relative';
text.style.top = (50-container_coords.y) + "px";
container.appendChild( main );