]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/simplified-editor/simplified-editor.component.ts
LP1849212: Improvements to the Simplified Marc Editor
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / simplified-editor / simplified-editor.component.ts
1 import {AfterViewInit, Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2 import {FormGroup, FormControl} from '@angular/forms';
3 import {MarcField, MarcRecord} from '../marcrecord';
4 import {TagTableService} from '../tagtable.service';
5 import {NetService} from '@eg/core/net.service';
6 import { ComboboxEntry } from '@eg/share/combobox/combobox.component';
7 import { Observable, of } from 'rxjs';
8 import { switchMap } from 'rxjs/operators';
9
10 const DEFAULT_RECORD_TYPE = 'BKS';
11
12 /**
13  * A simplified editor for basic MARC records, which
14  * does not require knowledge of MARC tags
15  */
16
17 @Component({
18   selector: 'eg-marc-simplified-editor',
19   templateUrl: './simplified-editor.component.html'
20 })
21 export class MarcSimplifiedEditorComponent implements AfterViewInit, OnInit {
22
23     @Input() buttonLabel: string;
24     @Output() xmlRecordEvent = new EventEmitter<string>();
25     @Input() defaultMarcForm: string;
26
27     fields: MarcField[] = [];
28     editor: FormGroup;
29     marcForms: ComboboxEntry[];
30     marcTypes: ComboboxEntry[];
31
32     // DOM id prefix to prevent id collisions.
33     idPrefix: string;
34
35     fieldIndex = 0;
36     subfieldLabels = {};
37
38     addField: (field: MarcField) => void;
39
40     editorFieldIdentifier: (field: MarcField, subfield: Array<any>) => string;
41
42     constructor(
43         private net: NetService,
44         private tagTable: TagTableService
45     ) {}
46
47     ngOnInit() {
48         // Add some randomness to the generated DOM IDs to ensure against clobbering
49         this.idPrefix = 'marc-simplified-editor-' + Math.floor(Math.random() * 100000);
50         this.editor = new FormGroup({
51             marcForm: new FormControl(),
52             marcType: new FormControl()
53         });
54
55         // Add a fieldId, and then add a new field to the array
56         this.addField = (field: MarcField) => {
57             field.fieldId = this.fieldIndex;
58             this.fields.push(field);
59             field.subfields.forEach((subfield) => {
60                 this.editor.addControl(this.editorFieldIdentifier(field, subfield), new FormControl(null, []));               
61             })
62             this.fieldIndex++;
63         };
64
65         this.editorFieldIdentifier = (field: MarcField, subfield: Array<any>) => {
66             return field.tag + subfield[0]; // e.g. 245a
67         }
68
69         this.net.request('open-ils.cat',
70             'open-ils.cat.biblio.fixed_field_values.by_rec_type',
71             DEFAULT_RECORD_TYPE, 'Form')
72             .subscribe((forms) => {
73                 this.marcForms = forms['Form'].map((form) => {
74                     return {id: form[0], label: form[1]}
75                 })
76             });
77
78         this.net.request('open-ils.cat',
79             'open-ils.cat.biblio.fixed_field_values.by_rec_type',
80             DEFAULT_RECORD_TYPE, 'Type')
81             .subscribe((types) => {
82                 this.marcTypes = types['Type'].map((type) => {
83                     return {id: type[0], label: type[1]}
84                 })
85             });
86
87     }
88
89     ngAfterViewInit() {
90         this.tagTable.loadTags({marcRecordType: 'biblio', ffType: DEFAULT_RECORD_TYPE}).then(table => {
91             this.fields.forEach((field) => {
92                 field.subfields.forEach((subfield) => {
93                     this.subfieldLabels[this.editorFieldIdentifier(field, subfield)] = table.getSubfieldLabel(field.tag, subfield[0]);
94                 })
95             });
96         });
97     }
98
99     emitXml() {
100         const record = new MarcRecord('<record xmlns="http://www.loc.gov/MARC21/slim"></record>');
101         // need to add the value to field.subfields[0][1]
102         this.fields.forEach((field) => {
103             field.subfields.forEach((subfield) => {
104                 if (subfield[1] === '') { // Default value has not been applied
105                     subfield[1] = this.editor.get(this.editorFieldIdentifier(field, subfield)).value;
106                 }  
107             })
108         });
109         record.fields = this.fields;
110
111         // We need to generate an accurate 008 before setting the Form fixed field
112         const field008 = record.newField({tag: '008', data: record.generate008()});
113         record.insertOrderedFields(field008);
114
115         record.setFixedField('Type', this.appropriateMarcType);
116         record.setFixedField('Form', this.appropriateMarcForm);
117         this.xmlRecordEvent.emit(record.toXml());
118     }
119
120     get appropriateMarcType(): string {
121         return this.editor.get('marcType').value ? this.editor.get('marcType').value.id : 'a';
122     }
123
124     get appropriateMarcForm(): string {
125         if (this.editor.get('marcForm').value) {
126             return this.editor.get('marcForm').value.id;
127         }
128         return this.defaultMarcForm ? this.defaultMarcForm : ' ';
129     }
130
131
132 }
133
134