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