From: Jukka Kurkela Date: Tue, 20 Jul 2021 11:51:16 +0000 (+0300) Subject: Fix: config.platform was ignored (#9442) X-Git-Tag: v3.5.0~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc7c58d46d8cb4e7019ce4d4227b0fa6ebc71ecd;p=thirdparty%2FChart.js.git Fix: config.platform was ignored (#9442) --- diff --git a/src/core/core.config.js b/src/core/core.config.js index ff97d5f75..255699bdf 100644 --- a/src/core/core.config.js +++ b/src/core/core.config.js @@ -128,6 +128,10 @@ export default class Config { this._resolverCache = new Map(); } + get platform() { + return this._config.platform; + } + get type() { return this._config.type; } diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 3d1f6d445..16c98cd97 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -2,11 +2,11 @@ import animator from './core.animator'; import defaults, {overrides} from './core.defaults'; import Interaction from './core.interaction'; import layouts from './core.layouts'; -import {BasicPlatform, DomPlatform} from '../platform'; +import {_detectPlatform} from '../platform'; import PluginService from './core.plugins'; import registry from './core.registry'; import Config, {determineAxis, getIndexAxis} from './core.config'; -import {retinaScale} from '../helpers/helpers.dom'; +import {retinaScale, _isDomSupported} from '../helpers/helpers.dom'; import {each, callback as callCallback, uid, valueOrDefault, _elementsEqual, isNullOrUndef, setsEqual} from '../helpers/helpers.core'; import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas'; // @ts-ignore @@ -44,16 +44,12 @@ function onAnimationProgress(context) { callCallback(animationOptions && animationOptions.onProgress, [context], chart); } -function isDomSupported() { - return typeof window !== 'undefined' && typeof document !== 'undefined'; -} - /** * Chart.js can take a string id of a canvas element, a 2d context, or a canvas element itself. * Attempt to unwrap the item passed into the chart constructor so that it is a canvas element (if possible). */ function getCanvas(item) { - if (isDomSupported() && typeof item === 'string') { + if (_isDomSupported() && typeof item === 'string') { item = document.getElementById(item); } else if (item && item.length) { // Support for array based queries (such as jQuery) @@ -76,10 +72,10 @@ const getChart = (key) => { class Chart { // eslint-disable-next-line max-statements - constructor(item, config) { + constructor(item, userConfig) { const me = this; - this.config = config = new Config(config); + const config = this.config = new Config(userConfig); const initialCanvas = getCanvas(item); const existingChart = getChart(initialCanvas); if (existingChart) { @@ -91,7 +87,7 @@ class Chart { const options = config.createResolver(config.chartOptionScopes(), me.getContext()); - this.platform = me._initializePlatform(initialCanvas, config); + this.platform = new (config.platform || _detectPlatform(initialCanvas))(); const context = me.platform.acquireContext(initialCanvas, options.aspectRatio); const canvas = context && context.canvas; @@ -206,18 +202,6 @@ class Chart { return me; } - /** - * @private - */ - _initializePlatform(canvas, config) { - if (config.platform) { - return new config.platform(); - } else if (!isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) { - return new BasicPlatform(); - } - return new DomPlatform(); - } - clear() { clearCanvas(this.canvas, this.ctx); return this; diff --git a/src/helpers/helpers.dom.js b/src/helpers/helpers.dom.js index 750af3907..8fe1f3f9b 100644 --- a/src/helpers/helpers.dom.js +++ b/src/helpers/helpers.dom.js @@ -1,5 +1,12 @@ import {INFINITY} from './helpers.math'; +/** + * @private + */ +export function _isDomSupported() { + return typeof window !== 'undefined' && typeof document !== 'undefined'; +} + /** * @private */ diff --git a/src/platform/index.js b/src/platform/index.js index 4d4136353..7ae28f5b0 100644 --- a/src/platform/index.js +++ b/src/platform/index.js @@ -1,3 +1,13 @@ -export {default as BasePlatform} from './platform.base'; -export {default as BasicPlatform} from './platform.basic'; -export {default as DomPlatform} from './platform.dom'; +import {_isDomSupported} from '../helpers'; +import BasePlatform from './platform.base'; +import BasicPlatform from './platform.basic'; +import DomPlatform from './platform.dom'; + +export function _detectPlatform(canvas) { + if (!_isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) { + return BasicPlatform; + } + return DomPlatform; +} + +export {BasePlatform, BasicPlatform, DomPlatform}; diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 393983852..ac0479b20 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -1935,4 +1935,14 @@ describe('Chart', function() { expect(active[0].element).toBe(meta.data[0]); }); }); + + describe('platform', function() { + it('should use the platform constructor provided in config', function() { + const chart = acquireChart({ + platform: Chart.platforms.BasicPlatform, + type: 'line', + }); + expect(chart.platform).toBeInstanceOf(Chart.platforms.BasicPlatform); + }); + }); });