From: Bill Erickson Date: Mon, 1 Apr 2019 16:45:25 +0000 (-0400) Subject: LP1822414 Angular format service formatValue pipe X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=d8a2068a48f9e1046fc2c574d7c8c345390c5d47;p=working%2FEvergreen.git LP1822414 Angular format service formatValue pipe Create a pipe version of FormatService.transform() function so strings may be generated from fields directly in templates. Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/app/common.module.ts b/Open-ILS/src/eg2/src/app/common.module.ts index 9361042074..ec06a91b80 100644 --- a/Open-ILS/src/eg2/src/app/common.module.ts +++ b/Open-ILS/src/eg2/src/app/common.module.ts @@ -13,7 +13,7 @@ They do not have to be added to the providers list. */ // consider moving these to core... -import {FormatService} from '@eg/core/format.service'; +import {FormatService, FormatValuePipe} from '@eg/core/format.service'; import {PrintService} from '@eg/share/print/print.service'; // Globally available components @@ -33,7 +33,8 @@ import {ProgressDialogComponent} from '@eg/share/dialog/progress.component'; ConfirmDialogComponent, PromptDialogComponent, ProgressInlineComponent, - ProgressDialogComponent + ProgressDialogComponent, + FormatValuePipe ], imports: [ CommonModule, @@ -52,7 +53,8 @@ import {ProgressDialogComponent} from '@eg/share/dialog/progress.component'; ConfirmDialogComponent, PromptDialogComponent, ProgressInlineComponent, - ProgressDialogComponent + ProgressDialogComponent, + FormatValuePipe ] }) diff --git a/Open-ILS/src/eg2/src/app/core/format.service.ts b/Open-ILS/src/eg2/src/app/core/format.service.ts index e788cd0e40..d2e8225a4c 100644 --- a/Open-ILS/src/eg2/src/app/core/format.service.ts +++ b/Open-ILS/src/eg2/src/app/core/format.service.ts @@ -1,4 +1,4 @@ -import {Injectable} from '@angular/core'; +import {Injectable, Pipe, PipeTransform} from '@angular/core'; import {DatePipe, CurrencyPipe} from '@angular/common'; import {IdlService, IdlObject} from '@eg/core/idl.service'; import {OrgService} from '@eg/core/org.service'; @@ -108,6 +108,10 @@ export class FormatService { case 'timestamp': const date = new Date(value); + if (Number.isNaN(date.getTime())) { + console.error('Invalid date in format service', value); + return ''; + } let fmt = this.dateFormat || 'shortDate'; if (params.datePlusTime) { fmt = this.dateTimeFormat || 'short'; @@ -131,3 +135,14 @@ export class FormatService { } } + +// Pipe-ify the above formating logic for use in templates +@Pipe({name: 'formatValue'}) +export class FormatValuePipe implements PipeTransform { + constructor(private formatter: FormatService) {} + // Add other filter params as needed to fill in the FormatParams + transform(value: string, datatype: string): string { + return this.formatter.transform({value: value, datatype: datatype}); + } +} +