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