]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/copies.component.ts
LP#1920815 Item edit link displayed for all users
[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 import {PermService} from '@eg/core/perm.service';
13
14 @Component({
15   selector: 'eg-catalog-copies',
16   templateUrl: 'copies.component.html'
17 })
18 export class CopiesComponent implements OnInit {
19
20     recId: number;
21     initDone = false;
22     usingCourseModule = false;
23     editableCopyLibs: number[] = [];
24     gridDataSource: GridDataSource;
25     copyContext: any; // grid context
26     @ViewChild('copyGrid', { static: true }) copyGrid: GridComponent;
27
28     @Input() set recordId(id: number) {
29         this.recId = id;
30         // Only force new data collection when recordId()
31         // is invoked after ngInit() has already run.
32         if (this.initDone) {
33             this.copyGrid.reload();
34         }
35     }
36
37     cellTextGenerator: GridCellTextGenerator;
38
39     openHoldingsEditor: (item: number) => void;
40
41     constructor(
42         private course: CourseService,
43         private net: NetService,
44         private org: OrgService,
45         private staffCat: StaffCatalogService,
46         private broadcaster: BroadcastService,
47         private perm: PermService
48     ) {
49         this.gridDataSource = new GridDataSource();
50     }
51
52     ngOnInit() {
53         this.initDone = true;
54         this.course.isOptedIn().then(res => {
55             this.usingCourseModule = res;
56         });
57
58         this.perm.hasWorkPermAt(['UPDATE_COPY'], true)
59             .then(result => {
60                 this.editableCopyLibs = result.UPDATE_COPY as number[];
61             });
62
63         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
64             // sorting not currently supported
65             return this.fetchCopies(pager);
66         };
67
68         this.copyContext = {
69             editable: (copy: any) => {
70                 return this.editableCopyLibs.some(lib => {
71                     return copy.circ_lib === lib
72                         || copy.call_number_owning_lib === lib;
73                 });
74             },
75             holdable: (copy: any) => {
76                 return copy.holdable === 't'
77                     && copy.location_holdable === 't'
78                     && copy.status_holdable === 't';
79             }
80         };
81
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
87         };
88
89         this.broadcaster.listen('eg.holdings.update').subscribe(data => {
90             if (data && data.records && data.records.includes(this.recId)) {
91                 this.copyGrid.reload();
92             }
93         });
94
95         this.openHoldingsEditor = (item: number) => {
96             window.open('/eg/staff/cat/item/' + item + '/edit', '_blank');
97         };
98
99     }
100
101     orgName(orgId: number): string {
102         return this.org.get(orgId).shortname();
103     }
104
105     fetchCopies(pager: Pager): Observable<any> {
106         if (!this.recId) { return of([]); }
107
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();
113
114         return this.net.request(
115             'open-ils.search',
116             'open-ils.search.bib.copies.staff',
117             this.recId,
118             this.staffCat.searchContext.searchOrg.id(),
119             copy_depth,
120             pager.limit,
121             pager.offset,
122             this.staffCat.prefOrg ? this.staffCat.prefOrg.id() : null
123         ).pipe(map(copy => {
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;
128                     });
129                 }
130             });
131             copy.active_date = copy.active_date || copy.create_date;
132             return copy;
133         }));
134     }
135 }
136
137