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;
37 openHoldingsEditor: (item: number) => void;
40 private course: CourseService,
41 private net: NetService,
42 private org: OrgService,
43 private staffCat: StaffCatalogService,
44 private broadcaster: BroadcastService
46 this.gridDataSource = new GridDataSource();
51 this.course.isOptedIn().then(res => {
52 this.usingCourseModule = res;
55 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
56 // sorting not currently supported
57 return this.fetchCopies(pager);
61 holdable: (copy: any) => {
62 return copy.holdable === 't'
63 && copy.location_holdable === 't'
64 && copy.status_holdable === 't';
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
75 this.broadcaster.listen('eg.holdings.update').subscribe(data => {
76 if (data && data.records && data.records.includes(this.recId)) {
77 this.copyGrid.reload();
81 this.openHoldingsEditor = (item: number) => {
82 window.open('/eg/staff/cat/item/' + item + '/edit', '_blank');
87 orgName(orgId: number): string {
88 return this.org.get(orgId).shortname();
91 fetchCopies(pager: Pager): Observable<any> {
92 if (!this.recId) { return of([]); }
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();
100 return this.net.request(
102 'open-ils.search.bib.copies.staff',
104 this.staffCat.searchContext.searchOrg.id(),
108 this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
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;
117 copy.active_date = copy.active_date || copy.create_date;