Skip to content

Commit

Permalink
Fix: config.platform was ignored (#9442)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jul 20, 2021
1 parent 8008488 commit bc7c58d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/core/core.config.js
Expand Up @@ -128,6 +128,10 @@ export default class Config {
this._resolverCache = new Map();
}

get platform() {
return this._config.platform;
}

get type() {
return this._config.type;
}
Expand Down
28 changes: 6 additions & 22 deletions src/core/core.controller.js
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions 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
*/
Expand Down
16 changes: 13 additions & 3 deletions 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};
10 changes: 10 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -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);
});
});
});

0 comments on commit bc7c58d

Please sign in to comment.