Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add L10nTimeAgo pipe & directive
- Loading branch information
Showing
13 changed files
with
399 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Directive, ElementRef, Input, Renderer2 } from '@angular/core'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
|
||
import { LocaleService } from '../services/locale.service'; | ||
import { BaseDirective } from '../models/base-directive'; | ||
import { RelativeTimeOptions, Unit } from '../models/types'; | ||
|
||
@Directive({ | ||
selector: '[l10nTimeAgo]' | ||
}) | ||
export class L10nTimeAgoDirective extends BaseDirective { | ||
|
||
@Input() set l10nTimeAgo(format: RelativeTimeOptions) { | ||
this.format = format; | ||
} | ||
|
||
@Input() public unit: Unit; | ||
|
||
@Input() public format: RelativeTimeOptions; | ||
|
||
constructor(protected locale: LocaleService, protected el: ElementRef, protected renderer: Renderer2) { | ||
super(el, renderer); | ||
} | ||
|
||
protected setup(): void { | ||
this.replace(); | ||
this.locale.defaultLocaleChanged.pipe(takeUntil(this.destroy)).subscribe( | ||
() => { this.replace(); } | ||
); | ||
} | ||
|
||
protected replace(): void { | ||
this.replaceText(); | ||
this.replaceAttributes(); | ||
} | ||
|
||
protected replaceText(): void { | ||
if (!!this.key) { | ||
this.setText(this.getValue(this.key)); | ||
} | ||
} | ||
|
||
protected replaceAttributes(): void { | ||
if (this.attributes.length > 0) { | ||
this.setAttributes(this.getAttributesData()); | ||
} | ||
} | ||
|
||
protected getValue(key: string): string { | ||
return this.locale.formatRelativeTime(key, this.unit, this.format); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NgModule } from '@angular/core'; | ||
|
||
import { L10nTimeAgoPipe } from '../pipes/l10n-time-ago.pipe'; | ||
import { L10nTimeAgoDirective } from '../directives/l10n-time-ago.directive'; | ||
|
||
/** | ||
* Provides extra pipes & directives. | ||
*/ | ||
@NgModule({ | ||
declarations: [ | ||
L10nTimeAgoPipe, | ||
L10nTimeAgoDirective | ||
], | ||
exports: [ | ||
L10nTimeAgoPipe, | ||
L10nTimeAgoDirective | ||
] | ||
}) | ||
export class LocalizationExtraModule { } |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
import { LocaleService } from '../services/locale.service'; | ||
import { RelativeTimeOptions, Unit } from '../models/types'; | ||
import { Logger } from '../models/logger'; | ||
|
||
@Pipe({ | ||
name: 'l10nTimeAgo', | ||
pure: true | ||
}) | ||
export class L10nTimeAgoPipe implements PipeTransform { | ||
|
||
constructor(protected locale: LocaleService) { } | ||
|
||
public transform( | ||
value: any, | ||
defaultLocale: string, | ||
unit: Unit, | ||
format?: RelativeTimeOptions | ||
): string | null { | ||
if (value == null || value === "" || value !== value) return null; | ||
if (typeof defaultLocale === "undefined") Logger.log('L10nTimeAgoPipe', 'missingDefaultLocale'); | ||
|
||
return this.locale.formatRelativeTime(value, unit, format, defaultLocale); | ||
} | ||
|
||
} |
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.