| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | import { TextAlignProperty } from 'csstype'; | 
					
						
							| 
									
										
										
										
											2020-05-12 19:52:53 +08:00
										 |  |  | import { DataFrame, Field, FieldType, getFieldDisplayName } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  | import { Column } from 'react-table'; | 
					
						
							|  |  |  | import { DefaultCell } from './DefaultCell'; | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | import { BarGaugeCell } from './BarGaugeCell'; | 
					
						
							| 
									
										
										
										
											2020-04-20 15:27:40 +08:00
										 |  |  | import { TableCellDisplayMode, TableCellProps, TableFieldOptions } from './types'; | 
					
						
							| 
									
										
										
										
											2020-04-09 13:42:31 +08:00
										 |  |  | import { css, cx } from 'emotion'; | 
					
						
							|  |  |  | import { withTableStyles } from './withTableStyles'; | 
					
						
							|  |  |  | import tinycolor from 'tinycolor2'; | 
					
						
							| 
									
										
										
										
											2020-04-21 02:10:03 +08:00
										 |  |  | import { JSONViewCell } from './JSONViewCell'; | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-13 21:34:23 +08:00
										 |  |  |   for (const [fieldIndex, field] of data.fields.entries()) { | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |     const fieldTableOptions = (field.config.custom || {}) as TableFieldOptions; | 
					
						
							| 
									
										
										
										
											2020-04-20 15:27:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-13 21:34:23 +08:00
										 |  |  |     if (fieldTableOptions.hidden) { | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |     if (fieldTableOptions.width) { | 
					
						
							|  |  |  |       availableWidth -= fieldTableOptions.width; | 
					
						
							|  |  |  |       fieldCountWithoutWidth -= 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 02:10:03 +08:00
										 |  |  |     const Cell = getCellComponent(fieldTableOptions.displayMode, field); | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |     columns.push({ | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |       Cell, | 
					
						
							| 
									
										
										
										
											2020-04-20 15:27:40 +08:00
										 |  |  |       id: fieldIndex.toString(), | 
					
						
							| 
									
										
										
										
											2020-05-12 19:52:53 +08:00
										 |  |  |       Header: getFieldDisplayName(field, data), | 
					
						
							| 
									
										
										
										
											2020-04-20 15:27:40 +08:00
										 |  |  |       accessor: (row: any, i: number) => { | 
					
						
							|  |  |  |         return field.values.get(i); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |       width: fieldTableOptions.width, | 
					
						
							| 
									
										
										
										
											2020-04-20 15:27:40 +08:00
										 |  |  |       minWidth: 50, | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 02:10:03 +08:00
										 |  |  | function getCellComponent(displayMode: TableCellDisplayMode, field: Field) { | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |   switch (displayMode) { | 
					
						
							| 
									
										
										
										
											2020-04-09 13:42:31 +08:00
										 |  |  |     case TableCellDisplayMode.ColorText: | 
					
						
							|  |  |  |       return withTableStyles(DefaultCell, getTextColorStyle); | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |     case TableCellDisplayMode.ColorBackground: | 
					
						
							| 
									
										
										
										
											2020-04-09 13:42:31 +08:00
										 |  |  |       return withTableStyles(DefaultCell, getBackgroundColorStyle); | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |     case TableCellDisplayMode.LcdGauge: | 
					
						
							|  |  |  |     case TableCellDisplayMode.GradientGauge: | 
					
						
							|  |  |  |       return BarGaugeCell; | 
					
						
							| 
									
										
										
										
											2020-04-21 02:10:03 +08:00
										 |  |  |     case TableCellDisplayMode.JSONView: | 
					
						
							|  |  |  |       return JSONViewCell; | 
					
						
							| 
									
										
										
										
											2020-01-13 18:12:19 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-04-21 02:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Default or Auto
 | 
					
						
							|  |  |  |   if (field.type === FieldType.other) { | 
					
						
							|  |  |  |     return JSONViewCell; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return DefaultCell; | 
					
						
							| 
									
										
										
										
											2019-12-23 13:22:54 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-09 13:42:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getTextColorStyle(props: TableCellProps) { | 
					
						
							|  |  |  |   const { field, cell, tableStyles } = props; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!field.display) { | 
					
						
							|  |  |  |     return tableStyles; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const displayValue = field.display(cell.value); | 
					
						
							|  |  |  |   if (!displayValue.color) { | 
					
						
							|  |  |  |     return tableStyles; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const extendedStyle = css`
 | 
					
						
							|  |  |  |     color: ${displayValue.color}; | 
					
						
							|  |  |  |   `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     ...tableStyles, | 
					
						
							|  |  |  |     tableCell: cx(tableStyles.tableCell, extendedStyle), | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getBackgroundColorStyle(props: TableCellProps) { | 
					
						
							|  |  |  |   const { field, cell, tableStyles } = props; | 
					
						
							|  |  |  |   if (!field.display) { | 
					
						
							|  |  |  |     return tableStyles; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const displayValue = field.display(cell.value); | 
					
						
							|  |  |  |   if (!displayValue.color) { | 
					
						
							|  |  |  |     return tableStyles; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const themeFactor = tableStyles.theme.isDark ? 1 : -0.7; | 
					
						
							|  |  |  |   const bgColor2 = tinycolor(displayValue.color) | 
					
						
							|  |  |  |     .darken(10 * themeFactor) | 
					
						
							|  |  |  |     .spin(5) | 
					
						
							|  |  |  |     .toRgbString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const extendedStyle = css`
 | 
					
						
							|  |  |  |     background: linear-gradient(120deg, ${bgColor2}, ${displayValue.color}); | 
					
						
							|  |  |  |     color: white; | 
					
						
							|  |  |  |     height: ${tableStyles.cellHeight}px; | 
					
						
							|  |  |  |     padding: ${tableStyles.cellPadding}px; | 
					
						
							|  |  |  |   `;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     ...tableStyles, | 
					
						
							|  |  |  |     tableCell: cx(tableStyles.tableCell, extendedStyle), | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |