forked from urish/angular-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-moment.js
563 lines (497 loc) · 17 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/* angular-moment.js / v0.10.1 / (c) 2013, 2014, 2015 Uri Shaked / MIT Licence */
'format amd';
/* global define */
(function () {
'use strict';
function angularMoment(angular, moment) {
/**
* @ngdoc overview
* @name angularMoment
*
* @description
* angularMoment module provides moment.js functionality for angular.js apps.
*/
return angular.module('angularMoment', [])
/**
* @ngdoc object
* @name angularMoment.config:angularMomentConfig
*
* @description
* Common configuration of the angularMoment module
*/
.constant('angularMomentConfig', {
/**
* @ngdoc property
* @name angularMoment.config.angularMomentConfig#preprocess
* @propertyOf angularMoment.config:angularMomentConfig
* @returns {string} The default preprocessor to apply
*
* @description
* Defines a default preprocessor to apply (e.g. 'unix', 'etc', ...). The default value is null,
* i.e. no preprocessor will be applied.
*/
preprocess: null, // e.g. 'unix', 'utc', ...
/**
* @ngdoc property
* @name angularMoment.config.angularMomentConfig#timezone
* @propertyOf angularMoment.config:angularMomentConfig
* @returns {string} The default timezone
*
* @description
* The default timezone (e.g. 'Europe/London'). Empty string by default (does not apply
* any timezone shift).
*/
timezone: '',
/**
* @ngdoc property
* @name angularMoment.config.angularMomentConfig#format
* @propertyOf angularMoment.config:angularMomentConfig
* @returns {string} The pre-conversion format of the date
*
* @description
* Specify the format of the input date. Essentially it's a
* default and saves you from specifying a format in every
* element. Overridden by element attr. Null by default.
*/
format: null,
/**
* @ngdoc property
* @name angularMoment.config.angularMomentConfig#statefulFilters
* @propertyOf angularMoment.config:angularMomentConfig
* @returns {boolean} Whether angular-moment filters should be stateless (or not)
*
* @description
* Specifies whether the filters included with angular-moment are stateful.
* Stateful filters will automatically re-evaluate whenever you change the timezone
* or language settings, but may negatively impact performance. true by default.
*/
statefulFilters: true
})
/**
* @ngdoc object
* @name angularMoment.object:moment
*
* @description
* moment global (as provided by the moment.js library)
*/
.constant('moment', moment)
/**
* @ngdoc object
* @name angularMoment.config:amTimeAgoConfig
* @module angularMoment
*
* @description
* configuration specific to the amTimeAgo directive
*/
.constant('amTimeAgoConfig', {
/**
* @ngdoc property
* @name angularMoment.config.amTimeAgoConfig#withoutSuffix
* @propertyOf angularMoment.config:amTimeAgoConfig
* @returns {boolean} Whether to include a suffix in am-time-ago directive
*
* @description
* Defaults to false.
*/
withoutSuffix: false,
/**
* @ngdoc property
* @name angularMoment.config.amTimeAgoConfig#serverTime
* @propertyOf angularMoment.config:amTimeAgoConfig
* @returns {number} Server time in milliseconds since the epoch
*
* @description
* If set, time ago will be calculated relative to the given value.
* If null, local time will be used. Defaults to null.
*/
serverTime: null,
/**
* @ngdoc property
* @name angularMoment.config.amTimeAgoConfig#titleFormat
* @propertyOf angularMoment.config:amTimeAgoConfig
* @returns {string} The format of the date to be displayed in the title of the element. If null,
* the directive set the title of the element.
*
* @description
* The format of the date used for the title of the element. null by default.
*/
titleFormat: null,
/**
* @ngdoc property
* @name angularMoment.config.amTimeAgoConfig#fullDateThreshold
* @propertyOf angularMoment.config:amTimeAgoConfig
* @returns {number} The minimum number of days for showing a full date instead of relative time
*
* @description
* The threshold for displaying a full date. The default is null, which means the date will always
* be relative, and full date will never be displayed.
*/
fullDateThreshold: null,
/**
* @ngdoc property
* @name angularMoment.config.amTimeAgoConfig#fullDateFormat
* @propertyOf angularMoment.config:amTimeAgoConfig
* @returns {string} The format to use when displaying a full date.
*
* @description
* Specify the format of the date when displayed as full date. null by default.
*/
fullDateFormat: null
})
/**
* @ngdoc directive
* @name angularMoment.directive:amTimeAgo
* @module angularMoment
*
* @restrict A
*/
.directive('amTimeAgo', ['$window', 'moment', 'amMoment', 'amTimeAgoConfig', 'angularMomentConfig', function ($window, moment, amMoment, amTimeAgoConfig, angularMomentConfig) {
return function (scope, element, attr) {
var activeTimeout = null;
var currentValue;
var currentFormat = angularMomentConfig.format;
var withoutSuffix = amTimeAgoConfig.withoutSuffix;
var titleFormat = amTimeAgoConfig.titleFormat;
var fullDateThreshold = amTimeAgoConfig.fullDateThreshold;
var fullDateFormat = amTimeAgoConfig.fullDateFormat;
var localDate = new Date().getTime();
var preprocess = angularMomentConfig.preprocess;
var modelName = attr.amTimeAgo;
var isTimeElement = ('TIME' === element[0].nodeName.toUpperCase());
function getNow() {
var now;
if (amTimeAgoConfig.serverTime) {
var localNow = new Date().getTime();
var nowMillis = localNow - localDate + amTimeAgoConfig.serverTime;
now = moment(nowMillis);
}
else {
now = moment();
}
return now;
}
function cancelTimer() {
if (activeTimeout) {
$window.clearTimeout(activeTimeout);
activeTimeout = null;
}
}
function updateTime(momentInstance) {
var daysAgo = getNow().diff(momentInstance, 'day');
var showFullDate = fullDateThreshold && daysAgo >= fullDateThreshold;
if (showFullDate) {
element.text(momentInstance.format(fullDateFormat));
} else {
element.text(momentInstance.from(getNow(), withoutSuffix));
}
if (titleFormat && !element.attr('title')) {
element.attr('title', momentInstance.local().format(titleFormat));
}
if (!showFullDate) {
var howOld = Math.abs(getNow().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 updateDateTimeAttr(value) {
if (isTimeElement) {
element.attr('datetime', value);
}
}
function updateMoment() {
cancelTimer();
if (currentValue) {
var momentValue = amMoment.preprocessDate(currentValue, preprocess, currentFormat);
updateTime(momentValue);
updateDateTimeAttr(momentValue.toISOString());
}
}
scope.$watch(modelName, function (value) {
if ((typeof value === 'undefined') || (value === null) || (value === '')) {
cancelTimer();
if (currentValue) {
element.text('');
updateDateTimeAttr('');
currentValue = null;
}
return;
}
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) {
if (typeof format !== 'undefined') {
currentFormat = format;
updateMoment();
}
});
attr.$observe('amPreprocess', function (newValue) {
preprocess = newValue;
updateMoment();
});
attr.$observe('amFullDateThreshold', function (newValue) {
fullDateThreshold = newValue;
updateMoment();
});
attr.$observe('amFullDateFormat', function (newValue) {
fullDateFormat = newValue;
updateMoment();
});
scope.$on('$destroy', function () {
cancelTimer();
});
scope.$on('amMoment:localeChanged', function () {
updateMoment();
});
};
}])
/**
* @ngdoc service
* @name angularMoment.service.amMoment
* @module angularMoment
*/
.service('amMoment', ['moment', '$rootScope', '$log', 'angularMomentConfig', function (moment, $rootScope, $log, angularMomentConfig) {
/**
* @ngdoc property
* @name angularMoment:amMoment#preprocessors
* @module angularMoment
*
* @description
* Defines the preprocessors for the preprocessDate method. By default, the following preprocessors
* are defined: utc, unix.
*/
this.preprocessors = {
utc: moment.utc,
unix: moment.unix
};
/**
* @ngdoc function
* @name angularMoment.service.amMoment#changeLocale
* @methodOf angularMoment.service.amMoment
*
* @description
* Changes the locale for moment.js and updates all the am-time-ago directive instances
* with the new locale. Also broadcasts an `amMoment:localeChanged` event on $rootScope.
*
* @param {string} locale Locale code (e.g. en, es, ru, pt-br, etc.)
* @param {object} customization object of locale strings to override
*/
this.changeLocale = function (locale, customization) {
var result = moment.locale(locale, customization);
if (angular.isDefined(locale)) {
$rootScope.$broadcast('amMoment:localeChanged');
}
return result;
};
/**
* @ngdoc function
* @name angularMoment.service.amMoment#changeTimezone
* @methodOf angularMoment.service.amMoment
*
* @description
* Changes the default timezone for amCalendar, amDateFormat and amTimeAgo. Also broadcasts an
* `amMoment:timezoneChanged` event on $rootScope.
*
* @param {string} timezone Timezone name (e.g. UTC)
*/
this.changeTimezone = function (timezone) {
angularMomentConfig.timezone = timezone;
$rootScope.$broadcast('amMoment:timezoneChanged');
};
/**
* @ngdoc function
* @name angularMoment.service.amMoment#preprocessDate
* @methodOf angularMoment.service.amMoment
*
* @description
* Preprocess a given value and convert it into a Moment instance appropriate for use in the
* am-time-ago directive and the filters.
*
* @param {*} value The value to be preprocessed
* @param {string} preprocess The name of the preprocessor the apply (e.g. utc, unix)
* @param {string=} format Specifies how to parse the value (see {@link http://momentjs.com/docs/#/parsing/string-format/})
* @return {Moment} A value that can be parsed by the moment library
*/
this.preprocessDate = function (value, preprocess, format) {
if (angular.isUndefined(preprocess)) {
preprocess = angularMomentConfig.preprocess;
}
if (this.preprocessors[preprocess]) {
return this.preprocessors[preprocess](value, format);
}
if (preprocess) {
$log.warn('angular-moment: Ignoring unsupported value for preprocess: ' + preprocess);
}
if (!isNaN(parseFloat(value)) && isFinite(value)) {
// Milliseconds since the epoch
return moment(parseInt(value, 10));
}
// else just returns the value as-is.
return moment(value, format);
};
/**
* @ngdoc function
* @name angularMoment.service.amMoment#applyTimezone
* @methodOf angularMoment.service.amMoment
*
* @description
* 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 a moment() instance to apply the timezone shift to
* @param {string=} timezone The timezone to apply. If none given, will apply the timezone
* configured in angularMomentConfig.timezone.
*
* @returns {Moment} The given moment with the timezone shift applied
*/
this.applyTimezone = function (aMoment, timezone) {
timezone = timezone || angularMomentConfig.timezone;
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;
};
}])
/**
* @ngdoc filter
* @name angularMoment.filter:amCalendar
* @module angularMoment
*/
.filter('amCalendar', ['moment', 'amMoment', 'angularMomentConfig', function (moment, amMoment, angularMomentConfig) {
function amCalendarFilter(value, preprocess) {
if (typeof value === 'undefined' || value === null) {
return '';
}
value = amMoment.preprocessDate(value, preprocess);
var date = moment(value);
if (!date.isValid()) {
return '';
}
return amMoment.applyTimezone(date).calendar();
}
// Since AngularJS 1.3, filters have to explicitly define being stateful
// (this is no longer the default).
amCalendarFilter.$stateful = angularMomentConfig.statefulFilters;
return amCalendarFilter;
}])
/**
* @ngdoc filter
* @name angularMoment.filter:amDifference
* @module angularMoment
*/
.filter('amDifference', ['moment', 'amMoment', 'angularMomentConfig', function (moment, amMoment, angularMomentConfig) {
function amDifferenceFilter(value, otherValue, unit, usePrecision, preprocessValue, preprocessOtherValue) {
if (typeof value === 'undefined' || value === null) {
return '';
}
value = amMoment.preprocessDate(value, preprocessValue);
var date = moment(value);
if (!date.isValid()) {
return '';
}
var date2;
if (typeof otherValue === 'undefined' || otherValue === null) {
date2 = moment();
} else {
otherValue = amMoment.preprocessDate(otherValue, preprocessOtherValue);
date2 = moment(otherValue);
if (!date2.isValid()) {
return '';
}
}
return amMoment.applyTimezone(date).diff(amMoment.applyTimezone(date2), unit, usePrecision);
}
amDifferenceFilter.$stateful = angularMomentConfig.statefulFilters;
return amDifferenceFilter;
}])
/**
* @ngdoc filter
* @name angularMoment.filter:amDateFormat
* @module angularMoment
* @function
*/
.filter('amDateFormat', ['moment', 'amMoment', 'angularMomentConfig', function (moment, amMoment, angularMomentConfig) {
function amDateFormatFilter(value, format, preprocess, timezone) {
if (typeof value === 'undefined' || value === null) {
return '';
}
value = amMoment.preprocessDate(value, preprocess);
var date = moment(value);
if (!date.isValid()) {
return '';
}
return amMoment.applyTimezone(date, timezone).format(format);
}
amDateFormatFilter.$stateful = angularMomentConfig.statefulFilters;
return amDateFormatFilter;
}])
/**
* @ngdoc filter
* @name angularMoment.filter:amDurationFormat
* @module angularMoment
* @function
*/
.filter('amDurationFormat', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) {
function amDurationFormatFilter(value, format, suffix) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return moment.duration(value, format).humanize(suffix);
}
amDurationFormatFilter.$stateful = angularMomentConfig.statefulFilters;
return amDurationFormatFilter;
}])
/**
* @ngdoc filter
* @name angularMoment.filter:amTimeAgo
* @module angularMoment
* @function
*/
.filter('amTimeAgo', ['moment', 'amMoment', 'angularMomentConfig', function (moment, amMoment, angularMomentConfig) {
function amTimeAgoFilter(value, preprocess, suffix) {
if (typeof value === 'undefined' || value === null) {
return '';
}
value = amMoment.preprocessDate(value, preprocess);
var date = moment(value);
if (!date.isValid()) {
return '';
}
return amMoment.applyTimezone(date).fromNow(suffix);
}
amTimeAgoFilter.$stateful = angularMomentConfig.statefulFilters;
return amTimeAgoFilter;
}]);
}
if (typeof define === 'function' && define.amd) {
define(['angular', 'moment'], angularMoment);
} else if (typeof module !== 'undefined' && module && module.exports) {
angularMoment(angular, require('moment'));
module.exports = 'angularMoment';
} else {
angularMoment(angular, window.moment);
}
})();