From: Bill Erickson Date: Wed, 20 Jun 2018 19:55:08 +0000 (-0400) Subject: LP#1775466 AngJS style minimal template parser continued X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=30f37070b6b5cb9f760a7edc16d77d2e94f5bf26;p=working%2FEvergreen.git LP#1775466 AngJS style minimal template parser continued Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/app/share/util/parser.service.ts b/Open-ILS/src/eg2/src/app/share/util/parser.service.ts index fc7a0dccae..cbe3830b1d 100644 --- a/Open-ILS/src/eg2/src/app/share/util/parser.service.ts +++ b/Open-ILS/src/eg2/src/app/share/util/parser.service.ts @@ -1,6 +1,8 @@ /** * AngularJS-style minimal template parser. * Original use case is supporting AngularJS style print templates. + * Template context data is applied only once at parsing time, there + * is no Angular-style data binding. * * Supports the following template constructs: * {{variables}} @@ -12,12 +14,11 @@ import {Injectable, EventEmitter} from '@angular/core'; import {FormatService} from '@eg/share/util/format.service'; +// Internal class for modeling a single template parsing instance. class ParserInstance { private context: any; - private domNode: HTMLElement; private format: FormatService; - private evalEnv: string; // FormatService injected by ParserService constructor(format: FormatService) { @@ -36,11 +37,11 @@ class ParserInstance { const doc = parser.parseFromString(html, "text/html"); // Parsing as html wraps the content in an wrapper - this.domNode = doc.getElementsByTagName('body')[0]; + const domNode = doc.getElementsByTagName('body')[0]; - this.traverse(this.domNode); + this.traverse(domNode); - return this.domNode.innerHTML; + return domNode.innerHTML; } // Process each node in the in-progress document. @@ -236,9 +237,27 @@ class ParserInstance { // Find the value within the context at the given path. getContextStringValue(dotpath: string): string { - const value = this.getContextValueAt(dotpath); - // TODO IDL stuff when possible for formatting (e.g. dates) - return this.format.transform({value: value}); + + // Variable replacements may contain filters. + const pieces = dotpath.split('|'); + const path = pieces[0].trim(); + const filter = pieces[1]; + const data = { + value: this.getContextValueAt(path) + }; + + // Apply some minimal filter handling for now + // TODO: teach the format service about processing due dates. + if (filter) { + filter = filter.trim(); + if (filter.startsWith('date')) { + data.datatype = 'timestamp'; + } else if (filter.startsWith('currency')) { + data.datatype = 'money'; + } + } + + return this.format.transform(data); } } diff --git a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts index e2e7d30b73..46dcf9d359 100644 --- a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts @@ -140,7 +140,8 @@ export class SandboxComponent implements OnInit {
{{dest_address.street1}}
{{dest_address.street2}}
-
Slip Date: {{today}}
+
Slip Date: {{today | date:foo}}
+
Cost: {{cost | currency}}
  1. Barcode: {{copy.barcode}}
    @@ -173,6 +174,7 @@ export class SandboxComponent implements OnInit { boolTrue: true, boolFalse: false, today: new Date(), + cost: 23.3, copies: [ {barcode: 'abc123', title: 'welcome to the jungle', parts: ['a', 'b', 'c']}, {barcode: 'def456', title: 'hello mudda, hello fadda', parts: ['x','y']}