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 TimeAgoPipe = (function () {
|
18 | function TimeAgoPipe(cdRef, ngZone) {
|
19 | this.cdRef = cdRef;
|
20 | this.ngZone = ngZone;
|
21 | }
|
22 | TimeAgoPipe.prototype.transform = function (value, omitSuffix) {
|
23 | if (this.hasChanged(value, omitSuffix)) {
|
24 | this.lastTime = this.getTime(value);
|
25 | this.lastValue = value;
|
26 | this.lastOmitSuffix = omitSuffix;
|
27 | this.lastLocale = this.getLocale(value);
|
28 | this.removeTimer();
|
29 | this.createTimer();
|
30 | this.lastText = momentConstructor(value).from(momentConstructor(), omitSuffix);
|
31 | }
|
32 | else {
|
33 | this.createTimer();
|
34 | }
|
35 | return this.lastText;
|
36 | };
|
37 | TimeAgoPipe.prototype.ngOnDestroy = function () {
|
38 | this.removeTimer();
|
39 | };
|
40 | TimeAgoPipe.prototype.createTimer = function () {
|
41 | var _this = this;
|
42 | if (this.currentTimer) {
|
43 | return;
|
44 | }
|
45 | var momentInstance = momentConstructor(this.lastValue);
|
46 | var timeToUpdate = this.getSecondsUntilUpdate(momentInstance) * 1000;
|
47 | this.currentTimer = this.ngZone.runOutsideAngular(function () {
|
48 | if (typeof window !== 'undefined') {
|
49 | return window.setTimeout(function () {
|
50 | _this.lastText = momentConstructor(_this.lastValue).from(momentConstructor(), _this.lastOmitSuffix);
|
51 | _this.currentTimer = null;
|
52 | _this.ngZone.run(function () { return _this.cdRef.markForCheck(); });
|
53 | }, timeToUpdate);
|
54 | }
|
55 | });
|
56 | };
|
57 | TimeAgoPipe.prototype.removeTimer = function () {
|
58 | if (this.currentTimer) {
|
59 | window.clearTimeout(this.currentTimer);
|
60 | this.currentTimer = null;
|
61 | }
|
62 | };
|
63 | TimeAgoPipe.prototype.getSecondsUntilUpdate = function (momentInstance) {
|
64 | var howOld = Math.abs(momentConstructor().diff(momentInstance, 'minute'));
|
65 | if (howOld < 1) {
|
66 | return 1;
|
67 | }
|
68 | else if (howOld < 60) {
|
69 | return 30;
|
70 | }
|
71 | else if (howOld < 180) {
|
72 | return 300;
|
73 | }
|
74 | else {
|
75 | return 3600;
|
76 | }
|
77 | };
|
78 | TimeAgoPipe.prototype.hasChanged = function (value, omitSuffix) {
|
79 | return this.getTime(value) !== this.lastTime
|
80 | || this.getLocale(value) !== this.lastLocale
|
81 | || omitSuffix !== this.lastOmitSuffix;
|
82 | };
|
83 | TimeAgoPipe.prototype.getTime = function (value) {
|
84 | if (moment.isDate(value)) {
|
85 | return value.getTime();
|
86 | }
|
87 | else if (moment.isMoment(value)) {
|
88 | return value.valueOf();
|
89 | }
|
90 | else {
|
91 | return momentConstructor(value).valueOf();
|
92 | }
|
93 | };
|
94 | TimeAgoPipe.prototype.getLocale = function (value) {
|
95 | return moment.isMoment(value) ? value.locale() : null;
|
96 | };
|
97 | TimeAgoPipe = __decorate([
|
98 | core_1.Pipe({ name: 'amTimeAgo', pure: false }),
|
99 | __metadata("design:paramtypes", [core_1.ChangeDetectorRef, core_1.NgZone])
|
100 | ], TimeAgoPipe);
|
101 | return TimeAgoPipe;
|
102 | }());
|
103 | exports.TimeAgoPipe = TimeAgoPipe;
|
104 |
|
\ | No newline at end of file |