LP1825851 Server managed/processed print templates
[working/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     constructor(
35         private locale: LocaleService,
36         private auth: AuthService,
37         private store: StoreService
38     ) {
39         this.onPrintRequest$ = new EventEmitter<PrintRequest>();
40     }
41
42     print(printReq: PrintRequest) {
43         this.onPrintRequest$.emit(printReq);
44     }
45
46     reprintLast() {
47         const prev = this.store.getLocalItem('eg.print.last_printed');
48
49         if (prev) {
50             const req: PrintRequest = {
51                 text: prev.content,
52                 printContext: prev.context || 'default',
53                 contentType: prev.content_type || 'text/html',
54                 showDialog: Boolean(prev.show_dialog)
55             };
56
57             this.print(req);
58         }
59     }
60
61     compileRemoteTemplate(printReq: PrintRequest): Promise<PrintTemplateResponse> {
62
63         const formData: FormData = new FormData();
64
65         formData.append('ses', this.auth.token());
66         if (printReq.templateName) {
67             formData.append('template_name', printReq.templateName);
68         }
69         if (printReq.templateId) {
70             formData.append('template_id', '' + printReq.templateId);
71         }
72         if (printReq.templateOwner) {
73             formData.append('template_owner', '' + printReq.templateOwner);
74         }
75         formData.append('template_data', js2JSON(printReq.contextData));
76         formData.append('template_locale', this.locale.currentLocaleCode());
77
78         // Sometimes we want to know the time zone of the browser/user,
79         // regardless of any org unit settings.
80         if (OpenSRF.tz) {
81             formData.append('client_timezone', OpenSRF.tz);
82         }
83
84         return new Promise((resolve, reject) => {
85             const xhttp = new XMLHttpRequest();
86             xhttp.onreadystatechange = function() {
87                 if (this.readyState === 4) {
88                     if (this.status === 200) {
89                         resolve({
90                             content: xhttp.responseText,
91                             contentType: this.getResponseHeader('content-type')
92                         });
93                     } else {
94                         reject('Error compiling print template');
95                     }
96                 }
97             };
98             xhttp.open('POST', PRINT_TEMPLATE_PATH, true);
99             xhttp.send(formData);
100         });
101
102     }
103 }
104