| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  | import { CSSProperties } from 'react'; | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  | import { set as lodashSet, omit } from 'lodash'; | 
					
						
							|  |  |  | import { FieldConfigSource, PanelPlugin } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  | import { PanelModel } from '../../state/PanelModel'; | 
					
						
							|  |  |  | import { DisplayMode } from './types'; | 
					
						
							| 
									
										
										
										
											2020-02-11 21:57:16 +08:00
										 |  |  | import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants'; | 
					
						
							| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function calculatePanelSize(mode: DisplayMode, width: number, height: number, panel: PanelModel): CSSProperties { | 
					
						
							|  |  |  |   if (mode === DisplayMode.Fill) { | 
					
						
							|  |  |  |     return { width, height }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const colWidth = (window.innerWidth - GRID_CELL_VMARGIN * 4) / GRID_COLUMN_COUNT; | 
					
						
							|  |  |  |   const pWidth = colWidth * panel.gridPos.w; | 
					
						
							|  |  |  |   const pHeight = GRID_CELL_HEIGHT * panel.gridPos.h; | 
					
						
							|  |  |  |   const scale = Math.min(width / pWidth, height / pHeight); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (mode === DisplayMode.Exact && pWidth <= width && pHeight <= height) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       width: pWidth, | 
					
						
							|  |  |  |       height: pHeight, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     width: pWidth * scale, | 
					
						
							|  |  |  |     height: pHeight * scale, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-04 19:58:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function supportsDataQuery(plugin: PanelPlugin | undefined): boolean { | 
					
						
							|  |  |  |   return plugin?.meta.skipDataQuery === false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const updateDefaultFieldConfigValue = ( | 
					
						
							|  |  |  |   config: FieldConfigSource, | 
					
						
							|  |  |  |   name: string, | 
					
						
							|  |  |  |   value: any, | 
					
						
							|  |  |  |   isCustom?: boolean | 
					
						
							|  |  |  | ) => { | 
					
						
							|  |  |  |   let defaults = { ...config.defaults }; | 
					
						
							|  |  |  |   const remove = value === undefined || value === null || ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (isCustom) { | 
					
						
							|  |  |  |     if (defaults.custom) { | 
					
						
							|  |  |  |       if (remove) { | 
					
						
							|  |  |  |         defaults.custom = omit(defaults.custom, name); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         defaults.custom = lodashSet({ ...defaults.custom }, name, value); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else if (!remove) { | 
					
						
							|  |  |  |       defaults.custom = lodashSet({ ...defaults.custom }, name, value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else if (remove) { | 
					
						
							|  |  |  |     defaults = omit(defaults, name); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     defaults = lodashSet({ ...defaults }, name, value); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     ...config, | 
					
						
							|  |  |  |     defaults, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |