]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/copies.component.ts
LP1835982 Grid cell text generator API migration
[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
11 @Component({
12   selector: 'eg-catalog-copies',
13   templateUrl: 'copies.component.html'
14 })
15 export class CopiesComponent implements OnInit {
16
17     recId: number;
18     initDone = false;
19     gridDataSource: GridDataSource;
20     copyContext: any; // grid context
21     @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
22
23     @Input() set recordId(id: number) {
24         this.recId = id;
25         // Only force new data collection when recordId()
26         // is invoked after ngInit() has already run.
27         if (this.initDone) {
28             this.copyGrid.reload();
29         }
30     }
31
32     cellTextGenerator: GridCellTextGenerator;
33
34     constructor(
35         private net: NetService,
36         private org: OrgService,
37         private staffCat: StaffCatalogService,
38     ) {
39         this.gridDataSource = new GridDataSource();
40     }
41
42     ngOnInit() {
43         this.initDone = true;
44
45         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
46             // sorting not currently supported
47             return this.fetchCopies(pager);
48         };
49
50         this.copyContext = {
51             holdable: (copy: any) => {
52                 return copy.holdable === 't'
53                     && copy.location_holdable === 't'
54                     && copy.status_holdable === 't';
55             }
56         };
57
58         this.cellTextGenerator = {
59             callnumber: row => `${row.call_number_prefix_label} ` +
60                 `${row.call_number_label} ${row.call_number_suffix_label}`,
61             holdable: row => this.copyContext.holdable(row),
62             barcode: row => row.barcode
63         };
64     }
65
66     collectData() {
67         if (!this.recId) { return; }
68     }
69
70     orgName(orgId: number): string {
71         return this.org.get(orgId).shortname();
72     }
73
74     fetchCopies(pager: Pager): Observable<any> {
75         if (!this.recId) { return of([]); }
76
77         // "Show Result from All Libraries" i.e. global search displays
78         // copies from all branches, sorted by search/pref libs.
79         const copy_depth = this.staffCat.searchContext.global ?
80             this.org.root().ou_type().depth() :
81             this.staffCat.searchContext.searchOrg.ou_type().depth();
82
83         return this.net.request(
84             'open-ils.search',
85             'open-ils.search.bib.copies.staff',
86             this.recId,
87             this.staffCat.searchContext.searchOrg.id(),
88             copy_depth,
89             pager.limit,
90             pager.offset,
91             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
92         ).pipe(map(copy => {
93             copy.active_date = copy.active_date || copy.create_date;
94             return copy;
95         }));
96     }
97 }
98
99