1 import {Component, ViewChild, Input, Output, OnInit, EventEmitter} 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 import {PcrudService} from '@eg/core/pcrud.service';
6 import {DialogComponent} from '@eg/share/dialog/dialog.component';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {MarcField} from './marcrecord';
9 import {MarcEditContext} from './editor-context';
10 import {Pager} from '@eg/share/util/pager';
11 import {MarcEditorDialogComponent} from './editor-dialog.component';
14 * MARC Authority Linking Dialog
18 selector: 'eg-authority-linking-dialog',
19 templateUrl: './authority-linking-dialog.component.html'
22 export class AuthorityLinkingDialogComponent
23 extends DialogComponent implements OnInit {
25 @Input() bibField: MarcField;
26 @Input() thesauri: string = null;
27 @Input() controlSet: number = null;
28 @Input() pager: Pager;
29 @Input() context: MarcEditContext;
31 browseData: any[] = [];
33 // If false, show the raw MARC field data.
34 showAs: 'heading' | 'marc' = 'heading';
38 selectedSubfields: string[] = [];
40 cni: string; // Control Number Identifier
42 @ViewChild('marcEditDialog', {static: false})
43 marcEditDialog: MarcEditorDialogComponent;
46 private modal: NgbModal,
47 private auth: AuthService,
48 private org: OrgService,
49 private pcrud: PcrudService,
50 private net: NetService) {
56 this.pager = new Pager();
60 this.onOpen$.subscribe(_ => this.initData());
63 fieldHash(field?: MarcField): any {
64 if (!field) { field = this.bibField; }
70 subfields: field.subfields.map(sf => [sf[0], sf[1]])
76 this.pager.offset = 0;
78 this.org.settings(['cat.marc_control_number_identifier']).then(s => {
79 this.cni = s['cat.marc_control_number_identifier'] ||
80 'Set cat.marc_control_number_identifier in Library Settings';
83 this.pcrud.search('acsbf',
84 {tag: this.bibField.tag},
85 {flesh: 1, flesh_fields: {acsbf: ['authority_field']}},
86 {atomic: true, anonymous: true}
88 ).subscribe(bibMetas => {
89 if (bibMetas.length === 0) { return; }
92 if (this.controlSet) {
93 bibMeta = bibMetas.filter(b =>
94 this.controlSet === +b.authority_field().control_set());
96 bibMeta = bibMetas[0];
100 this.authMeta = bibMeta.authority_field();
101 this.bibField.subfields.forEach(sf =>
102 this.selectedSubfields[sf[0]] =
103 this.isControlledBibSf(sf[0])
111 getPage(direction: number) {
112 this.browseData = [];
116 } else if (direction < 0) {
119 this.pager.offset = 0;
122 const hash = this.fieldHash();
124 // Only search the selected subfields
126 hash.subfields.filter(sf => this.selectedSubfields[sf[0]]);
128 if (hash.subfields.length === 0) { return; }
132 'open-ils.cat.authority.bib_field.linking_browse',
133 hash, this.pager.limit,
134 this.pager.offset, this.thesauri
135 ).subscribe(entry => this.browseData.push(entry));
138 applyHeading(authField: MarcField) {
141 'open-ils.cat.authority.bib_field.overlay_authority',
142 this.fieldHash(), this.fieldHash(authField), this.controlSet
143 ).subscribe(field => this.close(field));
146 isControlledBibSf(sf: string): boolean {
147 return this.authMeta ?
148 this.authMeta.sf_list().includes(sf) : false;
151 setSubfieldZero(authId: number) {
152 const sfZero = this.bibField.subfields.filter(sf => sf[0] === '0')[0];
154 this.context.deleteSubfield(this.bibField, sfZero);
156 this.context.insertSubfield(this.bibField,
157 ['0', `(${this.cni})${authId}`, this.bibField.subfields.length]);
159 // Reset the validation state.
160 this.bibField.authChecked = null;
161 this.bibField.authValid = null;
164 createNewAuthority(editFirst?: boolean) {
166 const method = editFirst ?
167 'open-ils.cat.authority.record.create_from_bib.readonly' :
168 'open-ils.cat.authority.record.create_from_bib';
171 'open-ils.cat', method,
172 this.fieldHash(), this.cni, this.auth.token()
173 ).subscribe(record => {
175 this.marcEditDialog.recordXml = record;
176 this.marcEditDialog.open({size: 'xl'})
177 .subscribe(saveEvent => {
178 if (saveEvent && saveEvent.recordId) {
179 this.setSubfieldZero(saveEvent.recordId);
184 this.setSubfieldZero(record.id());