LP#1801984 Upgrading Angular 6 to Angular 7
[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} 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') 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     constructor(
33         private net: NetService,
34         private org: OrgService,
35         private staffCat: StaffCatalogService,
36     ) {
37         this.gridDataSource = new GridDataSource();
38     }
39
40     ngOnInit() {
41         this.initDone = true;
42
43         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
44             // sorting not currently supported
45             return this.fetchCopies(pager);
46         };
47
48         this.copyContext = {
49             holdable: (copy: any) => {
50                 return copy.holdable === 't'
51                     && copy.location_holdable === 't'
52                     && copy.status_holdable === 't';
53             }
54         };
55     }
56
57     collectData() {
58         if (!this.recId) { return; }
59     }
60
61     orgName(orgId: number): string {
62         return this.org.get(orgId).shortname();
63     }
64
65     fetchCopies(pager: Pager): Observable<any> {
66         if (!this.recId) { return of([]); }
67
68         // "Show Result from All Libraries" i.e. global search displays
69         // copies from all branches, sorted by search/pref libs.
70         const copy_depth = this.staffCat.searchContext.global ?
71             this.org.root().ou_type().depth() :
72             this.staffCat.searchContext.searchOrg.ou_type().depth();
73
74         return this.net.request(
75             'open-ils.search',
76             'open-ils.search.bib.copies.staff',
77             this.recId,
78             this.staffCat.searchContext.searchOrg.id(),
79             copy_depth,
80             pager.limit,
81             pager.offset,
82             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
83         ).pipe(map(copy => {
84             copy.active_date = copy.active_date || copy.create_date;
85             return copy;
86         }));
87     }
88 }
89
90