Skip to content

Commit 6e0b8b1

Browse files
committed
Move descriptors out of defaults
1 parent 057cfe0 commit 6e0b8b1

15 files changed

+83
-35
lines changed

src/controllers/controller.bubble.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ BubbleController.defaults = {
149149
plugins: {
150150
tooltip: {
151151
callbacks: {
152-
_scriptable: false,
153152
title() {
154153
// Title doesn't make sense for scatter since we format the data as a point
155154
return '';

src/controllers/controller.doughnut.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ DoughnutController.defaults = {
355355
plugins: {
356356
legend: {
357357
labels: {
358-
_scriptable: false,
359358
generateLabels(chart) {
360359
const data = chart.data;
361360
if (data.labels.length && data.datasets.length) {
@@ -386,7 +385,6 @@ DoughnutController.defaults = {
386385
},
387386
tooltip: {
388387
callbacks: {
389-
_scriptable: false,
390388
title() {
391389
return '';
392390
},

src/controllers/controller.polarArea.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ PolarAreaController.defaults = {
153153
plugins: {
154154
legend: {
155155
labels: {
156-
_scriptable: false,
157156
generateLabels(chart) {
158157
const data = chart.data;
159158
if (data.labels.length && data.datasets.length) {

src/controllers/controller.scatter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ ScatterController.defaults = {
3131
plugins: {
3232
tooltip: {
3333
callbacks: {
34-
_scriptable: false,
3534
title() {
3635
return ''; // doesn't make sense for scatter since data are formatted as a point
3736
},

src/core/core.animations.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const numbers = ['x', 'y', 'borderWidth', 'radius', 'tension'];
77
const colors = ['borderColor', 'backgroundColor'];
88

99
defaults.set('animation', {
10-
_scriptable: (name) => name !== 'onProgress' && name !== 'onComplete' && name !== 'fn',
11-
_indexable: false,
12-
_fallback: false,
13-
1410
// Plain properties can be overridden in each object
1511
duration: 1000,
1612
easing: 'easeOutQuart',
@@ -58,6 +54,12 @@ defaults.set('animation', {
5854
}
5955
});
6056

57+
defaults.describe('animation', {
58+
_scriptable: (name) => name !== 'onProgress' && name !== 'onComplete' && name !== 'fn',
59+
_indexable: false,
60+
_fallback: false,
61+
});
62+
6163
export default class Animations {
6264
constructor(chart, animations) {
6365
this._chart = chart;

src/core/core.config.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ export default class Config {
163163
scopeKeys.forEach(key => addIfFound(options, key));
164164
scopeKeys.forEach(key => addIfFound(defaults, key));
165165

166+
const descriptors = defaults.descriptors;
167+
scopeKeys.forEach(key => addIfFound(descriptors, key));
168+
166169
return [...scopes];
167170
}
168171

@@ -207,12 +210,12 @@ export default class Config {
207210

208211
function needContext(proxy, names) {
209212
const {_scriptable = true, _indexable = true} = proxy;
210-
const scriptableFn = isFunction(_scriptable);
211-
const indexableFn = isFunction(_indexable);
213+
const scriptable = prop => isFunction(_scriptable) ? _scriptable(prop) : _scriptable;
214+
const indexable = prop => isFunction(_indexable) ? _indexable(prop) : _indexable;
212215
for (const prop of names) {
213216

214-
if (((scriptableFn ? _scriptable(prop) : _scriptable) && isFunction(proxy[prop]))
215-
|| ((indexableFn ? _indexable(prop) : _indexable) && isArray(proxy[prop]))) {
217+
if ((scriptable(prop) && isFunction(proxy[prop]))
218+
|| (indexable(prop) && isArray(proxy[prop]))) {
216219
return true;
217220
}
218221
}

src/core/core.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function getCanvas(item) {
6666
return item;
6767
}
6868

69-
const chartOptionsScopes = config => [config.options, defaults.controllers[config.type] || {}, defaults];
69+
const chartOptionsScopes = config => [config.options, defaults.controllers[config.type] || {}, defaults, defaults.descriptors];
7070

7171
class Chart {
7272

src/core/core.datasetController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Animations from './core.animations';
2-
import {isObject, isArray, valueOrDefault, resolveObjectKey} from '../helpers/helpers.core';
2+
import {isObject, isArray, valueOrDefault, resolveObjectKey, defined} from '../helpers/helpers.core';
33
import {listenArrayEvents, unlistenArrayEvents} from '../helpers/helpers.collection';
44
import {sign} from '../helpers/helpers.math';
55
import defaults from './core.defaults';
@@ -760,7 +760,7 @@ export default class DatasetController {
760760
const cache = me._cachedDataOpts;
761761
const cacheKey = elementType + '-' + mode;
762762
const cached = cache[cacheKey];
763-
const sharing = me.enableOptionSharing;
763+
const sharing = me.enableOptionSharing && defined(index);
764764
if (cached) {
765765
return cloneIfNoSharing(cached, sharing);
766766
}

src/core/core.defaults.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {getHoverColor} from '../helpers/helpers.color';
22
import {isObject, merge, valueOrDefault} from '../helpers/helpers.core';
33

4+
const privateSymbol = Symbol('private');
5+
46
/**
57
* @param {object} node
68
* @param {string} key
@@ -23,9 +25,7 @@ function getScope(node, key) {
2325
* Note: class is exported for typedoc
2426
*/
2527
export class Defaults {
26-
constructor() {
27-
this._scriptable = (name) => name !== 'onClick' && name !== 'onHover';
28-
this._indexable = (name) => name !== 'events';
28+
constructor(descriptors) {
2929
this.animation = undefined;
3030
this.backgroundColor = 'rgba(0,0,0,0.1)';
3131
this.borderColor = 'rgba(0,0,0,0.1)';
@@ -48,16 +48,13 @@ export class Defaults {
4848
weight: null
4949
};
5050
this.hover = {
51-
_fallback: 'interaction',
5251
onHover: null
5352
};
5453
this.hoverBackgroundColor = (ctx, options) => getHoverColor(options.backgroundColor);
5554
this.hoverBorderColor = (ctx, options) => getHoverColor(options.borderColor);
5655
this.hoverColor = (ctx, options) => getHoverColor(options.color);
5756
this.indexAxis = null;
5857
this.interaction = {
59-
_scroptable: false,
60-
_indexable: false,
6158
mode: 'nearest',
6259
intersect: true
6360
};
@@ -70,6 +67,13 @@ export class Defaults {
7067
this.scale = undefined;
7168
this.scales = {};
7269
this.showLine = true;
70+
71+
Object.defineProperty(this, privateSymbol, {
72+
value: Object.create(null),
73+
writable: false
74+
});
75+
76+
this.describe(descriptors);
7377
}
7478

7579
/**
@@ -90,6 +94,22 @@ export class Defaults {
9094
return getScope(this, scope);
9195
}
9296

97+
/**
98+
* @param {string|object} scope
99+
* @param {object} [values]
100+
*/
101+
describe(scope, values) {
102+
const root = this[privateSymbol];
103+
if (typeof scope === 'string') {
104+
return merge(getScope(root, scope), values);
105+
}
106+
return merge(getScope(root, ''), scope);
107+
}
108+
109+
get descriptors() {
110+
return this[privateSymbol];
111+
}
112+
93113
/**
94114
* Routes the named defaults to fallback to another scope/name.
95115
* This routing is useful when those target values, like defaults.color, are changed runtime.
@@ -138,4 +158,14 @@ export class Defaults {
138158
}
139159

140160
// singleton instance
141-
export default new Defaults();
161+
export default new Defaults({
162+
_scriptable: (name) => name !== 'onClick' && name !== 'onHover',
163+
_indexable: (name) => name !== 'events',
164+
hover: {
165+
_fallback: 'interaction'
166+
},
167+
interaction: {
168+
_scroptable: false,
169+
_indexable: false,
170+
}
171+
});

src/core/core.typedRegistry.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ function registerDefaults(item, scope, parentScope) {
8888
if (item.defaultRoutes) {
8989
routeDefaults(scope, item.defaultRoutes);
9090
}
91+
92+
if (item.descriptors) {
93+
defaults.describe(scope, item.descriptors);
94+
}
9195
}
9296

9397
function routeDefaults(scope, routes) {

0 commit comments

Comments
 (0)