1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, Observer, of} from 'rxjs';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {OrgService} from '@eg/core/org.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {Pager} from '@eg/share/util/pager';
8 import {ServerStoreService} from '@eg/core/server-store.service';
9 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
10 import {GridComponent} from '@eg/share/grid/grid.component';
11 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
12 import {MarkDamagedDialogComponent
13 } from '@eg/staff/share/holdings/mark-damaged-dialog.component';
14 import {MarkMissingDialogComponent
15 } from '@eg/staff/share/holdings/mark-missing-dialog.component';
16 import {HoldRetargetDialogComponent
17 } from '@eg/staff/share/holds/retarget-dialog.component';
18 import {HoldTransferDialogComponent} from './transfer-dialog.component';
19 import {HoldCancelDialogComponent} from './cancel-dialog.component';
20 import {HoldManageDialogComponent} from './manage-dialog.component';
21 import {PrintService} from '@eg/share/print/print.service';
23 /** Holds grid with access to detail page and other actions */
26 selector: 'eg-holds-grid',
27 templateUrl: 'grid.component.html'
29 export class HoldsGridComponent implements OnInit {
31 // If either are set/true, the pickup lib selector will display
32 @Input() initialPickupLib: number | IdlObject;
33 @Input() hidePickupLibFilter: boolean;
36 @Input() persistKey: string;
38 @Input() preFetchSetting: string;
40 @Input() printTemplate: string;
42 // If set, all holds are fetched on grid load and sorting/paging all
43 // happens in the client. If false, sorting and paging occur on
45 enablePreFetch: boolean;
47 // How to sort when no sort parameters have been applied
48 // via grid controls. This uses the eg-grid sort format:
49 // [{name: fname, dir: 'asc'}, {name: fname2, dir: 'desc'}]
50 @Input() defaultSort: any[];
52 mode: 'list' | 'detail' | 'manage' = 'list';
57 gridDataSource: GridDataSource;
60 transferTarget: number;
62 @ViewChild('holdsGrid', { static: false }) private holdsGrid: GridComponent;
63 @ViewChild('progressDialog', { static: true })
64 private progressDialog: ProgressDialogComponent;
65 @ViewChild('transferDialog', { static: true })
66 private transferDialog: HoldTransferDialogComponent;
67 @ViewChild('markDamagedDialog', { static: true })
68 private markDamagedDialog: MarkDamagedDialogComponent;
69 @ViewChild('markMissingDialog', { static: true })
70 private markMissingDialog: MarkMissingDialogComponent;
71 @ViewChild('retargetDialog', { static: true })
72 private retargetDialog: HoldRetargetDialogComponent;
73 @ViewChild('cancelDialog', { static: true })
74 private cancelDialog: HoldCancelDialogComponent;
75 @ViewChild('manageDialog', { static: true })
76 private manageDialog: HoldManageDialogComponent;
80 @Input() set recordId(id: number) {
82 if (this.initDone) { // reload on update
83 this.holdsGrid.reload();
88 @Input() set userId(id: number) {
91 this.holdsGrid.reload();
95 // Include holds canceled on or after the provided date.
96 // If no value is passed, canceled holds are not displayed.
97 _showCanceledSince: Date;
98 @Input() set showCanceledSince(show: Date) {
99 this._showCanceledSince = show;
100 if (this.initDone) { // reload on update
101 this.holdsGrid.reload();
105 // Include holds fulfilled on or after hte provided date.
106 // If no value is passed, fulfilled holds are not displayed.
107 _showFulfilledSince: Date;
108 @Input() set showFulfilledSince(show: Date) {
109 this._showFulfilledSince = show;
110 if (this.initDone) { // reload on update
111 this.holdsGrid.reload();
115 cellTextGenerator: GridCellTextGenerator;
118 private net: NetService,
119 private org: OrgService,
120 private store: ServerStoreService,
121 private auth: AuthService,
122 private printer: PrintService
124 this.gridDataSource = new GridDataSource();
125 this.enablePreFetch = null;
129 this.initDone = true;
130 this.pickupLib = this.org.get(this.initialPickupLib);
132 if (this.preFetchSetting) {
133 this.store.getItem(this.preFetchSetting).then(
134 applied => this.enablePreFetch = Boolean(applied)
138 if (!this.defaultSort) {
139 this.defaultSort = [{name: 'request_time', dir: 'asc'}];
142 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
144 if (!this.hidePickupLibFilter && !this.plCompLoaded) {
145 // When the pickup lib selector is active, avoid any
146 // data fetches until it has settled on a default value.
147 // Once the final value is applied, its onchange will
148 // fire and we'll be back here with plCompLoaded=true.
152 sort = sort.length > 0 ? sort : this.defaultSort;
153 return this.fetchHolds(pager, sort);
156 // Text-ify function for cells that use display templates.
157 this.cellTextGenerator = {
158 title: row => row.title,
159 cp_barcode: row => (row.cp_barcode == null) ? '' : row.cp_barcode,
160 patron_barcode: row => row.ucard_barcode
164 // Returns true after all data/settings/etc required to render the
165 // grid have been fetched.
166 initComplete(): boolean {
167 return this.enablePreFetch !== null;
170 pickupLibChanged(org: IdlObject) {
171 this.pickupLib = org;
172 this.holdsGrid.reload();
175 preFetchHolds(apply: boolean) {
176 this.enablePreFetch = apply;
179 setTimeout(() => this.holdsGrid.reload());
182 if (this.preFetchSetting) {
184 this.store.setItem(this.preFetchSetting, apply);
188 applyFilters(): any {
189 const filters: any = {
190 is_staff_request: true,
191 fulfillment_time: this._showFulfilledSince ?
192 this._showFulfilledSince.toISOString() : null,
193 cancel_time: this._showCanceledSince ?
194 this._showCanceledSince.toISOString() : null,
197 if (this.pickupLib) {
199 this.org.descendants(this.pickupLib, true);
202 if (this._recordId) {
203 filters.record_id = this._recordId;
207 filters.usr_id = this._userId;
213 fetchHolds(pager: Pager, sort: any[]): Observable<any> {
215 // We need at least one filter.
216 if (!this._recordId && !this.pickupLib && !this._userId) {
220 const filters = this.applyFilters();
222 const orderBy: any = [];
223 if (sort.length > 0) {
224 sort.forEach(obj => {
225 const subObj: any = {};
226 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
227 orderBy.push(subObj);
231 const limit = this.enablePreFetch ? null : pager.limit;
232 const offset = this.enablePreFetch ? 0 : pager.offset;
234 let observer: Observer<any>;
235 const observable = new Observable(obs => observer = obs);
237 this.progressDialog.open();
238 this.progressDialog.update({value: 0, max: 1});
243 'open-ils.circ.hold.wide_hash.stream',
244 this.auth.token(), filters, orderBy, limit, offset
248 if (first) { // First response is the hold count.
249 this.holdsCount = Number(holdData);
252 } else { // Subsequent responses are hold data blobs
254 this.progressDialog.update(
255 {value: ++loadCount, max: this.holdsCount});
257 observer.next(holdData);
261 this.progressDialog.close();
265 this.progressDialog.close();
273 showDetails(rows: any[]) {
274 this.showDetail(rows[0]);
277 showDetail(row: any) {
279 this.mode = 'detail';
280 this.detailHold = row;
284 showManager(rows: any[]) {
286 this.mode = 'manage';
287 this.editHolds = rows.map(r => r.id);
291 handleModify(rowsModified: boolean) {
295 // give the grid a chance to render then ask it to reload
296 setTimeout(() => this.holdsGrid.reload());
302 showRecentCircs(rows: any[]) {
305 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
306 window.open(url, '_blank');
310 showPatron(rows: any[]) {
313 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
314 window.open(url, '_blank');
318 showManageDialog(rows: any[]) {
319 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
320 if (holdIds.length > 0) {
321 this.manageDialog.holdIds = holdIds;
322 this.manageDialog.open({size: 'lg'}).subscribe(
325 this.holdsGrid.reload();
332 showTransferDialog(rows: any[]) {
333 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
334 if (holdIds.length > 0) {
335 this.transferDialog.holdIds = holdIds;
336 this.transferDialog.open({}).subscribe(
339 this.holdsGrid.reload();
346 async showMarkDamagedDialog(rows: any[]) {
347 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
348 if (copyIds.length === 0) { return; }
350 let rowsModified = false;
352 const markNext = async(ids: number[]) => {
353 if (ids.length === 0) {
354 return Promise.resolve();
357 this.markDamagedDialog.copyId = ids.pop();
358 return this.markDamagedDialog.open({size: 'lg'}).subscribe(
360 if (ok) { rowsModified = true; }
361 return markNext(ids);
363 dismiss => markNext(ids)
367 await markNext(copyIds);
369 this.holdsGrid.reload();
373 showMarkMissingDialog(rows: any[]) {
374 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
375 if (copyIds.length > 0) {
376 this.markMissingDialog.copyIds = copyIds;
377 this.markMissingDialog.open({}).subscribe(
380 this.holdsGrid.reload();
387 showRetargetDialog(rows: any[]) {
388 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
389 if (holdIds.length > 0) {
390 this.retargetDialog.holdIds = holdIds;
391 this.retargetDialog.open({}).subscribe(
394 this.holdsGrid.reload();
401 showCancelDialog(rows: any[]) {
402 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
403 if (holdIds.length > 0) {
404 this.cancelDialog.holdIds = holdIds;
405 this.cancelDialog.open({}).subscribe(
408 this.holdsGrid.reload();
416 // Request a page with no limit to get all of the wide holds for
417 // printing. Call requestPage() directly instead of grid.reload()
418 // since we may already have the data.
420 const pager = new Pager();
424 if (this.gridDataSource.sort.length === 0) {
425 this.gridDataSource.sort = this.defaultSort;
428 this.gridDataSource.requestPage(pager).then(() => {
429 if (this.gridDataSource.data.length > 0) {
431 templateName: this.printTemplate || 'holds_for_bib',
432 contextData: this.gridDataSource.data,
433 printContext: 'default'