]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-toolbar.component.ts
fbc826c06230e34280e9b49ebd5649ad51af93b6
[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     @Input() disableSaveSettings = false;
20
21     renderedGroups: {[group: string]: boolean};
22
23     csvExportInProgress: boolean;
24     csvExportUrl: SafeUrl;
25     csvExportFileName: string;
26
27     constructor(private sanitizer: DomSanitizer) {
28         this.renderedGroups = {};
29     }
30
31     ngOnInit() {
32         this.sortActions();
33     }
34
35     sortActions() {
36         const actions = this.gridContext.toolbarActions;
37
38         const unGrouped = actions.filter(a => !a.group)
39         .sort((a, b) => {
40             return a.label < b.label ? -1 : 1;
41         });
42
43         const grouped = actions.filter(a => Boolean(a.group))
44         .sort((a, b) => {
45             if (a.group === b.group) {
46                 return a.label < b.label ? -1 : 1;
47             } else {
48                 return a.group < b.group ? -1 : 1;
49             }
50         });
51
52         // Insert group markers for rendering
53         const seen: any = {};
54         const grouped2: any[] = [];
55         grouped.forEach(action => {
56             if (!seen[action.group]) {
57                 seen[action.group] = true;
58                 const act = new GridToolbarAction();
59                 act.label = action.group;
60                 act.isGroup = true;
61                 grouped2.push(act);
62             }
63             grouped2.push(action);
64         });
65
66         this.gridContext.toolbarActions = unGrouped.concat(grouped2);
67     }
68
69     saveGridConfig() {
70         // TODO: when server-side settings are supported, this operation
71         // may offer to save to user/workstation OR org unit settings
72         // depending on perms.
73
74         this.gridContext.saveGridConfig().then(
75             // hide the with config after saving
76             ok => this.colWidthConfig.isVisible = false,
77             err => console.error(`Error saving columns: ${err}`)
78         );
79     }
80
81     performButtonAction(button: GridToolbarButton) {
82         const rows = this.gridContext.getSelectedRows();
83         button.onClick.emit();
84         if (button.action) { button.action(); }
85     }
86
87     printHtml() {
88         this.gridPrinter.printGrid();
89     }
90
91     generateCsvExportUrl($event) {
92
93         if (this.csvExportInProgress) {
94             // This is secondary href click handler.  Give the
95             // browser a moment to start the download, then reset
96             // the CSV download attributes / state.
97             setTimeout(() => {
98                 this.csvExportUrl = null;
99                 this.csvExportFileName = '';
100                 this.csvExportInProgress = false;
101                }, 500
102             );
103             return;
104         }
105
106         this.csvExportInProgress = true;
107
108         // let the file name describe the grid
109         this.csvExportFileName = (
110             this.gridContext.persistKey || 'eg_grid_data'
111         ).replace(/\s+/g, '_') + '.csv';
112
113         this.gridContext.gridToCsv().then(csv => {
114             const blob = new Blob([csv], {type : 'text/plain'});
115             const win: any = window; // avoid TS errors
116             this.csvExportUrl = this.sanitizer.bypassSecurityTrustUrl(
117                 (win.URL || win.webkitURL).createObjectURL(blob)
118             );
119
120             // Fire the 2nd click event now that the browser has
121             // information on how to download the CSV file.
122             setTimeout(() => $event.target.click());
123         });
124
125         $event.preventDefault();
126     }
127 }
128
129