Browse Source

Fix bindto to accept null and empty string

pull/1003/head
Masayuki Tanaka 10 years ago
parent
commit
edcd907a41
  1. 17
      c3.js
  2. 10
      c3.min.js
  3. 50
      spec/core-spec.js
  4. 17
      src/core.js

17
c3.js

@ -158,7 +158,15 @@
if ($$.initBrush) { $$.initBrush(); }
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()) {
$$.selectChart = d3.select(document.createElement('div')).style('opacity', 0);
$$.observeInserted($$.selectChart);
@ -864,7 +872,12 @@
};
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) {
if (mutation.type === 'childList' && mutation.previousSibling) {
observer.disconnect();

10
c3.min.js vendored

File diff suppressed because one or more lines are too long

50
spec/core-spec.js

@ -54,14 +54,52 @@ describe('c3 chart', function () {
describe('bindto', function () {
it('should accept d3.selection object', function () {
args.bindto = d3.select('#chart');
expect(true).toBeTruthy();
describe('selector', function () {
it('update args', function () {
d3.select('#chart').html('');
args.bindto = '#chart';
expect(true).toBeTruthy();
});
it('should be created', function () {
var svg = d3.select('#chart svg');
expect(svg.size()).toBe(1);
});
});
it('should be created', function () {
var svg = d3.select('#chart svg');
expect(svg).not.toBeNull();
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 () {
var svg = d3.select('#chart svg');
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);
});
});
});

17
src/core.js

@ -153,7 +153,15 @@ c3_chart_internal_fn.initWithData = function (data) {
if ($$.initBrush) { $$.initBrush(); }
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()) {
$$.selectChart = d3.select(document.createElement('div')).style('opacity', 0);
$$.observeInserted($$.selectChart);
@ -859,7 +867,12 @@ c3_chart_internal_fn.updateDimension = function (withoutAxis) {
};
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) {
if (mutation.type === 'childList' && mutation.previousSibling) {
observer.disconnect();

Loading…
Cancel
Save