]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-toolbar.component.ts
LP1808268 eg2 grid action disableOnRows sanity check
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-toolbar.component.ts
1 import {Component, Input, OnInit, Host} from '@angular/core';
2 import {DomSanitizer, SafeUrl} from '@angular/platform-browser';
3 import {Pager} from '@eg/share/util/pager';
4 import {GridColumn, GridColumnSet, GridToolbarButton,
5     GridToolbarAction, GridContext, GridDataSource} from '@eg/share/grid/grid';
6 import {GridColumnWidthComponent} from './grid-column-width.component';
7 import {GridPrintComponent} from './grid-print.component';
8
9 @Component({
10   selector: 'eg-grid-toolbar',
11   templateUrl: 'grid-toolbar.component.html'
12 })
13
14 export class GridToolbarComponent implements OnInit {
15
16     @Input() gridContext: GridContext;
17     @Input() colWidthConfig: GridColumnWidthComponent;
18     @Input() gridPrinter: GridPrintComponent;
19
20     csvExportInProgress: boolean;
21     csvExportUrl: SafeUrl;
22     csvExportFileName: string;
23
24     constructor(private sanitizer: DomSanitizer) {}
25
26     ngOnInit() {}
27
28     saveGridConfig() {
29         // TODO: when server-side settings are supported, this operation
30         // may offer to save to user/workstation OR org unit settings
31         // depending on perms.
32
33         this.gridContext.saveGridConfig().then(
34             // hide the with config after saving
35             ok => this.colWidthConfig.isVisible = false,
36             err => console.error(`Error saving columns: ${err}`)
37         );
38     }
39
40     performAction(action: GridToolbarAction) {
41         action.action(this.gridContext.getSelectedRows());
42     }
43
44     shouldDisableAction(action: GridToolbarAction) {
45         if (action.disableOnRows) {
46             return action.disableOnRows(this.gridContext.getSelectedRows());
47         }
48         return false;
49     }
50
51     printHtml() {
52         this.gridPrinter.printGrid();
53     }
54
55     generateCsvExportUrl($event) {
56
57         if (this.csvExportInProgress) {
58             // This is secondary href click handler.  Give the
59             // browser a moment to start the download, then reset
60             // the CSV download attributes / state.
61             setTimeout(() => {
62                 this.csvExportUrl = null;
63                 this.csvExportFileName = '';
64                 this.csvExportInProgress = false;
65                }, 500
66             );
67             return;
68         }
69
70         this.csvExportInProgress = true;
71
72         // let the file name describe the grid
73         this.csvExportFileName = (
74             this.gridContext.persistKey || 'eg_grid_data'
75         ).replace(/\s+/g, '_') + '.csv';
76
77         this.gridContext.gridToCsv().then(csv => {
78             const blob = new Blob([csv], {type : 'text/plain'});
79             const win: any = window; // avoid TS errors
80             this.csvExportUrl = this.sanitizer.bypassSecurityTrustUrl(
81                 (win.URL || win.webkitURL).createObjectURL(blob)
82             );
83
84             // Fire the 2nd click event now that the browser has
85             // information on how to download the CSV file.
86             setTimeout(() => $event.target.click());
87         });
88
89         $event.preventDefault();
90     }
91 }
92
93