2020-11-06 22:30:52 +01:00
|
|
|
import { isPlatformBrowser } from '@angular/common';
|
|
|
|
import { Component, OnInit, OnDestroy, ChangeDetectionStrategy, Input, ChangeDetectorRef, OnChanges, PLATFORM_ID, Inject } from '@angular/core';
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-time-since',
|
2020-02-29 17:42:50 +01:00
|
|
|
template: `{{ text }}`,
|
2020-02-16 16:15:07 +01:00
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
})
|
2020-03-25 12:41:16 +01:00
|
|
|
export class TimeSinceComponent implements OnInit, OnChanges, OnDestroy {
|
2020-11-06 22:30:52 +01:00
|
|
|
isBrowser: boolean = isPlatformBrowser(this.platformId);
|
2020-02-16 16:15:07 +01:00
|
|
|
interval: number;
|
2020-02-29 17:42:50 +01:00
|
|
|
text: string;
|
2020-07-20 07:21:30 +02:00
|
|
|
intervals = {};
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
@Input() time: number;
|
2020-02-27 19:09:07 +01:00
|
|
|
@Input() fastRender = false;
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
constructor(
|
2020-11-06 22:30:52 +01:00
|
|
|
private ref: ChangeDetectorRef,
|
|
|
|
@Inject(PLATFORM_ID) private platformId: any,
|
2020-07-20 07:21:30 +02:00
|
|
|
) {
|
|
|
|
if (document.body.clientWidth < 768) {
|
|
|
|
this.intervals = {
|
|
|
|
year: 31536000,
|
|
|
|
month: 2592000,
|
|
|
|
week: 604800,
|
|
|
|
day: 86400,
|
|
|
|
hour: 3600,
|
|
|
|
min: 60,
|
|
|
|
sec: 1
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.intervals = {
|
|
|
|
year: 31536000,
|
|
|
|
month: 2592000,
|
|
|
|
week: 604800,
|
|
|
|
day: 86400,
|
|
|
|
hour: 3600,
|
|
|
|
minute: 60,
|
|
|
|
second: 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-11-06 22:30:52 +01:00
|
|
|
if (!this.isBrowser) {
|
|
|
|
this.text = this.calculate();
|
|
|
|
this.ref.markForCheck();
|
|
|
|
return;
|
|
|
|
}
|
2020-02-16 16:15:07 +01:00
|
|
|
this.interval = window.setInterval(() => {
|
2020-02-29 17:42:50 +01:00
|
|
|
this.text = this.calculate();
|
2020-02-16 16:15:07 +01:00
|
|
|
this.ref.markForCheck();
|
2020-02-27 19:09:07 +01:00
|
|
|
}, 1000 * (this.fastRender ? 1 : 60));
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 12:41:16 +01:00
|
|
|
ngOnChanges() {
|
|
|
|
this.text = this.calculate();
|
|
|
|
this.ref.markForCheck();
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
ngOnDestroy() {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
|
2020-02-29 17:42:50 +01:00
|
|
|
calculate() {
|
|
|
|
const seconds = Math.floor((+new Date() - +new Date(this.time * 1000)) / 1000);
|
|
|
|
if (seconds < 60) {
|
2020-02-29 21:32:12 +01:00
|
|
|
return '< 1 minute';
|
2020-02-29 17:42:50 +01:00
|
|
|
}
|
|
|
|
let counter;
|
2020-07-20 07:21:30 +02:00
|
|
|
for (const i in this.intervals) {
|
|
|
|
if (this.intervals.hasOwnProperty(i)) {
|
|
|
|
counter = Math.floor(seconds / this.intervals[i]);
|
2020-02-29 17:42:50 +01:00
|
|
|
if (counter > 0) {
|
|
|
|
if (counter === 1) {
|
|
|
|
return counter + ' ' + i; // singular (1 day ago)
|
|
|
|
} else {
|
|
|
|
return counter + ' ' + i + 's'; // plural (2 days ago)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|