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 performAction(action: GridToolbarAction) {
81 const rows = this.gridContext.getSelectedRows();
82 action.onClick.emit(rows);
83 if (action.action) { action.action(rows); }
86 performButtonAction(button: GridToolbarButton) {
87 const rows = this.gridContext.getSelectedRows();
88 button.onClick.emit();
89 if (button.action) { button.action(); }
92 shouldDisableAction(action: GridToolbarAction) {
93 if (action.disableOnRows) {
94 return action.disableOnRows(this.gridContext.getSelectedRows());
100 this.gridPrinter.printGrid();
103 generateCsvExportUrl($event) {
105 if (this.csvExportInProgress) {
106 // This is secondary href click handler. Give the
107 // browser a moment to start the download, then reset
108 // the CSV download attributes / state.
110 this.csvExportUrl = null;
111 this.csvExportFileName = '';
112 this.csvExportInProgress = false;
118 this.csvExportInProgress = true;
120 // let the file name describe the grid
121 this.csvExportFileName = (
122 this.gridContext.persistKey || 'eg_grid_data'
123 ).replace(/\s+/g, '_') + '.csv';
125 this.gridContext.gridToCsv().then(csv => {
126 const blob = new Blob([csv], {type : 'text/plain'});
127 const win: any = window; // avoid TS errors
128 this.csvExportUrl = this.sanitizer.bypassSecurityTrustUrl(
129 (win.URL || win.webkitURL).createObjectURL(blob)
132 // Fire the 2nd click event now that the browser has
133 // information on how to download the CSV file.
134 setTimeout(() => $event.target.click());
137 $event.preventDefault();