LP1852782 Prevents data fields swapping to control fields
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / rich-editor.component.ts
1 import {Component, Input, Output, OnInit, AfterViewInit, EventEmitter,
2     OnDestroy} from '@angular/core';
3 import {filter} from 'rxjs/operators';
4 import {IdlService} from '@eg/core/idl.service';
5 import {OrgService} from '@eg/core/org.service';
6 import {ServerStoreService} from '@eg/core/server-store.service';
7 import {TagTableService} from './tagtable.service';
8 import {MarcRecord, MarcField} from './marcrecord';
9 import {MarcEditContext} from './editor-context';
10
11
12 /**
13  * MARC Record rich editor interface.
14  */
15
16 @Component({
17   selector: 'eg-marc-rich-editor',
18   templateUrl: './rich-editor.component.html',
19   styleUrls: ['rich-editor.component.css']
20 })
21
22 export class MarcRichEditorComponent implements OnInit {
23
24     @Input() context: MarcEditContext;
25     get record(): MarcRecord { return this.context.record; }
26
27     dataLoaded: boolean;
28     showHelp: boolean;
29     randId = Math.floor(Math.random() * 100000);
30     stackSubfields: boolean;
31
32     constructor(
33         private idl: IdlService,
34         private org: OrgService,
35         private store: ServerStoreService,
36         private tagTable: TagTableService
37     ) {}
38
39     ngOnInit() {
40
41         this.store.getItem('cat.marcedit.stack_subfields')
42         .then(stack => this.stackSubfields = stack);
43
44         this.init().then(_ =>
45             this.context.recordChange.subscribe(__ => this.init()));
46
47         // Changing the Type fixed field means loading new meta-metadata.
48         this.record.fixedFieldChange.pipe(filter(code => code === 'Type'))
49         .subscribe(_ => this.init());
50     }
51
52     init(): Promise<any> {
53         this.dataLoaded = false;
54
55         if (!this.record) { return Promise.resolve(); }
56
57         return Promise.all([
58             this.tagTable.loadTagTable({marcRecordType: this.context.recordType}),
59             this.tagTable.getFfPosTable(this.record.recordType()),
60             this.tagTable.getFfValueTable(this.record.recordType())
61         ]).then(_ =>
62             // setTimeout forces all of our sub-components to rerender
63             // themselves each time init() is called.  Without this,
64             // changing the record Type would only re-render the fixed
65             // fields editor when data had to be fetched from the
66             // network.  (Sometimes the data is cached).
67             setTimeout(() => this.dataLoaded = true)
68         );
69     }
70
71     stackSubfieldsChange() {
72         if (this.stackSubfields) {
73             this.store.setItem('cat.marcedit.stack_subfields', true);
74         } else {
75             this.store.removeItem('cat.marcedit.stack_subfields');
76         }
77     }
78
79     undoCount(): number {
80         return this.context.undoStack.length;
81     }
82
83     redoCount(): number {
84         return this.context.redoStack.length;
85     }
86
87     undo() {
88         this.context.requestUndo();
89     }
90
91     redo() {
92         this.context.requestRedo();
93     }
94
95     controlFields(): MarcField[] {
96         return this.record.fields.filter(f => f.isCtrlField);
97     }
98
99     dataFields(): MarcField[] {
100         return this.record.fields.filter(f => !f.isCtrlField);
101     }
102 }
103
104
105