From: Evert Timberg Date: Sun, 13 Mar 2016 16:24:33 +0000 (-0400) Subject: Fix animation bug. Helpers.findNextWhere no longer returned the index of the item... X-Git-Tag: v2.0.0~20^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2129%2Fhead;p=thirdparty%2FChart.js.git Fix animation bug. Helpers.findNextWhere no longer returned the index of the item. This causes the wrong animation object to be removed. --- diff --git a/src/core/core.animation.js b/src/core/core.animation.js index 5e3dfa547..6ab5f6c63 100644 --- a/src/core/core.animation.js +++ b/src/core/core.animation.js @@ -52,11 +52,11 @@ module.exports = function(Chart) { }, // Cancel the animation for a given chart instance cancelAnimation: function(chartInstance) { - var index = helpers.findNextWhere(this.animations, function(animationWrapper) { + var index = helpers.findIndex(this.animations, function(animationWrapper) { return animationWrapper.chartInstance === chartInstance; }); - if (index) { + if (index !== -1) { this.animations.splice(index, 1); chartInstance.animating = false; } @@ -75,7 +75,8 @@ module.exports = function(Chart) { this.dropFrames = this.dropFrames % 1; } - for (var i = 0; i < this.animations.length; i++) { + var i = 0; + while (i < this.animations.length) { if (this.animations[i].animationObject.currentStep === null) { this.animations[i].animationObject.currentStep = 0; } @@ -98,9 +99,10 @@ module.exports = function(Chart) { // executed the last frame. Remove the animation. this.animations[i].chartInstance.animating = false; + this.animations.splice(i, 1); - // Keep the index in place to offset the splice - i--; + } else { + ++i; } } diff --git a/src/core/core.helpers.js b/src/core/core.helpers.js index c5f884e9e..37355f00a 100644 --- a/src/core/core.helpers.js +++ b/src/core/core.helpers.js @@ -201,6 +201,23 @@ module.exports = function(Chart) { return filtered; }; + helpers.findIndex = function(arrayToSearch, callback, thisArg) { + var index = -1; + if (Array.prototype.findIndex) { + index = arrayToSearch.findIndex(callback, thisArg); + } else { + for (var i = 0; i < arrayToSearch.length; ++i) { + thisArg = thisArg !== undefined ? thisArg : arrayToSearch; + + if (callback.call(thisArg, arrayToSearch[i], i, arrayToSearch)) { + index = i; + break; + } + } + } + + return index; + }; helpers.findNextWhere = function(arrayToSearch, filterCallback, startIndex) { // Default to start of the array if (startIndex === undefined || startIndex === null) {