]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Deprecate Chart.Controller (and nested `chart`)
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 28 Jan 2017 11:43:54 +0000 (12:43 +0100)
committerEvert Timberg <evert.timberg+github@gmail.com>
Fri, 10 Feb 2017 23:37:56 +0000 (18:37 -0500)
src/core/core.controller.js
src/core/core.js

index b38df58b291d4b94f0af1a1e119a98b9dcdcb53a..070aa0356b3ea23d296e6160a40bc0492790501a 100644 (file)
@@ -38,7 +38,7 @@ module.exports = function(Chart) {
 
        /**
         * Updates the config of the chart
-        * @param chart {Chart.Controller} chart to update the options for
+        * @param chart {Chart} chart to update the options for
         */
        function updateConfig(chart) {
                var newOptions = chart.options;
@@ -56,58 +56,66 @@ module.exports = function(Chart) {
                chart.tooltip._options = newOptions.tooltips;
        }
 
-       /**
-        * @class Chart.Controller
-        * The main controller of a chart.
-        */
-       Chart.Controller = function(item, config, instance) {
-               var me = this;
-
-               config = initConfig(config);
-
-               var context = platform.acquireContext(item, config);
-               var canvas = context && context.canvas;
-               var height = canvas && canvas.height;
-               var width = canvas && canvas.width;
-
-               instance.ctx = context;
-               instance.canvas = canvas;
-               instance.config = config;
-               instance.width = width;
-               instance.height = height;
-               instance.aspectRatio = height? width / height : null;
-
-               me.id = helpers.uid();
-               me.chart = instance;
-               me.config = config;
-               me.options = config.options;
-               me._bufferedRender = false;
-
-               // Add the chart instance to the global namespace
-               Chart.instances[me.id] = me;
-
-               Object.defineProperty(me, 'data', {
-                       get: function() {
-                               return me.config.data;
-                       }
-               });
-
-               if (!context || !canvas) {
-                       // The given item is not a compatible context2d element, let's return before finalizing
-                       // the chart initialization but after setting basic chart / controller properties that
-                       // can help to figure out that the chart is not valid (e.g chart.canvas !== null);
-                       // https://github.com/chartjs/Chart.js/issues/2807
-                       console.error("Failed to create chart: can't acquire context from the given item");
-                       return me;
-               }
+       helpers.extend(Chart.prototype, /** @lends Chart */ {
+               /**
+                * @private
+                */
+               construct: function(item, config) {
+                       var me = this;
 
-               me.initialize();
-               me.update();
+                       config = initConfig(config);
+
+                       var context = platform.acquireContext(item, config);
+                       var canvas = context && context.canvas;
+                       var height = canvas && canvas.height;
+                       var width = canvas && canvas.width;
+
+                       me.id = helpers.uid();
+                       me.ctx = context;
+                       me.canvas = canvas;
+                       me.config = config;
+                       me.width = width;
+                       me.height = height;
+                       me.aspectRatio = height? width / height : null;
+                       me.options = config.options;
+                       me._bufferedRender = false;
 
-               return me;
-       };
+                       /**
+                        * Provided for backward compatibility, Chart and Chart.Controller have been merged,
+                        * the "instance" still need to be defined since it might be called from plugins.
+                        * @prop Chart#chart
+                        * @deprecated since version 2.6.0
+                        * @todo remove at version 3
+                        * @private
+                        */
+                       me.chart = me;
+                       me.controller = me;  // chart.chart.controller #inception
+
+                       // Add the chart instance to the global namespace
+                       Chart.instances[me.id] = me;
 
-       helpers.extend(Chart.Controller.prototype, /** @lends Chart.Controller.prototype */ {
+                       Object.defineProperty(me, 'data', {
+                               get: function() {
+                                       return me.config.data;
+                               }
+                       });
+
+                       if (!context || !canvas) {
+                               // The given item is not a compatible context2d element, let's return before finalizing
+                               // the chart initialization but after setting basic chart / controller properties that
+                               // can help to figure out that the chart is not valid (e.g chart.canvas !== null);
+                               // https://github.com/chartjs/Chart.js/issues/2807
+                               console.error("Failed to create chart: can't acquire context from the given item");
+                               return;
+                       }
+
+                       me.initialize();
+                       me.update();
+               },
+
+               /**
+                * @private
+                */
                initialize: function() {
                        var me = this;
 
@@ -749,4 +757,13 @@ module.exports = function(Chart) {
                        return changed;
                }
        });
+
+       /**
+        * Provided for backward compatibility, use Chart instead.
+        * @class Chart.Controller
+        * @deprecated since version 2.6.0
+        * @todo remove at version 3
+        * @private
+        */
+       Chart.Controller = Chart;
 };
index cea3cf30baeaa88184bb9e5d519b652bfe974b5f..69e9cf9a037a76ad66ce308b506417a95e63939c 100755 (executable)
@@ -4,8 +4,8 @@ module.exports = function() {
 
        // Occupy the global variable of Chart, and create a simple base class
        var Chart = function(item, config) {
-               this.controller = new Chart.Controller(item, config, this);
-               return this.controller;
+               this.construct(item, config);
+               return this;
        };
 
        // Globally expose the defaults to allow for user updating/changing