Lp 1913219: Use window.open for staff catalog edit link
[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     constructor(
38         private course: CourseService,
39         private net: NetService,
40         private org: OrgService,
41         private staffCat: StaffCatalogService,
42         private broadcaster: BroadcastService
43     ) {
44         this.gridDataSource = new GridDataSource();
45     }
46
47     ngOnInit() {
48         this.initDone = true;
49         this.course.isOptedIn().then(res => {
50             this.usingCourseModule = res;
51         });
52
53         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
54             // sorting not currently supported
55             return this.fetchCopies(pager);
56         };
57
58         this.copyContext = {
59             holdable: (copy: any) => {
60                 return copy.holdable === 't'
61                     && copy.location_holdable === 't'
62                     && copy.status_holdable === 't';
63             }
64         };
65
66         this.cellTextGenerator = {
67             callnumber: row => (`${row.call_number_prefix_label} ` +
68                 `${row.call_number_label} ${row.call_number_suffix_label}`).trim(),
69             holdable: row => this.copyContext.holdable(row),
70             barcode: row => row.barcode
71         };
72
73         this.broadcaster.listen('eg.holdings.update').subscribe(data => {
74             if (data && data.records && data.records.includes(this.recId)) {
75                 this.copyGrid.reload();
76             }
77         });
78     }
79
80     orgName(orgId: number): string {
81         return this.org.get(orgId).shortname();
82     }
83
84     fetchCopies(pager: Pager): Observable<any> {
85         if (!this.recId) { return of([]); }
86
87         // "Show Result from All Libraries" i.e. global search displays
88         // copies from all branches, sorted by search/pref libs.
89         const copy_depth = this.staffCat.searchContext.global ?
90             this.org.root().ou_type().depth() :
91             this.staffCat.searchContext.searchOrg.ou_type().depth();
92
93         return this.net.request(
94             'open-ils.search',
95             'open-ils.search.bib.copies.staff',
96             this.recId,
97             this.staffCat.searchContext.searchOrg.id(),
98             copy_depth,
99             pager.limit,
100             pager.offset,
101             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
102         ).pipe(map(copy => {
103             this.org.settings('circ.course_materials_opt_in').then(res => {
104                 if (res['circ.course_materials_opt_in']) {
105                     this.course.getCoursesFromMaterial(copy.id).then(courseList => {
106                         copy._courses = courseList;
107                     });
108                 }
109             });
110             copy.active_date = copy.active_date || copy.create_date;
111             return copy;
112         }));
113     }
114 }
115
116