forked from inorganik/countUp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-countUp.js
136 lines (117 loc) · 4.92 KB
/
angular-countUp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function (angular) {
// Count-Up directive
// --------------------------------------------
//
// * **Class:** CountUp
// * **Author:** Jamie Perkins
//
// Creates a counting animation for numbers
// REQUIRED attributes:
// - endVal
//
// DEPENDENCY: countUp.js
'use strict';
var module = angular.module('countUpModule', []);
/**
* count-up attribute directive
*
* @param {number} startVal - (optional) The value you want to begin at, default 0
* @param {number} countUp - The value you want to arrive at
* @param {number} duration - (optional) Duration in seconds, default 2.
* @param {number} decimals - (optional) Number of decimal places in number, default 0
* @param {boolean} reanimateOnClick - (optional) Config if reanimate on click event, default true.
* @param {string} filter - (optional) Filter expression to apply to animated values
* @param {object} options - (optional) Provides for extra configuration, such as easing.
*/
module.directive('countUp', [ '$filter', function($filter) {
return {
restrict: 'A',
scope: {
startVal: '=?',
endVal: '=?',
duration: '=?',
decimals: '=?',
reanimateOnClick: '=?',
filter: '@',
options: '=?'
},
link: function ($scope, $el, $attrs) {
var options = {};
if ($scope.filter) {
var filterFunction = createFilterFunction();
options.formattingFn = filterFunction;
}
if ($scope.options) {
angular.extend(options, $scope.options);
}
var countUp = createCountUp($scope.startVal, $scope.endVal, $scope.decimals, $scope.duration);
function createFilterFunction() {
var filterParams = $scope.filter.split(':');
var filterName = filterParams.shift();
return function(value) {
var filterCallParams = [value];
Array.prototype.push.apply(filterCallParams, filterParams);
value = $filter(filterName).apply(null, filterCallParams);
return value;
};
}
function createCountUp(sta, end, dec, dur) {
sta = sta || 0;
if (isNaN(sta)) sta = Number(sta.match(/[\d\-\.]+/g).join('')); // strip non-numerical characters
end = end || 0;
if (isNaN(end)) end = Number(end.match(/[\d\-\.]+/g).join('')); // strip non-numerical characters
dur = Number(dur) || 2;
dec = Number(dec) || 0;
// construct countUp
var countUp = new CountUp($el[0], sta, end, dec, dur, options);
if (end > 999) {
// make easing smoother for large numbers
countUp = new CountUp($el[0], sta, end - 100, dec, dur / 2, options);
}
return countUp;
}
function animate() {
countUp.reset();
if ($scope.endVal > 999) {
countUp.start(function() {
countUp.update($scope.endVal);
});
}
else {
countUp.start();
}
}
// fire on scroll-spy event, or right away
if ($attrs.scrollSpyEvent) {
// listen for scroll spy event
$scope.$on($attrs.scrollSpyEvent, function (event, data) {
if (data === $attrs.id) {
animate();
}
});
}
else {
animate();
}
// re-animate on click
var reanimateOnClick = angular.isDefined($scope.reanimateOnClick) ? $scope.reanimateOnClick : true;
if (reanimateOnClick) {
$el.on('click', function() {
animate();
});
}
$scope.$watch('endVal', function (newValue, oldValue) {
if (newValue === null || newValue === oldValue) {
return;
}
if (countUp !== null) {
countUp.update($scope.endVal);
} else {
countUp = createCountUp($scope.startVal, $scope.endVal, $scope.decimals, $scope.duration);
animate();
}
});
}
};
}]);
})(angular);