@@ -73,6 +73,8 @@ ScrollBox.barColor = '#808BA4';
73
73
* @param {number } position.t Top side (in pixels)
74
74
* @param {number } position.w Width (in pixels)
75
75
* @param {number } position.h Height (in pixels)
76
+ * @param {string } [position.direction='down']
77
+ * Either 'down', 'left', 'right' or 'up'
76
78
* @param {number } [translateX=0] Horizontal offset (in pixels)
77
79
* @param {number } [translateY=0] Vertical offset (in pixels)
78
80
*/
@@ -88,48 +90,55 @@ ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
88
90
w = this . position . w ,
89
91
t = this . position . t ,
90
92
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' ) ,
91
98
boxW = w ,
92
99
boxH = h ,
93
100
boxL , boxR ,
94
101
boxT , boxB ;
95
102
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
+ }
102
107
103
- if ( 0 <= l && l <= fullWidth ) {
108
+ var isVertical = isDown || isUp ;
109
+ if ( isVertical ) {
104
110
boxL = l ;
105
111
boxR = boxL + boxW ;
106
- }
107
- else {
108
- // align left
109
- boxL = 0 ;
110
- boxR = boxL + boxW ;
111
- }
112
112
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
+ }
116
125
}
117
126
else {
118
- // align top
119
- boxT = 0 ;
127
+ boxT = t ;
120
128
boxB = boxT + boxH ;
121
- }
122
-
123
- if ( boxR > fullWidth ) {
124
- // align right
125
- boxR = fullWidth ;
126
- boxL = boxR - boxW ;
127
- }
128
129
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
+ }
133
142
}
134
143
135
144
this . _box = {
@@ -143,8 +152,11 @@ ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
143
152
var needsHorizontalScrollBar = ( w > boxW ) ,
144
153
hbarW = ScrollBox . barLength + 2 * ScrollBox . barPad ,
145
154
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 ;
148
160
149
161
var hbar = this . container . selectAll ( 'rect.scrollbar-horizontal' ) . data (
150
162
( needsHorizontalScrollBar ) ? [ 0 ] : [ ] ) ;
@@ -181,8 +193,11 @@ ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
181
193
var needsVerticalScrollBar = ( h > boxH ) ,
182
194
vbarW = ScrollBox . barWidth + 2 * ScrollBox . barPad ,
183
195
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 ;
186
201
187
202
var vbar = this . container . selectAll ( 'rect.scrollbar-vertical' ) . data (
188
203
( needsVerticalScrollBar ) ? [ 0 ] : [ ] ) ;
0 commit comments