LP1825851 Server managed/processed print templates
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / grid.component.ts
index e0e894d..eb670d0 100644 (file)
@@ -5,6 +5,7 @@ import {NetService} from '@eg/core/net.service';
 import {OrgService} from '@eg/core/org.service';
 import {AuthService} from '@eg/core/auth.service';
 import {Pager} from '@eg/share/util/pager';
+import {ServerStoreService} from '@eg/core/server-store.service';
 import {GridDataSource} from '@eg/share/grid/grid';
 import {GridComponent} from '@eg/share/grid/grid.component';
 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
@@ -17,6 +18,7 @@ import {HoldRetargetDialogComponent
 import {HoldTransferDialogComponent} from './transfer-dialog.component';
 import {HoldCancelDialogComponent} from './cancel-dialog.component';
 import {HoldManageDialogComponent} from './manage-dialog.component';
+import {PrintService} from '@eg/share/print/print.service';
 
 /** Holds grid with access to detail page and other actions */
 
@@ -33,6 +35,15 @@ export class HoldsGridComponent implements OnInit {
     // Grid persist key
     @Input() persistKey: string;
 
+    @Input() preFetchSetting: string;
+
+    @Input() printTemplate: string;
+
+    // If set, all holds are fetched on grid load and sorting/paging all
+    // happens in the client.  If false, sorting and paging occur on
+    // the server.
+    enablePreFetch: boolean;
+
     // How to sort when no sort parameters have been applied
     // via grid controls.  This uses the eg-grid sort format:
     // [{name: fname, dir: 'asc'}, {name: fname2, dir: 'desc'}]
@@ -46,7 +57,6 @@ export class HoldsGridComponent implements OnInit {
     detailHold: any;
     editHolds: number[];
     transferTarget: number;
-    copyStatuses: {[id: string]: IdlObject};
 
     @ViewChild('holdsGrid') private holdsGrid: GridComponent;
     @ViewChild('progressDialog')
@@ -104,34 +114,60 @@ export class HoldsGridComponent implements OnInit {
     constructor(
         private net: NetService,
         private org: OrgService,
-        private auth: AuthService
+        private store: ServerStoreService,
+        private auth: AuthService,
+        private printer: PrintService
     ) {
         this.gridDataSource = new GridDataSource();
-        this.copyStatuses = {};
+        this.enablePreFetch = null;
     }
 
     ngOnInit() {
         this.initDone = true;
         this.pickupLib = this.org.get(this.initialPickupLib);
 
-        this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
+        if (this.preFetchSetting) {
 
-            if (this.defaultSort && sort.length === 0) {
-                // Only use initial sort if sorting has not been modified
-                // by the grid's own sort controls.
-                sort = this.defaultSort;
-            }
+                this.store.getItem(this.preFetchSetting).then(
+                    applied => this.enablePreFetch = Boolean(applied)
+                );
 
-            // sorting not currently supported
+        }
+
+        if (!this.defaultSort) {
+            this.defaultSort = [{name: 'request_time', dir: 'asc'}];
+        }
+
+        this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
+            sort = sort.length > 0 ? sort : this.defaultSort;
             return this.fetchHolds(pager, sort);
         };
     }
 
+    // Returns true after all data/settings/etc required to render the
+    // grid have been fetched.
+    initComplete(): boolean {
+        return this.enablePreFetch !== null;
+    }
+
     pickupLibChanged(org: IdlObject) {
         this.pickupLib = org;
         this.holdsGrid.reload();
     }
 
+    preFetchHolds(apply: boolean) {
+        this.enablePreFetch = apply;
+
+        if (apply) {
+            setTimeout(() => this.holdsGrid.reload());
+        }
+
+        if (this.preFetchSetting) {
+            // fire and forget
+            this.store.setItem(this.preFetchSetting, apply);
+        }
+    }
+
     applyFilters(): any {
         const filters: any = {
             is_staff_request: true,
@@ -167,11 +203,16 @@ export class HoldsGridComponent implements OnInit {
         const filters = this.applyFilters();
 
         const orderBy: any = [];
-        sort.forEach(obj => {
-            const subObj: any = {};
-            subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
-            orderBy.push(subObj);
-        });
+        if (sort.length > 0) {
+            sort.forEach(obj => {
+                const subObj: any = {};
+                subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
+                orderBy.push(subObj);
+            });
+        }
+
+        const limit = this.enablePreFetch ? null : pager.limit;
+        const offset = this.enablePreFetch ? 0 : pager.offset;
 
         let observer: Observer<any>;
         const observable = new Observable(obs => observer = obs);
@@ -183,10 +224,7 @@ export class HoldsGridComponent implements OnInit {
         this.net.request(
             'open-ils.circ',
             'open-ils.circ.hold.wide_hash.stream',
-            // Pre-fetch all holds consistent with AngJS version
-            this.auth.token(), filters, orderBy
-            // Alternatively, fetch holds in pages.
-            // this.auth.token(), filters, orderBy, pager.limit, pager.offset
+            this.auth.token(), filters, orderBy, limit, offset
         ).subscribe(
             holdData => {
 
@@ -264,13 +302,12 @@ export class HoldsGridComponent implements OnInit {
         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
         if (holdIds.length > 0) {
             this.manageDialog.holdIds = holdIds;
-            this.manageDialog.open({size: 'lg'}).then(
+            this.manageDialog.open({size: 'lg'}).subscribe(
                 rowsModified => {
                     if (rowsModified) {
                         this.holdsGrid.reload();
                     }
-                },
-                dismissed => {}
+                }
             );
         }
     }
@@ -279,13 +316,12 @@ export class HoldsGridComponent implements OnInit {
         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
         if (holdIds.length > 0) {
             this.transferDialog.holdIds = holdIds;
-            this.transferDialog.open({}).then(
+            this.transferDialog.open({}).subscribe(
                 rowsModified => {
                     if (rowsModified) {
                         this.holdsGrid.reload();
                     }
-                },
-                dismissed => {}
+                }
             );
         }
     }
@@ -302,7 +338,7 @@ export class HoldsGridComponent implements OnInit {
             }
 
             this.markDamagedDialog.copyId = ids.pop();
-            this.markDamagedDialog.open({size: 'lg'}).then(
+            return this.markDamagedDialog.open({size: 'lg'}).subscribe(
                 ok => {
                     if (ok) { rowsModified = true; }
                     return markNext(ids);
@@ -321,13 +357,12 @@ export class HoldsGridComponent implements OnInit {
         const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
         if (copyIds.length > 0) {
             this.markMissingDialog.copyIds = copyIds;
-            this.markMissingDialog.open({}).then(
+            this.markMissingDialog.open({}).subscribe(
                 rowsModified => {
                     if (rowsModified) {
                         this.holdsGrid.reload();
                     }
-                },
-                dismissed => {} // avoid console errors
+                }
             );
         }
     }
@@ -336,13 +371,12 @@ export class HoldsGridComponent implements OnInit {
         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
         if (holdIds.length > 0) {
             this.retargetDialog.holdIds = holdIds;
-            this.retargetDialog.open({}).then(
+            this.retargetDialog.open({}).subscribe(
                 rowsModified => {
                     if (rowsModified) {
                         this.holdsGrid.reload();
                     }
-                },
-                dismissed => {}
+                }
             );
         }
     }
@@ -351,16 +385,41 @@ export class HoldsGridComponent implements OnInit {
         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
         if (holdIds.length > 0) {
             this.cancelDialog.holdIds = holdIds;
-            this.cancelDialog.open({}).then(
+            this.cancelDialog.open({}).subscribe(
                 rowsModified => {
                     if (rowsModified) {
                         this.holdsGrid.reload();
                     }
-                },
-                dismissed => {}
+                }
             );
         }
     }
+
+    printHolds() {
+        // Request a page with no limit to get all of the wide holds for
+        // printing.  Call requestPage() directly instead of grid.reload()
+        // since we may already have the data.
+
+        const pager = new Pager();
+        pager.offset = 0;
+        pager.limit = null;
+
+        if (this.gridDataSource.sort.length === 0) {
+            this.gridDataSource.sort = this.defaultSort;
+        }
+
+        this.gridDataSource.requestPage(pager).then(() => {
+            if (this.gridDataSource.data.length > 0) {
+                this.printer.print({
+                    templateName: this.printTemplate || 'holds_for_bib',
+                    contextData: this.gridDataSource.data,
+                    printContext: 'default'
+                });
+            }
+        });
+    }
 }
 
 
+
+