]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/copies.component.ts
9e288ee61d47f2ee8dfadd7590011c310bc70b1a
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / copies.component.ts
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';
11
12 @Component({
13   selector: 'eg-catalog-copies',
14   templateUrl: 'copies.component.html'
15 })
16 export class CopiesComponent implements OnInit {
17
18     recId: number;
19     initDone = false;
20     gridDataSource: GridDataSource;
21     copyContext: any; // grid context
22     @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
23
24     @Input() set recordId(id: number) {
25         this.recId = id;
26         // Only force new data collection when recordId()
27         // is invoked after ngInit() has already run.
28         if (this.initDone) {
29             this.copyGrid.reload();
30         }
31     }
32
33     cellTextGenerator: GridCellTextGenerator;
34
35     constructor(
36         private net: NetService,
37         private org: OrgService,
38         private staffCat: StaffCatalogService,
39         private broadcaster: BroadcastService
40     ) {
41         this.gridDataSource = new GridDataSource();
42     }
43
44     ngOnInit() {
45         this.initDone = true;
46
47         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
48             // sorting not currently supported
49             return this.fetchCopies(pager);
50         };
51
52         this.copyContext = {
53             holdable: (copy: any) => {
54                 return copy.holdable === 't'
55                     && copy.location_holdable === 't'
56                     && copy.status_holdable === 't';
57             }
58         };
59
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
65         };
66
67         this.broadcaster.listen('eg.holdings.update').subscribe(data => {
68             if (data && data.records && data.records.includes(this.recId)) {
69                 this.copyGrid.reload();
70             }
71         });
72     }
73
74     orgName(orgId: number): string {
75         return this.org.get(orgId).shortname();
76     }
77
78     fetchCopies(pager: Pager): Observable<any> {
79         if (!this.recId) { return of([]); }
80
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();
86
87         return this.net.request(
88             'open-ils.search',
89             'open-ils.search.bib.copies.staff',
90             this.recId,
91             this.staffCat.searchContext.searchOrg.id(),
92             copy_depth,
93             pager.limit,
94             pager.offset,
95             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
96         ).pipe(map(copy => {
97             copy.active_date = copy.active_date || copy.create_date;
98             return copy;
99         }));
100     }
101 }
102
103