Skip to content

Commit 8f05316

Browse files
committed
legend: keep scrollbar position after toggle
* Only set the scrollbar position during first render. * Only register the wheel and drag listeners during first render. * If the scrollbar is to be shown, ensure only the expanded scrollbox is drawn. Fixes #578
1 parent b5c9827 commit 8f05316

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

src/components/legend/draw.js

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -198,86 +198,91 @@ module.exports = function draw(gd) {
198198
// legend, background and border, scroll box and scroll bar
199199
Lib.setTranslate(legend, lx, ly);
200200

201-
bg.attr({
202-
width: legendWidth - opts.borderwidth,
203-
height: legendHeight - opts.borderwidth,
204-
x: opts.borderwidth / 2,
205-
y: opts.borderwidth / 2
206-
});
207-
208-
var scrollPosition = scrollBox.attr('data-scroll') || 0;
209-
210-
Lib.setTranslate(scrollBox, 0, scrollPosition);
201+
var scrollBarYMax = legendHeight -
202+
constants.scrollBarHeight -
203+
2 * constants.scrollBarMargin,
204+
scrollBoxYMax = opts.height - legendHeight,
205+
scrollBarY,
206+
scrollBoxY;
207+
208+
if(opts.height <= legendHeight || gd._context.staticPlot) {
209+
// if scrollbar should not be shown.
210+
bg.attr({
211+
width: legendWidth - opts.borderwidth,
212+
height: legendHeight - opts.borderwidth,
213+
x: opts.borderwidth / 2,
214+
y: opts.borderwidth / 2
215+
});
211216

212-
clipPath.select('rect').attr({
213-
width: legendWidth - 2 * opts.borderwidth,
214-
height: legendHeight - 2 * opts.borderwidth,
215-
x: opts.borderwidth - scrollPosition,
216-
y: opts.borderwidth
217-
});
217+
Lib.setTranslate(scrollBox, 0, 0);
218218

219-
scrollBox.call(Drawing.setClipUrl, clipId);
219+
clipPath.select('rect').attr({
220+
width: legendWidth - 2 * opts.borderwidth,
221+
height: legendHeight - 2 * opts.borderwidth,
222+
x: opts.borderwidth,
223+
y: opts.borderwidth
224+
});
220225

221-
// If scrollbar should be shown.
222-
if(opts.height - legendHeight > 0 && !gd._context.staticPlot) {
226+
scrollBox.call(Drawing.setClipUrl, clipId);
227+
}
228+
else {
229+
scrollBarY = constants.scrollBarMargin,
230+
scrollBoxY = scrollBox.attr('data-scroll') || 0;
223231

224232
// increase the background and clip-path width
225233
// by the scrollbar width and margin
226234
bg.attr({
227235
width: legendWidth -
228236
2 * opts.borderwidth +
229237
constants.scrollBarWidth +
230-
constants.scrollBarMargin
238+
constants.scrollBarMargin,
239+
height: legendHeight - opts.borderwidth,
240+
x: opts.borderwidth / 2,
241+
y: opts.borderwidth / 2
231242
});
232243

233244
clipPath.select('rect').attr({
234245
width: legendWidth -
235246
2 * opts.borderwidth +
236247
constants.scrollBarWidth +
237-
constants.scrollBarMargin
248+
constants.scrollBarMargin,
249+
height: legendHeight - 2 * opts.borderwidth,
250+
x: opts.borderwidth,
251+
y: opts.borderwidth - scrollBoxY
238252
});
239253

240-
if(gd.firstRender) {
241-
// Move scrollbar to starting position
242-
scrollHandler(constants.scrollBarMargin, 0);
243-
}
254+
scrollBox.call(Drawing.setClipUrl, clipId);
244255

245-
var scrollBarYMax = legendHeight -
246-
constants.scrollBarHeight -
247-
2 * constants.scrollBarMargin,
248-
scrollBoxYMax = opts.height - legendHeight,
249-
scrollBarY = constants.scrollBarMargin,
250-
scrollBoxY = 0;
251-
252-
scrollHandler(scrollBarY, scrollBoxY);
253-
254-
legend.on('wheel', null);
255-
legend.on('wheel', function() {
256-
scrollBoxY = Lib.constrain(
257-
scrollBox.attr('data-scroll') -
258-
d3.event.deltaY / scrollBarYMax * scrollBoxYMax,
259-
-scrollBoxYMax, 0);
260-
scrollBarY = constants.scrollBarMargin -
261-
scrollBoxY / scrollBoxYMax * scrollBarYMax;
256+
if(gd.firstRender) {
262257
scrollHandler(scrollBarY, scrollBoxY);
263-
d3.event.preventDefault();
264-
});
265258

266-
scrollBar.on('.drag', null);
267-
scrollBox.on('.drag', null);
268-
var drag = d3.behavior.drag().on('drag', function() {
269-
scrollBarY = Lib.constrain(
270-
d3.event.y - constants.scrollBarHeight / 2,
271-
constants.scrollBarMargin,
272-
constants.scrollBarMargin + scrollBarYMax);
273-
scrollBoxY = - (scrollBarY - constants.scrollBarMargin) /
274-
scrollBarYMax * scrollBoxYMax;
275-
scrollHandler(scrollBarY, scrollBoxY);
276-
});
259+
legend.on('wheel', null);
260+
legend.on('wheel', function() {
261+
scrollBoxY = Lib.constrain(
262+
scrollBox.attr('data-scroll') -
263+
d3.event.deltaY / scrollBarYMax * scrollBoxYMax,
264+
-scrollBoxYMax, 0);
265+
scrollBarY = constants.scrollBarMargin -
266+
scrollBoxY / scrollBoxYMax * scrollBarYMax;
267+
scrollHandler(scrollBarY, scrollBoxY);
268+
d3.event.preventDefault();
269+
});
277270

278-
scrollBar.call(drag);
279-
scrollBox.call(drag);
271+
scrollBar.on('.drag', null);
272+
scrollBox.on('.drag', null);
273+
var drag = d3.behavior.drag().on('drag', function() {
274+
scrollBarY = Lib.constrain(
275+
d3.event.y - constants.scrollBarHeight / 2,
276+
constants.scrollBarMargin,
277+
constants.scrollBarMargin + scrollBarYMax);
278+
scrollBoxY = - (scrollBarY - constants.scrollBarMargin) /
279+
scrollBarYMax * scrollBoxYMax;
280+
scrollHandler(scrollBarY, scrollBoxY);
281+
});
280282

283+
scrollBar.call(drag);
284+
scrollBox.call(drag);
285+
}
281286
}
282287

283288

0 commit comments

Comments
 (0)