From 8e1e39dfa2581508b4bbf0a8bb2e4da2e48a06fc Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 28 Oct 2019 13:53:46 -0400 Subject: [PATCH] LP1830391 Angular Hatch enabled flag lookup repair 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 Signed-off-by: Jason Boyer --- .../src/app/share/print/print.component.html | 7 +- .../src/app/share/print/print.component.ts | 68 ++++++++++++------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/share/print/print.component.html b/Open-ILS/src/eg2/src/app/share/print/print.component.html index 823b5d8c18..9fb4debd4c 100644 --- a/Open-ILS/src/eg2/src/app/share/print/print.component.html +++ b/Open-ILS/src/eg2/src/app/share/print/print.component.html @@ -14,13 +14,14 @@ Error generating print content for template name="{{name}}" / id="{{id}}"
+ -
-
- + + +
diff --git a/Open-ILS/src/eg2/src/app/share/print/print.component.ts b/Open-ILS/src/eg2/src/app/share/print/print.component.ts index 708ebf3fd6..6af06bd8f9 100644 --- a/Open-ILS/src/eg2/src/app/share/print/print.component.ts +++ b/Open-ILS/src/eg2/src/app/share/print/print.component.ts @@ -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 { + 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 = `
${printReq.text}
`; - } + // 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 = `
${printReq.text}
`; + } + + this.htmlContainer.innerHTML = printReq.text; + } + }); }); } @@ -149,12 +167,17 @@ export class PrintComponent implements OnInit { } } - dispatchPrint(printReq: PrintRequest) { + dispatchPrint(printReq: PrintRequest): Promise { 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) { -- 2.43.2