]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/copies.component.ts
LP1835982 Grid cell print values option
[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} 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     cellPrintValues: (row: any, cell: GridColumn) => string;
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         // Text-ify function for cells that use display templates.
59         this.cellPrintValues = (row: any, cell: GridColumn): string => {
60             switch (cell.name) {
61                 case 'callnumber':
62                     return `${row.call_number_prefix_label} ` +
63                         `${row.call_number_label} ${row.call_number_suffix_label}`;
64                 case 'holdable':
65                     return this.copyContext.holdable(row);
66                 case 'barcode':
67                     return row.barcode;
68             }
69         };
70     }
71
72     collectData() {
73         if (!this.recId) { return; }
74     }
75
76     orgName(orgId: number): string {
77         return this.org.get(orgId).shortname();
78     }
79
80     fetchCopies(pager: Pager): Observable<any> {
81         if (!this.recId) { return of([]); }
82
83         // "Show Result from All Libraries" i.e. global search displays
84         // copies from all branches, sorted by search/pref libs.
85         const copy_depth = this.staffCat.searchContext.global ?
86             this.org.root().ou_type().depth() :
87             this.staffCat.searchContext.searchOrg.ou_type().depth();
88
89         return this.net.request(
90             'open-ils.search',
91             'open-ils.search.bib.copies.staff',
92             this.recId,
93             this.staffCat.searchContext.searchOrg.id(),
94             copy_depth,
95             pager.limit,
96             pager.offset,
97             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
98         ).pipe(map(copy => {
99             copy.active_date = copy.active_date || copy.create_date;
100             return copy;
101         }));
102     }
103 }
104
105