1 import {Component, Input, OnInit} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {DomSanitizer, SafeUrl} from '@angular/platform-browser';
4 import {GridToolbarButton, GridToolbarAction, GridContext} from '@eg/share/grid/grid';
5 import {GridColumnWidthComponent} from './grid-column-width.component';
6 import {GridPrintComponent} from './grid-print.component';
9 selector: 'eg-grid-toolbar',
10 templateUrl: 'grid-toolbar.component.html'
13 export class GridToolbarComponent implements OnInit {
15 @Input() gridContext: GridContext;
16 @Input() colWidthConfig: GridColumnWidthComponent;
17 @Input() gridPrinter: GridPrintComponent;
18 @Input() disableSaveSettings = false;
20 renderedGroups: {[group: string]: boolean} = {};
22 csvExportInProgress: boolean;
23 csvExportUrl: SafeUrl;
24 csvExportFileName: string;
27 private router: Router,
28 private sanitizer: DomSanitizer
36 const actions = this.gridContext.toolbarActions;
38 const unGrouped = actions.filter(a => !a.group)
40 return a.label < b.label ? -1 : 1;
43 const grouped = actions.filter(a => Boolean(a.group))
45 if (a.group === b.group) {
46 return a.label < b.label ? -1 : 1;
48 return a.group < b.group ? -1 : 1;
52 // Insert group markers for rendering
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;
63 grouped2.push(action);
66 this.gridContext.toolbarActions = unGrouped.concat(grouped2);
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.
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}`)
81 performButtonAction(button: GridToolbarButton) {
82 const rows = this.gridContext.getSelectedRows();
83 console.log('BUTTON ACTION', button.routerLink);
84 if (button.routerLink) {
85 this.router.navigate([button.routerLink]);
87 button.onClick.emit(rows);
88 if (button.action) { button.action(); }
93 this.gridPrinter.printGrid();
96 generateCsvExportUrl($event) {
98 if (this.csvExportInProgress) {
99 // This is secondary href click handler. Give the
100 // browser a moment to start the download, then reset
101 // the CSV download attributes / state.
103 this.csvExportUrl = null;
104 this.csvExportFileName = '';
105 this.csvExportInProgress = false;
111 this.csvExportInProgress = true;
113 // let the file name describe the grid
114 this.csvExportFileName = (
115 this.gridContext.persistKey || 'eg_grid_data'
116 ).replace(/\s+/g, '_') + '.csv';
118 this.gridContext.gridToCsv().then(csv => {
119 const blob = new Blob([csv], {type : 'text/plain'});
120 const win: any = window; // avoid TS errors
121 this.csvExportUrl = this.sanitizer.bypassSecurityTrustUrl(
122 (win.URL || win.webkitURL).createObjectURL(blob)
125 // Fire the 2nd click event now that the browser has
126 // information on how to download the CSV file.
127 setTimeout(() => $event.target.click());
130 $event.preventDefault();