]> 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: Users can attach brief bib records and e-resources to courses
[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, ValidationErrors, ValidatorFn, FormArray} from '@angular/forms';
3 import {MarcField, MarcRecord} from '../marcrecord';
4 import {TagTableService} from '../tagtable.service';
5
6 /**
7  * A simplified editor for basic MARC records, which
8  * does not require knowledge of MARC tags
9  */
10
11 @Component({
12   selector: 'eg-marc-simplified-editor',
13   templateUrl: './simplified-editor.component.html'
14 })
15 export class MarcSimplifiedEditorComponent implements AfterViewInit, OnInit {
16
17     @Input() buttonLabel: string;
18     @Output() xmlRecordEvent = new EventEmitter<string>();
19
20     fields: MarcField[] = [];
21     editor: FormGroup;
22
23     // DOM id prefix to prevent id collisions.
24     idPrefix: string;
25
26     fieldIndex = 0;
27     fieldLabels: string[] = [];
28
29     addField: (field: MarcField) => void;
30
31     constructor(
32         private tagTable: TagTableService
33     ) {}
34
35     ngOnInit() {
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({});
39
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, []));
45             this.fieldIndex++;
46         };
47
48     }
49
50     ngAfterViewInit() {
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]);
54             });
55         });
56     }
57
58     emitXml() {
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;
64             }
65         });
66         record.fields = this.fields;
67         this.xmlRecordEvent.emit(record.toXml());
68     }
69
70 }
71
72