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