-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-ticker.js
76 lines (64 loc) · 3.21 KB
/
ng-ticker.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
angular.module('ngTicker', [])
.factory('ngTicker', ['$rootScope', '$interval', function ($rootScope, $interval) {
return {
tickDown: function (expiry, tickerInstance) {
var tickerFunc, startTicker
startTicker = function () {
tickerFunc = $interval(function () {
var formattedExpiry = moment(moment(expiry), moment.ISO_8601)
var totalSeconds = moment(formattedExpiry).diff(moment(), 'seconds')
var days = Math.floor(totalSeconds / 86400)
var hours = Math.floor((totalSeconds - (days * 86400)) / 3600)
var minutes = Math.floor((totalSeconds - (days * 86400) - (hours * 3600)) / 60)
var seconds = totalSeconds - (days * 86400) - (hours * 3600) - (minutes * 60)
// Round off seconds
seconds = Math.round(seconds * 100) / 100
var tickMeta = {
days: (days < 0 ? '00' : (days < 10 ? '0' + days : days)),
hours: (hours < 0 ? '00' : (hours < 10 ? '0' + hours : hours)),
minutes: (hours < 0 ? '00' : (minutes < 10 ? '0' + minutes : minutes)),
seconds: (hours < 0 ? '00' : (seconds < 10 ? '0' + seconds : seconds)),
}
// Broadcast ticker events
if (JSON.stringify(tickMeta) === JSON.stringify({'days': '00', 'hours': '00', 'minutes': '00', 'seconds': '00'})) {
$rootScope.$broadcast('ngTicker:tick' + tickerInstance, tickMeta)
$rootScope.$broadcast('ngTicker:expired' + tickerInstance, tickMeta)
$interval.cancel(tickerFunc)
} else {
$rootScope.$broadcast('ngTicker:tick' + tickerInstance, tickMeta)
}
}, 1000)
}
startTicker()
},
tickUp: function (startTime, tickerInstance) {
var tickerFunc, startTicker
startTicker = function () {
tickerFunc = $interval(function () {
var formattedExpiry = moment(moment(startTime), moment.ISO_8601)
var totalSeconds = moment().diff(moment(formattedExpiry), 'seconds')
var duration = moment.duration(totalSeconds + 1, 'seconds')
var days = Math.floor(totalSeconds / 86400)
var hours = duration.hours()
var minutes = duration.minutes()
var seconds = duration.seconds()
var tickMeta = {
days: (days < 0 ? '00' : (days < 10 ? '0' + days : days)),
hours: (hours < 0 ? '00' : (hours < 10 ? '0' + hours : hours)),
minutes: (minutes < 0 ? '00' : (minutes < 10 ? '0' + minutes : minutes)),
seconds: (seconds < 0 ? '00' : (seconds < 10 ? '0' + seconds : seconds))
}
// Broadcast ticker events
if (JSON.stringify(tickMeta) === JSON.stringify({'days': '00', 'hours': '00', 'minutes': '00', 'seconds': '00'})) {
$rootScope.$broadcast('ngTicker:tick' + tickerInstance, tickMeta)
$rootScope.$broadcast('ngTicker:notStarted' + tickerInstance, tickMeta)
$interval.cancel(tickerFunc)
} else {
$rootScope.$broadcast('ngTicker:tick' + tickerInstance, tickMeta)
}
}, 1000)
}
startTicker()
}
}
}])