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';
17 import {HoldingsService} from '@eg/staff/share/holdings/holdings.service';
19 interface MarcSavedEvent {
26 * MARC Record editor main interface.
30 selector: 'eg-marc-editor',
31 templateUrl: './editor.component.html'
34 export class MarcEditorComponent implements OnInit {
36 editorTab: 'rich' | 'flat';
37 sources: ComboboxEntry[];
38 context: MarcEditContext;
40 // True if the save request is in flight
43 @Input() recordType: 'biblio' | 'authority' = 'biblio';
45 @Input() set recordId(id: number) {
47 if (this.record && this.record.id === id) { return; }
51 get recordId(): number {
52 return this.record ? this.record.id : null;
55 @Input() set recordXml(xml: string) {
61 get record(): MarcRecord {
62 return this.context.record;
65 // Tell us which record source to select by default.
66 // Useful for new records and in-place editing from bare XML.
67 @Input() recordSource: number;
69 // If true, saving records to the database is assumed to
70 // happen externally. IOW, the record editor is just an
71 // in-place MARC modification interface.
72 @Input() inPlaceMode: boolean;
74 // In inPlaceMode, this is emitted in lieu of saving the record
75 // in th database. When inPlaceMode is false, this is emitted after
76 // the record is successfully saved.
77 @Output() recordSaved: EventEmitter<MarcSavedEvent>;
79 @ViewChild('sourceSelector', {static: false}) sourceSelector: ComboboxComponent;
80 @ViewChild('confirmDelete', {static: false}) confirmDelete: ConfirmDialogComponent;
81 @ViewChild('confirmUndelete', {static: false}) confirmUndelete: ConfirmDialogComponent;
82 @ViewChild('cannotDelete', {static: false}) cannotDelete: ConfirmDialogComponent;
83 @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
84 @ViewChild('failMsg', {static: false}) failMsg: StringComponent;
86 fastItemLabel: string;
87 fastItemBarcode: string;
91 private evt: EventService,
92 private idl: IdlService,
93 private net: NetService,
94 private auth: AuthService,
95 private org: OrgService,
96 private pcrud: PcrudService,
97 private toast: ToastService,
98 private holdings: HoldingsService,
99 private store: ServerStoreService
102 this.recordSaved = new EventEmitter<MarcSavedEvent>();
103 this.context = new MarcEditContext();
105 this.recordSaved.subscribe(_ => this.dataSaving = false);
110 this.context.recordType = this.recordType;
112 this.store.getItem('cat.marcedit.flateditor').then(
113 useFlat => this.editorTab = useFlat ? 'flat' : 'rich');
115 if (this.recordType !== 'biblio') { return; }
117 this.pcrud.retrieveAll('cbs').subscribe(
118 src => this.sources.push({id: +src.id(), label: src.source()}),
121 this.sources = this.sources.sort((a, b) =>
122 a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
125 if (this.recordSource) {
126 this.sourceSelector.applyEntryId(this.recordSource);
132 changesPending(): boolean {
133 return this.context.changesPending;
136 clearPendingChanges() {
137 this.context.changesPending = false;
140 // Remember the last used tab as the preferred tab.
141 tabChange(evt: NgbTabChangeEvent) {
143 // Avoid undo persistence across tabs since that could result
144 // in changes getting lost.
145 this.context.resetUndos();
147 if (evt.nextId === 'flat') {
148 this.store.setItem('cat.marcedit.flateditor', true);
150 this.store.removeItem('cat.marcedit.flateditor');
154 saveRecord(): Promise<any> {
155 const xml = this.record.toXml();
156 this.dataSaving = true;
158 // Save actions clears any pending changes.
159 this.context.changesPending = false;
160 this.context.resetUndos();
162 let sourceName: string = null;
163 let sourceId: number = null;
165 if (this.sourceSelector && this.sourceSelector.selected) {
166 sourceName = this.sourceSelector.selected.label;
167 sourceId = this.sourceSelector.selected.id;
171 marcXml: xml, bibSource: sourceId, recordId: this.recordId};
173 if (this.inPlaceMode) {
174 // Let the caller have the modified XML and move on.
175 this.recordSaved.emit(emission);
176 return Promise.resolve();
181 if (this.record.id) { // Editing an existing record
183 promise = this.modifyRecord(xml, sourceName, sourceId);
187 promise = this.createRecord(xml, sourceName);
190 // NOTE we do not reinitialize our record with the MARC returned
191 // from the server after a create/update, which means our record
192 // may be out of sync, e.g. missing 901* values. It's the
193 // callers responsibility to tear us down and rebuild us.
194 return promise.then(marcXml => {
195 if (!marcXml) { return null; }
196 this.successMsg.current().then(msg => this.toast.success(msg));
197 emission.marcXml = marcXml;
198 emission.recordId = this.recordId;
199 this.recordSaved.emit(emission);
205 modifyRecord(marcXml: string, sourceName: string, sourceId: number): Promise<any> {
206 const method = 'open-ils.cat.biblio.record.marc.replace';
208 return this.net.request('open-ils.cat', method,
209 this.auth.token(), this.record.id, marcXml, sourceName
211 ).toPromise().then(response => {
213 const evt = this.evt.parse(response);
216 this.failMsg.current().then(msg => this.toast.warning(msg));
217 this.dataSaving = false;
221 return response.marc();
225 createRecord(marcXml: string, sourceName?: string): Promise<any> {
227 const method = this.recordType === 'biblio' ?
228 'open-ils.cat.biblio.record.xml.create' :
229 'open-ils.cat.authority.record.import';
231 return this.net.request('open-ils.cat', method,
232 this.auth.token(), marcXml, sourceName
233 ).toPromise().then(response => {
235 const evt = this.evt.parse(response);
239 this.failMsg.current().then(msg => this.toast.warning(msg));
240 this.dataSaving = false;
244 this.record.id = response.id();
245 return response.marc();
249 fromId(id: number): Promise<any> {
250 return this.pcrud.retrieve('bre', id)
251 .toPromise().then(bib => {
252 this.context.record = new MarcRecord(bib.marc());
254 this.record.deleted = bib.deleted() === 't';
256 this.sourceSelector.applyEntryId(+bib.source());
261 fromXml(xml: string) {
262 this.context.record = new MarcRecord(xml);
263 this.record.id = null;
266 deleteRecord(): Promise<any> {
268 return this.confirmDelete.open().toPromise()
270 if (!yes) { return; }
272 return this.net.request('open-ils.cat',
273 'open-ils.cat.biblio.record_entry.delete',
274 this.auth.token(), this.record.id).toPromise()
278 const evt = this.evt.parse(resp);
280 if (evt.textcode === 'RECORD_NOT_EMPTY') {
281 return this.cannotDelete.open().toPromise();
287 return this.fromId(this.record.id)
288 .then(_ => this.recordSaved.emit(
289 {marcXml: this.record.toXml(), recordId: this.recordId}));
294 undeleteRecord(): Promise<any> {
296 return this.confirmUndelete.open().toPromise()
298 if (!yes) { return; }
300 return this.net.request('open-ils.cat',
301 'open-ils.cat.biblio.record_entry.undelete',
302 this.auth.token(), this.record.id).toPromise()
306 const evt = this.evt.parse(resp);
307 if (evt) { console.error(evt); return alert(evt); }
309 return this.fromId(this.record.id)
310 .then(_ => this.recordSaved.emit(
311 {marcXml: this.record.toXml(), recordId: this.recordId}));
316 // Spawns the copy editor with the requested barcode and
317 // call number label. Called after our record is saved.
319 if (this.showFastAdd && this.fastItemLabel && this.fastItemBarcode) {
322 label: this.fastItemLabel,
323 barcode: this.fastItemBarcode,
327 this.holdings.spawnAddHoldingsUi(this.recordId, null, [fastItem]);