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} 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';
56 gridDataSource: GridDataSource;
59 transferTarget: number;
61 @ViewChild('holdsGrid', { static: false }) private holdsGrid: GridComponent;
62 @ViewChild('progressDialog', { static: true })
63 private progressDialog: ProgressDialogComponent;
64 @ViewChild('transferDialog', { static: true })
65 private transferDialog: HoldTransferDialogComponent;
66 @ViewChild('markDamagedDialog', { static: true })
67 private markDamagedDialog: MarkDamagedDialogComponent;
68 @ViewChild('markMissingDialog', { static: true })
69 private markMissingDialog: MarkMissingDialogComponent;
70 @ViewChild('retargetDialog', { static: true })
71 private retargetDialog: HoldRetargetDialogComponent;
72 @ViewChild('cancelDialog', { static: true })
73 private cancelDialog: HoldCancelDialogComponent;
74 @ViewChild('manageDialog', { static: true })
75 private manageDialog: HoldManageDialogComponent;
79 @Input() set recordId(id: number) {
81 if (this.initDone) { // reload on update
82 this.holdsGrid.reload();
87 @Input() set userId(id: number) {
90 this.holdsGrid.reload();
94 // Include holds canceled on or after the provided date.
95 // If no value is passed, canceled holds are not displayed.
96 _showCanceledSince: Date;
97 @Input() set showCanceledSince(show: Date) {
98 this._showCanceledSince = show;
99 if (this.initDone) { // reload on update
100 this.holdsGrid.reload();
104 // Include holds fulfilled on or after hte provided date.
105 // If no value is passed, fulfilled holds are not displayed.
106 _showFulfilledSince: Date;
107 @Input() set showFulfilledSince(show: Date) {
108 this._showFulfilledSince = show;
109 if (this.initDone) { // reload on update
110 this.holdsGrid.reload();
114 cellPrintValues: (row: any, cell: GridColumn) => string;
117 private net: NetService,
118 private org: OrgService,
119 private store: ServerStoreService,
120 private auth: AuthService,
121 private printer: PrintService
123 this.gridDataSource = new GridDataSource();
124 this.enablePreFetch = null;
128 this.initDone = true;
129 this.pickupLib = this.org.get(this.initialPickupLib);
131 if (this.preFetchSetting) {
133 this.store.getItem(this.preFetchSetting).then(
134 applied => this.enablePreFetch = Boolean(applied)
139 if (!this.defaultSort) {
140 this.defaultSort = [{name: 'request_time', dir: 'asc'}];
143 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
144 sort = sort.length > 0 ? sort : this.defaultSort;
145 return this.fetchHolds(pager, sort);
148 // Text-ify function for cells that use display templates.
149 this.cellPrintValues = (row: any, cell: GridColumn): string => {
152 'cp_barcode': row.cp_barcode
157 // Returns true after all data/settings/etc required to render the
158 // grid have been fetched.
159 initComplete(): boolean {
160 return this.enablePreFetch !== null;
163 pickupLibChanged(org: IdlObject) {
164 this.pickupLib = org;
165 this.holdsGrid.reload();
168 preFetchHolds(apply: boolean) {
169 this.enablePreFetch = apply;
172 setTimeout(() => this.holdsGrid.reload());
175 if (this.preFetchSetting) {
177 this.store.setItem(this.preFetchSetting, apply);
181 applyFilters(): any {
182 const filters: any = {
183 is_staff_request: true,
184 fulfillment_time: this._showFulfilledSince ?
185 this._showFulfilledSince.toISOString() : null,
186 cancel_time: this._showCanceledSince ?
187 this._showCanceledSince.toISOString() : null,
190 if (this.pickupLib) {
192 this.org.descendants(this.pickupLib, true);
195 if (this._recordId) {
196 filters.record_id = this._recordId;
200 filters.usr_id = this._userId;
206 fetchHolds(pager: Pager, sort: any[]): Observable<any> {
208 // We need at least one filter.
209 if (!this._recordId && !this.pickupLib && !this._userId) {
213 const filters = this.applyFilters();
215 const orderBy: any = [];
216 if (sort.length > 0) {
217 sort.forEach(obj => {
218 const subObj: any = {};
219 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
220 orderBy.push(subObj);
224 const limit = this.enablePreFetch ? null : pager.limit;
225 const offset = this.enablePreFetch ? 0 : pager.offset;
227 let observer: Observer<any>;
228 const observable = new Observable(obs => observer = obs);
230 this.progressDialog.open();
231 this.progressDialog.update({value: 0, max: 1});
236 'open-ils.circ.hold.wide_hash.stream',
237 this.auth.token(), filters, orderBy, limit, offset
241 if (first) { // First response is the hold count.
242 this.holdsCount = Number(holdData);
245 } else { // Subsequent responses are hold data blobs
247 this.progressDialog.update(
248 {value: ++loadCount, max: this.holdsCount});
250 observer.next(holdData);
254 this.progressDialog.close();
258 this.progressDialog.close();
266 showDetails(rows: any[]) {
267 this.showDetail(rows[0]);
270 showDetail(row: any) {
272 this.mode = 'detail';
273 this.detailHold = row;
277 showManager(rows: any[]) {
279 this.mode = 'manage';
280 this.editHolds = rows.map(r => r.id);
284 handleModify(rowsModified: boolean) {
288 // give the grid a chance to render then ask it to reload
289 setTimeout(() => this.holdsGrid.reload());
295 showRecentCircs(rows: any[]) {
298 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
299 window.open(url, '_blank');
303 showPatron(rows: any[]) {
306 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
307 window.open(url, '_blank');
311 showManageDialog(rows: any[]) {
312 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
313 if (holdIds.length > 0) {
314 this.manageDialog.holdIds = holdIds;
315 this.manageDialog.open({size: 'lg'}).subscribe(
318 this.holdsGrid.reload();
325 showTransferDialog(rows: any[]) {
326 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
327 if (holdIds.length > 0) {
328 this.transferDialog.holdIds = holdIds;
329 this.transferDialog.open({}).subscribe(
332 this.holdsGrid.reload();
339 async showMarkDamagedDialog(rows: any[]) {
340 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
341 if (copyIds.length === 0) { return; }
343 let rowsModified = false;
345 const markNext = async(ids: number[]) => {
346 if (ids.length === 0) {
347 return Promise.resolve();
350 this.markDamagedDialog.copyId = ids.pop();
351 return this.markDamagedDialog.open({size: 'lg'}).subscribe(
353 if (ok) { rowsModified = true; }
354 return markNext(ids);
356 dismiss => markNext(ids)
360 await markNext(copyIds);
362 this.holdsGrid.reload();
366 showMarkMissingDialog(rows: any[]) {
367 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
368 if (copyIds.length > 0) {
369 this.markMissingDialog.copyIds = copyIds;
370 this.markMissingDialog.open({}).subscribe(
373 this.holdsGrid.reload();
380 showRetargetDialog(rows: any[]) {
381 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
382 if (holdIds.length > 0) {
383 this.retargetDialog.holdIds = holdIds;
384 this.retargetDialog.open({}).subscribe(
387 this.holdsGrid.reload();
394 showCancelDialog(rows: any[]) {
395 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
396 if (holdIds.length > 0) {
397 this.cancelDialog.holdIds = holdIds;
398 this.cancelDialog.open({}).subscribe(
401 this.holdsGrid.reload();
409 // Request a page with no limit to get all of the wide holds for
410 // printing. Call requestPage() directly instead of grid.reload()
411 // since we may already have the data.
413 const pager = new Pager();
417 if (this.gridDataSource.sort.length === 0) {
418 this.gridDataSource.sort = this.defaultSort;
421 this.gridDataSource.requestPage(pager).then(() => {
422 if (this.gridDataSource.data.length > 0) {
424 templateName: this.printTemplate || 'holds_for_bib',
425 contextData: this.gridDataSource.data,
426 printContext: 'default'