This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.js
82 lines (63 loc) · 1.67 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import arrayEquals from 'array-equal';
import extend from 'extend';
import { logger } from './aurelia-charts';
export let Config = class Config {
constructor() {
this.defaults = {
library: undefined,
type: 'line',
libraries: {}
};
this.charts = {};
this.scales = [];
}
registerChart(library, type, target) {
this.charts[library] = this.charts[library] || {};
this.charts[library][type] = target;
return this;
}
registerScales(target, ...scales) {
this.scales.push({
constructor: target,
scales: scales
});
return this;
}
configure(defaults) {
extend(true, this, defaults);
return this;
}
chartsByScale(...scale) {
let result = [];
this.scales.forEach(chartScales => {
chartScales.scales.forEach(chartScale => {
if (arrayEquals(scale, chartScale)) {
result.push(chartScales.constructor);
}
});
});
return result;
}
chart(value) {
if (typeof value === 'undefined') {
return this.chart(this.defaults);
}
if (typeof value === 'string') {
return this.chart({
library: undefined,
type: value
});
}
if (typeof value === 'object') {
let type = value.type || this.defaults.type;
let libName = value.library || this.defaults.libraries[type] || this.defaults.library;
let library = this.charts[libName];
if (typeof library === 'undefined') {
logger.warn(`${ value.library } is not a registered library. Either define a default library or tell what library to use`);
return undefined;
}
return library[type];
}
return undefined;
}
};