forked from urish/angular-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-moment.js
More file actions
168 lines (144 loc) · 4.59 KB
/
angular-moment.js
File metadata and controls
168 lines (144 loc) · 4.59 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* angular-moment.js / v0.6.1 / (c) 2013, 2014 Uri Shaked / MIT Licence */
(function(){
'use strict';
/**
* Apply a timezone onto a given moment object - if moment-timezone.js is included
* Otherwise, it'll not apply any timezone shift.
* @param {Moment} aMoment
* @param {string} timezone
* @returns {Moment}
*/
function applyTimezone(aMoment, timezone, $log) {
if (aMoment && timezone) {
if (aMoment.tz) {
aMoment = aMoment.tz(timezone);
} else {
$log.warn('angular-moment: timezone specified but moment.tz() is undefined. Did you forget to include moment-timezone.js?');
}
}
return aMoment;
}
angular.module('angularMoment', [])
/**
* Common configuration of the angularMoment module
*/
.constant('angularMomentConfig', {
timezone: '' // e.g. 'Europe/London'
})
.constant('amTimeAgoConfig', { withoutSuffix: false})
.directive('amTimeAgo', ['$window', 'amTimeAgoConfig', function ($window, amTimeAgoConfig) {
return function (scope, element, attr) {
var activeTimeout = null;
var currentValue;
var currentFormat;
var withoutSuffix = amTimeAgoConfig.withoutSuffix;
function cancelTimer() {
if (activeTimeout) {
$window.clearTimeout(activeTimeout);
activeTimeout = null;
}
}
function updateTime(momentInstance) {
element.text(momentInstance.fromNow(withoutSuffix));
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 = $window.setTimeout(function () {
updateTime(momentInstance);
}, secondsUntilUpdate * 1000);
}
function updateMoment() {
cancelTimer();
updateTime($window.moment(currentValue, currentFormat));
}
scope.$watch(attr.amTimeAgo, function (value) {
if ((typeof value === 'undefined') || (value === null) || (value === '')) {
cancelTimer();
if (currentValue) {
element.text('');
currentValue = null;
}
return;
}
if (angular.isNumber(value)) {
// Milliseconds since the epoch
value = new Date(value);
}
// else assume the given value is already a date
currentValue = value;
updateMoment();
});
if (angular.isDefined(attr.amWithoutSuffix)) {
scope.$watch(attr.amWithoutSuffix, function (value) {
if (typeof value === 'boolean') {
withoutSuffix = value;
updateMoment();
} else {
withoutSuffix = amTimeAgoConfig.withoutSuffix;
}
});
}
attr.$observe('amFormat', function (format) {
currentFormat = format;
if (currentValue) {
updateMoment();
}
});
scope.$on('$destroy', function () {
cancelTimer();
});
scope.$on('amMoment:languageChange', function () {
updateMoment();
});
};
}])
.factory('amMoment', ['$window', '$rootScope', function ($window, $rootScope) {
return {
changeLanguage: function (lang) {
$window.moment.lang(lang);
$rootScope.$broadcast('amMoment:languageChange');
}
};
}])
.filter('amCalendar', ['$window', '$log', 'angularMomentConfig', function ($window, $log, angularMomentConfig) {
return function (value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
if (!isNaN(parseFloat(value)) && isFinite(value)) {
// Milliseconds since the epoch
value = new Date(parseInt(value, 10));
}
// else assume the given value is already a date
return applyTimezone($window.moment(value), angularMomentConfig.timezone, $log).calendar();
};
}])
.filter('amDateFormat', ['$window', '$log', 'angularMomentConfig', function ($window, $log, angularMomentConfig) {
return function (value, format) {
if (typeof value === 'undefined' || value === null) {
return '';
}
if (!isNaN(parseFloat(value)) && isFinite(value)) {
// Milliseconds since the epoch
value = new Date(parseInt(value, 10));
}
// else assume the given value is already a date
return applyTimezone($window.moment(value), angularMomentConfig.timezone, $log).format(format);
};
}])
.filter('amDurationFormat', ['$window', function ($window) {
return function (value, format, suffix) {
if (typeof value === 'undefined' || value === null) {
return '';
}
// else assume the given value is already a duration in a format (miliseconds, etc)
return $window.moment.duration(value, format).humanize(suffix);
};
}]);
})();