Skip to content

Commit

Permalink
fix(internal): Fix on unexpected resize event firing (#572)
Browse files Browse the repository at this point in the history
The call of getBoundingClientRect on elements with visibility:hidden
w/o defining position, can possibly cause 'resize' event.

Fix #571
Close #572
  • Loading branch information
netil authored Aug 31, 2018
1 parent 6b81526 commit dd5af9c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
4 changes: 3 additions & 1 deletion spec/internals/bb-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ describe("Interface & initialization", () => {
container.style.width = width + "px";

// run the resize handler
d3.select(window).on("resize.bb")();
chart.internal.api.internal.charts.forEach(c => {
c.internal.resizeFunction();
});

setTimeout(() => {
expect(+chart1.internal.svg.attr("width")).to.be.equal(width);
Expand Down
3 changes: 1 addition & 2 deletions src/api/api.chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Copyright (c) 2017 NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {select as d3Select} from "d3-selection";
import Chart from "../internals/Chart";
import {window} from "../internals/browser";
import {notEmpty, isDefined, extend} from "../internals/util";
Expand Down Expand Up @@ -77,7 +76,7 @@ extend(Chart.prototype, {
// clear timers
isDefined($$.resizeTimeout) && window.clearTimeout($$.resizeTimeout);

d3Select(window).on("resize.bb", null);
window.removeEventListener("resize", $$.resizeFunction);
$$.selectChart.classed("bb", false).html("");

// releasing references
Expand Down
5 changes: 4 additions & 1 deletion src/axis/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ export default class Axis {
!isYAxis && this.updateXAxisTickValues(targetsToShow, axis);

const dummy = $$.selectChart.append("svg")
.style("visibility", "hidden");
.style("visibility", "hidden")
.style("position", "fixed")
.style("top", "0px")
.style("left", "0px");

dummy.call(axis).selectAll("text")
.each(function() {
Expand Down
31 changes: 8 additions & 23 deletions src/internals/ChartInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {transition as d3Transition} from "d3-transition";

import Axis from "../axis/Axis";
import CLASS from "../config/classes";
import {notEmpty, asHalfPixel, getOption, isValue, isArray, isFunction, isDefined, isString, isNumber, isObject, callFn} from "./util";
import {notEmpty, asHalfPixel, getOption, isValue, isArray, isFunction, isString, isNumber, isObject, callFn} from "./util";

/**
* Internal chart class.
Expand Down Expand Up @@ -1124,24 +1124,19 @@ export default class ChartInternal {

if (config.resize_auto) {
$$.resizeFunction.add(() => {
isDefined($$.resizeTimeout) &&
if ($$.resizeTimeout) {
window.clearTimeout($$.resizeTimeout);
$$.resizeTimeout = null;
}

$$.resizeTimeout = window.setTimeout(() => {
delete $$.resizeTimeout;
$$.api.flush();
}, 100);
$$.resizeTimeout = window.setTimeout($$.api.flush, 100);
});
}

$$.resizeFunction.add(() => config.onresized.call($$));

// attach resize event
// get the possible previous attached
const resizeEvents = d3Select(window).on("resize.bb");

resizeEvents && $$.resizeFunction.add(resizeEvents);
d3Select(window).on("resize.bb", $$.resizeFunction);
window.addEventListener("resize", $$.resizeFunction);
}

generateResize() {
Expand All @@ -1151,18 +1146,8 @@ export default class ChartInternal {
resizeFunctions.forEach(f => f());
}

callResizeFunctions.add = function(f) {
resizeFunctions.push(f);
};

callResizeFunctions.remove = function(f) {
for (let i = 0, len = resizeFunctions.length; i < len; i++) {
if (resizeFunctions[i] === f) {
resizeFunctions.splice(i, 1);
break;
}
}
};
callResizeFunctions.add = f => resizeFunctions.push(f);
callResizeFunctions.remove = f => resizeFunctions.splice(resizeFunctions.indexOf(f), 1);

return callResizeFunctions;
}
Expand Down

0 comments on commit dd5af9c

Please sign in to comment.