server templates: ang and angjs cont. user/berick/server-print-templates
authorBill Erickson <berickxx@gmail.com>
Fri, 19 Apr 2019 21:06:23 +0000 (17:06 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 19 Apr 2019 21:06:23 +0000 (17:06 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/print/print.component.ts
Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.ts
Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.module.ts
Open-ILS/web/js/ui/default/staff/admin/workstation/app.js

index 4f69949..de27bb8 100644 (file)
@@ -49,35 +49,59 @@ export class PrintComponent implements OnInit {
 
         this.isPrinting = true;
 
-        this.applyTemplate(printReq);
-
-        // Give templates a chance to render before printing
-        setTimeout(() => {
-            this.dispatchPrint(printReq);
-            this.reset();
+        this.applyTemplate(printReq).then(() => {
+            // Give templates a chance to render before printing
+            setTimeout(() => {
+                this.dispatchPrint(printReq);
+                this.reset();
+            });
         });
     }
 
-    applyTemplate(printReq: PrintRequest) {
+    applyTemplate(printReq: PrintRequest): Promise<any> {
 
         if (printReq.template) {
-            // Inline template.  Let Angular do the interpolationwork.
+            // Local Angular template.
             this.template = printReq.template;
             this.context = {$implicit: printReq.contextData};
-            return;
+            return Promise.resolve();
+        } 
+
+        let promise;
+
+        // Precompiled text
+        if (printReq.text) {
+            promise = Promise.resolve();
+
+        } else if (printReq.templateName || printReq.templateId) {
+            
+            promise = this.printer.compileRemoteTemplate(printReq).then(
+                response => {
+                    printReq.text = response.content;
+                    printReq.contentType = response.contentType;
+                },
+                err => {
+                    console.error("Error compiling template", printReq);
+                    return Promise.reject(new Error(
+                        'Error compiling server-hosted print template'));
+                }
+            );
+
+        } else {
+            console.error("Cannot find template", printReq);
+            return Promise.reject(new Error("Cannot find print template"));
         }
 
-        if (printReq.text && true /* !this.hatch.isActive */) {
-            // Insert HTML into the browser DOM for in-browser printing only.
+        return promise.then(() => {
 
-            if (printReq.contentType === 'text/plain') {
-                // Wrap text/plain content in pre's to prevent
-                // unintended html formatting.
+            if (printReq.contentType === 'text/plain' && true /* this.hatch.isActive */) {
+                // When adding text output to DOM for rendering, wrap in 
+                // pre to avoid unintended HTML formatting.
                 printReq.text = `<pre>${printReq.text}</pre>`;
             }
 
             this.htmlContainer.innerHTML = printReq.text;
-        }
+        });
     }
 
     // Clear the print data
@@ -120,7 +144,10 @@ export class PrintComponent implements OnInit {
     printViaHatch(printReq: PrintRequest) {
 
         // Send a full HTML document to Hatch
-        const html = `<html><body>${printReq.text}</body></html>`;
+        let html = printReq.text;
+        if (printReq.contentType === 'text/html') {
+            html = `<html><body>${printReq.text}</body></html>`;
+        }
 
         /*
         this.hatch.print({
index 507bdbd..19c9bf0 100644 (file)
@@ -85,7 +85,6 @@ export class PrintTemplateComponent implements OnInit {
         }
     }
 
-
     container(): any {
         // Only present when its tab is visible
         return document.getElementById('template-preview-pane');
index 8eb7f92..abb0075 100644 (file)
@@ -15,6 +15,7 @@ import {PrintService} from '@eg/share/print/print.service';
 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
 import {FormatService} from '@eg/core/format.service';
 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
+import {SampleDataService} from '@eg/share/util/sample-data.service';
 
 @Component({
   templateUrl: 'sandbox.component.html'
@@ -73,7 +74,8 @@ export class SandboxComponent implements OnInit {
         private strings: StringService,
         private toast: ToastService,
         private format: FormatService,
-        private printer: PrintService
+        private printer: PrintService,
+        private samples: SampleDataService
     ) {
     }
 
@@ -227,40 +229,17 @@ export class SandboxComponent implements OnInit {
     testServerPrint() {
 
         // Note these values can be IDL objects or plain hashes.
-        const patron = this.idl.create('au');
-        const address = this.idl.create('aua');
-        patron.first_given_name('Crosby');
-        patron.second_given_name('Stills');
-        patron.family_name('Nash');
-        address.street1('123 Pineapple Road');
-        address.street2('Apt #4');
-        address.city('Bahama');
-        address.state('NC');
-        address.post_code('555444');
-
         const templateData = {
-            patron: patron,
-            address: address
+            patron:  this.samples.listOfThings('au')[0],
+            address: this.samples.listOfThings('aua')[0]
         }
 
         // NOTE: eventually this will be baked into the print service.
-        this.printer.compileRemoteTemplate({
-            templateName: 'address-label',
+        this.printer.print({
+            templateName: 'patron_address',
             contextData: templateData,
             printContext: 'default'
-        }).then(
-            response => {
-                console.log(response.contentType);
-                console.log(response.content);
-                this.printer.print({
-                    printContext: 'default',
-                    contentType: response.contentType,
-                    text: response.content,
-                    showDialog: true
-                });
-            }
-        );
+        });
     }
 }
 
-
index 58910dd..d1ca609 100644 (file)
@@ -2,6 +2,7 @@ import {NgModule} from '@angular/core';
 import {StaffCommonModule} from '@eg/staff/common.module';
 import {SandboxRoutingModule} from './routing.module';
 import {SandboxComponent} from './sandbox.component';
+import {SampleDataService} from '@eg/share/util/sample-data.service';
 
 @NgModule({
   declarations: [
@@ -12,6 +13,7 @@ import {SandboxComponent} from './sandbox.component';
     SandboxRoutingModule,
   ],
   providers: [
+    SampleDataService
   ]
 })
 
index d863844..0364d2b 100644 (file)
@@ -616,7 +616,7 @@ function($scope , $q , egCore , ngToast) {
 
     $scope.template_changed = function() {
         $scope.print.load_failed = false;
-        egCore.print.getPrintTemplate($scope.print.template_name)
+        egCore.print.getPrintTemplate({template: $scope.print.template_name})
         .then(
             function(html) { 
                 $scope.print.template_content = html;