1 import {AfterViewInit, Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2 import {FormGroup, FormControl, ValidationErrors, ValidatorFn, FormArray} from '@angular/forms';
3 import {MarcField, MarcRecord} from '../marcrecord';
4 import {TagTableService} from '../tagtable.service';
7 * A simplified editor for basic MARC records, which
8 * does not require knowledge of MARC tags
12 selector: 'eg-marc-simplified-editor',
13 templateUrl: './simplified-editor.component.html'
15 export class MarcSimplifiedEditorComponent implements AfterViewInit, OnInit {
17 @Input() buttonLabel: string;
18 @Output() xmlRecordEvent = new EventEmitter<string>();
20 fields: MarcField[] = [];
23 // DOM id prefix to prevent id collisions.
27 fieldLabels: string[] = [];
29 addField: (field: MarcField) => void;
32 private tagTable: TagTableService
36 // Add some randomness to the generated DOM IDs to ensure against clobbering
37 this.idPrefix = 'marc-simplified-editor-' + Math.floor(Math.random() * 100000);
38 this.editor = new FormGroup({});
40 // Add a fieldId, and then add a new field to the array
41 this.addField = (field: MarcField) => {
42 field.fieldId = this.fieldIndex;
43 this.fields.push(field);
44 this.editor.addControl(String(this.fieldIndex), new FormControl(null, []));
51 this.tagTable.loadTags({marcRecordType: 'biblio', ffType: 'BKS'}).then(table => {
52 this.fields.forEach((field) => {
53 this.fieldLabels[field.fieldId] = table.getSubfieldLabel(field.tag, field.subfields[0][0]);
59 const record = new MarcRecord('<record xmlns="http://www.loc.gov/MARC21/slim"></record>');
60 // need to add the value to field.subfields[0][1]
61 this.fields.forEach((field) => {
62 if (field.subfields[0][1] === '') { // Default value has not been applied
63 field.subfields[0][1] = this.editor.get(String(field.fieldId)).value;
66 record.fields = this.fields;
67 this.xmlRecordEvent.emit(record.toXml());