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';
10 const DEFAULT_RECORD_TYPE = 'BKS';
13 * A simplified editor for basic MARC records, which
14 * does not require knowledge of MARC tags
18 selector: 'eg-marc-simplified-editor',
19 templateUrl: './simplified-editor.component.html'
21 export class MarcSimplifiedEditorComponent implements AfterViewInit, OnInit {
23 @Input() buttonLabel: string;
24 @Output() xmlRecordEvent = new EventEmitter<string>();
25 @Input() defaultMarcForm: string;
27 fields: MarcField[] = [];
29 marcForms: ComboboxEntry[];
30 marcTypes: ComboboxEntry[];
32 // DOM id prefix to prevent id collisions.
38 addField: (field: MarcField) => void;
40 editorFieldIdentifier: (field: MarcField, subfield: Array<any>) => string;
43 private net: NetService,
44 private tagTable: TagTableService
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()
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, []));
65 this.editorFieldIdentifier = (field: MarcField, subfield: Array<any>) => {
66 return field.tag + subfield[0]; // e.g. 245a
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]}
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]}
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]);
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;
109 record.fields = this.fields;
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);
115 record.setFixedField('Type', this.appropriateMarcType);
116 record.setFixedField('Form', this.appropriateMarcForm);
117 this.xmlRecordEvent.emit(record.toXml());
120 get appropriateMarcType(): string {
121 return this.editor.get('marcType').value ? this.editor.get('marcType').value.id : 'a';
124 get appropriateMarcForm(): string {
125 if (this.editor.get('marcForm').value) {
126 return this.editor.get('marcForm').value.id;
128 return this.defaultMarcForm ? this.defaultMarcForm : ' ';