forked from onecx/onecx-portal-ui-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/generator-harnesses
- Loading branch information
Showing
20 changed files
with
168 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,88 @@ | ||
import { Pipe } from '@angular/core' | ||
import { TimeagoPipe } from 'ngx-timeago' | ||
import { ChangeDetectorRef, NgZone, OnDestroy, Pipe, PipeTransform } from '@angular/core' | ||
import { TranslatePipe, TranslateService } from '@ngx-translate/core' | ||
|
||
@Pipe({ | ||
name: 'timeago', | ||
}) | ||
// eslint-disable-next-line @angular-eslint/use-pipe-transform-interface | ||
export class OcxTimeAgoPipe extends TimeagoPipe {} | ||
export class OcxTimeAgoPipe extends TranslatePipe implements OnDestroy, PipeTransform { | ||
private timer: number | undefined | null | ||
constructor( | ||
private changeDetectorRef: ChangeDetectorRef, | ||
private ngZone: NgZone, | ||
private translateService: TranslateService | ||
) { | ||
super(translateService, changeDetectorRef) | ||
} | ||
override transform(value: string) { | ||
this.removeTimer() | ||
const d = new Date(value) | ||
const now = new Date() | ||
const seconds = Math.round(Math.abs((now.getTime() - d.getTime()) / 1000)) | ||
const timeToUpdate = Number.isNaN(seconds) ? 1000 : this.getSecondsUntilUpdate(seconds) * 1000 | ||
this.timer = this.ngZone.runOutsideAngular(() => { | ||
if (typeof window !== 'undefined') { | ||
return window.setTimeout(() => { | ||
this.ngZone.run(() => this.changeDetectorRef.markForCheck()) | ||
}, timeToUpdate) | ||
} | ||
return null | ||
}) | ||
const minutes = Math.round(Math.abs(seconds / 60)) | ||
const hours = Math.round(Math.abs(minutes / 60)) | ||
const days = Math.round(Math.abs(hours / 24)) | ||
const months = Math.round(Math.abs(days / 30.416)) | ||
const years = Math.round(Math.abs(days / 365)) | ||
let translationKey = 'UNKNOWN' | ||
if (Number.isNaN(seconds)) { | ||
translationKey = 'NAN' | ||
} else if (seconds <= 45) { | ||
translationKey = 'A_FEW_SECONDS_AGO' | ||
} else if (seconds <= 90) { | ||
translationKey = 'A_MINUTE_AGO' | ||
} else if (minutes <= 45) { | ||
translationKey = 'MINUTES_AGO' | ||
} else if (minutes <= 90) { | ||
translationKey = 'AN_HOUR_AGO' | ||
} else if (hours <= 22) { | ||
translationKey = 'HOURS_AGO' | ||
} else if (hours <= 36) { | ||
translationKey = 'A_DAY_AGO' | ||
} else if (days <= 25) { | ||
translationKey = 'DAYS_AGO' | ||
} else if (days <= 45) { | ||
translationKey = 'A_MONTH_AGO' | ||
} else if (days <= 345) { | ||
translationKey = 'MONTHS_AGO' | ||
} else if (days <= 545) { | ||
translationKey = 'A_YEAR_AGO' | ||
} else { | ||
translationKey = 'YEARS_AGO' | ||
} | ||
return super.transform('OCX_TIMEAGO.' + translationKey, { minutes, hours, days, months, years }) | ||
} | ||
override ngOnDestroy(): void { | ||
this.removeTimer() | ||
super.ngOnDestroy() | ||
} | ||
private removeTimer() { | ||
if (this.timer) { | ||
window.clearTimeout(this.timer) | ||
this.timer = null | ||
} | ||
} | ||
private getSecondsUntilUpdate(seconds: number) { | ||
const min = 60 | ||
const hr = min * 60 | ||
const day = hr * 24 | ||
if (seconds < min) { | ||
return 2 | ||
} else if (seconds < hr) { | ||
return 30 | ||
} else if (seconds < day) { | ||
return 300 | ||
} else { | ||
return 3600 | ||
} | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
libs/angular-accelerator/src/lib/utils/ocxtimeagointl.utils.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.