|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +import { Component, Inject } from "@angular/core"; |
| 16 | +import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog"; |
| 17 | +import { ColDef, CsvExportParams } from "ag-grid-community"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Data passed to DownloadOptionsComponent from the grid |
| 21 | + */ |
| 22 | +export interface DownloadOptionsDialogData { |
| 23 | + name: string; |
| 24 | + |
| 25 | + columns: ColDef<unknown>[]; |
| 26 | + |
| 27 | + /** |
| 28 | + * Number of rows selected, should be undefined when only a single. |
| 29 | + */ |
| 30 | + selectedRows: number | undefined; |
| 31 | + |
| 32 | + visibleRows: number; |
| 33 | + |
| 34 | + allRows: number; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Controller for the DownloadOptions component. |
| 39 | + */ |
| 40 | +@Component({ |
| 41 | + selector: "tp-download-options", |
| 42 | + styleUrls: ["./download-options-dialog.component.scss"], |
| 43 | + templateUrl: "./download-options-dialog.component.html" |
| 44 | +}) |
| 45 | +export class DownloadOptionsDialogComponent { |
| 46 | + public fileName: string; |
| 47 | + |
| 48 | + public visibleColumns: Array<ColDef<unknown>>; |
| 49 | + |
| 50 | + public columns: Array<ColDef<unknown>>; |
| 51 | + |
| 52 | + public includeHidden = false; |
| 53 | + public includeHeaders = true; |
| 54 | + |
| 55 | + public includeFiltered = false; |
| 56 | + |
| 57 | + public onlySelected = false; |
| 58 | + |
| 59 | + /** |
| 60 | + * Number of selected rows, undefined if single selection. |
| 61 | + */ |
| 62 | + public selectedRows: number | undefined; |
| 63 | + public allRows: number; |
| 64 | + public visibleRows: number; |
| 65 | + |
| 66 | + /** 'C'SV delimiter */ |
| 67 | + public seperator = ","; |
| 68 | + |
| 69 | + constructor(private readonly dialogRef: MatDialogRef<DownloadOptionsDialogComponent, |
| 70 | + CsvExportParams>, @Inject(MAT_DIALOG_DATA) data: DownloadOptionsDialogData) { |
| 71 | + this.fileName = data.name; |
| 72 | + this.selectedRows = data.selectedRows; |
| 73 | + this.allRows = data.allRows; |
| 74 | + this.visibleRows = data.visibleRows; |
| 75 | + this.visibleColumns = []; |
| 76 | + this.columns = []; |
| 77 | + for(const col of data.columns) { |
| 78 | + if(!col.hide) { |
| 79 | + this.visibleColumns.push(col); |
| 80 | + } |
| 81 | + this.columns.push(col); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Called when submitting the form, converts data into export params. |
| 87 | + */ |
| 88 | + public onSubmit(): void { |
| 89 | + const params: CsvExportParams = { |
| 90 | + allColumns: this.includeHidden, |
| 91 | + columnSeparator: this.seperator, |
| 92 | + exportedRows: this.includeFiltered ? "all" : "filteredAndSorted", |
| 93 | + fileName: `${this.fileName}.csv`, |
| 94 | + onlySelected: this.onlySelected, |
| 95 | + skipColumnHeaders: !this.includeHeaders, |
| 96 | + }; |
| 97 | + this.dialogRef.close(params); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments