]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Helpers improvements 2551/head
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 14 May 2016 19:59:40 +0000 (15:59 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 14 May 2016 19:59:40 +0000 (15:59 -0400)
src/core/core.helpers.js

index 5d40947133f369b53515f9bd302fffca911234c6..44a22f0e9bcb303ec642b2a1a36f2c7ac7c7b414 100644 (file)
@@ -5,7 +5,6 @@
 var color = require('chartjs-color');
 
 module.exports = function(Chart) {
-
        //Global Chart helpers object for utility methods and classes
        var helpers = Chart.helpers = {};
 
@@ -194,15 +193,19 @@ module.exports = function(Chart) {
                }
        };
        helpers.where = function(collection, filterCallback) {
-               var filtered = [];
+               if (helpers.isArray(collection) && Array.prototype.filter) {
+                       return collection.filter(filterCallback);
+               } else {
+                       var filtered = [];
 
-               helpers.each(collection, function(item) {
-                       if (filterCallback(item)) {
-                               filtered.push(item);
-                       }
-               });
+                       helpers.each(collection, function(item) {
+                               if (filterCallback(item)) {
+                                       filtered.push(item);
+                               }
+                       });
 
-               return filtered;
+                       return filtered;
+               }
        };
        helpers.findIndex = function(arrayToSearch, callback, thisArg) {
                var index = -1;
@@ -653,9 +656,10 @@ module.exports = function(Chart) {
                        canvas = evt.currentTarget || evt.srcElement,
                        boundingRect = canvas.getBoundingClientRect();
 
-               if (e.touches && e.touches.length > 0) {
-                       mouseX = e.touches[0].clientX;
-                       mouseY = e.touches[0].clientY;
+               var touches = e.touches;
+               if (touches && touches.length > 0) {
+                       mouseX = touches[0].clientX;
+                       mouseY = touches[0].clientY;
 
                } else {
                        mouseX = e.clientX;
@@ -703,19 +707,19 @@ module.exports = function(Chart) {
        };
        helpers.bindEvents = function(chartInstance, arrayOfEvents, handler) {
                // Create the events object if it's not already present
-               if (!chartInstance.events)
-                       chartInstance.events = {};
+               var events = chartInstance.events = chartInstance.events || {};
 
                helpers.each(arrayOfEvents, function(eventName) {
-                       chartInstance.events[eventName] = function() {
+                       events[eventName] = function() {
                                handler.apply(chartInstance, arguments);
                        };
-                       helpers.addEvent(chartInstance.chart.canvas, eventName, chartInstance.events[eventName]);
+                       helpers.addEvent(chartInstance.chart.canvas, eventName, events[eventName]);
                });
        };
        helpers.unbindEvents = function(chartInstance, arrayOfEvents) {
+               var canvas = chartInstance.chart.canvas;
                helpers.each(arrayOfEvents, function(handler, eventName) {
-                       helpers.removeEvent(chartInstance.chart.canvas, eventName, handler);
+                       helpers.removeEvent(canvas, eventName, handler);
                });
        };
 
@@ -791,13 +795,14 @@ module.exports = function(Chart) {
        };
        helpers.retinaScale = function(chart) {
                var ctx = chart.ctx;
-               var width = chart.canvas.width;
-               var height = chart.canvas.height;
+               var canvas = chart.canvas;
+               var width = canvas.width;
+               var height = canvas.height;
                var pixelRatio = chart.currentDevicePixelRatio = window.devicePixelRatio || 1;
 
                if (pixelRatio !== 1) {
-                       ctx.canvas.height = height * pixelRatio;
-                       ctx.canvas.width = width * pixelRatio;
+                       canvas.height = height * pixelRatio;
+                       canvas.width = width * pixelRatio;
                        ctx.scale(pixelRatio, pixelRatio);
 
                        // Store the device pixel ratio so that we can go backwards in `destroy`.
@@ -806,8 +811,8 @@ module.exports = function(Chart) {
                        chart.originalDevicePixelRatio = chart.originalDevicePixelRatio || pixelRatio;
                }
 
-               ctx.canvas.style.width = width + 'px';
-               ctx.canvas.style.height = height + 'px';
+               canvas.style.width = width + 'px';
+               canvas.style.height = height + 'px';
        };
        //-- Canvas methods
        helpers.clear = function(chart) {
@@ -818,12 +823,12 @@ module.exports = function(Chart) {
        };
        helpers.longestText = function(ctx, font, arrayOfStrings, cache) {
                cache = cache || {};
-               cache.data = cache.data || {};
-               cache.garbageCollect = cache.garbageCollect || [];
+               var data = cache.data = cache.data || {};
+               var gc = cache.garbageCollect = cache.garbageCollect || [];
 
                if (cache.font !== font) {
-                       cache.data = {};
-                       cache.garbageCollect = [];
+                       data = cache.data = {};
+                       gc = cache.garbageCollect = [];
                        cache.font = font;
                }
 
@@ -832,10 +837,10 @@ module.exports = function(Chart) {
                helpers.each(arrayOfStrings, function(string) {
                        // Undefined strings should not be measured
                        if (string !== undefined && string !== null) {
-                               var textWidth = cache.data[string];
+                               var textWidth = data[string];
                                if (!textWidth) {
-                                       textWidth = cache.data[string] = ctx.measureText(string).width;
-                                       cache.garbageCollect.push(string);
+                                       textWidth = data[string] = ctx.measureText(string).width;
+                                       gc.push(string);
                                }
 
                                if (textWidth > longest) {
@@ -844,12 +849,12 @@ module.exports = function(Chart) {
                        }
                });
 
-               var gcLen = cache.garbageCollect.length / 2;
+               var gcLen = gc.length / 2;
                if (gcLen > arrayOfStrings.length) {
                        for (var i = 0; i < gcLen; i++) {
-                               delete cache.data[cache.garbageCollect[i]];
+                               delete data[gc[i]];
                        }
-                       cache.garbageCollect.splice(0, gcLen);
+                       gc.splice(0, gcLen);
                }
 
                return longest;
@@ -893,16 +898,17 @@ module.exports = function(Chart) {
                }
 
                // Set the style
-               hiddenIframe.style.width = '100%';
-               hiddenIframe.style.display = 'block';
-               hiddenIframe.style.border = 0;
-               hiddenIframe.style.height = 0;
-               hiddenIframe.style.margin = 0;
-               hiddenIframe.style.position = 'absolute';
-               hiddenIframe.style.left = 0;
-               hiddenIframe.style.right = 0;
-               hiddenIframe.style.top = 0;
-               hiddenIframe.style.bottom = 0;
+               var style = hiddenIframe.style;
+               style.width = '100%';
+               style.display = 'block';
+               style.border = 0;
+               style.height = 0;
+               style.margin = 0;
+               style.position = 'absolute';
+               style.left = 0;
+               style.right = 0;
+               style.top = 0;
+               style.bottom = 0;
 
                // Insert the iframe so that contentWindow is available
                node.insertBefore(hiddenIframe, node.firstChild);