]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/catalog/marc-html.component.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / catalog / marc-html.component.ts
1 import {Component, OnInit, Input, ElementRef} from '@angular/core';
2 import {NetService} from '@eg/core/net.service';
3 import {OrgService} from '@eg/core/org.service';
4 import {AuthService} from '@eg/core/auth.service';
5
6 @Component({
7   selector: 'eg-marc-html',
8   // view is generated from MARC HTML
9   template: '<ng-template></ng-template>'
10 })
11 export class MarcHtmlComponent implements OnInit {
12
13     recId: number;
14     initDone = false;
15
16     @Input() set recordId(id: number) {
17         this.recId = id;
18         // Only force new data collection when recordId()
19         // is invoked after ngInit() has already run.
20         if (this.initDone) {
21             this.collectData();
22         }
23     }
24
25     get recordId(): number {
26         return this.recId;
27     }
28
29     private _recordXml: string;
30     @Input() set recordXml(xml: string) {
31         this._recordXml = xml;
32          if (this.initDone) {
33             this.collectData();
34         }
35     }
36
37     get recordXml(): string {
38         return this._recordXml;
39     }
40
41     private _recordType: string;
42     @Input() set recordType(rtype: string) {
43         this._recordType = rtype;
44     }
45
46     get recordType(): string {
47         return this._recordType;
48     }
49
50     constructor(
51         private elm: ElementRef,
52         private net: NetService,
53         private auth: AuthService
54     ) {}
55
56     ngOnInit() {
57         this.collectData().then(_ => this.initDone = true);
58     }
59
60     collectData(): Promise<any> {
61         if (!this.recordId && !this.recordXml) { return Promise.resolve(); }
62
63         let service = 'open-ils.search';
64         let method = 'open-ils.search.biblio.record.html';
65         let params: any[] = [this.recordId];
66
67         switch (this.recordType) {
68
69             case 'authority':
70                 method = 'open-ils.search.authority.to_html';
71                 break;
72
73             case 'vandelay-authority':
74                 params.unshift(this.auth.token());
75                 service = 'open-ils.vandelay';
76                 method = 'open-ils.vandelay.queued_authority_record.html';
77                 break;
78
79             case 'vandelay-bib':
80                 params.unshift(this.auth.token());
81                 service = 'open-ils.vandelay';
82                 method = 'open-ils.vandelay.queued_bib_record.html';
83                 break;
84         }
85
86         // Bib/auth variants support generating HTML directly from MARC XML
87         if (!this.recordId && (
88             this.recordType === 'bib' || this.recordType === 'authority')) {
89             params = [null, null, this.recordXml];
90         }
91
92         return this.net.requestWithParamList(service, method, params)
93         .toPromise().then(html => this.injectHtml(html));
94     }
95
96     injectHtml(html: string) {
97
98         // Remove embedded labels and actions.
99         html = html.replace(
100             /<button onclick="window.print(.*?)<\/button>/, '');
101
102         html = html.replace(/<title>(.*?)<\/title>/, '');
103
104         // remove reference to nonexistant CSS file
105         html = html.replace(/<link(.*?)\/>/, '');
106
107         // there shouldn't be any, but while we're at it,
108         // kill any embedded script tags
109         html = html.replace(/<script(.*?)<\/script>/, '');
110
111         this.elm.nativeElement.innerHTML = html;
112     }
113 }
114
115