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';
7 selector: 'eg-marc-html',
8 // view is generated from MARC HTML
9 template: '<ng-template></ng-template>'
11 export class MarcHtmlComponent implements OnInit {
16 @Input() set recordId(id: number) {
18 // Only force new data collection when recordId()
19 // is invoked after ngInit() has already run.
25 get recordId(): number {
29 private _recordXml: string;
30 @Input() set recordXml(xml: string) {
31 this._recordXml = xml;
37 get recordXml(): string {
38 return this._recordXml;
41 private _recordType: string;
42 @Input() set recordType(rtype: string) {
43 this._recordType = rtype;
46 get recordType(): string {
47 return this._recordType;
51 private elm: ElementRef,
52 private net: NetService,
53 private auth: AuthService
57 this.collectData().then(_ => this.initDone = true);
60 collectData(): Promise<any> {
61 if (!this.recordId && !this.recordXml) { return Promise.resolve(); }
63 let service = 'open-ils.search';
64 let method = 'open-ils.search.biblio.record.html';
65 let params: any[] = [this.recordId];
67 switch (this.recordType) {
70 method = 'open-ils.search.authority.to_html';
73 case 'vandelay-authority':
74 params.unshift(this.auth.token());
75 service = 'open-ils.vandelay';
76 method = 'open-ils.vandelay.queued_authority_record.html';
80 params.unshift(this.auth.token());
81 service = 'open-ils.vandelay';
82 method = 'open-ils.vandelay.queued_bib_record.html';
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];
92 return this.net.requestWithParamList(service, method, params)
93 .toPromise().then(html => this.injectHtml(html));
96 injectHtml(html: string) {
98 // Remove embedded labels and actions.
100 /<button onclick="window.print(.*?)<\/button>/, '');
102 html = html.replace(/<title>(.*?)<\/title>/, '');
104 // remove reference to nonexistant CSS file
105 html = html.replace(/<link(.*?)\/>/, '');
107 // there shouldn't be any, but while we're at it,
108 // kill any embedded script tags
109 html = html.replace(/<script(.*?)<\/script>/, '');
111 this.elm.nativeElement.innerHTML = html;