]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Chart.register: preserve existing defaults (#8052)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 14 Nov 2020 19:38:38 +0000 (21:38 +0200)
committerGitHub <noreply@github.com>
Sat, 14 Nov 2020 19:38:38 +0000 (14:38 -0500)
src/core/core.typedRegistry.js
test/specs/core.registry.tests.js

index 7e70cb1124fa9a14a85ef6e2c8e23cdb7537405c..187a3d1617beeaa58fff316c1c730f8683f9e6f7 100644 (file)
@@ -42,10 +42,6 @@ export default class TypedRegistry {
                        return scope;
                }
 
-               if (Object.keys(defaults.get(scope)).length) {
-                       throw new Error('Can not register "' + id + '", because "defaults.' + scope + '" would collide with existing defaults');
-               }
-
                items[id] = item;
                registerDefaults(item, scope, parentScope);
 
@@ -81,10 +77,13 @@ export default class TypedRegistry {
 }
 
 function registerDefaults(item, scope, parentScope) {
-       // Inherit the parent's defaults
-       const itemDefaults = parentScope
-               ? Object.assign({}, defaults.get(parentScope), item.defaults)
-               : item.defaults;
+       // Inherit the parent's defaults and keep existing defaults
+       const itemDefaults = Object.assign(
+               Object.create(null),
+               parentScope && defaults.get(parentScope),
+               item.defaults,
+               defaults.get(scope)
+       );
 
        defaults.set(scope, itemDefaults);
 
index 8c349ab708f9e567787c710f211a3c8319447aae..e93207dc04e5edcf630ad3a6a2aa9c71923a3d29 100644 (file)
@@ -284,14 +284,18 @@ describe('Chart.registry', function() {
                expect(afterUnregisterCount).withContext('afterUnregister').toBe(4);
        });
 
-       it('should not register anything that would collide with existing defaults', function() {
-               Chart.defaults.controllers.test = {test: true};
+       it('should preserve existing defaults', function() {
+               Chart.defaults.controllers.test = {test1: true};
+
                class testController extends Chart.DatasetController {}
                testController.id = 'test';
-               expect(function() {
-                       Chart.register(testController);
-               }).toThrow(new Error('Can not register "test", because "defaults.controllers.test" would collide with existing defaults'));
-               delete Chart.defaults.controllers.test;
+               testController.defaults = {test1: false, test2: true};
+
+               Chart.register(testController);
+               expect(Chart.defaults.controllers.test).toEqual({test1: true, test2: true});
+
+               Chart.unregister(testController);
+               expect(Chart.defaults.controllers.test).not.toBeDefined();
        });
 
        describe('should handle multiple items', function() {