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';
14 selector: 'eg-catalog-copies',
15 templateUrl: 'copies.component.html'
17 export class CopiesComponent implements OnInit {
21 usingCourseModule = false;
22 gridDataSource: GridDataSource;
23 copyContext: any; // grid context
24 @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
26 @Input() set recordId(id: number) {
28 // Only force new data collection when recordId()
29 // is invoked after ngInit() has already run.
31 this.copyGrid.reload();
35 cellTextGenerator: GridCellTextGenerator;
38 private course: CourseService,
39 private net: NetService,
40 private org: OrgService,
41 private staffCat: StaffCatalogService,
42 private broadcaster: BroadcastService
44 this.gridDataSource = new GridDataSource();
49 this.course.isOptedIn().then(res => {
50 this.usingCourseModule = res;
53 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
54 // sorting not currently supported
55 return this.fetchCopies(pager);
59 holdable: (copy: any) => {
60 return copy.holdable === 't'
61 && copy.location_holdable === 't'
62 && copy.status_holdable === 't';
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
73 this.broadcaster.listen('eg.holdings.update').subscribe(data => {
74 if (data && data.records && data.records.includes(this.recId)) {
75 this.copyGrid.reload();
80 orgName(orgId: number): string {
81 return this.org.get(orgId).shortname();
84 fetchCopies(pager: Pager): Observable<any> {
85 if (!this.recId) { return of([]); }
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();
93 return this.net.request(
95 'open-ils.search.bib.copies.staff',
97 this.staffCat.searchContext.searchOrg.id(),
101 this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
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;
110 copy.active_date = copy.active_date || copy.create_date;