-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
Copy pathhelpers.config.ts
456 lines (420 loc) · 12.7 KB
/
helpers.config.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* eslint-disable @typescript-eslint/no-use-before-define */
import type {AnyObject} from '../types/basic.js';
import type {ChartMeta} from '../types/index.js';
import type {
ResolverObjectKey,
ResolverCache,
ResolverProxy,
DescriptorDefaults,
Descriptor,
ContextCache,
ContextProxy
} from './helpers.config.types.js';
import {isArray, isFunction, isObject, resolveObjectKey, _capitalize} from './helpers.core.js';
export * from './helpers.config.types.js';
/**
* Creates a Proxy for resolving raw values for options.
* @param scopes - The option scopes to look for values, in resolution order
* @param prefixes - The prefixes for values, in resolution order.
* @param rootScopes - The root option scopes
* @param fallback - Parent scopes fallback
* @param getTarget - callback for getting the target for changed values
* @returns Proxy
* @private
*/
export function _createResolver<
T extends AnyObject[] = AnyObject[],
R extends AnyObject[] = T
>(
scopes: T,
prefixes = [''],
rootScopes?: R,
fallback?: ResolverObjectKey,
getTarget = () => scopes[0]
) {
const finalRootScopes = rootScopes || scopes;
if (typeof fallback === 'undefined') {
fallback = _resolve('_fallback', scopes);
}
const cache: ResolverCache<T, R> = {
[Symbol.toStringTag]: 'Object',
_cacheable: true,
_scopes: scopes,
_rootScopes: finalRootScopes,
_fallback: fallback,
_getTarget: getTarget,
override: (scope: AnyObject) => _createResolver([scope, ...scopes], prefixes, finalRootScopes, fallback),
};
return new Proxy(cache, {
/**
* A trap for the delete operator.
*/
deleteProperty(target, prop: string) {
delete target[prop]; // remove from cache
delete target._keys; // remove cached keys
delete scopes[0][prop]; // remove from top level scope
return true;
},
/**
* A trap for getting property values.
*/
get(target, prop: string) {
return _cached(target, prop,
() => _resolveWithPrefixes(prop, prefixes, scopes, target));
},
/**
* A trap for Object.getOwnPropertyDescriptor.
* Also used by Object.hasOwnProperty.
*/
getOwnPropertyDescriptor(target, prop) {
return Reflect.getOwnPropertyDescriptor(target._scopes[0], prop);
},
/**
* A trap for Object.getPrototypeOf.
*/
getPrototypeOf() {
return Reflect.getPrototypeOf(scopes[0]);
},
/**
* A trap for the in operator.
*/
has(target, prop: string) {
return getKeysFromAllScopes(target).includes(prop);
},
/**
* A trap for Object.getOwnPropertyNames and Object.getOwnPropertySymbols.
*/
ownKeys(target) {
return getKeysFromAllScopes(target);
},
/**
* A trap for setting property values.
*/
set(target, prop: string, value) {
const storage = target._storage || (target._storage = getTarget());
target[prop] = storage[prop] = value; // set to top level scope + cache
delete target._keys; // remove cached keys
return true;
}
}) as ResolverProxy<T, R>;
}
/**
* Returns an Proxy for resolving option values with context.
* @param proxy - The Proxy returned by `_createResolver`
* @param context - Context object for scriptable/indexable options
* @param subProxy - The proxy provided for scriptable options
* @param descriptorDefaults - Defaults for descriptors
* @private
*/
export function _attachContext<
T extends AnyObject[] = AnyObject[],
R extends AnyObject[] = T
>(
proxy: ResolverProxy<T, R>,
context: AnyObject,
subProxy?: ResolverProxy<T, R>,
descriptorDefaults?: DescriptorDefaults
) {
const cache: ContextCache<T, R> = {
_cacheable: false,
_proxy: proxy,
_context: context,
_subProxy: subProxy,
_stack: new Set(),
_descriptors: _descriptors(proxy, descriptorDefaults),
setContext: (ctx: AnyObject) => _attachContext(proxy, ctx, subProxy, descriptorDefaults),
override: (scope: AnyObject) => _attachContext(proxy.override(scope), context, subProxy, descriptorDefaults)
};
return new Proxy(cache, {
/**
* A trap for the delete operator.
*/
deleteProperty(target, prop) {
delete target[prop]; // remove from cache
delete proxy[prop]; // remove from proxy
return true;
},
/**
* A trap for getting property values.
*/
get(target, prop: string, receiver) {
return _cached(target, prop,
() => _resolveWithContext(target, prop, receiver));
},
/**
* A trap for Object.getOwnPropertyDescriptor.
* Also used by Object.hasOwnProperty.
*/
getOwnPropertyDescriptor(target, prop) {
return target._descriptors.allKeys
? Reflect.has(proxy, prop) ? {enumerable: true, configurable: true} : undefined
: Reflect.getOwnPropertyDescriptor(proxy, prop);
},
/**
* A trap for Object.getPrototypeOf.
*/
getPrototypeOf() {
return Reflect.getPrototypeOf(proxy);
},
/**
* A trap for the in operator.
*/
has(target, prop) {
return Reflect.has(proxy, prop);
},
/**
* A trap for Object.getOwnPropertyNames and Object.getOwnPropertySymbols.
*/
ownKeys() {
return Reflect.ownKeys(proxy);
},
/**
* A trap for setting property values.
*/
set(target, prop, value) {
proxy[prop] = value; // set to proxy
delete target[prop]; // remove from cache
return true;
}
}) as ContextProxy<T, R>;
}
/**
* @private
*/
export function _descriptors(
proxy: ResolverCache,
defaults: DescriptorDefaults = {scriptable: true, indexable: true}
): Descriptor {
const {_scriptable = defaults.scriptable, _indexable = defaults.indexable, _allKeys = defaults.allKeys} = proxy;
return {
allKeys: _allKeys,
scriptable: _scriptable,
indexable: _indexable,
isScriptable: isFunction(_scriptable) ? _scriptable : () => _scriptable,
isIndexable: isFunction(_indexable) ? _indexable : () => _indexable
};
}
const readKey = (prefix: string, name: string) => prefix ? prefix + _capitalize(name) : name;
const needsSubResolver = (prop: string, value: unknown) => isObject(value) && prop !== 'adapters' &&
(Object.getPrototypeOf(value) === null || value.constructor === Object);
function _cached(
target: AnyObject,
prop: string,
resolve: () => unknown
) {
if (Object.prototype.hasOwnProperty.call(target, prop) || prop === 'constructor') {
return target[prop];
}
const value = resolve();
// cache the resolved value
target[prop] = value;
return value;
}
function _resolveWithContext(
target: ContextCache,
prop: string,
receiver: AnyObject
) {
const {_proxy, _context, _subProxy, _descriptors: descriptors} = target;
let value = _proxy[prop]; // resolve from proxy
// resolve with context
if (isFunction(value) && descriptors.isScriptable(prop)) {
value = _resolveScriptable(prop, value, target, receiver);
}
if (isArray(value) && value.length) {
value = _resolveArray(prop, value, target, descriptors.isIndexable);
}
if (needsSubResolver(prop, value)) {
// if the resolved value is an object, create a sub resolver for it
value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors);
}
return value;
}
function _resolveScriptable(
prop: string,
getValue: (ctx: AnyObject, sub: AnyObject) => unknown,
target: ContextCache,
receiver: AnyObject
) {
const {_proxy, _context, _subProxy, _stack} = target;
if (_stack.has(prop)) {
throw new Error('Recursion detected: ' + Array.from(_stack).join('->') + '->' + prop);
}
_stack.add(prop);
let value = getValue(_context, _subProxy || receiver);
_stack.delete(prop);
if (needsSubResolver(prop, value)) {
// When scriptable option returns an object, create a resolver on that.
value = createSubResolver(_proxy._scopes, _proxy, prop, value);
}
return value;
}
function _resolveArray(
prop: string,
value: unknown[],
target: ContextCache,
isIndexable: (key: string) => boolean
) {
const {_proxy, _context, _subProxy, _descriptors: descriptors} = target;
if (typeof _context.index !== 'undefined' && isIndexable(prop)) {
return value[_context.index % value.length];
} else if (isObject(value[0])) {
// Array of objects, return array or resolvers
const arr = value;
const scopes = _proxy._scopes.filter(s => s !== arr);
value = [];
for (const item of arr) {
const resolver = createSubResolver(scopes, _proxy, prop, item);
value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors));
}
}
return value;
}
function resolveFallback(
fallback: ResolverObjectKey | ((prop: ResolverObjectKey, value: unknown) => ResolverObjectKey),
prop: ResolverObjectKey,
value: unknown
) {
return isFunction(fallback) ? fallback(prop, value) : fallback;
}
const getScope = (key: ResolverObjectKey, parent: AnyObject) => key === true ? parent
: typeof key === 'string' ? resolveObjectKey(parent, key) : undefined;
function addScopes(
set: Set<AnyObject>,
parentScopes: AnyObject[],
key: ResolverObjectKey,
parentFallback: ResolverObjectKey,
value: unknown
) {
for (const parent of parentScopes) {
const scope = getScope(key, parent);
if (scope) {
set.add(scope);
const fallback = resolveFallback(scope._fallback, key, value);
if (typeof fallback !== 'undefined' && fallback !== key && fallback !== parentFallback) {
// When we reach the descriptor that defines a new _fallback, return that.
// The fallback will resume to that new scope.
return fallback;
}
} else if (scope === false && typeof parentFallback !== 'undefined' && key !== parentFallback) {
// Fallback to `false` results to `false`, when falling back to different key.
// For example `interaction` from `hover` or `plugins.tooltip` and `animation` from `animations`
return null;
}
}
return false;
}
function createSubResolver(
parentScopes: AnyObject[],
resolver: ResolverCache,
prop: ResolverObjectKey,
value: unknown
) {
const rootScopes = resolver._rootScopes;
const fallback = resolveFallback(resolver._fallback, prop, value);
const allScopes = [...parentScopes, ...rootScopes];
const set = new Set<AnyObject>();
set.add(value);
let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value);
if (key === null) {
return false;
}
if (typeof fallback !== 'undefined' && fallback !== prop) {
key = addScopesFromKey(set, allScopes, fallback, key, value);
if (key === null) {
return false;
}
}
return _createResolver(Array.from(set), [''], rootScopes, fallback,
() => subGetTarget(resolver, prop as string, value));
}
function addScopesFromKey(
set: Set<AnyObject>,
allScopes: AnyObject[],
key: ResolverObjectKey,
fallback: ResolverObjectKey,
item: unknown
) {
while (key) {
key = addScopes(set, allScopes, key, fallback, item);
}
return key;
}
function subGetTarget(
resolver: ResolverCache,
prop: string,
value: unknown
) {
const parent = resolver._getTarget();
if (!(prop in parent)) {
parent[prop] = {};
}
const target = parent[prop];
if (isArray(target) && isObject(value)) {
// For array of objects, the object is used to store updated values
return value;
}
return target || {};
}
function _resolveWithPrefixes(
prop: string,
prefixes: string[],
scopes: AnyObject[],
proxy: ResolverProxy
) {
let value: unknown;
for (const prefix of prefixes) {
value = _resolve(readKey(prefix, prop), scopes);
if (typeof value !== 'undefined') {
return needsSubResolver(prop, value)
? createSubResolver(scopes, proxy, prop, value)
: value;
}
}
}
function _resolve(key: string, scopes: AnyObject[]) {
for (const scope of scopes) {
if (!scope) {
continue;
}
const value = scope[key];
if (typeof value !== 'undefined') {
return value;
}
}
}
function getKeysFromAllScopes(target: ResolverCache) {
let keys = target._keys;
if (!keys) {
keys = target._keys = resolveKeysFromAllScopes(target._scopes);
}
return keys;
}
function resolveKeysFromAllScopes(scopes: AnyObject[]) {
const set = new Set<string>();
for (const scope of scopes) {
for (const key of Object.keys(scope).filter(k => !k.startsWith('_'))) {
set.add(key);
}
}
return Array.from(set);
}
export function _parseObjectDataRadialScale(
meta: ChartMeta<'line' | 'scatter'>,
data: AnyObject[],
start: number,
count: number
) {
const {iScale} = meta;
const {key = 'r'} = this._parsing;
const parsed = new Array<{r: unknown}>(count);
let i: number, ilen: number, index: number, item: AnyObject;
for (i = 0, ilen = count; i < ilen; ++i) {
index = i + start;
item = data[index];
parsed[i] = {
r: iScale.parse(resolveObjectKey(item, key), index)
};
}
return parsed;
}