Skip to content

Commit f17773f

Browse files
committed
updatemenu: ScrollBox#setTranslate to take pixels
* Changed `ScrollBox#setTranslate(translateX, translateY)` to take pixels. * Previously, invoking `ScrollBox#setTranslate(xf, yf)` would require to access `ScrollBox#_box` to compute `xf` and `yf`. * Now, it's possible to drag the buttons in the scrollbox beyond the scrollbox.
1 parent ef5210e commit f17773f

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

src/components/updatemenus/draw.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,7 @@ function drawButtons(gd, gHeader, gButton, menuOpts) {
345345
}
346346
translateY -= constants.gapButton;
347347

348-
var yf = translateY / (scrollBox.position.h - scrollBox._box.h);
349-
scrollBox.setTranslate(0, yf);
348+
scrollBox.setTranslate(0, translateY);
350349
}
351350
}
352351
else {
@@ -357,8 +356,7 @@ function drawButtons(gd, gHeader, gButton, menuOpts) {
357356
}
358357
translateX -= constants.gapButton;
359358

360-
var xf = translateX / (scrollBox.position.w - scrollBox._box.w);
361-
scrollBox.setTranslate(xf, 0);
359+
scrollBox.setTranslate(translateX, 0);
362360
}
363361
}
364362
}

src/components/updatemenus/scrollbox.js

+34-38
Original file line numberDiff line numberDiff line change
@@ -287,24 +287,18 @@ ScrollBox.prototype.disable = function disable() {
287287
* @method
288288
*/
289289
ScrollBox.prototype._onBoxDrag = function onBarDrag() {
290-
var xf = this._xf,
291-
yf = this._yf;
290+
var translateX = this._translateX,
291+
translateY = this._translateY;
292292

293293
if(this._hbar) {
294-
var translateXMax = this.position.w - this._box.w;
295-
296-
xf = Lib.constrain(xf - d3.event.dx / translateXMax, 0, 1);
294+
translateX -= d3.event.dx;
297295
}
298-
else xf = 0;
299296

300297
if(this._vbar) {
301-
var translateYMax = this.position.h - this._box.h;
302-
303-
yf = Lib.constrain(yf - d3.event.dy / translateYMax, 0, 1);
298+
translateY -= d3.event.dy;
304299
}
305-
else yf = 0;
306300

307-
this.setTranslate(xf, yf);
301+
this.setTranslate(translateX, translateY);
308302
};
309303

310304
/**
@@ -313,53 +307,51 @@ ScrollBox.prototype._onBoxDrag = function onBarDrag() {
313307
* @method
314308
*/
315309
ScrollBox.prototype._onBarDrag = function onBarDrag() {
316-
var xf = this._xf,
317-
yf = this._yf;
310+
var translateX = this._translateX,
311+
translateY = this._translateY;
318312

319313
if(this._hbar) {
320-
var translateXMax = this.position.w - this._box.w,
321-
translateX = xf * translateXMax,
322-
xMin = translateX + this._hbarXMin,
314+
var xMin = translateX + this._hbarXMin,
323315
xMax = xMin + this._hbarTranslateMax,
324-
x = Lib.constrain(d3.event.x, xMin, xMax);
316+
x = Lib.constrain(d3.event.x, xMin, xMax),
317+
xf = (x - xMin) / (xMax - xMin);
325318

326-
xf = (x - xMin) / (xMax - xMin);
319+
var translateXMax = this.position.w - this._box.w;
320+
321+
translateX = xf * translateXMax;
327322
}
328-
else xf = 0;
329323

330324
if(this._vbar) {
331-
var translateYMax = this.position.h - this._box.h,
332-
translateY = yf * translateYMax,
333-
yMin = translateY + this._vbarYMin,
325+
var yMin = translateY + this._vbarYMin,
334326
yMax = yMin + this._vbarTranslateMax,
335-
y = Lib.constrain(d3.event.y, yMin, yMax);
327+
y = Lib.constrain(d3.event.y, yMin, yMax),
328+
yf = (y - yMin) / (yMax - yMin);
329+
330+
var translateYMax = this.position.h - this._box.h;
336331

337-
yf = (y - yMin) / (yMax - yMin);
332+
translateY = yf * translateYMax;
338333
}
339-
else yf = 0;
340334

341-
this.setTranslate(xf, yf);
335+
this.setTranslate(translateX, translateY);
342336
};
343337

344338
/**
345339
* Set clip path and scroll bar translate transform
346340
*
347341
* @method
348-
* @param {number} [xf=0] Horizontal position as a container fraction
349-
* @param {number} [yf=0] Vertical position as a container fraction
342+
* @param {number} [translateX=0] Horizontal offset (in pixels)
343+
* @param {number} [translateY=0] Vertical offset (in pixels)
350344
*/
351-
ScrollBox.prototype.setTranslate = function setTranslate(xf, yf) {
352-
xf = Lib.constrain(xf || 0, 0, 1);
353-
yf = Lib.constrain(yf || 0, 0, 1);
345+
ScrollBox.prototype.setTranslate = function setTranslate(translateX, translateY) {
346+
// store translateX and translateY (needed by mouse event handlers)
347+
var translateXMax = this.position.w - this._box.w,
348+
translateYMax = this.position.h - this._box.h;
354349

355-
// store xf and yf (needed by ScrollBox.prototype._on*Drag)
356-
this._xf = xf;
357-
this._yf = yf;
350+
translateX = Lib.constrain(translateX || 0, 0, translateXMax);
351+
translateY = Lib.constrain(translateY || 0, 0, translateYMax);
358352

359-
var translateXMax = this.position.w - this._box.w,
360-
translateYMax = this.position.h - this._box.h,
361-
translateX = xf * translateXMax,
362-
translateY = yf * translateYMax;
353+
this._translateX = translateX;
354+
this._translateY = translateY;
363355

364356
this.container.call(Lib.setTranslate,
365357
this._box.l - this.position.l - translateX,
@@ -373,12 +365,16 @@ ScrollBox.prototype.setTranslate = function setTranslate(xf, yf) {
373365
}
374366

375367
if(this._hbar) {
368+
var xf = translateX / translateXMax;
369+
376370
this._hbar.call(Lib.setTranslate,
377371
translateX + xf * this._hbarTranslateMax,
378372
translateY);
379373
}
380374

381375
if(this._vbar) {
376+
var yf = translateY / translateYMax;
377+
382378
this._vbar.call(Lib.setTranslate,
383379
translateX,
384380
translateY + yf * this._vbarTranslateMax);

0 commit comments

Comments
 (0)