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 {MarcRecord} from './marcrecord';
9 import {ComboboxEntry, ComboboxComponent
10 } from '@eg/share/combobox/combobox.component';
11 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
15 * MARC Record editor main interface.
19 selector: 'eg-marc-editor',
20 templateUrl: './editor.component.html'
23 export class MarcEditorComponent implements OnInit {
26 editorTab: 'rich' | 'flat';
27 sources: ComboboxEntry[];
29 @Input() set recordId(id: number) {
31 if (this.record && this.record.id === id) { return; }
35 @Input() set recordXml(xml: string) {
36 if (xml) { this.fromXml(xml); }
39 // If true, saving records to the database is assumed to
40 // happen externally. IOW, the record editor is just an
41 // in-place MARC modification interface.
44 // In inPlaceMode, this is emitted in lieu of saving the record
45 // in th database. When inPlaceMode is false, this is emitted after
46 // the record is successfully saved.
47 @Output() recordSaved: EventEmitter<string>;
49 @ViewChild('sourceSelector') sourceSelector: ComboboxComponent;
50 @ViewChild('confirmDelete') confirmDelete: ConfirmDialogComponent;
51 @ViewChild('confirmUndelete') confirmUndelete: ConfirmDialogComponent;
52 @ViewChild('cannotDelete') cannotDelete: ConfirmDialogComponent;
55 private evt: EventService,
56 private idl: IdlService,
57 private net: NetService,
58 private auth: AuthService,
59 private org: OrgService,
60 private pcrud: PcrudService
63 this.recordSaved = new EventEmitter<string>();
67 // Default to flat for now since it's all that's supported.
68 this.editorTab = 'flat';
70 this.pcrud.retrieveAll('cbs').subscribe(
71 src => this.sources.push({id: +src.id(), label: src.source()}),
74 this.sources = this.sources.sort((a, b) =>
75 a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
81 saveRecord(): Promise<any> {
82 const xml = this.record.toXml();
84 if (this.inPlaceMode) {
85 // Let the caller have the modified XML and move on.
86 this.recordSaved.emit(xml);
87 return Promise.resolve();
90 const source = this.sourceSelector.selected ?
91 this.sourceSelector.selected.label : null; // 'label' not a typo
93 if (this.record.id) { // Editing an existing record
95 const method = 'open-ils.cat.biblio.record.marc.replace';
97 return this.net.request('open-ils.cat', method,
98 this.auth.token(), this.record.id, xml, source
99 ).toPromise().then(response => {
101 const evt = this.evt.parse(response);
108 this.recordSaved.emit(xml);
113 // TODO: create a new record
117 fromId(id: number): Promise<any> {
118 return this.pcrud.retrieve('bre', id)
119 .toPromise().then(bib => {
120 this.record = new MarcRecord(bib.marc());
122 this.record.deleted = bib.deleted() === 't';
124 this.sourceSelector.applyEntryId(+bib.source());
129 fromXml(xml: string) {
130 this.record = new MarcRecord(xml);
131 this.record.id = null;
134 deleteRecord(): Promise<any> {
136 return this.confirmDelete.open().toPromise()
138 if (!yes) { return; }
140 return this.net.request('open-ils.cat',
141 'open-ils.cat.biblio.record_entry.delete',
142 this.auth.token(), this.record.id).toPromise()
146 const evt = this.evt.parse(resp);
148 if (evt.textcode === 'RECORD_NOT_EMPTY') {
149 return this.cannotDelete.open().toPromise();
155 return this.fromId(this.record.id)
156 .then(_ => this.recordSaved.emit(this.record.toXml()));
161 undeleteRecord(): Promise<any> {
163 return this.confirmUndelete.open().toPromise()
165 if (!yes) { return; }
167 return this.net.request('open-ils.cat',
168 'open-ils.cat.biblio.record_entry.undelete',
169 this.auth.token(), this.record.id).toPromise()
173 const evt = this.evt.parse(resp);
174 if (evt) { console.error(evt); return alert(evt); }
176 return this.fromId(this.record.id)
177 .then(_ => this.recordSaved.emit(this.record.toXml()));