]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Plugin options default scriptable/indexable=false (#8497)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 23 Feb 2021 15:54:42 +0000 (17:54 +0200)
committerGitHub <noreply@github.com>
Tue, 23 Feb 2021 15:54:42 +0000 (17:54 +0200)
* Plugin options default scriptable/indexable=false

* Update test

* Inherti desciptors

* Remove unreachable code

* remove unintentional change

* remove double default

src/core/core.config.js
src/core/core.plugins.js
src/helpers/helpers.config.js
test/specs/core.plugin.tests.js

index d2fa83cb025f06305eaa041552d5d31e1ca23af4..ef703865c78fa86d75947a641d62a12beaefae41 100644 (file)
@@ -314,11 +314,12 @@ export default class Config {
    * @param {object[]} scopes
    * @param {object} [context]
    * @param {string[]} [prefixes]
+   * @param {{scriptable: boolean, indexable: boolean}} [descriptorDefaults]
    */
-  createResolver(scopes, context, prefixes = ['']) {
+  createResolver(scopes, context, prefixes = [''], descriptorDefaults) {
     const {resolver} = getResolver(this._resolverCache, scopes, prefixes);
     return isObject(context)
-      ? _attachContext(resolver, isFunction(context) ? context() : context)
+      ? _attachContext(resolver, context, undefined, descriptorDefaults)
       : resolver;
   }
 }
index 5655ba3a934306dbeae351de6d52e2138d87fff1..62cb543bc4bbc1dde4d00af875119f508f61cdaa 100644 (file)
@@ -166,5 +166,5 @@ function createDescriptors(chart, plugins, options, all) {
 function pluginOpts(config, plugin, opts, context) {
   const keys = config.pluginScopeKeys(plugin);
   const scopes = config.getOptionScopes(opts, keys);
-  return config.createResolver(scopes, context);
+  return config.createResolver(scopes, context, [''], {scriptable: false, indexable: false});
 }
index 3af16a66a1897a85c6bd240b2f1acd7bf16d363f..fb60c81c42b335803720fa6b63eb89721daf9312 100644 (file)
@@ -74,18 +74,19 @@ export function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fa
  * @param {object} proxy - The Proxy returned by `_createResolver`
  * @param {object} context - Context object for scriptable/indexable options
  * @param {object} [subProxy] - The proxy provided for scriptable options
+ * @param {{scriptable: boolean, indexable: boolean}} [descriptorDefaults] - Defaults for descriptors
  * @private
  */
-export function _attachContext(proxy, context, subProxy) {
+export function _attachContext(proxy, context, subProxy, descriptorDefaults) {
   const cache = {
     _cacheable: false,
     _proxy: proxy,
     _context: context,
     _subProxy: subProxy,
     _stack: new Set(),
-    _descriptors: _descriptors(proxy),
-    setContext: (ctx) => _attachContext(proxy, ctx, subProxy),
-    override: (scope) => _attachContext(proxy.override(scope), context, subProxy)
+    _descriptors: _descriptors(proxy, descriptorDefaults),
+    setContext: (ctx) => _attachContext(proxy, ctx, subProxy, descriptorDefaults),
+    override: (scope) => _attachContext(proxy.override(scope), context, subProxy, descriptorDefaults)
   };
   return new Proxy(cache, {
     /**
@@ -138,9 +139,11 @@ export function _attachContext(proxy, context, subProxy) {
 /**
  * @private
  */
-export function _descriptors(proxy) {
-  const {_scriptable = true, _indexable = true} = proxy;
+export function _descriptors(proxy, defaults = {scriptable: true, indexable: true}) {
+  const {_scriptable = defaults.scriptable, _indexable = defaults.indexable} = proxy;
   return {
+    scriptable: _scriptable,
+    indexable: _indexable,
     isScriptable: isFunction(_scriptable) ? _scriptable : () => _scriptable,
     isIndexable: isFunction(_indexable) ? _indexable : () => _indexable
   };
@@ -177,7 +180,7 @@ function _resolveWithContext(target, prop, receiver) {
   }
   if (needsSubResolver(prop, value)) {
     // if the resolved value is an object, crate a sub resolver for it
-    value = _attachContext(value, _context, _subProxy && _subProxy[prop]);
+    value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors);
   }
   return value;
 }
@@ -199,7 +202,7 @@ function _resolveScriptable(prop, value, target, receiver) {
 }
 
 function _resolveArray(prop, value, target, isIndexable) {
-  const {_proxy, _context, _subProxy} = target;
+  const {_proxy, _context, _subProxy, _descriptors: descriptors} = target;
 
   if (defined(_context.index) && isIndexable(prop)) {
     value = value[_context.index % value.length];
@@ -210,7 +213,7 @@ function _resolveArray(prop, value, target, isIndexable) {
     value = [];
     for (const item of arr) {
       const resolver = createSubResolver(scopes, _proxy, prop, item);
-      value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop]));
+      value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors));
     }
   }
   return value;
index 9a70f10a7e883281b65d78b087952cbf65bdb62c..7168ce928ba7dc9029296bd3ac061d14cab82d73 100644 (file)
@@ -361,5 +361,36 @@ describe('Chart.plugins', function() {
       // The plugin on the chart should only be started once
       expect(results).toEqual([1]);
     });
+
+    it('should default to false for _scriptable, _indexable', function(done) {
+      const plugin = {
+        id: 'test',
+        start: function(chart, args, opts) {
+          expect(opts.fun).toEqual(jasmine.any(Function));
+          expect(opts.fun()).toEqual('test');
+          expect(opts.arr).toEqual([1, 2, 3]);
+
+          expect(opts.sub.subfun).toEqual(jasmine.any(Function));
+          expect(opts.sub.subfun()).toEqual('subtest');
+          expect(opts.sub.subarr).toEqual([3, 2, 1]);
+          done();
+        }
+      };
+      window.acquireChart({
+        options: {
+          plugins: {
+            test: {
+              fun: () => 'test',
+              arr: [1, 2, 3],
+              sub: {
+                subfun: () => 'subtest',
+                subarr: [3, 2, 1],
+              }
+            }
+          }
+        },
+        plugins: [plugin]
+      });
+    });
   });
 });