833fa0010d9fee07b2486a66095fe9f688236ad1
[working/Evergreen.git] /
1 import {Directive, Host, Input, OnInit, ViewChildren, QueryList, AfterViewInit} from '@angular/core';
2 import {MarcSimplifiedEditorComponent} from './simplified-editor.component';
3 import {MarcField, MarcSubfield} from '../marcrecord';
4 import {MarcSimplifiedEditorSubfieldDirective} from './simplified-editor-subfield.directive';
5
6 /**
7  * A field that a user can edit, which will later be
8  * compiled into MARC
9  */
10
11 @Directive({
12   selector: 'eg-marc-simplified-editor-field',
13 })
14 export class MarcSimplifiedEditorFieldDirective implements OnInit, AfterViewInit {
15
16   @Input() tag = 'a';
17   @Input() ind1 = ' ';
18   @Input() ind2 = ' ';
19
20   @ViewChildren(MarcSimplifiedEditorSubfieldDirective)
21     subfields: QueryList<MarcSimplifiedEditorSubfieldDirective>;
22
23   marcVersion: MarcField;
24
25   constructor(@Host() private editor: MarcSimplifiedEditorComponent) {}
26
27   ngOnInit() {
28     this.marcVersion = {
29       tag: this.tag,
30       subfields: [],
31       authValid: false,
32       authChecked: false,
33       isCtrlField: false,
34       isControlfield: () => false,
35       indicator: (ind: number) => (ind === 1) ? this.ind1 : this.ind2,
36       deleteExactSubfields: (...subfield: MarcSubfield[]) => 0, // not used by the simplified editor
37     };
38   }
39
40   ngAfterViewInit() {
41     this.subfields.forEach((subfield: MarcSimplifiedEditorSubfieldDirective, index: number) => {
42       this.marcVersion.subfields.push(
43         [
44           subfield.code,
45           subfield.defaultValue ? subfield.defaultValue : '',
46           index
47         ]
48       );
49     });
50     this.editor.addField(this.marcVersion);
51   }
52
53 }
54
55
56