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';
8 selector: 'eg-grid-toolbar',
9 templateUrl: 'grid-toolbar.component.html'
12 export class GridToolbarComponent implements OnInit {
14 @Input() gridContext: GridContext;
15 @Input() colWidthConfig: GridColumnWidthComponent;
16 @Input() gridPrinter: GridPrintComponent;
17 @Input() disableSaveSettings = false;
19 renderedGroups: {[group: string]: boolean};
21 csvExportInProgress: boolean;
22 csvExportUrl: SafeUrl;
23 csvExportFileName: string;
25 constructor(private sanitizer: DomSanitizer) {
26 this.renderedGroups = {};
34 const actions = this.gridContext.toolbarActions;
36 const unGrouped = actions.filter(a => !a.group)
38 return a.label < b.label ? -1 : 1;
41 const grouped = actions.filter(a => Boolean(a.group))
43 if (a.group === b.group) {
44 return a.label < b.label ? -1 : 1;
46 return a.group < b.group ? -1 : 1;
50 // Insert group markers for rendering
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;
61 grouped2.push(action);
64 this.gridContext.toolbarActions = unGrouped.concat(grouped2);
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.
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}`)
79 performButtonAction(button: GridToolbarButton) {
80 const rows = this.gridContext.getSelectedRows();
81 button.onClick.emit();
82 if (button.action) { button.action(); }
86 this.gridPrinter.printGrid();
89 generateCsvExportUrl($event) {
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.
96 this.csvExportUrl = null;
97 this.csvExportFileName = '';
98 this.csvExportInProgress = false;
104 this.csvExportInProgress = true;
106 // let the file name describe the grid
107 this.csvExportFileName = (
108 this.gridContext.persistKey || 'eg_grid_data'
109 ).replace(/\s+/g, '_') + '.csv';
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)
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());
123 $event.preventDefault();