]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/editor.component.ts
138161611d803a20fec814fa45995862938b07a6
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / editor.component.ts
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
18 interface MarcSavedEvent {
19     marcXml: string;
20     bibSource?: number;
21 }
22
23 /**
24  * MARC Record editor main interface.
25  */
26
27 @Component({
28   selector: 'eg-marc-editor',
29   templateUrl: './editor.component.html'
30 })
31
32 export class MarcEditorComponent implements OnInit {
33
34     editorTab: 'rich' | 'flat';
35     sources: ComboboxEntry[];
36     context: MarcEditContext;
37
38     @Input() recordType: 'biblio' | 'authority' = 'biblio';
39
40     @Input() set recordId(id: number) {
41         if (!id) { return; }
42         if (this.record && this.record.id === id) { return; }
43         this.fromId(id);
44     }
45
46     @Input() set recordXml(xml: string) {
47         if (xml) {
48             this.fromXml(xml);
49         }
50     }
51
52     get record(): MarcRecord {
53         return this.context.record;
54     }
55
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;
59
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;
64
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>;
69
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;
76
77     constructor(
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
86     ) {
87         this.sources = [];
88         this.recordSaved = new EventEmitter<MarcSavedEvent>();
89         this.context = new MarcEditContext();
90     }
91
92     ngOnInit() {
93
94         this.context.recordType = this.recordType;
95
96         this.store.getItem('cat.marcedit.flateditor').then(
97             useFlat => this.editorTab = useFlat ? 'flat' : 'rich');
98
99         this.pcrud.retrieveAll('cbs').subscribe(
100             src => this.sources.push({id: +src.id(), label: src.source()}),
101             _ => {},
102             () => {
103                 this.sources = this.sources.sort((a, b) =>
104                     a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
105                 );
106
107                 if (this.recordSource) {
108                     this.sourceSelector.applyEntryId(this.recordSource);
109                 }
110             }
111         );
112     }
113
114     // Remember the last used tab as the preferred tab.
115     tabChange(evt: NgbTabChangeEvent) {
116
117         // Avoid undo persistence across tabs since that could result
118         // in changes getting lost.
119         this.context.resetUndos();
120
121         if (evt.nextId === 'flat') {
122             this.store.setItem('cat.marcedit.flateditor', true);
123         } else {
124             this.store.removeItem('cat.marcedit.flateditor');
125         }
126     }
127
128     saveRecord(): Promise<any> {
129         const xml = this.record.toXml();
130
131         let sourceName: string = null;
132         let sourceId: number = null;
133
134         if (this.sourceSelector.selected) {
135             sourceName = this.sourceSelector.selected.label;
136             sourceId = this.sourceSelector.selected.id;
137         }
138
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();
143         }
144
145         if (this.record.id) { // Editing an existing record
146
147             const method = 'open-ils.cat.biblio.record.xml.update';
148
149             return this.net.request('open-ils.cat', method,
150                 this.auth.token(), this.record.id, xml, sourceName
151             ).toPromise().then(response => {
152
153                 const evt = this.evt.parse(response);
154                 if (evt) {
155                     console.error(evt);
156                     this.failMsg.current().then(msg => this.toast.warning(msg));
157                     return;
158                 }
159
160                 this.successMsg.current().then(msg => this.toast.success(msg));
161                 this.recordSaved.emit({marcXml: xml, bibSource: sourceId});
162                 return response;
163             });
164
165         } else {
166             // TODO: create a new record
167         }
168     }
169
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());
174             this.record.id = id;
175             this.record.deleted = bib.deleted() === 't';
176             if (bib.source()) {
177                 this.sourceSelector.applyEntryId(+bib.source());
178             }
179         });
180     }
181
182     fromXml(xml: string) {
183         this.context.record = new MarcRecord(xml);
184         this.record.id = null;
185     }
186
187     deleteRecord(): Promise<any> {
188
189         return this.confirmDelete.open().toPromise()
190         .then(yes => {
191             if (!yes) { return; }
192
193             return this.net.request('open-ils.cat',
194                 'open-ils.cat.biblio.record_entry.delete',
195                 this.auth.token(), this.record.id).toPromise()
196
197             .then(resp => {
198
199                 const evt = this.evt.parse(resp);
200                 if (evt) {
201                     if (evt.textcode === 'RECORD_NOT_EMPTY') {
202                         return this.cannotDelete.open().toPromise();
203                     } else {
204                         console.error(evt);
205                         return alert(evt);
206                     }
207                 }
208                 return this.fromId(this.record.id)
209                 .then(_ => this.recordSaved.emit(
210                     {marcXml: this.record.toXml()}));
211             });
212         });
213     }
214
215     undeleteRecord(): Promise<any> {
216
217         return this.confirmUndelete.open().toPromise()
218         .then(yes => {
219             if (!yes) { return; }
220
221             return this.net.request('open-ils.cat',
222                 'open-ils.cat.biblio.record_entry.undelete',
223                 this.auth.token(), this.record.id).toPromise()
224
225             .then(resp => {
226
227                 const evt = this.evt.parse(resp);
228                 if (evt) { console.error(evt); return alert(evt); }
229
230                 return this.fromId(this.record.id)
231                 .then(_ => this.recordSaved.emit(
232                     {marcXml: this.record.toXml()}));
233             });
234         });
235     }
236 }
237