Skip to content

Commit

Permalink
Add platform basic implementation (fallback) (chartjs#4708)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simonbrunel authored and yofreke committed Dec 30, 2017
1 parent a087e86 commit 05d7022
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
15 changes: 15 additions & 0 deletions 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;
}
};
7 changes: 7 additions & 0 deletions src/platforms/platform.dom.js
Expand Up @@ -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}';

Expand Down
5 changes: 3 additions & 2 deletions 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
Expand Down

0 comments on commit 05d7022

Please sign in to comment.