ed31980213575791d3aec0c556a2d7e3163ff54e
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / holdings.component.ts
1 import {Component, OnInit, Input, ViewChild, ViewEncapsulation
2     } from '@angular/core';
3 import {Router} from '@angular/router';
4 import {Observable, Observer, of} from 'rxjs';
5 import {map} from 'rxjs/operators';
6 import {Pager} from '@eg/share/util/pager';
7 import {IdlObject, IdlService} from '@eg/core/idl.service';
8 import {StaffCatalogService} from '../catalog.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
13 import {GridComponent} from '@eg/share/grid/grid.component';
14 import {GridToolbarCheckboxComponent
15     } from '@eg/share/grid/grid-toolbar-checkbox.component';
16 import {StoreService} from '@eg/core/store.service';
17 import {ServerStoreService} from '@eg/core/server-store.service';
18 import {MarkDamagedDialogComponent
19     } from '@eg/staff/share/holdings/mark-damaged-dialog.component';
20 import {MarkMissingDialogComponent
21     } from '@eg/staff/share/holdings/mark-missing-dialog.component';
22 import {AnonCacheService} from '@eg/share/util/anon-cache.service';
23 import {HoldingsService} from '@eg/staff/share/holdings/holdings.service';
24 import {CopyAlertsDialogComponent
25     } from '@eg/staff/share/holdings/copy-alerts-dialog.component';
26 import {ReplaceBarcodeDialogComponent
27     } from '@eg/staff/share/holdings/replace-barcode-dialog.component';
28 import {DeleteHoldingDialogComponent
29     } from '@eg/staff/share/holdings/delete-volcopy-dialog.component';
30 import {BucketDialogComponent
31     } from '@eg/staff/share/buckets/bucket-dialog.component';
32 import {ConjoinedItemsDialogComponent
33     } from '@eg/staff/share/holdings/conjoined-items-dialog.component';
34 import {MakeBookableDialogComponent
35     } from '@eg/staff/share/booking/make-bookable-dialog.component';
36 import {TransferItemsComponent
37     } from '@eg/staff/share/holdings/transfer-items.component';
38 import {TransferHoldingsComponent
39     } from '@eg/staff/share/holdings/transfer-holdings.component';
40 import {AlertDialogComponent} from '@eg/share/dialog/alert.component';
41 import {BroadcastService} from '@eg/share/util/broadcast.service';
42
43
44 // The holdings grid models a single HoldingsTree, composed of HoldingsTreeNodes
45 // flattened on-demand into a list of HoldingEntry objects.
46 export class HoldingsTreeNode {
47     children: HoldingsTreeNode[];
48     nodeType: 'org' | 'callNum' | 'copy';
49     target: any;
50     parentNode: HoldingsTreeNode;
51     expanded: boolean;
52     copyCount: number;
53     callNumCount: number;
54     constructor() {
55         this.children = [];
56     }
57 }
58
59 class HoldingsTree {
60     root: HoldingsTreeNode;
61     constructor() {
62         this.root = new HoldingsTreeNode();
63     }
64 }
65
66 export class HoldingsEntry {
67     index: number;
68     // org unit shortname, call number label, or copy barcode
69     locationLabel: string;
70     // location label indentation depth
71     locationDepth: number | null;
72     callNumCount: number | null;
73     copyCount: number | null;
74     callNumberLabel: string;
75     copy: IdlObject;
76     callNum: IdlObject;
77     circ: IdlObject;
78     treeNode: HoldingsTreeNode;
79 }
80
81 @Component({
82   selector: 'eg-holdings-maintenance',
83   templateUrl: 'holdings.component.html',
84   styleUrls: ['holdings.component.css'],
85   encapsulation: ViewEncapsulation.None
86 })
87 export class HoldingsMaintenanceComponent implements OnInit {
88
89     initDone = false;
90     gridDataSource: GridDataSource;
91     gridTemplateContext: any;
92     @ViewChild('holdingsGrid', { static: true }) holdingsGrid: GridComponent;
93
94     // Manage visibility of various sub-sections
95     @ViewChild('callNumsCheckbox', { static: true })
96         private callNumsCheckbox: GridToolbarCheckboxComponent;
97     @ViewChild('copiesCheckbox', { static: true })
98         private copiesCheckbox: GridToolbarCheckboxComponent;
99     @ViewChild('emptyCallNumsCheckbox', { static: true })
100         private emptyCallNumsCheckbox: GridToolbarCheckboxComponent;
101     @ViewChild('emptyLibsCheckbox', { static: true })
102         private emptyLibsCheckbox: GridToolbarCheckboxComponent;
103     @ViewChild('markDamagedDialog', { static: true })
104         private markDamagedDialog: MarkDamagedDialogComponent;
105     @ViewChild('markMissingDialog', { static: true })
106         private markMissingDialog: MarkMissingDialogComponent;
107     @ViewChild('copyAlertsDialog', { static: true })
108         private copyAlertsDialog: CopyAlertsDialogComponent;
109     @ViewChild('replaceBarcode', { static: true })
110         private replaceBarcode: ReplaceBarcodeDialogComponent;
111     @ViewChild('deleteHolding', { static: true })
112         private deleteHolding: DeleteHoldingDialogComponent;
113     @ViewChild('bucketDialog', { static: true })
114         private bucketDialog: BucketDialogComponent;
115     @ViewChild('conjoinedDialog', { static: true })
116         private conjoinedDialog: ConjoinedItemsDialogComponent;
117     @ViewChild('makeBookableDialog', { static: true })
118         private makeBookableDialog: MakeBookableDialogComponent;
119     @ViewChild('transferItems', {static: false})
120         private transferItems: TransferItemsComponent;
121     @ViewChild('transferHoldings', {static: false})
122         private transferHoldings: TransferHoldingsComponent;
123     @ViewChild('transferAlert', {static: false})
124         private transferAlert: AlertDialogComponent;
125
126     holdingsTree: HoldingsTree;
127
128     // nodeType => id => tree node cache
129     treeNodeCache: {[nodeType: string]: {[id: number]: HoldingsTreeNode}};
130
131     // When true and a grid reload is called, the holdings data will be
132     // re-fetched from the server.
133     refreshHoldings: boolean;
134
135     // Used as a row identifier in th grid, since we're mixing object types.
136     gridIndex: number;
137
138     // List of copies whose due date we need to retrieve.
139     itemCircsNeeded: IdlObject[];
140
141     // When true draw the grid based on the stored preferences.
142     // When not true, render based on the current "expanded" state of each node.
143     // Rendering from prefs happens on initial load and when any prefs change.
144     renderFromPrefs: boolean;
145
146     rowClassCallback: (row: any) => string;
147     cellTextGenerator: GridCellTextGenerator;
148
149     private _recId: number;
150     @Input() set recordId(id: number) {
151         this._recId = id;
152         // Only force new data collection when recordId()
153         // is invoked after ngInit() has already run.
154         if (this.initDone) {
155             this.hardRefresh();
156         }
157     }
158     get recordId(): number {
159         return this._recId;
160     }
161
162     contextOrg: IdlObject;
163
164     constructor(
165         private router: Router,
166         private org: OrgService,
167         private idl: IdlService,
168         private pcrud: PcrudService,
169         private auth: AuthService,
170         private staffCat: StaffCatalogService,
171         private store: ServerStoreService,
172         private localStore: StoreService,
173         private holdings: HoldingsService,
174         private broadcaster: BroadcastService,
175         private anonCache: AnonCacheService
176     ) {
177         // Set some sane defaults before settings are loaded.
178         this.gridDataSource = new GridDataSource();
179         this.refreshHoldings = true;
180         this.renderFromPrefs = true;
181
182         // TODO: need a separate setting for this?
183         this.contextOrg = this.staffCat.searchContext.searchOrg;
184
185         this.rowClassCallback = (row: any): string => {
186             if (row.callNum) {
187                 if (row.copy) {
188                     return 'holdings-copy-row';
189                 } else {
190                     return 'holdings-callNum-row';
191                 }
192             } else {
193                 // Add a generic org unit class and a depth-specific
194                 // class for styling different levels of the org tree.
195                 return 'holdings-org-row holdings-org-row-' +
196                     row.treeNode.target.ou_type().depth();
197             }
198         };
199
200         // Text-ify function for cells that use display templates.
201         this.cellTextGenerator = {
202             owner_label: row => row.locationLabel,
203             holdable: row => row.copy ?
204                 this.gridTemplateContext.copyIsHoldable(row.copy) : ''
205         };
206
207         this.gridTemplateContext = {
208             toggleExpandRow: (row: HoldingsEntry) => {
209                 row.treeNode.expanded = !row.treeNode.expanded;
210
211                 if (!row.treeNode.expanded) {
212                     // When collapsing a node, all child nodes should be
213                     // collapsed as well.
214                     const traverse = (node: HoldingsTreeNode) => {
215                         node.expanded = false;
216                         node.children.forEach(traverse);
217                     };
218                     traverse(row.treeNode);
219                 }
220
221                 this.holdingsGrid.reload();
222             },
223
224             copyIsHoldable: (copy: IdlObject): boolean => {
225                 return copy.holdable() === 't'
226                     && copy.location().holdable() === 't'
227                     && copy.status().holdable() === 't';
228             }
229         };
230     }
231
232     ngOnInit() {
233         this.initDone = true;
234
235         this.broadcaster.listen('eg.holdings.update').subscribe(data => {
236             if (data && data.records && data.records.includes(this.recordId)) {
237                 this.refreshHoldings = true;
238                 this.holdingsGrid.reload();
239             }
240         });
241
242         // These are pre-cached via the catalog resolver.
243         const settings = this.store.getItemBatchCached([
244             'cat.holdings_show_empty_org',
245             'cat.holdings_show_empty',
246             'cat.holdings_show_copies',
247             'cat.holdings_show_vols'
248         ]);
249
250         // Show call numbers by default when no preference is set.
251         let showCallNums = settings['cat.holdings_show_vols'];
252         if (showCallNums === null) { showCallNums = true; }
253
254         this.callNumsCheckbox.checked(showCallNums);
255         this.copiesCheckbox.checked(settings['cat.holdings_show_copies']);
256         this.emptyCallNumsCheckbox.checked(settings['cat.holdings_show_empty']);
257         this.emptyLibsCheckbox.checked(settings['cat.holdings_show_empty_org']);
258
259         this.initHoldingsTree();
260         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
261             return this.fetchHoldings(pager);
262         };
263     }
264
265     contextOrgChanged(org: IdlObject) {
266         this.contextOrg = org;
267         this.hardRefresh();
268     }
269
270     hardRefresh() {
271         this.renderFromPrefs = true;
272         this.refreshHoldings = true;
273         this.initHoldingsTree();
274         this.holdingsGrid.reload();
275     }
276
277     toggleShowCopies(value: boolean) {
278         this.store.setItem('cat.holdings_show_copies', value);
279         if (value) {
280             // Showing copies implies showing call numbers
281             this.callNumsCheckbox.checked(true);
282         }
283         this.renderFromPrefs = true;
284         this.holdingsGrid.reload();
285     }
286
287     toggleShowCallNums(value: boolean) {
288         this.store.setItem('cat.holdings_show_vols', value);
289         if (!value) {
290             // Hiding call numbers implies hiding empty call numbers and copies.
291             this.copiesCheckbox.checked(false);
292             this.emptyCallNumsCheckbox.checked(false);
293         }
294         this.renderFromPrefs = true;
295         this.holdingsGrid.reload();
296     }
297
298     toggleShowEmptyCallNums(value: boolean) {
299         this.store.setItem('cat.holdings_show_empty', value);
300         if (value) {
301             this.callNumsCheckbox.checked(true);
302         }
303         this.renderFromPrefs = true;
304         this.holdingsGrid.reload();
305     }
306
307     toggleShowEmptyLibs(value: boolean) {
308         this.store.setItem('cat.holdings_show_empty_org', value);
309         this.renderFromPrefs = true;
310         this.holdingsGrid.reload();
311     }
312
313     onRowActivate(row: any) {
314         if (row.copy) {
315             // Launch copy editor?
316         } else {
317             this.gridTemplateContext.toggleExpandRow(row);
318         }
319     }
320
321     initHoldingsTree() {
322
323         const visibleOrgs = this.org.fullPath(this.contextOrg, true);
324
325         // The initial tree simply matches the org unit tree
326         const traverseOrg = (node: HoldingsTreeNode) => {
327             node.target.children().forEach((org: IdlObject) => {
328                 if (visibleOrgs.indexOf(org.id()) === -1) {
329                     return; // Org is outside of scope
330                 }
331                 const nodeChild = new HoldingsTreeNode();
332                 nodeChild.nodeType = 'org';
333                 nodeChild.target = org;
334                 nodeChild.parentNode = node;
335                 node.children.push(nodeChild);
336                 this.treeNodeCache.org[org.id()] = nodeChild;
337                 traverseOrg(nodeChild);
338             });
339         };
340
341         this.treeNodeCache = {
342             org: {},
343             callNum: {},
344             copy: {}
345         };
346
347         this.holdingsTree = new HoldingsTree();
348         this.holdingsTree.root.nodeType = 'org';
349         this.holdingsTree.root.target = this.org.root();
350         this.treeNodeCache.org[this.org.root().id()] = this.holdingsTree.root;
351
352         traverseOrg(this.holdingsTree.root);
353     }
354
355     // Org node children are sorted with any child org nodes pushed to the
356     // front, followed by the call number nodes sorted alphabetcially by label.
357     sortOrgNodeChildren(node: HoldingsTreeNode) {
358         node.children = node.children.sort((a, b) => {
359             if (a.nodeType === 'org') {
360                 if (b.nodeType === 'org') {
361                     return a.target.shortname() < b.target.shortname() ? -1 : 1;
362                 } else {
363                     return -1;
364                 }
365             } else if (b.nodeType === 'org') {
366                 return 1;
367             } else {
368                 // TODO: should this use label sortkey instead of
369                 // the compiled call number label?
370                 return a.target._label < b.target._label ? -1 : 1;
371             }
372         });
373     }
374
375     // Sets call number and copy count sums to nodes that need it.
376     // Applies the initial expansed state of each container node.
377     setTreeCounts(node: HoldingsTreeNode) {
378
379         if (node.nodeType === 'org') {
380             node.copyCount = 0;
381             node.callNumCount = 0;
382         } else if (node.nodeType === 'callNum') {
383             node.copyCount = 0;
384         }
385
386         let hasChildOrgWithData = false;
387         let hasChildOrgSansData = false;
388         node.children.forEach(child => {
389             this.setTreeCounts(child);
390             if (node.nodeType === 'org') {
391                 node.copyCount += child.copyCount;
392                 if (child.nodeType === 'callNum') {
393                     node.callNumCount++;
394                 } else {
395                     hasChildOrgWithData = child.callNumCount > 0;
396                     hasChildOrgSansData = child.callNumCount === 0;
397                     node.callNumCount += child.callNumCount;
398                 }
399             } else if (node.nodeType === 'callNum') {
400                 node.copyCount = node.children.length;
401                 if (this.renderFromPrefs) {
402                     node.expanded = this.copiesCheckbox.checked();
403                 }
404             }
405         });
406
407         if (this.renderFromPrefs && node.nodeType === 'org') {
408             if (node.copyCount > 0 && this.callNumsCheckbox.checked()) {
409                 node.expanded = true;
410             } else if (node.callNumCount > 0 && this.emptyCallNumsCheckbox.checked()) {
411                 node.expanded = true;
412             } else if (hasChildOrgWithData) {
413                 node.expanded = true;
414             } else if (hasChildOrgSansData && this.emptyLibsCheckbox.checked()) {
415                 node.expanded = true;
416             } else {
417                 node.expanded = false;
418             }
419         }
420     }
421
422     // Create HoldingsEntry objects for tree nodes that should be displayed
423     // and relays them to the grid via the observer.
424     propagateTreeEntries(observer: Observer<HoldingsEntry>, node: HoldingsTreeNode) {
425         const entry = new HoldingsEntry();
426         entry.treeNode = node;
427         entry.index = this.gridIndex++;
428
429         switch (node.nodeType) {
430             case 'org':
431                 if (node.callNumCount === 0
432                     && !this.emptyLibsCheckbox.checked()) {
433                     return;
434                 }
435                 entry.locationLabel = node.target.shortname();
436                 entry.locationDepth = node.target.ou_type().depth();
437                 entry.copyCount = node.copyCount;
438                 entry.callNumCount = node.callNumCount;
439                 this.sortOrgNodeChildren(node);
440                 break;
441
442             case 'callNum':
443                 if (this.renderFromPrefs) {
444                     if (!this.callNumsCheckbox.checked()) {
445                         return;
446                     }
447                     if (node.copyCount === 0
448                         && !this.emptyCallNumsCheckbox.checked()) {
449                         return;
450                     }
451                 }
452                 entry.locationLabel = node.target._label;
453                 entry.locationDepth = node.parentNode.target.ou_type().depth() + 1;
454                 entry.callNumberLabel = entry.locationLabel;
455                 entry.callNum = node.target;
456                 entry.copyCount = node.copyCount;
457                 break;
458
459             case 'copy':
460                 entry.locationLabel = node.target.barcode();
461                 entry.locationDepth = node.parentNode.parentNode.target.ou_type().depth() + 2;
462                 entry.callNumberLabel = node.parentNode.target.label(); // TODO
463                 entry.callNum = node.parentNode.target;
464                 entry.copy = node.target;
465                 entry.circ = node.target._circ;
466                 break;
467         }
468
469         // Tell the grid about the node entry
470         observer.next(entry);
471
472         if (node.expanded) {
473             // Process the child nodes.
474             node.children.forEach(child =>
475                 this.propagateTreeEntries(observer, child));
476         }
477     }
478
479     // Turns the tree into a list of entries for grid display
480     flattenHoldingsTree(observer: Observer<HoldingsEntry>) {
481         this.gridIndex = 0;
482         this.setTreeCounts(this.holdingsTree.root);
483         this.propagateTreeEntries(observer, this.holdingsTree.root);
484         observer.complete();
485         this.renderFromPrefs = false;
486     }
487
488     // Grab call numbers, copies, and related data.
489     fetchHoldings(pager: Pager): Observable<any> {
490         if (!this.recordId) { return of([]); }
491
492         return new Observable<any>(observer => {
493
494             if (!this.refreshHoldings) {
495                 this.flattenHoldingsTree(observer);
496                 return;
497             }
498
499             this.itemCircsNeeded = [];
500
501             this.pcrud.search('acn',
502                 {   record: this.recordId,
503                     owning_lib: this.org.fullPath(this.contextOrg, true),
504                     deleted: 'f',
505                     label: {'!=' : '##URI##'}
506                 }, {
507                     flesh: 3,
508                     flesh_fields: {
509                         acp: ['status', 'location', 'circ_lib', 'parts',
510                             'age_protect', 'copy_alerts', 'latest_inventory'],
511                         acn: ['prefix', 'suffix', 'copies'],
512                         acli: ['inventory_workstation']
513                     }
514                 },
515                 {authoritative: true}
516             ).subscribe(
517                 callNum => this.appendCallNum(callNum),
518                 err => {},
519                 ()  => {
520                     this.refreshHoldings = false;
521                     this.fetchCircs().then(
522                         ok => this.flattenHoldingsTree(observer)
523                     );
524                 }
525             );
526         });
527     }
528
529     // Retrieve circulation objects for checked out items.
530     fetchCircs(): Promise<any> {
531         const copyIds = this.itemCircsNeeded.map(copy => copy.id());
532         if (copyIds.length === 0) { return Promise.resolve(); }
533
534         return this.pcrud.search('circ', {
535             target_copy: copyIds,
536             checkin_time: null
537         }).pipe(map(circ => {
538             const copy = this.itemCircsNeeded.filter(
539                 c => Number(c.id()) === Number(circ.target_copy()))[0];
540             copy._circ = circ;
541         })).toPromise();
542     }
543
544     // Compile prefix + label + suffix into field callNum._label;
545     setCallNumLabel(callNum: IdlObject) {
546         const pfx = callNum.prefix() ? callNum.prefix().label() : '';
547         const sfx = callNum.suffix() ? callNum.suffix().label() : '';
548         callNum._label = pfx ? pfx + ' ' : '';
549         callNum._label += callNum.label();
550         callNum._label += sfx ? ' ' + sfx : '';
551     }
552
553     // Create the tree node for the call number if it doesn't already exist.
554     // Do the same for its linked copies.
555     appendCallNum(callNum: IdlObject) {
556         let callNumNode = this.treeNodeCache.callNum[callNum.id()];
557         this.setCallNumLabel(callNum);
558
559         if (callNumNode) {
560             const pNode = this.treeNodeCache.org[callNum.owning_lib()];
561             if (callNumNode.parentNode.target.id() !== pNode.target.id()) {
562                 // Call number owning library changed.  Un-link it from the
563                 // previous org unit collection before adding to the new one.
564                 // XXX TODO: ^--
565                 callNumNode.parentNode = pNode;
566                 callNumNode.parentNode.children.push(callNumNode);
567             }
568         } else {
569             callNumNode = new HoldingsTreeNode();
570             callNumNode.nodeType = 'callNum';
571             callNumNode.parentNode = this.treeNodeCache.org[callNum.owning_lib()];
572             callNumNode.parentNode.children.push(callNumNode);
573             this.treeNodeCache.callNum[callNum.id()] = callNumNode;
574         }
575
576         callNumNode.target = callNum;
577
578         callNum.copies()
579             .filter((copy: IdlObject) => (copy.deleted() !== 't'))
580             .sort((a: IdlObject, b: IdlObject) => a.barcode() < b.barcode() ? -1 : 1)
581             .forEach((copy: IdlObject) => this.appendCopy(callNumNode, copy));
582     }
583
584     // Find or create a copy node.
585     appendCopy(callNumNode: HoldingsTreeNode, copy: IdlObject) {
586         let copyNode = this.treeNodeCache.copy[copy.id()];
587
588         if (copyNode) {
589             const oldParent = copyNode.parentNode;
590             if (oldParent.target.id() !== callNumNode.target.id()) {
591                 // TODO: copy changed owning call number.  Remove it from
592                 // the previous call number before adding to the new call number.
593                 copyNode.parentNode = callNumNode;
594                 callNumNode.children.push(copyNode);
595             }
596         } else {
597             // New node required
598             copyNode = new HoldingsTreeNode();
599             copyNode.nodeType = 'copy';
600             callNumNode.children.push(copyNode);
601             copyNode.parentNode = callNumNode;
602             this.treeNodeCache.copy[copy.id()] = copyNode;
603         }
604
605         copyNode.target = copy;
606         const stat = Number(copy.status().id());
607
608         if (stat === 1 /* checked out */ || stat === 16 /* long overdue */) {
609             // Avoid looking up circs on items that are not checked out.
610             this.itemCircsNeeded.push(copy);
611         }
612     }
613
614     // Which copies in the grid are selected.
615     selectedCopyIds(rows: HoldingsEntry[], skipStatus?: number): number[] {
616         return this.selectedCopies(rows, skipStatus).map(c => Number(c.id()));
617     }
618
619     selectedVolIds(rows: HoldingsEntry[]): number[] {
620         return rows
621             .filter(r => Boolean(r.callNum))
622             .map(r => Number(r.callNum.id()));
623     }
624
625     selectedCopies(rows: HoldingsEntry[], skipStatus?: number): IdlObject[] {
626         let copyRows = rows.filter(r => Boolean(r.copy)).map(r => r.copy);
627         if (skipStatus) {
628             copyRows = copyRows.filter(
629                 c => Number(c.status().id()) !== Number(skipStatus));
630         }
631         return copyRows;
632     }
633
634     selectedCallNumIds(rows: HoldingsEntry[]): number[] {
635         return this.selectedCallNums(rows).map(cn => cn.id());
636     }
637
638     selectedCallNums(rows: HoldingsEntry[]): IdlObject[] {
639         return rows
640             .filter(r => r.treeNode.nodeType === 'callNum')
641             .map(r => r.callNum);
642     }
643
644
645     async showMarkDamagedDialog(rows: HoldingsEntry[]) {
646         const copyIds = this.selectedCopyIds(rows, 14 /* ignore damaged */);
647
648         if (copyIds.length === 0) { return; }
649
650         let rowsModified = false;
651
652         const markNext = async(ids: number[]) => {
653             if (ids.length === 0) {
654                 return Promise.resolve();
655             }
656
657             this.markDamagedDialog.copyId = ids.pop();
658             return this.markDamagedDialog.open({size: 'lg'}).subscribe(
659                 ok => {
660                     if (ok) { rowsModified = true; }
661                     return markNext(ids);
662                 },
663                 dismiss => markNext(ids)
664             );
665         };
666
667         await markNext(copyIds);
668         if (rowsModified) {
669             this.refreshHoldings = true;
670             this.holdingsGrid.reload();
671         }
672     }
673
674     showMarkMissingDialog(rows: any[]) {
675         const copyIds = this.selectedCopyIds(rows, 4 /* ignore missing */);
676         if (copyIds.length > 0) {
677             this.markMissingDialog.copyIds = copyIds;
678             this.markMissingDialog.open({}).subscribe(
679                 rowsModified => {
680                     if (rowsModified) {
681                         this.refreshHoldings = true;
682                         this.holdingsGrid.reload();
683                     }
684                 },
685                 dismissed => {} // avoid console errors
686             );
687         }
688     }
689
690     // Mark record, library, and potentially the selected call number
691     // as the current transfer target.
692     markLibCnForTransfer(rows: HoldingsEntry[]) {
693         if (rows.length === 0) {
694             return;
695         }
696
697         // Action may only apply to a single org or call number row.
698         const node = rows[0].treeNode;
699         if (node.nodeType === 'copy') { return; }
700
701         let orgId: number;
702
703         if (node.nodeType === 'org') {
704             orgId = node.target.id();
705
706             // Clear call number target when performed on an org unit row
707             this.localStore.removeLocalItem('eg.cat.transfer_target_vol');
708
709         } else if (node.nodeType === 'callNum') {
710
711             // All call number nodes are children of org nodes.
712             orgId = node.parentNode.target.id();
713
714             // Add call number target when performed on a call number row.
715             this.localStore.setLocalItem(
716                 'eg.cat.transfer_target_vol', node.target.id());
717         }
718
719         // Track lib and record to support transfering items from
720         // a different bib record to this record at the selected
721         // owning lib.
722         this.localStore.setLocalItem('eg.cat.transfer_target_lib', orgId);
723         this.localStore.setLocalItem('eg.cat.transfer_target_record', this.recordId);
724     }
725
726     openAngJsWindow(path: string) {
727         const url = `/eg/staff/${path}`;
728         window.open(url, '_blank');
729     }
730
731     openItemHolds(rows: HoldingsEntry[]) {
732         if (rows.length > 0 && rows[0].copy) {
733             this.openAngJsWindow(`cat/item/${rows[0].copy.id()}/holds`);
734         }
735     }
736
737     openItemStatusList(rows: HoldingsEntry[]) {
738         const ids = this.selectedCopyIds(rows);
739         if (ids.length > 0) {
740             return this.openAngJsWindow(`cat/item/search/${ids.join(',')}`);
741         }
742     }
743
744     openItemStatus(rows: HoldingsEntry[]) {
745         if (rows.length > 0 && rows[0].copy) {
746            return this.openAngJsWindow(`cat/item/${rows[0].copy.id()}`);
747         }
748     }
749
750     openItemTriggeredEvents(rows: HoldingsEntry[]) {
751         if (rows.length > 0 && rows[0].copy) {
752            return this.openAngJsWindow(
753                `cat/item/${rows[0].copy.id()}/triggered_events`);
754         }
755     }
756
757     openItemPrintLabels(rows: HoldingsEntry[]) {
758         const ids = this.selectedCopyIds(rows);
759         if (ids.length === 0) { return; }
760
761         this.anonCache.setItem(null, 'print-labels-these-copies', {copies: ids})
762         .then(key => this.openAngJsWindow(`cat/printlabels/${key}`));
763     }
764
765     openHoldingEdit(rows: HoldingsEntry[], hideVols: boolean, hideCopies: boolean) {
766
767         // Avoid adding call number edit entries for call numbers
768         // that are already represented by selected items.
769
770         const copies = this.selectedCopies(rows);
771         const copyVols = copies.map(c => Number(c.call_number()));
772
773         const volIds = [];
774         this.selectedVolIds(rows).forEach(id => {
775             if (!copyVols.includes(id)) {
776                 volIds.push(id);
777             }
778         });
779
780         this.holdings.spawnAddHoldingsUi(
781             this.recordId,
782             volIds,
783             null,
784             copies.map(c => Number(c.id())),
785             hideCopies,
786             hideVols
787         );
788     }
789
790     openHoldingAdd(rows: HoldingsEntry[], addCallNums: boolean, addCopies: boolean) {
791
792         // The user may select a set of call numbers by selecting call
793         // number and/or item rows.  Owning libs for new call numbers may
794         // also come from org unit row selection.
795         const orgs = {};
796         const callNums = [];
797         rows.forEach(r => {
798             if (r.treeNode.nodeType === 'callNum') {
799                 callNums.push(r.callNum);
800
801             } else if (r.treeNode.nodeType === 'copy') {
802                 callNums.push(r.treeNode.parentNode.target);
803
804             } else if (r.treeNode.nodeType === 'org') {
805                 const org = r.treeNode.target;
806                 if (org.ou_type().can_have_vols() === 't') {
807                     orgs[org.id()] = true;
808                 }
809             }
810         });
811
812         if (addCopies && !addCallNums) {
813             // Adding copies to an existing set of call numbers.
814             if (callNums.length > 0) {
815                 const callNumIds = callNums.map(v => Number(v.id()));
816                 this.holdings.spawnAddHoldingsUi(this.recordId, callNumIds);
817             }
818
819         } else if (addCallNums) {
820             const entries = [];
821
822             // Use selected call numbers as basis for new call numbers.
823             callNums.forEach(v =>
824                 entries.push({label: v.label(), owner: v.owning_lib()}));
825
826             // Use selected org units as owning libs for new call numbers
827             Object.keys(orgs).forEach(id => entries.push({owner: id}));
828
829             if (entries.length === 0) {
830                 // Otherwise create new call numbers for "here"
831                 entries.push({owner: this.auth.user().ws_ou()});
832             }
833
834             this.holdings.spawnAddHoldingsUi(
835                 this.recordId, null, entries, null, !addCopies);
836         }
837     }
838
839     openItemNotes(rows: HoldingsEntry[], mode: string) {
840         const copyIds = this.selectedCopyIds(rows);
841         if (copyIds.length === 0) { return; }
842
843         this.copyAlertsDialog.copyIds = copyIds;
844         this.copyAlertsDialog.mode = mode;
845         this.copyAlertsDialog.open({size: 'lg'}).subscribe(
846             modified => {
847                 if (modified) {
848                     this.hardRefresh();
849                 }
850             }
851         );
852     }
853
854     openReplaceBarcodeDialog(rows: HoldingsEntry[]) {
855         const ids = this.selectedCopyIds(rows);
856         if (ids.length === 0) { return; }
857         this.replaceBarcode.copyIds = ids;
858         this.replaceBarcode.open({}).subscribe(
859             modified => {
860                 if (modified) {
861                     this.hardRefresh();
862                 }
863             }
864         );
865     }
866
867     // mode 'callNums' -- only delete empty call numbers
868     // mode 'copies' -- only delete selected copies
869     // mode 'both' -- delete selected copies and selected call numbers, plus all
870     // copies linked to selected call numbers, regardless of whether they are selected.
871     deleteHoldings(rows: HoldingsEntry[], mode: 'callNums' | 'copies' | 'both') {
872         const callNumHash: any = {};
873
874         if (mode === 'callNums' || mode === 'both') {
875             // Collect the call numbers to be deleted.
876             rows.filter(r => r.treeNode.nodeType === 'callNum').forEach(r => {
877                 const callNum = this.idl.clone(r.callNum);
878                 if (mode === 'callNums') {
879                     if (callNum.copies().length > 0) {
880                         // cannot delete non-empty call number in this mode.
881                         return;
882                     }
883                 } else {
884                     callNum.copies().forEach(c => c.isdeleted(true));
885                 }
886                 callNum.isdeleted(true);
887                 callNumHash[callNum.id()] = callNum;
888             });
889         }
890
891         if (mode === 'copies' || mode === 'both') {
892             // Collect the copies to be deleted, including their call numbers
893             // since the API expects fleshed call number objects.
894             rows.filter(r => r.treeNode.nodeType === 'copy').forEach(r => {
895                 const callNum = r.treeNode.parentNode.target;
896                 if (!callNumHash[callNum.id()]) {
897                     callNumHash[callNum.id()] = this.idl.clone(callNum);
898                     callNumHash[callNum.id()].copies([]);
899                 }
900                 const copy = this.idl.clone(r.copy);
901                 copy.isdeleted(true);
902                 callNumHash[callNum.id()].copies().push(copy);
903             });
904         }
905
906         if (Object.keys(callNumHash).length === 0) {
907             // No data to process.
908             return;
909         }
910
911         // Note forceDeleteCopies should not be necessary here, since we
912         // manually marked all copies as deleted on deleted call numbers in
913         // "both" mode.
914         this.deleteHolding.forceDeleteCopies = mode === 'both';
915         this.deleteHolding.callNums = Object.values(callNumHash);
916         this.deleteHolding.open({size: 'sm'}).subscribe(
917             modified => {
918                 if (modified) {
919                     this.hardRefresh();
920                 }
921             }
922         );
923     }
924
925     requestItems(rows: HoldingsEntry[]) {
926         const copyIds = this.selectedCopyIds(rows);
927         if (copyIds.length === 0) { return; }
928         const params = {target: copyIds, holdFor: 'staff'};
929         this.router.navigate(['/staff/catalog/hold/C'], {queryParams: params});
930     }
931
932     openBucketDialog(rows: HoldingsEntry[]) {
933         const copyIds = this.selectedCopyIds(rows);
934         if (copyIds.length > 0) {
935             this.bucketDialog.bucketClass = 'copy';
936             this.bucketDialog.itemIds = copyIds;
937             this.bucketDialog.open({size: 'lg'});
938         }
939     }
940
941     openConjoinedDialog(rows: HoldingsEntry[]) {
942         const copyIds = this.selectedCopyIds(rows);
943         if (copyIds.length > 0) {
944             this.conjoinedDialog.copyIds = copyIds;
945             this.conjoinedDialog.open({size: 'sm'});
946         }
947     }
948
949     bookItems(rows: HoldingsEntry[]) {
950         const copyIds = this.selectedCopyIds(rows);
951         if (copyIds.length > 0) {
952             this.router.navigate(
953                 ['staff', 'booking', 'create_reservation', 'for_resource', rows.filter(r => Boolean(r.copy))[0].copy.barcode()]
954             );
955         }
956     }
957
958     makeBookable(rows: HoldingsEntry[]) {
959         const copyIds = this.selectedCopyIds(rows);
960         if (copyIds.length > 0) {
961             this.makeBookableDialog.copyIds = copyIds;
962             this.makeBookableDialog.open({});
963         }
964     }
965
966     manageReservations(rows: HoldingsEntry[]) {
967         const copyIds = this.selectedCopyIds(rows);
968         if (copyIds.length > 0) {
969             this.router.navigate(
970                 ['staff', 'booking', 'manage_reservations', 'by_resource', rows.filter(r => Boolean(r.copy))[0].copy.barcode()]
971             );
972         }
973     }
974
975     transferSelectedItems(rows: HoldingsEntry[]) {
976         if (rows.length === 0) { return; }
977
978         const cnId =
979             this.localStore.getLocalItem('eg.cat.transfer_target_vol');
980
981         const orgId =
982             this.localStore.getLocalItem('eg.cat.transfer_target_lib');
983
984         const recId =
985             this.localStore.getLocalItem('eg.cat.transfer_target_record');
986
987         let promise;
988
989         if (cnId) { // Direct call number transfer
990
991             const itemIds = this.selectedCopyIds(rows);
992             promise = this.transferItems.transferItems(itemIds, cnId);
993
994         } else if (orgId && recId) { // "Auto" transfer
995
996             // Clone the items to be modified to avoid any unexpected
997             // modifications and fesh the call numbers.
998             const items = this.idl.clone(this.selectedCopies(rows));
999             items.forEach(i => i.call_number(
1000                 this.treeNodeCache.callNum[i.call_number()].target));
1001
1002             console.log(items);
1003             promise = this.transferItems.autoTransferItems(items, recId, orgId);
1004
1005         } else {
1006             promise = this.transferAlert.open().toPromise();
1007         }
1008
1009         promise.then(success => success ?  this.hardRefresh() : null);
1010     }
1011
1012     transferSelectedHoldings(rows: HoldingsEntry[]) {
1013         const callNums = this.selectedCallNums(rows);
1014         if (callNums.length === 0) { return; }
1015
1016         const orgId =
1017             this.localStore.getLocalItem('eg.cat.transfer_target_lib');
1018
1019         let recId =
1020             this.localStore.getLocalItem('eg.cat.transfer_target_record');
1021
1022         if (orgId) {
1023             // When transferring holdings (call numbers) between org units,
1024             // limit transfers to within the current record.
1025             recId = this.recordId;
1026
1027         } else if (!recId) {
1028             // No destinations applied.
1029             return this.transferAlert.open();
1030         }
1031
1032         this.transferHoldings.targetRecId = recId;
1033         this.transferHoldings.targetOrgId = orgId;
1034         this.transferHoldings.callNums = callNums;
1035
1036         this.transferHoldings.transferHoldings()
1037         .then(success => success ?  this.hardRefresh() : null);
1038     }
1039 }
1040