1 import {Component, OnInit, AfterViewInit, ViewChild, HostListener} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {tap} from 'rxjs/operators';
4 import {IdlObject, IdlService} from '@eg/core/idl.service';
5 import {EventService} from '@eg/core/event.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {NetService} from '@eg/core/net.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {HoldingsService, CallNumData} from '@eg/staff/share/holdings/holdings.service';
11 import {VolCopyContext} from './volcopy';
12 import {ProgressInlineComponent} from '@eg/share/dialog/progress-inline.component';
13 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
14 import {AnonCacheService} from '@eg/share/util/anon-cache.service';
15 import {VolCopyService} from './volcopy.service';
16 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
17 import {BroadcastService} from '@eg/share/util/broadcast.service';
18 import {CopyAttrsComponent} from './copy-attrs.component';
24 'call_number', 'location', 'parts', 'tags',
25 'creator', 'editor', 'stat_cat_entries', 'notes'
32 interface EditSession {
34 // Unset if editing in multi-record mode
40 // Adding to or creating new call numbers
43 // Hide the volumes editor
46 // Hide the copy attrs editor.
51 templateUrl: 'volcopy.component.html'
53 export class VolCopyComponent implements OnInit {
55 context: VolCopyContext;
57 sessionExpired = false;
59 tab = 'holdings'; // holdings | attrs | config
60 target: string; // item | callnumber | record | session
61 targetId: string; // id value or session string
65 changesPending = false;
66 routingAllowed = false;
68 @ViewChild('pendingChangesDialog', {static: false})
69 pendingChangesDialog: ConfirmDialogComponent;
71 @ViewChild('copyAttrs', {static: false}) copyAttrs: CopyAttrsComponent;
74 private router: Router,
75 private route: ActivatedRoute,
76 private evt: EventService,
77 private idl: IdlService,
78 private org: OrgService,
79 private net: NetService,
80 private auth: AuthService,
81 private pcrud: PcrudService,
82 private cache: AnonCacheService,
83 private broadcaster: BroadcastService,
84 private holdings: HoldingsService,
85 private volcopy: VolCopyService
89 this.route.paramMap.subscribe(
90 (params: ParamMap) => this.negotiateRoute(params));
93 negotiateRoute(params: ParamMap) {
94 this.tab = params.get('tab') || 'holdings';
95 this.target = params.get('target');
96 this.targetId = params.get('target_id');
98 if (this.volcopy.currentContext) {
99 // Avoid clobbering the context on route change.
100 this.context = this.volcopy.currentContext;
102 this.context = new VolCopyContext();
103 this.context.org = this.org; // inject;
106 switch (this.target) {
108 this.context.copyId = +this.targetId;
111 this.context.volId = +this.targetId;
114 this.context.recordId = +this.targetId;
117 this.context.session = this.targetId;
121 if (this.volcopy.currentContext) {
122 this.loading = false;
125 // Avoid refetching the data during route changes.
126 this.volcopy.currentContext = this.context;
131 load(copyIds?: number[]): Promise<any> {
132 this.sessionExpired = false;
134 this.context.reset();
136 return this.volcopy.load()
137 .then(_ => this.fetchHoldings(copyIds))
138 .then(_ => this.volcopy.applyVolLabels(
139 this.context.volNodes().map(n => n.target)))
140 .then(_ => this.context.sortHoldings())
141 .then(_ => this.context.setRecordId())
143 // unified display has no 'attrs' tab
144 if (this.volcopy.defaults.values.unified_display
145 && this.tab === 'attrs') {
146 this.tab = 'holdings';
150 .then(_ => this.loading = false);
153 fetchHoldings(copyIds?: number[]): Promise<any> {
155 if (copyIds && copyIds.length > 0) {
156 // Reloading copies that were just edited.
157 return this.fetchCopies(copyIds);
159 } else if (this.context.session) {
160 this.context.sessionType = 'mixed';
161 return this.fetchSession(this.context.session);
163 } else if (this.context.copyId) {
164 this.context.sessionType = 'copy';
165 return this.fetchCopies(this.context.copyId);
167 } else if (this.context.volId) {
168 this.context.sessionType = 'vol';
169 return this.fetchVols(this.context.volId);
171 } else if (this.context.recordId) {
172 this.context.sessionType = 'record';
173 return this.fetchRecords(this.context.recordId);
177 // Changing a tab in the UI means changing the route.
178 // Changing the route ultimately results in changing the tab.
179 beforeTabChange(evt: NgbNavChangeEvent) {
180 evt.preventDefault();
182 let promise = this.canDeactivate();
183 if (!(promise instanceof Promise)) {
184 promise = Promise.resolve(promise);
189 this.routingAllowed = true;
190 this.tab = evt.nextId;
198 `/staff/cat/volcopy/${this.tab}/${this.target}/${this.targetId}`;
200 // Retain search parameters
201 this.router.navigate([url], {queryParamsHandling: 'merge'});
204 fetchSession(session: string): Promise<any> {
206 return this.cache.getItem(session, 'edit-these-copies')
207 .then((editSession: EditSession) => {
210 this.loading = false;
211 this.sessionExpired = true;
212 return Promise.reject('Session Expired');
215 console.debug('Edit Session', editSession);
217 this.context.recordId = editSession.record_id;
219 if (editSession.copies && editSession.copies.length > 0) {
220 return this.fetchCopies(editSession.copies);
223 const volsToFetch = [];
224 const volsToCreate = [];
225 editSession.raw.forEach((volData: CallNumData) => {
226 this.context.fastAdd = volData.fast_add === true;
228 if (volData.callnumber > 0) {
229 volsToFetch.push(volData);
231 volsToCreate.push(volData);
235 let promise = Promise.resolve();
236 if (volsToFetch.length > 0) {
237 promise = promise.then(_ =>
238 this.fetchVolsStubCopies(volsToFetch));
241 if (volsToCreate.length > 0) {
242 promise = promise.then(_ =>
243 this.createVolsStubCopies(volsToCreate));
250 // Creating new vols. Each gets a stub copy.
251 createVolsStubCopies(volDataList: CallNumData[]): Promise<any> {
254 volDataList.forEach(volData => {
256 const vol = this.volcopy.createStubVol(
257 this.context.recordId,
258 volData.owner || this.auth.user().ws_ou()
261 if (volData.label) {vol.label(volData.label); }
263 volData.callnumber = vol.id(); // wanted by addStubCopies
265 this.context.findOrCreateVolNode(vol);
268 return this.addStubCopies(vols, volDataList)
269 .then(_ => this.volcopy.setVolClassLabels(vols));
272 // Fetch vols by ID, but instead of retrieving their copies
273 // add a stub copy to each.
274 fetchVolsStubCopies(volDataList: CallNumData[]): Promise<any> {
276 const volIds = volDataList.map(volData => volData.callnumber);
279 return this.pcrud.search('acn', {id: volIds})
280 .pipe(tap((vol: IdlObject) => vols.push(vol))).toPromise()
281 .then(_ => this.addStubCopies(vols, volDataList));
284 // Add a stub copy to each vol using data from the edit session.
285 addStubCopies(vols: IdlObject[], volDataList: CallNumData[]): Promise<any> {
288 vols.forEach(vol => {
289 const volData = volDataList.filter(
290 vData => vData.callnumber === vol.id())[0];
293 this.volcopy.createStubCopy(vol, {circLib: volData.owner});
295 this.context.findOrCreateCopyNode(copy);
299 return this.volcopy.setCopyStatus(copies);
302 fetchCopies(copyIds: number | number[]): Promise<any> {
303 const ids = [].concat(copyIds);
304 if (ids.length === 0) { return Promise.resolve(); }
305 return this.pcrud.search('acp', {id: ids}, COPY_FLESH)
306 .pipe(tap(copy => this.context.findOrCreateCopyNode(copy)))
310 // Fetch call numbers and linked copies by call number ids.
311 fetchVols(volIds?: number | number[]): Promise<any> {
312 const ids = [].concat(volIds);
313 if (ids.length === 0) { return Promise.resolve(); }
315 return this.pcrud.search('acn', {id: ids})
316 .pipe(tap(vol => this.context.findOrCreateVolNode(vol)))
317 .toPromise().then(_ => {
318 return this.pcrud.search('acp',
319 {call_number: ids, deleted: 'f'}, COPY_FLESH
320 ).pipe(tap(copy => this.context.findOrCreateCopyNode(copy))
325 // Fetch call numbers and copies by record ids.
326 fetchRecords(recordIds: number | number[]): Promise<any> {
327 const ids = [].concat(recordIds);
329 return this.pcrud.search('acn',
330 {record: ids, deleted: 'f', label: {'!=' : '##URI##'}},
331 {}, {idlist: true, atomic: true}
332 ).toPromise().then(volIds => this.fetchVols(volIds));
336 save(close?: boolean): Promise<any> {
339 if (this.copyAttrs) {
340 // Won't exist on any non-attrs page.
341 this.copyAttrs.applyPendingChanges();
344 // Volume update API wants volumes fleshed with copies, instead
345 // of the other way around, which is what we have here.
346 const volumes: IdlObject[] = [];
348 this.context.volNodes().forEach(volNode => {
349 const newVol = this.idl.clone(volNode.target);
350 const copies: IdlObject[] = [];
352 volNode.children.forEach(copyNode => {
353 const copy = copyNode.target;
355 if (copy.isnew() && !copy.barcode()) {
356 // A new copy w/ no barcode is a stub copy sitting
357 // on an empty call number. Ignore it.
361 // Be sure to include copies when the volume is changed
362 // without any changes to the copies. This ensures the
363 // API knows when we are modifying a subset of the total
364 // copies on a volume, e.g. when changing volume labels
365 if (newVol.ischanged()) { copy.ischanged(true); }
367 if (copy.ischanged() || copy.isnew() || copy.isdeleted()) {
368 const copyClone = this.idl.clone(copy);
369 // De-flesh call number
370 copyClone.call_number(copy.call_number().id());
371 copies.push(copyClone);
375 newVol.copies(copies);
377 if (newVol.ischanged() || newVol.isnew() || copies.length > 0) {
378 volumes.push(newVol);
382 this.context.volsToDelete.forEach(vol => {
383 const cloneVol = this.idl.clone(vol);
384 // No need to flesh copies -- they'll be force deleted.
386 volumes.push(cloneVol);
389 this.context.copiesToDelete.forEach(copy => {
390 const cloneCopy = this.idl.clone(copy);
391 const copyVol = cloneCopy.call_number();
392 cloneCopy.call_number(copyVol.id()); // de-flesh
394 let vol = volumes.filter(v => v.id() === copyVol.id())[0];
397 vol.copies().push(cloneCopy);
399 vol = this.idl.clone(copyVol);
400 vol.copies([cloneCopy]);
406 // De-flesh before posting
407 volumes.forEach(vol => {
408 vol.copies().forEach(copy => {
409 ['editor', 'creator', 'location'].forEach(field => {
410 if (typeof copy[field]() === 'object') {
411 copy[field](copy[field]().id());
417 let promise: Promise<number[]> = Promise.resolve([]);
419 if (volumes.length > 0) {
420 promise = this.saveApi(volumes, false, close);
423 return promise.then(copyIds => {
425 // In addition to the copies edited in this update call,
426 // reload any other copies that were previously loaded.
427 const ids: any = {}; // dedupe
428 this.context.copyList()
430 .filter(id => id > 0) // scrub the new copy IDs
432 .forEach(id => ids[id] = true);
434 copyIds = Object.keys(ids).map(id => Number(id));
437 return this.openPrintLabels(copyIds)
438 .then(_ => setTimeout(() => window.close()));
441 return this.load(Object.keys(ids).map(id => Number(id)));
444 this.loading = false;
445 this.changesPending = false;
449 broadcastChanges(volumes: IdlObject[]) {
451 const volIds = volumes.map(v => v.id());
455 volumes.forEach(vol => {
456 if (!recIds.includes(vol.record())) {
457 recIds.push(vol.record());
459 vol.copies().forEach(copy => copyIds.push(copy.id()));
462 this.broadcaster.broadcast('eg.holdings.update', {
469 saveApi(volumes: IdlObject[], override?:
470 boolean, close?: boolean): Promise<number[]> {
472 let method = 'open-ils.cat.asset.volume.fleshed.batch.update';
473 if (override) { method += '.override'; }
475 return this.net.request('open-ils.cat',
476 method, this.auth.token(), volumes, true,
477 { auto_merge_vols: true,
479 return_copy_ids: true,
480 force_delete_copies: true
483 ).toPromise().then(copyIds => {
485 const evt = this.evt.parse(copyIds);
488 // TODO: handle overrides?
489 // return this.saveApi(volumes, true, close);
490 this.loading = false;
492 return Promise.reject();
495 this.broadcastChanges(volumes);
501 toggleCheckbox(field: string) {
502 this.volcopy.defaults.values[field] =
503 !this.volcopy.defaults.values[field];
504 this.volcopy.saveDefaults();
507 openPrintLabels(copyIds?: number[]): Promise<any> {
508 if (!this.volcopy.defaults.values.print_labels) {
509 return Promise.resolve();
512 if (!copyIds || copyIds.length === 0) {
513 copyIds = this.context.copyList()
514 .map(c => c.id()).filter(id => id > 0);
517 return this.net.request(
519 'open-ils.actor.anon_cache.set_value',
520 null, 'print-labels-these-copies', {copies : copyIds}
522 ).toPromise().then(key => {
524 const url = '/eg/staff/cat/printlabels/' + key;
525 setTimeout(() => window.open(url, '_blank'));
529 isNotSaveable(): boolean {
530 return !(this.volsCanSave && this.attrsCanSave);
533 volsCanSaveChange(can: boolean) {
534 this.volsCanSave = can;
535 this.changesPending = true;
538 attrsCanSaveChange(can: boolean) {
539 this.attrsCanSave = can;
540 this.changesPending = true;
543 @HostListener('window:beforeunload', ['$event'])
544 canDeactivate($event?: Event): boolean | Promise<boolean> {
546 if (this.routingAllowed) {
547 // We call canDeactive manually when routing between volcopy
548 // tabs. If routingAllowed, it means we'ave already confirmed
549 // the tag change is OK.
550 this.routingAllowed = false;
554 const editing = this.copyAttrs ? this.copyAttrs.hasActiveInput() : false;
556 if (!editing && !this.changesPending) { return true; }
558 // Each warning dialog clears the current "changes are pending"
559 // flag so the user is not presented with the dialog again
560 // unless new changes are made. The 'editing' value will reset
561 // since the attrs component is getting destroyed.
562 this.changesPending = false;
564 if ($event) { // window.onbeforeunload
565 $event.preventDefault();
566 $event.returnValue = true;
568 } else { // tab OR route change.
569 return this.pendingChangesDialog.open().toPromise();