LP1822414 Angular format service formatValue pipe
authorBill Erickson <berickxx@gmail.com>
Mon, 1 Apr 2019 16:45:25 +0000 (12:45 -0400)
committerBill Erickson <berickxx@gmail.com>
Mon, 1 Apr 2019 16:45:39 +0000 (12:45 -0400)
Create a pipe version of FormatService.transform() function so strings
may be generated from fields directly in templates.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/common.module.ts
Open-ILS/src/eg2/src/app/core/format.service.ts

index 9361042..ec06a91 100644 (file)
@@ -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
   ]
 })
 
index e788cd0..d2e8225 100644 (file)
@@ -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});
+    }
+}
+