Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject iframe for responsive charts only #3364

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,28 @@ module.exports = function(Chart) {

me.id = helpers.uid();
me.chart = instance;
me.config = instance.config;
me.options = me.config.options;
me.config = config;
me.options = config.options;

// Add the chart instance to the global namespace
Chart.instances[me.id] = me;

Object.defineProperty(me, 'data', {
get: function() {
return me.config.data;
}
});

// Always bind this so that if the responsive state changes we still work
helpers.addResizeListener(canvas.parentNode, function() {
if (me.config.options.responsive) {
// Responsiveness is currently based on the use of an iframe, however this method causes
// performance issues and could be troublesome when used with ad blockers. So make sure
// that the user is still able to create a chart without iframe when responsive is false.
// See https://github.com/chartjs/Chart.js/issues/2210
if (me.options.responsive) {
helpers.addResizeListener(canvas.parentNode, function() {
me.resize();
}
});

// Add the chart instance to the global namespace
Chart.instances[me.id] = me;
});

if (me.options.responsive) {
// Silent resize before chart draws
// Initial resize before chart draws (must be silent to preserve initial animations).
me.resize(true);
}

Expand Down Expand Up @@ -684,11 +685,11 @@ module.exports = function(Chart) {
me.stop();
me.clear();

helpers.unbindEvents(me, me.events);

if (canvas) {
helpers.unbindEvents(me, me.events);
helpers.removeResizeListener(canvas.parentNode);
releaseCanvas(canvas);
me.chart.canvas = null;
}

// if we scaled the canvas in response to a devicePixelRatio !== 1, we need to undo that transform here
Expand Down
17 changes: 13 additions & 4 deletions src/core/core.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,15 +980,24 @@ module.exports = function(Chart) {
// Insert the iframe so that contentWindow is available
node.insertBefore(iframe, node.firstChild);

// Let's keep track of this added iframe and thus avoid DOM query when removing it.
node._chartjs = {
resizer: iframe
};

this.addEvent(iframe.contentWindow || iframe, 'resize', callback);
};
helpers.removeResizeListener = function(node) {
var hiddenIframe = node.querySelector('.chartjs-hidden-iframe');
if (!node || !node._chartjs) {
return;
}

// Remove the resize detect iframe
if (hiddenIframe) {
hiddenIframe.parentNode.removeChild(hiddenIframe);
var iframe = node._chartjs.resizer;
if (iframe) {
iframe.parentNode.removeChild(iframe);
}

delete node._chartjs;
};
helpers.isArray = Array.isArray?
function(obj) {
Expand Down
32 changes: 31 additions & 1 deletion test/core.controller.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ describe('Chart.Controller', function() {
rw: 165, rh: 85,
});
});

it('should NOT inject the resizer element', function() {
var chart = acquireChart({
options: {
responsive: false
}
});

var wrapper = chart.chart.canvas.parentNode;
expect(wrapper.childNodes.length).toBe(1);
expect(wrapper.firstChild.tagName).toBe('CANVAS');
});
});

describe('config.options.responsive: true (maintainAspectRatio: false)', function() {
Expand Down Expand Up @@ -449,7 +461,6 @@ describe('Chart.Controller', function() {
describe('controller.destroy', function() {
it('should restore canvas (and context) initial values', function(done) {
var chart = acquireChart({
type: 'line',
options: {
responsive: true,
maintainAspectRatio: false
Expand Down Expand Up @@ -484,5 +495,24 @@ describe('Chart.Controller', function() {
done();
});
});

it('should remove the resizer element when responsive: true', function() {
var chart = acquireChart({
options: {
responsive: true
}
});

var wrapper = chart.chart.canvas.parentNode;
var resizer = wrapper.firstChild;

expect(wrapper.childNodes.length).toBe(2);
expect(resizer.tagName).toBe('IFRAME');

chart.destroy();

expect(wrapper.childNodes.length).toBe(1);
expect(wrapper.firstChild.tagName).toBe('CANVAS');
});
});
});
8 changes: 4 additions & 4 deletions test/mockContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,23 @@
window.document.body.appendChild(wrapper);

chart = new Chart(canvas.getContext("2d"), config);
chart.__test_persistent = options.persistent;
chart._test_persistent = options.persistent;
chart._test_wrapper = wrapper;
charts[chart.id] = chart;
return chart;
}

function releaseChart(chart) {
chart.destroy();
chart.chart.canvas.parentNode.remove();
chart._test_wrapper.remove();
delete charts[chart.id];
delete chart;
}

afterEach(function() {
// Auto releasing acquired charts
for (var id in charts) {
var chart = charts[id];
if (!chart.__test_persistent) {
if (!chart._test_persistent) {
releaseChart(chart);
}
}
Expand Down