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.
This commit is contained in:
Simon Brunel 2017-09-02 11:04:10 +02:00 committed by GitHub
parent e3b70e0420
commit c7464ebf91
3 changed files with 25 additions and 2 deletions

View File

@ -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;
}
};

View File

@ -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}';

View File

@ -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