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';
10 selector: 'eg-grid-toolbar',
11 templateUrl: 'grid-toolbar.component.html'
14 export class GridToolbarComponent implements OnInit {
16 @Input() gridContext: GridContext;
17 @Input() colWidthConfig: GridColumnWidthComponent;
18 @Input() gridPrinter: GridPrintComponent;
20 renderedGroups: {[group: string]: boolean};
22 csvExportInProgress: boolean;
23 csvExportUrl: SafeUrl;
24 csvExportFileName: string;
26 constructor(private sanitizer: DomSanitizer) {
27 this.renderedGroups = {};
35 const actions = this.gridContext.toolbarActions;
37 const unGrouped = actions.filter(a => !a.group)
39 return a.label < b.label ? -1 : 1;
42 const grouped = actions.filter(a => Boolean(a.group))
44 if (a.group === b.group) {
45 return a.label < b.label ? -1 : 1;
47 return a.group < b.group ? -1 : 1;
51 // Insert group markers for rendering
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;
62 grouped2.push(action);
65 this.gridContext.toolbarActions = unGrouped.concat(grouped2);
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.
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}`)
80 performButtonAction(button: GridToolbarButton) {
81 const rows = this.gridContext.getSelectedRows();
82 button.onClick.emit();
83 if (button.action) { button.action(); }
87 this.gridPrinter.printGrid();
90 generateCsvExportUrl($event) {
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.
97 this.csvExportUrl = null;
98 this.csvExportFileName = '';
99 this.csvExportInProgress = false;
105 this.csvExportInProgress = true;
107 // let the file name describe the grid
108 this.csvExportFileName = (
109 this.gridContext.persistKey || 'eg_grid_data'
110 ).replace(/\s+/g, '_') + '.csv';
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)
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());
124 $event.preventDefault();