Commit edcd907a authored by Masayuki Tanaka's avatar Masayuki Tanaka

Fix bindto to accept null and empty string

parent c1f4e97a
...@@ -158,7 +158,15 @@ ...@@ -158,7 +158,15 @@
if ($$.initBrush) { $$.initBrush(); } if ($$.initBrush) { $$.initBrush(); }
if ($$.initZoom) { $$.initZoom(); } if ($$.initZoom) { $$.initZoom(); }
$$.selectChart = typeof config.bindto.node === 'function' ? config.bindto : d3.select(config.bindto); if (!config.bindto) {
$$.selectChart = d3.selectAll([]);
}
else if (typeof config.bindto.node === 'function') {
$$.selectChart = config.bindto;
}
else {
$$.selectChart = d3.select(config.bindto);
}
if ($$.selectChart.empty()) { if ($$.selectChart.empty()) {
$$.selectChart = d3.select(document.createElement('div')).style('opacity', 0); $$.selectChart = d3.select(document.createElement('div')).style('opacity', 0);
$$.observeInserted($$.selectChart); $$.observeInserted($$.selectChart);
...@@ -864,7 +872,12 @@ ...@@ -864,7 +872,12 @@
}; };
c3_chart_internal_fn.observeInserted = function (selection) { c3_chart_internal_fn.observeInserted = function (selection) {
var $$ = this, observer = new MutationObserver(function (mutations) { var $$ = this, observer;
if (typeof MutationObserver === 'undefined') {
window.console.error("MutationObserver not defined.");
return;
}
observer= new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) { mutations.forEach(function (mutation) {
if (mutation.type === 'childList' && mutation.previousSibling) { if (mutation.type === 'childList' && mutation.previousSibling) {
observer.disconnect(); observer.disconnect();
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -54,14 +54,52 @@ describe('c3 chart', function () { ...@@ -54,14 +54,52 @@ describe('c3 chart', function () {
describe('bindto', function () { describe('bindto', function () {
it('should accept d3.selection object', function () { describe('selector', function () {
args.bindto = d3.select('#chart'); it('update args', function () {
d3.select('#chart').html('');
args.bindto = '#chart';
expect(true).toBeTruthy(); expect(true).toBeTruthy();
}); });
it('should be created', function () {
var svg = d3.select('#chart svg');
expect(svg.size()).toBe(1);
});
});
describe('d3.selection object', function () {
it('update args', function () {
d3.select('#chart').html('');
args.bindto = d3.select('#chart');
expect(true).toBeTruthy();
});
it('should be created', function () { it('should be created', function () {
var svg = d3.select('#chart svg'); var svg = d3.select('#chart svg');
expect(svg).not.toBeNull(); expect(svg.size()).toBe(1);
});
});
describe('null', function () {
it('update args', function () {
d3.select('#chart').html('');
args.bindto = null;
expect(true).toBeTruthy();
});
it('should not be created', function () {
var svg = d3.select('#chart svg');
expect(svg.size()).toBe(0);
});
});
describe('empty string', function () {
it('update args', function () {
d3.select('#chart').html('');
args.bindto = '';
expect(true).toBeTruthy();
});
it('should not be created', function () {
var svg = d3.select('#chart svg');
expect(svg.size()).toBe(0);
});
}); });
}); });
......
...@@ -153,7 +153,15 @@ c3_chart_internal_fn.initWithData = function (data) { ...@@ -153,7 +153,15 @@ c3_chart_internal_fn.initWithData = function (data) {
if ($$.initBrush) { $$.initBrush(); } if ($$.initBrush) { $$.initBrush(); }
if ($$.initZoom) { $$.initZoom(); } if ($$.initZoom) { $$.initZoom(); }
$$.selectChart = typeof config.bindto.node === 'function' ? config.bindto : d3.select(config.bindto); if (!config.bindto) {
$$.selectChart = d3.selectAll([]);
}
else if (typeof config.bindto.node === 'function') {
$$.selectChart = config.bindto;
}
else {
$$.selectChart = d3.select(config.bindto);
}
if ($$.selectChart.empty()) { if ($$.selectChart.empty()) {
$$.selectChart = d3.select(document.createElement('div')).style('opacity', 0); $$.selectChart = d3.select(document.createElement('div')).style('opacity', 0);
$$.observeInserted($$.selectChart); $$.observeInserted($$.selectChart);
...@@ -859,7 +867,12 @@ c3_chart_internal_fn.updateDimension = function (withoutAxis) { ...@@ -859,7 +867,12 @@ c3_chart_internal_fn.updateDimension = function (withoutAxis) {
}; };
c3_chart_internal_fn.observeInserted = function (selection) { c3_chart_internal_fn.observeInserted = function (selection) {
var $$ = this, observer = new MutationObserver(function (mutations) { var $$ = this, observer;
if (typeof MutationObserver === 'undefined') {
window.console.error("MutationObserver not defined.");
return;
}
observer= new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) { mutations.forEach(function (mutation) {
if (mutation.type === 'childList' && mutation.previousSibling) { if (mutation.type === 'childList' && mutation.previousSibling) {
observer.disconnect(); observer.disconnect();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment