]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/print/print.service.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / print / print.service.ts
1 import {Injectable, EventEmitter, TemplateRef} from '@angular/core';
2 import {tap} from 'rxjs/operators';
3 import {StoreService} from '@eg/core/store.service';
4 import {LocaleService} from '@eg/core/locale.service';
5 import {AuthService} from '@eg/core/auth.service';
6
7 declare var js2JSON: (jsThing: any) => string;
8 declare var OpenSRF;
9
10 const PRINT_TEMPLATE_PATH = '/print_template';
11
12 export interface PrintRequest {
13     template?: TemplateRef<any>;
14     templateName?: string;
15     templateOwner?: number; // org unit ID, follows ancestors
16     templateId?: number; // useful for testing templates
17     contextData?: any;
18     text?: string;
19     printContext: string;
20     contentType?: string; // defaults to text/html
21     showDialog?: boolean;
22 }
23
24 export interface PrintTemplateResponse {
25     contentType: string;
26     content: string;
27 }
28
29 @Injectable()
30 export class PrintService {
31
32     onPrintRequest$: EventEmitter<PrintRequest>;
33
34     // Emitted after a print request has been delivered to Hatch or
35     // window.print() has completed.  Note window.print() returning
36     // is not necessarily an indication the job has completed.
37     printJobQueued$: EventEmitter<PrintRequest>;
38
39     constructor(
40         private locale: LocaleService,
41         private auth: AuthService,
42         private store: StoreService
43     ) {
44         this.onPrintRequest$ = new EventEmitter<PrintRequest>();
45         this.printJobQueued$ = new EventEmitter<PrintRequest>();
46     }
47
48     print(printReq: PrintRequest) {
49         this.onPrintRequest$.emit(printReq);
50     }
51
52     reprintLast() {
53         const prev = this.store.getLocalItem('eg.print.last_printed');
54
55         if (prev) {
56             const req: PrintRequest = {
57                 text: prev.content,
58                 printContext: prev.context || 'default',
59                 contentType: prev.content_type || 'text/html',
60                 showDialog: Boolean(prev.show_dialog)
61             };
62
63             this.print(req);
64         }
65     }
66
67     compileRemoteTemplate(printReq: PrintRequest): Promise<PrintTemplateResponse> {
68
69         const formData: FormData = new FormData();
70
71         formData.append('ses', this.auth.token());
72         if (printReq.templateName) {
73             formData.append('template_name', printReq.templateName);
74         }
75         if (printReq.templateId) {
76             formData.append('template_id', '' + printReq.templateId);
77         }
78         if (printReq.templateOwner) {
79             formData.append('template_owner', '' + printReq.templateOwner);
80         }
81         formData.append('template_data', js2JSON(printReq.contextData));
82         formData.append('template_locale', this.locale.currentLocaleCode());
83
84         // Sometimes we want to know the time zone of the browser/user,
85         // regardless of any org unit settings.
86         if (OpenSRF.tz) {
87             formData.append('client_timezone', OpenSRF.tz);
88         }
89
90         return new Promise((resolve, reject) => {
91             const xhttp = new XMLHttpRequest();
92             xhttp.onreadystatechange = function() {
93                 if (this.readyState === 4) {
94                     if (this.status === 200) {
95                         resolve({
96                             content: xhttp.responseText,
97                             contentType: this.getResponseHeader('content-type')
98                         });
99                     } else if (this.status === 404) {
100                         console.error('No active template found: ', printReq);
101                         reject({notFound: true});
102                     }
103                     reject({});
104                 }
105             };
106             xhttp.open('POST', PRINT_TEMPLATE_PATH, true);
107             xhttp.send(formData);
108         });
109
110     }
111 }
112