Skip to content

Commit

Permalink
feat(core): Intent to ship global config
Browse files Browse the repository at this point in the history
Implementation of global configuration setting.

Fix #932
  • Loading branch information
netil authored Jun 12, 2019
1 parent a4d6dfa commit 1b524e1
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
74 changes: 74 additions & 0 deletions spec/internals/bb-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/* eslint-disable */
import bb from "../../src/core";
import util from "../assets/util";
import CLASS from "../../src/config/classes";7

describe("Interface & initialization", () => {
let chart;
Expand Down Expand Up @@ -197,4 +198,77 @@ describe("Interface & initialization", () => {
}, 200);
});
});

describe("set defaults options", () => {
let tickPrefix = "-A-";
let args = {
data: {
types: {
data1: "area",
data2: "area-spline"
}
},
axis: {
x: {
tick: {
format: x =>`${tickPrefix}${x}`
}
}
}
};

before(() => {
bb.defaults(args);
});

it("check if defaults options applied", () => {
chart = util.generate({
data: {
columns: [
["data1", 300, 350, 300, 0, 0, 0],
["data2", 130, 100, 140, 200, 150, 50]
]
}
});

expect(bb.defaults()).deep.equal(args);
expect(chart.config("data.types")).to.be.deep.equal(args.data.types);

chart.$.main.selectAll(`.${CLASS.axisX} .tick text`).each(function(d, i) {
expect(this.textContent).to.be.equal(`${tickPrefix}${i}`);
})
});

it("check if defaults options not applied", () => {
tickPrefix = "AB-";
args = {
data: {
columns: [
["data1", 300, 350, 300, 0, 0, 0],
["data2", 130, 100, 140, 200, 150, 50]
],
types: {
data1: "bar"
}
},
axis: {
x: {
tick: {
format: x =>`${tickPrefix}${x}`
}
}
}
};

chart = util.generate(args);

expect(chart.config("data.types")).to.be.deep.equal(
Object.assign({}, bb.defaults().data.types, args.data.types)
);

chart.$.main.selectAll(`.${CLASS.axisX} .tick text`).each(function(d, i) {
expect(this.textContent).to.be.equal(`${tickPrefix}${i}`);
})
});
});
});
37 changes: 35 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Chart from "./internals/Chart";
import ChartInternal from "./internals/ChartInternal";
import Axis from "./axis/Axis";
import {isObject, mergeObj} from "./internals/util";

import "./config/config";
import "./internals/scale";
Expand Down Expand Up @@ -58,10 +59,11 @@ import "./api/api.chart";
import "./api/api.tooltip";
import "./api/api.export";


// base CSS
import "./scss/billboard.scss";

let defaults = {};

/**
* @namespace bb
* @version #__VERSION__#
Expand Down Expand Up @@ -103,14 +105,45 @@ const bb = {
* chart.data("data1");
*/
generate(config) {
const inst = new Chart(config);
const options = mergeObj({}, defaults, config);
const inst = new Chart(options);

inst.internal.charts = this.instance;
this.instance.push(inst);

return inst;
},

/**
* Set or get default options globally.
* - **NOTE:**
* - The options values settings are valid within page context only.
* - If is called multiple times, will override the last value.
* @param {Options} options chart options
* @memberof bb
* @return {Options}
* @see {@link Options}
* @example
* // Set same option value as for `.generate()`
* bb.defaults({
* data: {
* type: "bar"
* }
* });
*
* bb.defaults(); // {data:{type: "bar"}}
*
* // data.type defaults to 'bar'
* var chart = bb.generate({ ... });
*/
defaults(options) {
if (isObject(options)) {
defaults = options;
}

return defaults;
},

/**
* An array containing instance created
* @property {Array} instance instance array
Expand Down
32 changes: 32 additions & 0 deletions src/internals/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,37 @@ const getUnique = data => data.filter((v, i, self) => self.indexOf(v) === i);
*/
const mergeArray = arr => (arr && arr.length ? arr.reduce((p, c) => p.concat(c)) : []);

/**
* Merge object returning new object
* @param {Object} target
* @param {Object} objectN
* @returns {Object} merged target object
* @private
*/
const mergeObj = (target, ...objectN) => {
if (!objectN.length || (objectN.length === 1 && !objectN[0])) {
return target;
}

const source = objectN.shift();

if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
const value = source[key];

if (isObject(value)) {
!target[key] && (target[key] = {});
target[key] = mergeObj(target[key], value);
} else {
target[key] = isArray(value) ?
value.concat() : value;
}
});
}

return mergeObj(target, ...objectN);
};

/**
* Sort value
* @param {Array} data value to be sorted
Expand Down Expand Up @@ -400,6 +431,7 @@ export {
isUndefined,
isValue,
mergeArray,
mergeObj,
notEmpty,
sanitise,
setTextValue,
Expand Down
9 changes: 9 additions & 0 deletions types/bb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export const bb: {
*/
generate(options: ChartOptions): Chart;

/**
* Set or get default options globally.
* - **NOTE:**
* - The options values settings are valid within page context only.
* - If is called multiple times, will override the last value.
* @param options Chart generation options
*/
defaults(options: ChartOptions): ChartOptions;

/**
* Version information
*/
Expand Down

0 comments on commit 1b524e1

Please sign in to comment.