]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/authority-linking-dialog.component.ts
LP1852782 MARC editor authority linking support
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / authority-linking-dialog.component.ts
1 import {Component, Input, Output, OnInit, EventEmitter} from '@angular/core';
2 import {NetService} from '@eg/core/net.service';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
6 import {MarcField} from './marcrecord';
7 import {Pager} from '@eg/share/util/pager';
8
9 /**
10  * MARC Authority Linking Dialog
11  */
12
13 @Component({
14   selector: 'eg-authority-linking-dialog',
15   templateUrl: './authority-linking-dialog.component.html'
16 })
17
18 export class AuthorityLinkingDialogComponent
19     extends DialogComponent implements OnInit {
20
21     @Input() bibField: MarcField;
22     @Input() thesauri: string = null;
23     @Input() controlSet: number = null;
24     @Input() pager: Pager;
25
26     browseData: any[] = [];
27
28     // If false, show the raw MARC field data.
29     showAs: 'heading' | 'marc' = 'heading';
30
31     authMeta: any;
32
33     selectedSubfields: string[] = [];
34
35     constructor(
36         private modal: NgbModal,
37         private pcrud: PcrudService,
38         private net: NetService) {
39         super(modal);
40     }
41
42     ngOnInit() {
43         if (!this.pager) {
44             this.pager = new Pager();
45             this.pager.limit = 5;
46         }
47
48         this.onOpen$.subscribe(_ => this.initData());
49     }
50
51     fieldHash(field?: MarcField): any {
52         if (!field) { field = this.bibField; }
53
54         return {
55             tag: field.tag,
56             ind1: field.ind1,
57             ind2: field.ind2,
58             subfields: field.subfields.map(sf => [sf[0], sf[1]])
59         };
60     }
61
62     initData() {
63
64        this.pager.offset = 0;
65
66        this.pcrud.search('acsbf',
67             {tag: this.bibField.tag},
68             {flesh: 1, flesh_fields: {acsbf: ['authority_field']}},
69             {atomic:  true, anonymous: true}
70
71         ).subscribe(bibMetas => {
72             if (bibMetas.length === 0) { return; }
73
74             let bibMeta;
75             if (this.controlSet) {
76                 bibMeta = bibMetas.filter(b =>
77                     this.controlSet === +b.authority_field().control_set());
78             } else {
79                 bibMeta = bibMetas[0];
80             }
81
82             if (bibMeta) {
83                 this.authMeta = bibMeta.authority_field();
84                 this.bibField.subfields.forEach(sf =>
85                     this.selectedSubfields[sf[0]] =
86                         this.isControlledBibSf(sf[0])
87                 );
88             }
89
90             this.getPage(0);
91         });
92     }
93
94     getPage(direction: number) {
95         this.browseData = [];
96
97         if (direction > 0) {
98             this.pager.offset++;
99         } else if (direction < 0) {
100             this.pager.offset--;
101         } else {
102             this.pager.offset = 0;
103         }
104
105         const hash = this.fieldHash();
106
107         // Only search the selected subfields
108         hash.subfields =
109             hash.subfields.filter(sf => this.selectedSubfields[sf[0]]);
110
111         if (hash.subfields.length === 0) { return; }
112
113         this.net.request(
114             'open-ils.cat',
115             'open-ils.cat.authority.bib_field.linking_browse',
116             hash, this.pager.limit,
117             this.pager.offset, this.thesauri
118         ).subscribe(entry => this.browseData.push(entry));
119     }
120
121     applyHeading(authField: MarcField) {
122         this.net.request(
123             'open-ils.cat',
124             'open-ils.cat.authority.bib_field.overlay_authority',
125             this.fieldHash(), this.fieldHash(authField), this.controlSet
126         ).subscribe(field => this.close(field));
127     }
128
129     isControlledBibSf(sf: string): boolean {
130         return this.authMeta ?
131             this.authMeta.sf_list().includes(sf) : false;
132     }
133 }
134