1 | "use strict";
|
2 |
|
3 | var __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 | };
|
9 | var __metadata = (this && this.__metadata) || function (k, v) {
|
10 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
11 | };
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var core_1 = require("@angular/core");
|
14 | var moment = require("moment");
|
15 |
|
16 | var momentConstructor = moment.default || moment;
|
17 | var CalendarPipe = (function () {
|
18 | function CalendarPipe(cdRef, ngZone) {
|
19 | var _this = this;
|
20 | this.cdRef = cdRef;
|
21 | this.ngZone = ngZone;
|
22 |
|
23 | CalendarPipe_1.initTimer(ngZone);
|
24 | CalendarPipe_1.refs++;
|
25 |
|
26 |
|
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 |
|
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 |
|
69 | CalendarPipe_1.midnight.emit(new Date());
|
70 |
|
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;
|
90 | };
|
91 | |
92 |
|
93 |
|
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 | }());
|
103 | exports.CalendarPipe = CalendarPipe;
|
104 |
|
\ | No newline at end of file |