1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, of} from 'rxjs';
3 import {map} from 'rxjs/operators';
4 import {NetService} from '@eg/core/net.service';
5 import {StaffCatalogService} from '../catalog.service';
6 import {Pager} from '@eg/share/util/pager';
7 import {OrgService} from '@eg/core/org.service';
8 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {BroadcastService} from '@eg/share/util/broadcast.service';
13 selector: 'eg-catalog-copies',
14 templateUrl: 'copies.component.html'
16 export class CopiesComponent implements OnInit {
20 gridDataSource: GridDataSource;
21 copyContext: any; // grid context
22 @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
24 @Input() set recordId(id: number) {
26 // Only force new data collection when recordId()
27 // is invoked after ngInit() has already run.
29 this.copyGrid.reload();
33 cellTextGenerator: GridCellTextGenerator;
36 private net: NetService,
37 private org: OrgService,
38 private staffCat: StaffCatalogService,
39 private broadcaster: BroadcastService
41 this.gridDataSource = new GridDataSource();
47 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
48 // sorting not currently supported
49 return this.fetchCopies(pager);
53 holdable: (copy: any) => {
54 return copy.holdable === 't'
55 && copy.location_holdable === 't'
56 && copy.status_holdable === 't';
60 this.cellTextGenerator = {
61 callnumber: row => (`${row.call_number_prefix_label} ` +
62 `${row.call_number_label} ${row.call_number_suffix_label}`).trim(),
63 holdable: row => this.copyContext.holdable(row),
64 barcode: row => row.barcode
67 this.broadcaster.listen('eg.holdings.update').subscribe(data => {
68 if (data && data.records && data.records.includes(this.recId)) {
69 this.copyGrid.reload();
74 orgName(orgId: number): string {
75 return this.org.get(orgId).shortname();
78 fetchCopies(pager: Pager): Observable<any> {
79 if (!this.recId) { return of([]); }
81 // "Show Result from All Libraries" i.e. global search displays
82 // copies from all branches, sorted by search/pref libs.
83 const copy_depth = this.staffCat.searchContext.global ?
84 this.org.root().ou_type().depth() :
85 this.staffCat.searchContext.searchOrg.ou_type().depth();
87 return this.net.request(
89 'open-ils.search.bib.copies.staff',
91 this.staffCat.searchContext.searchOrg.id(),
95 this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
97 copy.active_date = copy.active_date || copy.create_date;