1- if ( typeof exports !== 'undefined' ) {
2- if ( typeof module !== 'undefined' && module . exports ) {
3- exports = module . exports = graphScroll ;
4- }
5- exports . graphScroll = graphScroll ;
6- } else {
7- this . graphScroll = graphScroll ;
8- }
9-
10- function graphScroll ( ) {
11- var windowHeight ,
12- dispatch = d3 . dispatch ( "scroll" , "active" ) ,
13- sections = d3 . select ( 'null' ) ,
14- i = - 1 ,
15- sectionPos = [ ] ,
16- n ,
17- graph = d3 . select ( 'null' ) ,
18- isFixed = null ,
19- isBelow = null ,
20- container = d3 . select ( 'body' ) ,
21- containerStart = 0 ,
22- belowStart ,
23- eventId = Math . random ( ) ,
24- stickyTop
25-
26- function reposition ( ) {
27- var i1 = 0
28- sectionPos . forEach ( function ( d , i ) {
29- if ( d < pageYOffset - containerStart + 180 ) i1 = i
30- } )
31- i1 = Math . min ( n - 1 , i1 )
32- if ( i != i1 ) {
33- sections . classed ( 'graph-scroll-active' , function ( d , i ) { return i === i1 } )
34-
35- dispatch . active . call ( sections [ 0 ] [ i1 ] , i1 , i )
36-
37- i = i1
38- }
39-
40- var isBelow1 = pageYOffset > belowStart - 120
41- if ( isBelow != isBelow1 ) {
42- isBelow = isBelow1
43- graph . classed ( 'graph-scroll-below' , isBelow )
44- }
45-
46- var isFixed1 = ! isBelow && pageYOffset > containerStart - 120
47- if ( isFixed != isFixed1 ) {
48- isFixed = isFixed1
49- graph
50- . classed ( 'graph-scroll-fixed' , isFixed )
1+ ( function ( ) {
2+ if ( typeof exports !== 'undefined' ) {
3+ if ( typeof module !== 'undefined' && module . exports ) {
4+ exports = module . exports = graphScroll ;
515 }
6+ exports . graphScroll = graphScroll ;
7+ } else {
8+ this . graphScroll = graphScroll ;
9+ }
5210
53- if ( stickyTop ) {
54- graph . style ( 'padding-top' , ( isBelow || isFixed ? stickyTop : 0 ) + 'px' )
11+ function graphScroll ( ) {
12+ var windowHeight ,
13+ dispatch = d3 . dispatch ( "scroll" , "active" ) ,
14+ sections = d3 . select ( 'null' ) ,
15+ i = - 1 ,
16+ sectionPos = [ ] ,
17+ n ,
18+ graph = d3 . select ( 'null' ) ,
19+ isFixed = null ,
20+ isBelow = null ,
21+ container = d3 . select ( 'body' ) ,
22+ containerStart = 0 ,
23+ belowStart ,
24+ eventId = Math . random ( ) ,
25+ stickyTop
26+
27+ function reposition ( ) {
28+ var i1 = 0
29+ sectionPos . forEach ( function ( d , i ) {
30+ if ( d < pageYOffset - containerStart + 180 ) i1 = i
31+ } )
32+ i1 = Math . min ( n - 1 , i1 )
33+ if ( i != i1 ) {
34+ sections . classed ( 'graph-scroll-active' , function ( d , i ) { return i === i1 } )
35+
36+ dispatch . active . call ( sections [ 0 ] [ i1 ] , i1 , i )
37+
38+ i = i1
39+ }
40+
41+ var isBelow1 = pageYOffset > belowStart - 120
42+ if ( isBelow != isBelow1 ) {
43+ isBelow = isBelow1
44+ graph . classed ( 'graph-scroll-below' , isBelow )
45+ }
46+
47+ var isFixed1 = ! isBelow && pageYOffset > containerStart - 120
48+ if ( isFixed != isFixed1 ) {
49+ isFixed = isFixed1
50+ graph
51+ . classed ( 'graph-scroll-fixed' , isFixed )
52+ }
53+
54+ if ( stickyTop ) {
55+ graph . style ( 'padding-top' , ( isBelow || isFixed ? stickyTop : 0 ) + 'px' )
56+ }
5557 }
56- }
5758
58- function resize ( ) {
59- sectionPos = [ ]
60- var startPos
61- sections . each ( function ( d , i ) {
62- if ( ! i ) startPos = this . getBoundingClientRect ( ) . top
63- sectionPos . push ( this . getBoundingClientRect ( ) . top - startPos ) } )
59+ function resize ( ) {
60+ sectionPos = [ ]
61+ var startPos
62+ sections . each ( function ( d , i ) {
63+ if ( ! i ) startPos = this . getBoundingClientRect ( ) . top
64+ sectionPos . push ( this . getBoundingClientRect ( ) . top - startPos ) } )
6465
65- var containerBB = container . node ( ) . getBoundingClientRect ( )
66- var graphBB = graph . node ( ) . getBoundingClientRect ( )
66+ var containerBB = container . node ( ) . getBoundingClientRect ( )
67+ var graphBB = graph . node ( ) . getBoundingClientRect ( )
6768
68- containerStart = containerBB . top + pageYOffset
69- belowStart = containerBB . bottom - graphBB . height + pageYOffset
70- }
69+ containerStart = containerBB . top + pageYOffset
70+ belowStart = containerBB . bottom - graphBB . height + pageYOffset
71+ }
7172
72- function keydown ( ) {
73- if ( ! isFixed ) return
74- var delta
75- switch ( d3 . event . keyCode ) {
76- case 39 : // right arrow
77- if ( d3 . event . metaKey ) return
78- case 40 : // down arrow
79- case 34 : // page down
80- delta = d3 . event . metaKey ? Infinity : 1 ; break
81- case 37 : // left arrow
82- if ( d3 . event . metaKey ) return
83- case 38 : // up arrow
84- case 33 : // page up
85- delta = d3 . event . metaKey ? - Infinity : - 1 ; break
86- case 32 : // space
87- delta = d3 . event . shiftKey ? - 1 : 1
88- ; break
89- default : return
73+ function keydown ( ) {
74+ if ( ! isFixed ) return
75+ var delta
76+ switch ( d3 . event . keyCode ) {
77+ case 39 : // right arrow
78+ if ( d3 . event . metaKey ) return
79+ case 40 : // down arrow
80+ case 34 : // page down
81+ delta = d3 . event . metaKey ? Infinity : 1 ; break
82+ case 37 : // left arrow
83+ if ( d3 . event . metaKey ) return
84+ case 38 : // up arrow
85+ case 33 : // page up
86+ delta = d3 . event . metaKey ? - Infinity : - 1 ; break
87+ case 32 : // space
88+ delta = d3 . event . shiftKey ? - 1 : 1
89+ ; break
90+ default : return
91+ }
92+
93+ var i1 = Math . max ( 0 , Math . min ( i + delta , n - 1 ) )
94+ rv . scrollTo ( i1 )
95+
96+ d3 . event . preventDefault ( )
9097 }
9198
92- var i1 = Math . max ( 0 , Math . min ( i + delta , n - 1 ) )
93- rv . scrollTo ( i1 )
9499
95- d3 . event . preventDefault ( )
96- }
100+ var rv = { }
97101
102+ rv . scrollTo = function ( _x ) {
103+ if ( isNaN ( _x ) ) return rv
98104
99- var rv = { }
105+ d3 . select ( document . documentElement )
106+ . interrupt ( )
107+ . transition ( )
108+ . duration ( 500 )
109+ . tween ( "scroll" , function ( ) {
110+ var i = d3 . interpolateNumber ( pageYOffset , sectionPos [ _x ] + containerStart )
111+ return function ( t ) { scrollTo ( 0 , i ( t ) ) }
112+ } )
113+ return rv
114+ }
100115
101- rv . scrollTo = function ( _x ) {
102- if ( isNaN ( _x ) ) return rv
103116
104- d3 . select ( document . documentElement )
105- . interrupt ( )
106- . transition ( )
107- . duration ( 500 )
108- . tween ( "scroll" , function ( ) {
109- var i = d3 . interpolateNumber ( pageYOffset , sectionPos [ _x ] + containerStart )
110- return function ( t ) { scrollTo ( 0 , i ( t ) ) }
111- } )
112- return rv
113- }
117+ rv . container = function ( _x ) {
118+ if ( ! _x ) return container
114119
120+ container = _x
121+ return rv
122+ }
115123
116- rv . container = function ( _x ) {
117- if ( ! _x ) return container
124+ rv . graph = function ( _x ) {
125+ if ( ! _x ) return graph
118126
119- container = _x
120- return rv
121- }
127+ graph = _x
128+ return rv
129+ }
122130
123- rv . graph = function ( _x ) {
124- if ( ! _x ) return graph
131+ rv . eventId = function ( _x ) {
132+ if ( ! _x ) return eventId
125133
126- graph = _x
127- return rv
128- }
134+ eventId = _x
135+ return rv
136+ }
129137
130- rv . eventId = function ( _x ) {
131- if ( ! _x ) return eventId
138+ rv . stickyTop = function ( _x ) {
139+ if ( ! _x ) return stickyTop
132140
133- eventId = _x
134- return rv
135- }
141+ stickyTop = _x
142+ return rv
143+ }
136144
137- rv . stickyTop = function ( _x ) {
138- if ( ! _x ) return stickyTop
145+ rv . sections = function ( _x ) {
146+ if ( ! _x ) return sections
139147
140- stickyTop = _x
141- return rv
142- }
148+ sections = _x
149+ n = sections . size ( )
143150
144- rv . sections = function ( _x ) {
145- if ( ! _x ) return sections
151+ d3 . select ( window )
152+ . on ( 'scroll.gscroll' + eventId , reposition )
153+ . on ( 'resize.gscroll' + eventId , resize )
154+ . on ( 'keydown.gscroll' + eventId , keydown )
146155
147- sections = _x
148- n = sections . size ( )
156+ resize ( )
157+ d3 . timer ( function ( ) {
158+ reposition ( )
159+ return true
160+ } )
149161
150- d3 . select ( window )
151- . on ( 'scroll.gscroll' + eventId , reposition )
152- . on ( 'resize.gscroll' + eventId , resize )
153- . on ( 'keydown.gscroll' + eventId , keydown )
162+ return rv
163+ }
154164
155- resize ( )
156- d3 . timer ( function ( ) {
157- reposition ( )
158- return true
159- } )
165+ d3 . rebind ( rv , dispatch , "on" )
160166
161167 return rv
162168 }
163-
164- d3 . rebind ( rv , dispatch , "on" )
165-
166- return rv
167- }
169+
170+ } ) . call ( this )
0 commit comments