From: Simon Brunel Date: Sat, 2 Sep 2017 09:04:10 +0000 (+0200) Subject: Add platform basic implementation (fallback) (#4708) X-Git-Tag: v2.7.0~1^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c7464ebf91af37482912b434a9e5dc25cf46666e;p=thirdparty%2FChart.js.git Add platform basic implementation (fallback) (#4708) If `window` or `document` are `undefined`, a minimal platform implementation is used instead, which one only returns a context2d read from the given canvas/context. --- diff --git a/src/platforms/platform.basic.js b/src/platforms/platform.basic.js new file mode 100644 index 000000000..f510b3575 --- /dev/null +++ b/src/platforms/platform.basic.js @@ -0,0 +1,15 @@ +/** + * Platform fallback implementation (minimal). + * @see https://github.com/chartjs/Chart.js/pull/4591#issuecomment-319575939 + */ + +module.exports = { + acquireContext: function(item) { + if (item && item.canvas) { + // Support for any object associated to a canvas (including a context2d) + item = item.canvas; + } + + return item && item.getContext('2d') || null; + } +}; diff --git a/src/platforms/platform.dom.js b/src/platforms/platform.dom.js index 8fe9f2707..a14119ad4 100644 --- a/src/platforms/platform.dom.js +++ b/src/platforms/platform.dom.js @@ -305,6 +305,13 @@ function injectCSS(platform, css) { } module.exports = { + /** + * This property holds whether this platform is enabled for the current environment. + * Currently used by platform.js to select the proper implementation. + * @private + */ + _enabled: typeof window !== 'undefined' && typeof document !== 'undefined', + initialize: function() { var keyframes = 'from{opacity:0.99}to{opacity:1}'; diff --git a/src/platforms/platform.js b/src/platforms/platform.js index 8f4827732..c755f81fd 100644 --- a/src/platforms/platform.js +++ b/src/platforms/platform.js @@ -1,10 +1,11 @@ 'use strict'; var helpers = require('../helpers/index'); +var basic = require('./platform.basic'); +var dom = require('./platform.dom'); -// By default, select the browser (DOM) platform. // @TODO Make possible to select another platform at build time. -var implementation = require('./platform.dom'); +var implementation = dom._enabled ? dom : basic; /** * @namespace Chart.platform