LP1830391 Angular Hatch enabled flag lookup repair
authorBill Erickson <berickxx@gmail.com>
Mon, 28 Oct 2019 17:53:46 +0000 (13:53 -0400)
committerJason Boyer <JBoyer@eoli.info>
Fri, 13 Dec 2019 14:36:29 +0000 (09:36 -0500)
Fix the Angular Hatch enabled setting lookup to pull the value from the
workstation setting instead of localStorage.  This required shuffling a
few things around to support the asynchronicity.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jason Boyer <JBoyer@eoli.info>
Open-ILS/src/eg2/src/app/share/print/print.component.html
Open-ILS/src/eg2/src/app/share/print/print.component.ts

index 823b5d8..9fb4deb 100644 (file)
@@ -14,13 +14,14 @@ Error generating print content for template name="{{name}}" / id="{{id}}"
 <eg-string key='eg.print.template.error' [template]="notWorking"></eg-string>
 
 <div id='eg-print-container'>
+
   <!-- container for inline template compilation -->
   <ng-container *ngIf="template">
     <ng-container *ngTemplateOutlet="template; context:context">
     </ng-container>
   </ng-container>
-  <div id='eg-print-html-container'>
-  </div>
-<!-- container for pre-compiled HTML -->
+
+  <!-- container for pre-compiled HTML -->
+  <div id='eg-print-html-container'> </div>
 </div>
 
index 708ebf3..6af06bd 100644 (file)
@@ -28,6 +28,9 @@ export class PrintComponent implements OnInit {
 
     printQueue: PrintRequest[];
 
+    // True if Hatch printing is enabled and we're able to talk to Hatch.
+    useHatchPrinting: boolean = null;
+
     constructor(
         private renderer: Renderer2,
         private elm: ElementRef,
@@ -50,6 +53,19 @@ export class PrintComponent implements OnInit {
             this.renderer.selectRootElement('#eg-print-html-container');
     }
 
+
+    // Returns promise of true if Hatch should be used for printing.
+    // To avoid race conditions, always check this inline before
+    // relaying print requests.
+    checkHatchEnabled(): Promise<boolean> {
+        if (this.useHatchPrinting !== null) {
+            return Promise.resolve(this.useHatchPrinting);
+        }
+
+        return this.serverStore.getItem('eg.hatch.enable.printing')
+            .then(use => this.useHatchPrinting = (use && this.hatch.connect()));
+    }
+
     handlePrintRequest(printReq: PrintRequest) {
 
         if (this.isPrinting) {
@@ -63,8 +79,7 @@ export class PrintComponent implements OnInit {
         this.applyTemplate(printReq).then(() => {
             // Give templates a chance to render before printing
             setTimeout(() => {
-                this.dispatchPrint(printReq);
-                this.reset();
+                this.dispatchPrint(printReq).then(_ => this.reset());
             });
         });
     }
@@ -123,17 +138,20 @@ export class PrintComponent implements OnInit {
 
         return promise.then(() => {
 
-            // Insert HTML into the browser DOM for in-browser printing.
-            if (printReq.text && !this.useHatch()) {
+            return this.checkHatchEnabled().then(enabled => {
 
-                if (printReq.contentType === 'text/plain') {
-                // Wrap text/plain content in pre's to prevent
-                // unintended html formatting.
-                    printReq.text = `<pre>${printReq.text}</pre>`;
-                }
+                // Insert HTML into the browser DOM for in-browser printing.
+                if (printReq.text && !enabled) {
 
-                this.htmlContainer.innerHTML = printReq.text;
-            }
+                    if (printReq.contentType === 'text/plain') {
+                    // Wrap text/plain content in pre's to prevent
+                    // unintended html formatting.
+                        printReq.text = `<pre>${printReq.text}</pre>`;
+                    }
+
+                    this.htmlContainer.innerHTML = printReq.text;
+                }
+            });
         });
     }
 
@@ -149,12 +167,17 @@ export class PrintComponent implements OnInit {
         }
     }
 
-    dispatchPrint(printReq: PrintRequest) {
+    dispatchPrint(printReq: PrintRequest): Promise<any> {
 
         if (!printReq.text) {
+
+            // Extract the print container div from our component markup.
+            const container =
+                this.elm.nativeElement.querySelector('#eg-print-container');
+
             // Sometimes the results come from an externally-parsed HTML
             // template, other times they come from an in-page template.
-            printReq.text = this.elm.nativeElement.innerHTML;
+            printReq.text = container.innerHTML;
         }
 
         // Retain a copy of each printed document in localStorage
@@ -166,17 +189,14 @@ export class PrintComponent implements OnInit {
             show_dialog: printReq.showDialog
         });
 
-        if (this.useHatch()) {
-            this.printViaHatch(printReq);
-        } else {
-            // Here the needed HTML is already in the page.
-            window.print();
-        }
-    }
-
-    useHatch(): boolean {
-        return this.store.getLocalItem('eg.hatch.enable.printing')
-            && this.hatch.connect();
+        return this.checkHatchEnabled().then(enabled => {
+            if (enabled) {
+                this.printViaHatch(printReq);
+            } else {
+                // Here the needed HTML is already in the page.
+                window.print();
+            }
+        });
     }
 
     printViaHatch(printReq: PrintRequest) {