]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add platform basic implementation (fallback) (#4708)
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 2 Sep 2017 09:04:10 +0000 (11:04 +0200)
committerGitHub <noreply@github.com>
Sat, 2 Sep 2017 09:04:10 +0000 (11:04 +0200)
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.

src/platforms/platform.basic.js [new file with mode: 0644]
src/platforms/platform.dom.js
src/platforms/platform.js

diff --git a/src/platforms/platform.basic.js b/src/platforms/platform.basic.js
new file mode 100644 (file)
index 0000000..f510b35
--- /dev/null
@@ -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;
+       }
+};
index 8fe9f27073c58bf00665ba00f283af2d10c894bb..a14119ad425e16925ac84f42eda057e440ab9872 100644 (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}';
 
index 8f4827732b1bf0502d6084c486288a1e11b37759..c755f81fdb106e80757b7d3cfba352fd6ecbc369 100644 (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