| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | import { TextAlignProperty } from 'csstype'; | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  | import { DataFrame, Field, FieldType } from '@grafana/data'; | 
					
						
							|  |  |  | import { Column } from 'react-table'; | 
					
						
							|  |  |  | import { DefaultCell } from './DefaultCell'; | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | import { BarGaugeCell } from './BarGaugeCell'; | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  | import { BackgroundColoredCell } from './BackgroundColorCell'; | 
					
						
							| 
									
										
										
										
											2020-04-01 20:24:17 +08:00
										 |  |  | import { TableCellDisplayMode, TableFieldOptions, TableRow } from './types'; | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function getTableRows(data: DataFrame): TableRow[] { | 
					
						
							|  |  |  |   const tableData = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (let i = 0; i < data.length; i++) { | 
					
						
							|  |  |  |     const row: { [key: string]: string | number } = {}; | 
					
						
							|  |  |  |     for (let j = 0; j < data.fields.length; j++) { | 
					
						
							|  |  |  |       const prop = data.fields[j].name; | 
					
						
							|  |  |  |       row[prop] = data.fields[j].values.get(i); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     tableData.push(row); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return tableData; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 20:24:17 +08:00
										 |  |  | export function getTextAlign(field?: Field): TextAlignProperty { | 
					
						
							|  |  |  |   if (!field) { | 
					
						
							|  |  |  |     return 'left'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |   if (field.config.custom) { | 
					
						
							|  |  |  |     const custom = field.config.custom as TableFieldOptions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (custom.align) { | 
					
						
							|  |  |  |       case 'right': | 
					
						
							|  |  |  |         return 'right'; | 
					
						
							|  |  |  |       case 'left': | 
					
						
							|  |  |  |         return 'left'; | 
					
						
							|  |  |  |       case 'center': | 
					
						
							|  |  |  |         return 'center'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (field.type === FieldType.number) { | 
					
						
							|  |  |  |     return 'right'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 'left'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 16:26:20 +08:00
										 |  |  | export function getColumns(data: DataFrame, availableWidth: number, columnMinWidth: number): Column[] { | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |   const columns: Column[] = []; | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |   let fieldCountWithoutWidth = data.fields.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const field of data.fields) { | 
					
						
							|  |  |  |     const fieldTableOptions = (field.config.custom || {}) as TableFieldOptions; | 
					
						
							|  |  |  |     if (fieldTableOptions.width) { | 
					
						
							|  |  |  |       availableWidth -= fieldTableOptions.width; | 
					
						
							|  |  |  |       fieldCountWithoutWidth -= 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |     const Cell = getCellComponent(fieldTableOptions.displayMode); | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |     columns.push({ | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |       Cell, | 
					
						
							| 
									
										
										
										
											2020-04-01 20:24:17 +08:00
										 |  |  |       id: field.name, | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |       Header: field.name, | 
					
						
							|  |  |  |       accessor: field.name, | 
					
						
							|  |  |  |       width: fieldTableOptions.width, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // divide up the rest of the space
 | 
					
						
							|  |  |  |   const sharedWidth = availableWidth / fieldCountWithoutWidth; | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |   for (const column of columns) { | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |     if (!column.width) { | 
					
						
							| 
									
										
										
										
											2020-02-28 16:26:20 +08:00
										 |  |  |       column.width = Math.max(sharedWidth, columnMinWidth); | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |   return columns; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getCellComponent(displayMode: TableCellDisplayMode) { | 
					
						
							|  |  |  |   switch (displayMode) { | 
					
						
							|  |  |  |     case TableCellDisplayMode.ColorBackground: | 
					
						
							|  |  |  |       return BackgroundColoredCell; | 
					
						
							|  |  |  |     case TableCellDisplayMode.LcdGauge: | 
					
						
							|  |  |  |     case TableCellDisplayMode.GradientGauge: | 
					
						
							|  |  |  |       return BarGaugeCell; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       return DefaultCell; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | } |