@@ -73,6 +73,8 @@ ScrollBox.barColor = '#808BA4';
7373 * @param {number } position.t Top side (in pixels)
7474 * @param {number } position.w Width (in pixels)
7575 * @param {number } position.h Height (in pixels)
76+ * @param {string } [position.direction='down']
77+ * Either 'down', 'left', 'right' or 'up'
7678 * @param {number } [translateX=0] Horizontal offset (in pixels)
7779 * @param {number } [translateY=0] Vertical offset (in pixels)
7880 */
@@ -88,48 +90,55 @@ ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
8890 w = this . position . w ,
8991 t = this . position . t ,
9092 h = this . position . h ,
93+ direction = this . position . direction ,
94+ isDown = ( direction === 'down' ) ,
95+ isLeft = ( direction === 'left' ) ,
96+ isRight = ( direction === 'right' ) ,
97+ isUp = ( direction === 'up' ) ,
9198 boxW = w ,
9299 boxH = h ,
93100 boxL , boxR ,
94101 boxT , boxB ;
95102
96- if ( boxW > fullWidth ) boxW = fullWidth / 4 ;
97- if ( boxH > fullHeight ) boxH = fullHeight / 4 ;
98-
99- var minSize = 4 + ( ScrollBox . barLength + 2 * ScrollBox . barPad ) ;
100- boxW = Math . max ( boxW , minSize ) ;
101- boxH = Math . max ( boxH , minSize ) ;
103+ if ( ! isDown && ! isLeft && ! isRight && ! isUp ) {
104+ this . position . direction = 'down' ;
105+ isDown = true ;
106+ }
102107
103- if ( 0 <= l && l <= fullWidth ) {
108+ var isVertical = isDown || isUp ;
109+ if ( isVertical ) {
104110 boxL = l ;
105111 boxR = boxL + boxW ;
106- }
107- else {
108- // align left
109- boxL = 0 ;
110- boxR = boxL + boxW ;
111- }
112112
113- if ( 0 <= t && t <= fullHeight ) {
114- boxT = t ;
115- boxB = boxT + boxH ;
113+ if ( isDown ) {
114+ // anchor to top side
115+ boxT = t ;
116+ boxB = Math . min ( boxT + boxH , fullHeight ) ;
117+ boxH = boxB - boxT ;
118+ }
119+ else {
120+ // anchor to bottom side
121+ boxB = t + boxH ;
122+ boxT = Math . max ( boxB - boxH , 0 ) ;
123+ boxH = boxB - boxT ;
124+ }
116125 }
117126 else {
118- // align top
119- boxT = 0 ;
127+ boxT = t ;
120128 boxB = boxT + boxH ;
121- }
122-
123- if ( boxR > fullWidth ) {
124- // align right
125- boxR = fullWidth ;
126- boxL = boxR - boxW ;
127- }
128129
129- if ( boxB > fullHeight ) {
130- // align bottom
131- boxB = fullHeight ;
132- boxT = boxB - boxH ;
130+ if ( isLeft ) {
131+ // anchor to right side
132+ boxR = l + boxW ;
133+ boxL = Math . max ( boxR - boxW , 0 ) ;
134+ boxW = boxR - boxL ;
135+ }
136+ else {
137+ // anchor to left side
138+ boxL = l ;
139+ boxR = Math . min ( boxL + boxW , fullWidth ) ;
140+ boxW = boxR - boxL ;
141+ }
133142 }
134143
135144 this . _box = {
@@ -143,8 +152,11 @@ ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
143152 var needsHorizontalScrollBar = ( w > boxW ) ,
144153 hbarW = ScrollBox . barLength + 2 * ScrollBox . barPad ,
145154 hbarH = ScrollBox . barWidth + 2 * ScrollBox . barPad ,
146- hbarL = boxL ,
147- hbarT = ( boxB + hbarH < fullHeight ) ? boxB : fullHeight - hbarH ;
155+ // draw horizontal scrollbar on the bottom side
156+ hbarL = l ,
157+ hbarT = t + h ;
158+
159+ if ( hbarT + hbarH > fullHeight ) hbarT = fullHeight - hbarH ;
148160
149161 var hbar = this . container . selectAll ( 'rect.scrollbar-horizontal' ) . data (
150162 ( needsHorizontalScrollBar ) ? [ 0 ] : [ ] ) ;
@@ -181,8 +193,11 @@ ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
181193 var needsVerticalScrollBar = ( h > boxH ) ,
182194 vbarW = ScrollBox . barWidth + 2 * ScrollBox . barPad ,
183195 vbarH = ScrollBox . barLength + 2 * ScrollBox . barPad ,
184- vbarL = ( boxR + vbarW < fullWidth ) ? boxR : fullWidth - vbarW ,
185- vbarT = boxT ;
196+ // draw vertical scrollbar on the right side
197+ vbarL = l + w ,
198+ vbarT = t ;
199+
200+ if ( vbarL + vbarW > fullWidth ) vbarL = fullWidth - vbarW ;
186201
187202 var vbar = this . container . selectAll ( 'rect.scrollbar-vertical' ) . data (
188203 ( needsVerticalScrollBar ) ? [ 0 ] : [ ] ) ;
0 commit comments