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zD,WAAW,CA6FvB;IAAD,kBAAC;CAAA,AA7FD,IA6FC;AA7FY,kCAAW","sourcesContent":["/* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */\r\n\r\nimport {Pipe, ChangeDetectorRef, PipeTransform, OnDestroy, NgZone} from '@angular/core';\r\nimport * as moment from 'moment';\r\n\r\n// under systemjs, moment is actually exported as the default export, so we account for that\r\nconst momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment;\r\n\r\n@Pipe({name: 'amTimeAgo', pure: false})\r\nexport class TimeAgoPipe implements PipeTransform, OnDestroy {\r\n private currentTimer: number;\r\n\r\n private lastTime: Number;\r\n private lastValue: Date | moment.Moment;\r\n private lastOmitSuffix: boolean;\r\n private lastLocale?: string;\r\n private lastText: string;\r\n\r\n constructor(private cdRef: ChangeDetectorRef, private ngZone: NgZone) {\r\n }\r\n\r\n transform(value: Date | moment.Moment, omitSuffix?: boolean): string {\r\n if (this.hasChanged(value, omitSuffix)) {\r\n this.lastTime = this.getTime(value);\r\n this.lastValue = value;\r\n this.lastOmitSuffix = omitSuffix;\r\n this.lastLocale = this.getLocale(value);\r\n this.removeTimer();\r\n this.createTimer();\r\n this.lastText = momentConstructor(value).from(momentConstructor(), omitSuffix);\r\n\r\n } else {\r\n this.createTimer();\r\n }\r\n\r\n return this.lastText;\r\n }\r\n\r\n ngOnDestroy(): void {\r\n this.removeTimer();\r\n }\r\n\r\n\r\n private createTimer() {\r\n if (this.currentTimer) {\r\n return;\r\n }\r\n const momentInstance = momentConstructor(this.lastValue);\r\n\r\n const timeToUpdate = this.getSecondsUntilUpdate(momentInstance) * 1000;\r\n this.currentTimer = this.ngZone.runOutsideAngular(() => {\r\n if (typeof window !== 'undefined') {\r\n return window.setTimeout(() => {\r\n this.lastText = momentConstructor(this.lastValue).from(momentConstructor(), this.lastOmitSuffix);\r\n\r\n this.currentTimer = null;\r\n this.ngZone.run(() => this.cdRef.markForCheck());\r\n }, timeToUpdate);\r\n }\r\n });\r\n }\r\n\r\n\r\n private removeTimer() {\r\n if (this.currentTimer) {\r\n window.clearTimeout(this.currentTimer);\r\n this.currentTimer = null;\r\n }\r\n }\r\n\r\n private getSecondsUntilUpdate(momentInstance: moment.Moment) {\r\n const howOld = Math.abs(momentConstructor().diff(momentInstance, 'minute'));\r\n if (howOld < 1) {\r\n return 1;\r\n } else if (howOld < 60) {\r\n return 30;\r\n } else if (howOld < 180) {\r\n return 300;\r\n } else {\r\n return 3600;\r\n }\r\n }\r\n\r\n private hasChanged(value: Date | moment.Moment, omitSuffix?: boolean) {\r\n return this.getTime(value) !== this.lastTime\r\n || this.getLocale(value) !== this.lastLocale\r\n || omitSuffix !== this.lastOmitSuffix;\r\n }\r\n\r\n private getTime(value: Date | moment.Moment) {\r\n if (moment.isDate(value)) {\r\n return value.getTime();\r\n } else if (moment.isMoment(value)) {\r\n return value.valueOf();\r\n } else {\r\n return momentConstructor(value).valueOf();\r\n }\r\n }\r\n\r\n private getLocale(value: Date | moment.Moment): string {\r\n return moment.isMoment(value) ? value.locale() : null;\r\n }\r\n}\r\n\ninterface DecoratorInvocation {\n type: Function;\n args?: any[];\n}\n"]}
\No newline at end of file