UNPKG

4.78 kBJavaScriptView Raw
1"use strict";
2/* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
3var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
6 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
7 return c > 3 && r && Object.defineProperty(target, key, r), r;
8};
9var __metadata = (this && this.__metadata) || function (k, v) {
10 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13var core_1 = require("@angular/core");
14var moment = require("moment");
15// under systemjs, moment is actually exported as the default export, so we account for that
16var momentConstructor = moment.default || moment;
17var CalendarPipe = /** @class */ (function () {
18 function CalendarPipe(cdRef, ngZone) {
19 var _this = this;
20 this.cdRef = cdRef;
21 this.ngZone = ngZone;
22 // using a single static timer for all instances of this pipe for performance reasons
23 CalendarPipe_1.initTimer(ngZone);
24 CalendarPipe_1.refs++;
25 // values such as Today will need to be replaced with Yesterday after midnight,
26 // so make sure we subscribe to an EventEmitter that we set up to emit at midnight
27 this.midnightSub = CalendarPipe_1.midnight.subscribe(function () {
28 _this.ngZone.run(function () { return _this.cdRef.markForCheck(); });
29 });
30 }
31 CalendarPipe_1 = CalendarPipe;
32 CalendarPipe.prototype.transform = function (value) {
33 var args = [];
34 for (var _i = 1; _i < arguments.length; _i++) {
35 args[_i - 1] = arguments[_i];
36 }
37 var formats = null;
38 var referenceTime = null;
39 for (var i = 0, len = args.length; i < len; i++) {
40 if (args[i] !== null) {
41 if (typeof args[i] === 'object' && !moment.isMoment(args[i])) {
42 formats = args[i];
43 }
44 else {
45 referenceTime = momentConstructor(args[i]);
46 }
47 }
48 }
49 return momentConstructor(value).calendar(referenceTime, formats);
50 };
51 CalendarPipe.prototype.ngOnDestroy = function () {
52 if (CalendarPipe_1.refs > 0) {
53 CalendarPipe_1.refs--;
54 }
55 if (CalendarPipe_1.refs === 0) {
56 CalendarPipe_1.removeTimer();
57 }
58 this.midnightSub.unsubscribe();
59 };
60 CalendarPipe.initTimer = function (ngZone) {
61 // initialize the timer
62 if (!CalendarPipe_1.midnight) {
63 CalendarPipe_1.midnight = new core_1.EventEmitter();
64 if (typeof window !== 'undefined') {
65 var timeToUpdate_1 = CalendarPipe_1._getMillisecondsUntilUpdate();
66 CalendarPipe_1.timer = ngZone.runOutsideAngular(function () {
67 return window.setTimeout(function () {
68 // emit the current date
69 CalendarPipe_1.midnight.emit(new Date());
70 // refresh the timer
71 CalendarPipe_1.removeTimer();
72 CalendarPipe_1.initTimer(ngZone);
73 }, timeToUpdate_1);
74 });
75 }
76 }
77 };
78 CalendarPipe.removeTimer = function () {
79 if (CalendarPipe_1.timer) {
80 window.clearTimeout(CalendarPipe_1.timer);
81 CalendarPipe_1.timer = null;
82 CalendarPipe_1.midnight = null;
83 }
84 };
85 CalendarPipe._getMillisecondsUntilUpdate = function () {
86 var now = momentConstructor();
87 var tomorrow = momentConstructor().startOf('day').add(1, 'days');
88 var timeToMidnight = tomorrow.valueOf() - now.valueOf();
89 return timeToMidnight + 1000; // 1 second after midnight
90 };
91 /**
92 * @private Internal reference counter, so we can clean up when no instances are in use
93 * @type {number}
94 */
95 CalendarPipe.refs = 0;
96 CalendarPipe = CalendarPipe_1 = __decorate([
97 core_1.Pipe({ name: 'amCalendar', pure: false }),
98 __metadata("design:paramtypes", [core_1.ChangeDetectorRef, core_1.NgZone])
99 ], CalendarPipe);
100 return CalendarPipe;
101 var CalendarPipe_1;
102}());
103exports.CalendarPipe = CalendarPipe;
104//# sourceMappingURL=calendar.pipe.js.map
\No newline at end of file