1 import {Component, OnInit, TemplateRef, ElementRef, Renderer2} from '@angular/core';
2 import {PrintService, PrintRequest} from './print.service';
3 import {StoreService} from '@eg/core/store.service';
4 import {ServerStoreService} from '@eg/core/server-store.service';
5 import {HatchService, HatchMessage} from './hatch.service';
9 templateUrl: './print.component.html'
12 export class PrintComponent implements OnInit {
14 // Template that requires local processing
15 template: TemplateRef<any>;
17 // Context data used for processing the template.
20 // Insertion point for externally-compiled templates
21 htmlContainer: Element;
25 printQueue: PrintRequest[];
28 private renderer: Renderer2,
29 private elm: ElementRef,
30 private store: StoreService,
31 private serverStore: ServerStoreService,
32 private hatch: HatchService,
33 private printer: PrintService) {
34 this.isPrinting = false;
39 this.printer.onPrintRequest$.subscribe(
40 printReq => this.handlePrintRequest(printReq));
43 this.renderer.selectRootElement('#eg-print-html-container');
46 handlePrintRequest(printReq: PrintRequest) {
48 if (this.isPrinting) {
49 // Avoid print collisions by queuing requests as needed.
50 this.printQueue.push(printReq);
54 this.isPrinting = true;
56 this.applyTemplate(printReq);
58 // Give templates a chance to render before printing
60 this.dispatchPrint(printReq);
65 applyTemplate(printReq: PrintRequest) {
67 if (printReq.template) {
68 // Inline template. Let Angular do the interpolationwork.
69 this.template = printReq.template;
70 this.context = {$implicit: printReq.contextData};
74 if (printReq.text && !this.useHatch()) {
75 // Insert HTML into the browser DOM for in-browser printing only.
77 if (printReq.contentType === 'text/plain') {
78 // Wrap text/plain content in pre's to prevent
79 // unintended html formatting.
80 printReq.text = `<pre>${printReq.text}</pre>`;
83 this.htmlContainer.innerHTML = printReq.text;
87 // Clear the print data
89 this.isPrinting = false;
92 this.htmlContainer.innerHTML = '';
94 if (this.printQueue.length) {
95 this.handlePrintRequest(this.printQueue.pop());
99 dispatchPrint(printReq: PrintRequest) {
101 if (!printReq.text) {
102 // Sometimes the results come from an externally-parsed HTML
103 // template, other times they come from an in-page template.
104 printReq.text = this.elm.nativeElement.innerHTML;
107 // Retain a copy of each printed document in localStorage
108 // so it may be reprinted.
109 this.store.setLocalItem('eg.print.last_printed', {
110 content: printReq.text,
111 context: printReq.printContext,
112 content_type: printReq.contentType,
113 show_dialog: printReq.showDialog
116 if (this.useHatch()) {
117 this.printViaHatch(printReq);
119 // Here the needed HTML is already in the page.
124 useHatch(): boolean {
125 return this.store.getLocalItem('eg.hatch.enable.printing')
126 && this.hatch.connect();
129 printViaHatch(printReq: PrintRequest) {
131 // Send a full HTML document to Hatch
132 const html = `<html><body>${printReq.text}</body></html>`;
134 this.serverStore.getItem(`eg.print.config.${printReq.printContext}`)
137 const msg = new HatchMessage({
140 settings: config || {},
141 contentType: 'text/html',
142 showDialog: printReq.showDialog
145 this.hatch.sendRequest(msg).then(
146 ok => console.debug('Print request succeeded'),
147 err => console.warn('Print request failed', err)