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 {StringComponent} from '@eg/share/string/string.component';
10 import {MarcRecord} from './marcrecord';
11 import {ComboboxEntry, ComboboxComponent
12 } from '@eg/share/combobox/combobox.component';
13 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
17 * MARC Record editor main interface.
21 selector: 'eg-marc-editor',
22 templateUrl: './editor.component.html'
25 export class MarcEditorComponent implements OnInit {
28 editorTab: 'rich' | 'flat';
29 sources: ComboboxEntry[];
31 @Input() set recordId(id: number) {
33 if (this.record && this.record.id === id) { return; }
37 @Input() set recordXml(xml: string) {
38 if (xml) { this.fromXml(xml); }
41 // If true, saving records to the database is assumed to
42 // happen externally. IOW, the record editor is just an
43 // in-place MARC modification interface.
46 // In inPlaceMode, this is emitted in lieu of saving the record
47 // in th database. When inPlaceMode is false, this is emitted after
48 // the record is successfully saved.
49 @Output() recordSaved: EventEmitter<string>;
51 @ViewChild('sourceSelector') sourceSelector: ComboboxComponent;
52 @ViewChild('confirmDelete') confirmDelete: ConfirmDialogComponent;
53 @ViewChild('confirmUndelete') confirmUndelete: ConfirmDialogComponent;
54 @ViewChild('cannotDelete') cannotDelete: ConfirmDialogComponent;
55 @ViewChild('successMsg') successMsg: StringComponent;
56 @ViewChild('failMsg') failMsg: StringComponent;
59 private evt: EventService,
60 private idl: IdlService,
61 private net: NetService,
62 private auth: AuthService,
63 private org: OrgService,
64 private pcrud: PcrudService,
65 private toast: ToastService
68 this.recordSaved = new EventEmitter<string>();
72 // Default to flat for now since it's all that's supported.
73 this.editorTab = 'flat';
75 this.pcrud.retrieveAll('cbs').subscribe(
76 src => this.sources.push({id: +src.id(), label: src.source()}),
79 this.sources = this.sources.sort((a, b) =>
80 a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
86 saveRecord(): Promise<any> {
87 const xml = this.record.toXml();
89 if (this.inPlaceMode) {
90 // Let the caller have the modified XML and move on.
91 this.recordSaved.emit(xml);
92 return Promise.resolve();
95 const source = this.sourceSelector.selected ?
96 this.sourceSelector.selected.label : null; // 'label' not a typo
98 if (this.record.id) { // Editing an existing record
100 const method = 'open-ils.cat.biblio.record.marc.replace';
102 return this.net.request('open-ils.cat', method,
103 this.auth.token(), this.record.id, xml, source
104 ).toPromise().then(response => {
106 const evt = this.evt.parse(response);
109 this.failMsg.current().then(msg => this.toast.warning(msg));
113 this.successMsg.current().then(msg => this.toast.success(msg));
114 this.recordSaved.emit(xml);
119 // TODO: create a new record
123 fromId(id: number): Promise<any> {
124 return this.pcrud.retrieve('bre', id)
125 .toPromise().then(bib => {
126 this.record = new MarcRecord(bib.marc());
128 this.record.deleted = bib.deleted() === 't';
130 this.sourceSelector.applyEntryId(+bib.source());
135 fromXml(xml: string) {
136 this.record = new MarcRecord(xml);
137 this.record.id = null;
140 deleteRecord(): Promise<any> {
142 return this.confirmDelete.open().toPromise()
144 if (!yes) { return; }
146 return this.net.request('open-ils.cat',
147 'open-ils.cat.biblio.record_entry.delete',
148 this.auth.token(), this.record.id).toPromise()
152 const evt = this.evt.parse(resp);
154 if (evt.textcode === 'RECORD_NOT_EMPTY') {
155 return this.cannotDelete.open().toPromise();
161 return this.fromId(this.record.id)
162 .then(_ => this.recordSaved.emit(this.record.toXml()));
167 undeleteRecord(): Promise<any> {
169 return this.confirmUndelete.open().toPromise()
171 if (!yes) { return; }
173 return this.net.request('open-ils.cat',
174 'open-ils.cat.biblio.record_entry.undelete',
175 this.auth.token(), this.record.id).toPromise()
179 const evt = this.evt.parse(resp);
180 if (evt) { console.error(evt); return alert(evt); }
182 return this.fromId(this.record.id)
183 .then(_ => this.recordSaved.emit(this.record.toXml()));