Lp 1913219: Use window.open for staff catalog edit link; angular version
[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 import {CourseService} from '@eg/staff/share/course.service';
12
13 @Component({
14   selector: 'eg-catalog-copies',
15   templateUrl: 'copies.component.html'
16 })
17 export class CopiesComponent implements OnInit {
18
19     recId: number;
20     initDone = false;
21     usingCourseModule = false;
22     gridDataSource: GridDataSource;
23     copyContext: any; // grid context
24     @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
25
26     @Input() set recordId(id: number) {
27         this.recId = id;
28         // Only force new data collection when recordId()
29         // is invoked after ngInit() has already run.
30         if (this.initDone) {
31             this.copyGrid.reload();
32         }
33     }
34
35     cellTextGenerator: GridCellTextGenerator;
36
37     openHoldingsEditor: (item: number) => void;
38
39     constructor(
40         private course: CourseService,
41         private net: NetService,
42         private org: OrgService,
43         private staffCat: StaffCatalogService,
44         private broadcaster: BroadcastService
45     ) {
46         this.gridDataSource = new GridDataSource();
47     }
48
49     ngOnInit() {
50         this.initDone = true;
51         this.course.isOptedIn().then(res => {
52             this.usingCourseModule = res;
53         });
54
55         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
56             // sorting not currently supported
57             return this.fetchCopies(pager);
58         };
59
60         this.copyContext = {
61             holdable: (copy: any) => {
62                 return copy.holdable === 't'
63                     && copy.location_holdable === 't'
64                     && copy.status_holdable === 't';
65             }
66         };
67
68         this.cellTextGenerator = {
69             callnumber: row => (`${row.call_number_prefix_label} ` +
70                 `${row.call_number_label} ${row.call_number_suffix_label}`).trim(),
71             holdable: row => this.copyContext.holdable(row),
72             barcode: row => row.barcode
73         };
74
75         this.broadcaster.listen('eg.holdings.update').subscribe(data => {
76             if (data && data.records && data.records.includes(this.recId)) {
77                 this.copyGrid.reload();
78             }
79         });
80
81         this.openHoldingsEditor = (item: number) => {
82             window.open('/eg/staff/cat/item/' + item + '/edit', '_blank');
83         }
84
85     }
86
87     orgName(orgId: number): string {
88         return this.org.get(orgId).shortname();
89     }
90
91     fetchCopies(pager: Pager): Observable<any> {
92         if (!this.recId) { return of([]); }
93
94         // "Show Result from All Libraries" i.e. global search displays
95         // copies from all branches, sorted by search/pref libs.
96         const copy_depth = this.staffCat.searchContext.global ?
97             this.org.root().ou_type().depth() :
98             this.staffCat.searchContext.searchOrg.ou_type().depth();
99
100         return this.net.request(
101             'open-ils.search',
102             'open-ils.search.bib.copies.staff',
103             this.recId,
104             this.staffCat.searchContext.searchOrg.id(),
105             copy_depth,
106             pager.limit,
107             pager.offset,
108             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
109         ).pipe(map(copy => {
110             this.org.settings('circ.course_materials_opt_in').then(res => {
111                 if (res['circ.course_materials_opt_in']) {
112                     this.course.getCoursesFromMaterial(copy.id).then(courseList => {
113                         copy._courses = courseList;
114                     });
115                 }
116             });
117             copy.active_date = copy.active_date || copy.create_date;
118             return copy;
119         }));
120     }
121 }
122
123