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';
13 * MARC Record rich editor interface.
17 selector: 'eg-marc-rich-editor',
18 templateUrl: './rich-editor.component.html',
19 styleUrls: ['rich-editor.component.css']
22 export class MarcRichEditorComponent implements OnInit {
24 @Input() context: MarcEditContext;
25 get record(): MarcRecord { return this.context.record; }
29 randId = Math.floor(Math.random() * 100000);
30 stackSubfields: boolean;
33 private idl: IdlService,
34 private org: OrgService,
35 private store: ServerStoreService,
36 private tagTable: TagTableService
41 this.store.getItem('cat.marcedit.stack_subfields')
42 .then(stack => this.stackSubfields = stack);
45 this.context.recordChange.subscribe(__ => this.init()));
47 // Changing the Type fixed field means loading new meta-metadata.
48 this.record.fixedFieldChange.pipe(filter(code => code === 'Type'))
49 .subscribe(_ => this.init());
52 init(): Promise<any> {
53 this.dataLoaded = false;
55 if (!this.record) { return Promise.resolve(); }
58 this.tagTable.loadTagTable({marcRecordType: this.context.recordType}),
59 this.tagTable.getFfPosTable(this.record.recordType()),
60 this.tagTable.getFfValueTable(this.record.recordType())
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)
71 stackSubfieldsChange() {
72 if (this.stackSubfields) {
73 this.store.setItem('cat.marcedit.stack_subfields', true);
75 this.store.removeItem('cat.marcedit.stack_subfields');
80 return this.context.undoStack.length;
84 return this.context.redoStack.length;
88 this.context.requestUndo();
92 this.context.requestRedo();
95 controlFields(): MarcField[] {
96 return this.record.fields.filter(f => f.isCtrlField);
99 dataFields(): MarcField[] {
100 return this.record.fields.filter(f => !f.isCtrlField);