1 import {Component, Input, Output, OnInit, EventEmitter, ViewChild} from '@angular/core';
2 import {IdlService} from '@eg/core/idl.service';
3 import {EventService} from '@eg/core/event.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {ServerStoreService} from '@eg/core/server-store.service';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {MarcRecord} from './marcrecord';
12 import {ComboboxEntry, ComboboxComponent
13 } from '@eg/share/combobox/combobox.component';
14 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
15 import {MarcEditContext} from './editor-context';
16 import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
18 interface MarcSavedEvent {
24 * MARC Record editor main interface.
28 selector: 'eg-marc-editor',
29 templateUrl: './editor.component.html'
32 export class MarcEditorComponent implements OnInit {
34 editorTab: 'rich' | 'flat';
35 sources: ComboboxEntry[];
36 context: MarcEditContext;
38 @Input() recordType: 'biblio' | 'authority' = 'biblio';
40 @Input() set recordId(id: number) {
42 if (this.record && this.record.id === id) { return; }
46 @Input() set recordXml(xml: string) {
52 get record(): MarcRecord {
53 return this.context.record;
56 // Tell us which record source to select by default.
57 // Useful for new records and in-place editing from bare XML.
58 @Input() recordSource: number;
60 // If true, saving records to the database is assumed to
61 // happen externally. IOW, the record editor is just an
62 // in-place MARC modification interface.
63 @Input() inPlaceMode: boolean;
65 // In inPlaceMode, this is emitted in lieu of saving the record
66 // in th database. When inPlaceMode is false, this is emitted after
67 // the record is successfully saved.
68 @Output() recordSaved: EventEmitter<MarcSavedEvent>;
70 @ViewChild('sourceSelector', { static: true }) sourceSelector: ComboboxComponent;
71 @ViewChild('confirmDelete', { static: true }) confirmDelete: ConfirmDialogComponent;
72 @ViewChild('confirmUndelete', { static: true }) confirmUndelete: ConfirmDialogComponent;
73 @ViewChild('cannotDelete', { static: true }) cannotDelete: ConfirmDialogComponent;
74 @ViewChild('successMsg', { static: true }) successMsg: StringComponent;
75 @ViewChild('failMsg', { static: true }) failMsg: StringComponent;
78 private evt: EventService,
79 private idl: IdlService,
80 private net: NetService,
81 private auth: AuthService,
82 private org: OrgService,
83 private pcrud: PcrudService,
84 private toast: ToastService,
85 private store: ServerStoreService
88 this.recordSaved = new EventEmitter<MarcSavedEvent>();
89 this.context = new MarcEditContext();
94 this.context.recordType = this.recordType;
96 this.store.getItem('cat.marcedit.flateditor').then(
97 useFlat => this.editorTab = useFlat ? 'flat' : 'rich');
99 this.pcrud.retrieveAll('cbs').subscribe(
100 src => this.sources.push({id: +src.id(), label: src.source()}),
103 this.sources = this.sources.sort((a, b) =>
104 a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
107 if (this.recordSource) {
108 this.sourceSelector.applyEntryId(this.recordSource);
114 // Remember the last used tab as the preferred tab.
115 tabChange(evt: NgbTabChangeEvent) {
117 // Avoid undo persistence across tabs since that could result
118 // in changes getting lost.
119 this.context.resetUndos();
121 if (evt.nextId === 'flat') {
122 this.store.setItem('cat.marcedit.flateditor', true);
124 this.store.removeItem('cat.marcedit.flateditor');
128 saveRecord(): Promise<any> {
129 const xml = this.record.toXml();
131 let sourceName: string = null;
132 let sourceId: number = null;
134 if (this.sourceSelector.selected) {
135 sourceName = this.sourceSelector.selected.label;
136 sourceId = this.sourceSelector.selected.id;
139 if (this.inPlaceMode) {
140 // Let the caller have the modified XML and move on.
141 this.recordSaved.emit({marcXml: xml, bibSource: sourceId});
142 return Promise.resolve();
145 if (this.record.id) { // Editing an existing record
147 const method = 'open-ils.cat.biblio.record.xml.update';
149 return this.net.request('open-ils.cat', method,
150 this.auth.token(), this.record.id, xml, sourceName
151 ).toPromise().then(response => {
153 const evt = this.evt.parse(response);
156 this.failMsg.current().then(msg => this.toast.warning(msg));
160 this.successMsg.current().then(msg => this.toast.success(msg));
161 this.recordSaved.emit({marcXml: xml, bibSource: sourceId});
166 // TODO: create a new record
170 fromId(id: number): Promise<any> {
171 return this.pcrud.retrieve('bre', id)
172 .toPromise().then(bib => {
173 this.context.record = new MarcRecord(bib.marc());
175 this.record.deleted = bib.deleted() === 't';
177 this.sourceSelector.applyEntryId(+bib.source());
182 fromXml(xml: string) {
183 this.context.record = new MarcRecord(xml);
184 this.record.id = null;
187 deleteRecord(): Promise<any> {
189 return this.confirmDelete.open().toPromise()
191 if (!yes) { return; }
193 return this.net.request('open-ils.cat',
194 'open-ils.cat.biblio.record_entry.delete',
195 this.auth.token(), this.record.id).toPromise()
199 const evt = this.evt.parse(resp);
201 if (evt.textcode === 'RECORD_NOT_EMPTY') {
202 return this.cannotDelete.open().toPromise();
208 return this.fromId(this.record.id)
209 .then(_ => this.recordSaved.emit(
210 {marcXml: this.record.toXml()}));
215 undeleteRecord(): Promise<any> {
217 return this.confirmUndelete.open().toPromise()
219 if (!yes) { return; }
221 return this.net.request('open-ils.cat',
222 'open-ils.cat.biblio.record_entry.undelete',
223 this.auth.token(), this.record.id).toPromise()
227 const evt = this.evt.parse(resp);
228 if (evt) { console.error(evt); return alert(evt); }
230 return this.fromId(this.record.id)
231 .then(_ => this.recordSaved.emit(
232 {marcXml: this.record.toXml()}));