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';
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 cellTextGenerator: GridCellTextGenerator;
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.cellTextGenerator = {
150 title: row => row.title,
151 cp_barcode: row => row.cp_barcode
155 // Returns true after all data/settings/etc required to render the
156 // grid have been fetched.
157 initComplete(): boolean {
158 return this.enablePreFetch !== null;
161 pickupLibChanged(org: IdlObject) {
162 this.pickupLib = org;
163 this.holdsGrid.reload();
166 preFetchHolds(apply: boolean) {
167 this.enablePreFetch = apply;
170 setTimeout(() => this.holdsGrid.reload());
173 if (this.preFetchSetting) {
175 this.store.setItem(this.preFetchSetting, apply);
179 applyFilters(): any {
180 const filters: any = {
181 is_staff_request: true,
182 fulfillment_time: this._showFulfilledSince ?
183 this._showFulfilledSince.toISOString() : null,
184 cancel_time: this._showCanceledSince ?
185 this._showCanceledSince.toISOString() : null,
188 if (this.pickupLib) {
190 this.org.descendants(this.pickupLib, true);
193 if (this._recordId) {
194 filters.record_id = this._recordId;
198 filters.usr_id = this._userId;
204 fetchHolds(pager: Pager, sort: any[]): Observable<any> {
206 // We need at least one filter.
207 if (!this._recordId && !this.pickupLib && !this._userId) {
211 const filters = this.applyFilters();
213 const orderBy: any = [];
214 if (sort.length > 0) {
215 sort.forEach(obj => {
216 const subObj: any = {};
217 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
218 orderBy.push(subObj);
222 const limit = this.enablePreFetch ? null : pager.limit;
223 const offset = this.enablePreFetch ? 0 : pager.offset;
225 let observer: Observer<any>;
226 const observable = new Observable(obs => observer = obs);
228 this.progressDialog.open();
229 this.progressDialog.update({value: 0, max: 1});
234 'open-ils.circ.hold.wide_hash.stream',
235 this.auth.token(), filters, orderBy, limit, offset
239 if (first) { // First response is the hold count.
240 this.holdsCount = Number(holdData);
243 } else { // Subsequent responses are hold data blobs
245 this.progressDialog.update(
246 {value: ++loadCount, max: this.holdsCount});
248 observer.next(holdData);
252 this.progressDialog.close();
256 this.progressDialog.close();
264 showDetails(rows: any[]) {
265 this.showDetail(rows[0]);
268 showDetail(row: any) {
270 this.mode = 'detail';
271 this.detailHold = row;
275 showManager(rows: any[]) {
277 this.mode = 'manage';
278 this.editHolds = rows.map(r => r.id);
282 handleModify(rowsModified: boolean) {
286 // give the grid a chance to render then ask it to reload
287 setTimeout(() => this.holdsGrid.reload());
293 showRecentCircs(rows: any[]) {
296 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
297 window.open(url, '_blank');
301 showPatron(rows: any[]) {
304 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
305 window.open(url, '_blank');
309 showManageDialog(rows: any[]) {
310 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
311 if (holdIds.length > 0) {
312 this.manageDialog.holdIds = holdIds;
313 this.manageDialog.open({size: 'lg'}).subscribe(
316 this.holdsGrid.reload();
323 showTransferDialog(rows: any[]) {
324 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
325 if (holdIds.length > 0) {
326 this.transferDialog.holdIds = holdIds;
327 this.transferDialog.open({}).subscribe(
330 this.holdsGrid.reload();
337 async showMarkDamagedDialog(rows: any[]) {
338 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
339 if (copyIds.length === 0) { return; }
341 let rowsModified = false;
343 const markNext = async(ids: number[]) => {
344 if (ids.length === 0) {
345 return Promise.resolve();
348 this.markDamagedDialog.copyId = ids.pop();
349 return this.markDamagedDialog.open({size: 'lg'}).subscribe(
351 if (ok) { rowsModified = true; }
352 return markNext(ids);
354 dismiss => markNext(ids)
358 await markNext(copyIds);
360 this.holdsGrid.reload();
364 showMarkMissingDialog(rows: any[]) {
365 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
366 if (copyIds.length > 0) {
367 this.markMissingDialog.copyIds = copyIds;
368 this.markMissingDialog.open({}).subscribe(
371 this.holdsGrid.reload();
378 showRetargetDialog(rows: any[]) {
379 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
380 if (holdIds.length > 0) {
381 this.retargetDialog.holdIds = holdIds;
382 this.retargetDialog.open({}).subscribe(
385 this.holdsGrid.reload();
392 showCancelDialog(rows: any[]) {
393 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
394 if (holdIds.length > 0) {
395 this.cancelDialog.holdIds = holdIds;
396 this.cancelDialog.open({}).subscribe(
399 this.holdsGrid.reload();
407 // Request a page with no limit to get all of the wide holds for
408 // printing. Call requestPage() directly instead of grid.reload()
409 // since we may already have the data.
411 const pager = new Pager();
415 if (this.gridDataSource.sort.length === 0) {
416 this.gridDataSource.sort = this.defaultSort;
419 this.gridDataSource.requestPage(pager).then(() => {
420 if (this.gridDataSource.data.length > 0) {
422 templateName: this.printTemplate || 'holds_for_bib',
423 contextData: this.gridDataSource.data,
424 printContext: 'default'