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 import {PermService} from '@eg/core/perm.service';
15 selector: 'eg-catalog-copies',
16 templateUrl: 'copies.component.html'
18 export class CopiesComponent implements OnInit {
22 usingCourseModule = false;
23 editableCopyLibs: number[] = [];
24 gridDataSource: GridDataSource;
25 copyContext: any; // grid context
26 @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
28 @Input() set recordId(id: number) {
30 // Only force new data collection when recordId()
31 // is invoked after ngInit() has already run.
33 this.copyGrid.reload();
37 cellTextGenerator: GridCellTextGenerator;
39 openHoldingsEditor: (item: number) => void;
42 private course: CourseService,
43 private net: NetService,
44 private org: OrgService,
45 private staffCat: StaffCatalogService,
46 private broadcaster: BroadcastService,
47 private perm: PermService
49 this.gridDataSource = new GridDataSource();
54 this.course.isOptedIn().then(res => {
55 this.usingCourseModule = res;
58 this.perm.hasWorkPermAt(['UPDATE_COPY'], true)
60 this.editableCopyLibs = result.UPDATE_COPY as number[];
63 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
64 // sorting not currently supported
65 return this.fetchCopies(pager);
69 editable: (copy: any) => {
70 return this.editableCopyLibs.some(lib => {
71 return copy.circ_lib === lib
72 || copy.call_number_owning_lib === lib;
75 holdable: (copy: any) => {
76 return copy.holdable === 't'
77 && copy.location_holdable === 't'
78 && copy.status_holdable === 't';
82 this.cellTextGenerator = {
83 callnumber: row => (`${row.call_number_prefix_label} ` +
84 `${row.call_number_label} ${row.call_number_suffix_label}`).trim(),
85 holdable: row => this.copyContext.holdable(row),
86 barcode: row => row.barcode
89 this.broadcaster.listen('eg.holdings.update').subscribe(data => {
90 if (data && data.records && data.records.includes(this.recId)) {
91 this.copyGrid.reload();
95 this.openHoldingsEditor = (item: number) => {
96 window.open('/eg/staff/cat/item/' + item + '/edit', '_blank');
101 orgName(orgId: number): string {
102 return this.org.get(orgId).shortname();
105 fetchCopies(pager: Pager): Observable<any> {
106 if (!this.recId) { return of([]); }
108 // "Show Result from All Libraries" i.e. global search displays
109 // copies from all branches, sorted by search/pref libs.
110 const copy_depth = this.staffCat.searchContext.global ?
111 this.org.root().ou_type().depth() :
112 this.staffCat.searchContext.searchOrg.ou_type().depth();
114 return this.net.request(
116 'open-ils.search.bib.copies.staff',
118 this.staffCat.searchContext.searchOrg.id(),
122 this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
124 this.org.settings('circ.course_materials_opt_in').then(res => {
125 if (res['circ.course_materials_opt_in']) {
126 this.course.getCoursesFromMaterial(copy.id).then(courseList => {
127 copy._courses = courseList;
131 copy.active_date = copy.active_date || copy.create_date;