forked from urish/angular-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-moment.js
41 lines (35 loc) · 1.1 KB
/
angular-moment.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
/* angular-moment.js / v0.1.4 / (c) 2013 Uri Shaked / MIT Licence */
angular.module('angularMoment', [])
.directive('amTimeAgo', ['$window', '$timeout', function ($window, $timeout) {
'use strict';
return function (scope, element, attr) {
var activeTimeout = null;
function updateTime(momentInstance) {
element.text(momentInstance.fromNow());
var howOld = $window.moment().diff(momentInstance, 'minute');
var secondsUntilUpdate = 3600;
if (howOld < 1) {
secondsUntilUpdate = 1;
} else if (howOld < 60) {
secondsUntilUpdate = 30;
} else if (howOld < 180) {
secondsUntilUpdate = 300;
}
activeTimeout = $timeout(function () {
updateTime(momentInstance);
}, secondsUntilUpdate * 1000);
}
scope.$watch(attr.amTimeAgo, function (value) {
if (angular.isNumber(value)) {
// Milliseconds since the epoch
value = new Date(value);
}
// else assume the given value is already a date
if (activeTimeout) {
$timeout.cancel(activeTimeout);
activeTimeout = null;
}
updateTime($window.moment(value));
});
};
}]);