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 csvExportInProgress: boolean;
21 csvExportUrl: SafeUrl;
22 csvExportFileName: string;
24 constructor(private sanitizer: DomSanitizer) {}
29 // TODO: when server-side settings are supported, this operation
30 // may offer to save to user/workstation OR org unit settings
31 // depending on perms.
33 this.gridContext.saveGridConfig().then(
34 // hide the with config after saving
35 ok => this.colWidthConfig.isVisible = false,
36 err => console.error(`Error saving columns: ${err}`)
40 performAction(action: GridToolbarAction) {
41 action.action(this.gridContext.getSelectedRows());
44 shouldDisableAction(action: GridToolbarAction) {
45 return action.disableOnRows(this.gridContext.getSelectedRows());
49 this.gridPrinter.printGrid();
52 generateCsvExportUrl($event) {
54 if (this.csvExportInProgress) {
55 // This is secondary href click handler. Give the
56 // browser a moment to start the download, then reset
57 // the CSV download attributes / state.
59 this.csvExportUrl = null;
60 this.csvExportFileName = '';
61 this.csvExportInProgress = false;
67 this.csvExportInProgress = true;
69 // let the file name describe the grid
70 this.csvExportFileName = (
71 this.gridContext.persistKey || 'eg_grid_data'
72 ).replace(/\s+/g, '_') + '.csv';
74 this.gridContext.gridToCsv().then(csv => {
75 const blob = new Blob([csv], {type : 'text/plain'});
76 const win: any = window; // avoid TS errors
77 this.csvExportUrl = this.sanitizer.bypassSecurityTrustUrl(
78 (win.URL || win.webkitURL).createObjectURL(blob)
81 // Fire the 2nd click event now that the browser has
82 // information on how to download the CSV file.
83 setTimeout(() => $event.target.click());
86 $event.preventDefault();