Docs: merge 3.2 release notes
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / print / print.component.ts
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
5 @Component({
6     selector: 'eg-print',
7     templateUrl: './print.component.html'
8 })
9
10 export class PrintComponent implements OnInit {
11
12     // Template that requires local processing
13     template: TemplateRef<any>;
14
15     // Context data used for processing the template.
16     context: any;
17
18     // Insertion point for externally-compiled templates
19     htmlContainer: Element;
20
21     isPrinting: boolean;
22
23     printQueue: PrintRequest[];
24
25     constructor(
26         private renderer: Renderer2,
27         private elm: ElementRef,
28         private store: StoreService,
29         private printer: PrintService) {
30         this.isPrinting = false;
31         this.printQueue = [];
32     }
33
34     ngOnInit() {
35         this.printer.onPrintRequest$.subscribe(
36             printReq => this.handlePrintRequest(printReq));
37
38         this.htmlContainer =
39             this.renderer.selectRootElement('#eg-print-html-container');
40     }
41
42     handlePrintRequest(printReq: PrintRequest) {
43
44         if (this.isPrinting) {
45             // Avoid print collisions by queuing requests as needed.
46             this.printQueue.push(printReq);
47             return;
48         }
49
50         this.isPrinting = true;
51
52         this.applyTemplate(printReq);
53
54         // Give templates a chance to render before printing
55         setTimeout(() => {
56             this.dispatchPrint(printReq);
57             this.reset();
58         });
59     }
60
61     applyTemplate(printReq: PrintRequest) {
62
63         if (printReq.template) {
64             // Inline template.  Let Angular do the interpolationwork.
65             this.template = printReq.template;
66             this.context = {$implicit: printReq.contextData};
67             return;
68         }
69
70         if (printReq.text && true /* !this.hatch.isActive */) {
71             // Insert HTML into the browser DOM for in-browser printing only.
72
73             if (printReq.contentType === 'text/plain') {
74                 // Wrap text/plain content in pre's to prevent
75                 // unintended html formatting.
76                 printReq.text = `<pre>${printReq.text}</pre>`;
77             }
78
79             this.htmlContainer.innerHTML = printReq.text;
80         }
81     }
82
83     // Clear the print data
84     reset() {
85         this.isPrinting = false;
86         this.template = null;
87         this.context = null;
88         this.htmlContainer.innerHTML = '';
89
90         if (this.printQueue.length) {
91             this.handlePrintRequest(this.printQueue.pop());
92         }
93     }
94
95     dispatchPrint(printReq: PrintRequest) {
96
97         if (!printReq.text) {
98             // Sometimes the results come from an externally-parsed HTML
99             // template, other times they come from an in-page template.
100             printReq.text = this.elm.nativeElement.innerHTML;
101         }
102
103         // Retain a copy of each printed document in localStorage
104         // so it may be reprinted.
105         this.store.setLocalItem('eg.print.last_printed', {
106             content: printReq.text,
107             context: printReq.printContext,
108             content_type: printReq.contentType,
109             show_dialog: printReq.showDialog
110         });
111
112         if (0 /* this.hatch.isActive */) {
113             this.printViaHatch(printReq);
114         } else {
115             // Here the needed HTML is already in the page.
116             window.print();
117         }
118     }
119
120     printViaHatch(printReq: PrintRequest) {
121
122         // Send a full HTML document to Hatch
123         const html = `<html><body>${printReq.text}</body></html>`;
124
125         /*
126         this.hatch.print({
127             printContext: printReq.printContext,
128             content: html
129         });
130         */
131     }
132 }
133