| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  | import { omit } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-16 23:01:54 +08:00
										 |  |  | export function calculatePanelSize(mode: DisplayMode, width: number, height: number, panel: PanelModel) { | 
					
						
							| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  |   if (mode === DisplayMode.Fill) { | 
					
						
							|  |  |  |     return { width, height }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-19 23:42:45 +08:00
										 |  |  |   const panelPadding = 8 * 6; | 
					
						
							|  |  |  |   const sidebarWidth = 60; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const colWidth = (window.innerWidth - sidebarWidth - GRID_CELL_VMARGIN * 4) / GRID_COLUMN_COUNT; | 
					
						
							| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  |   const pWidth = colWidth * panel.gridPos.w; | 
					
						
							| 
									
										
										
										
											2021-05-19 23:42:45 +08:00
										 |  |  |   const pHeight = GRID_CELL_HEIGHT * panel.gridPos.h + panelPadding; | 
					
						
							| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  |   const scale = Math.min(width / pWidth, height / pHeight); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 23:42:45 +08:00
										 |  |  |   if (pWidth <= width && pHeight <= height) { | 
					
						
							| 
									
										
										
										
											2020-02-11 20:48:36 +08:00
										 |  |  |     return { | 
					
						
							|  |  |  |       width: pWidth, | 
					
						
							|  |  |  |       height: pHeight, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     width: pWidth * scale, | 
					
						
							|  |  |  |     height: pHeight * scale, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-04 19:58:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 19:42:14 +08:00
										 |  |  | export function supportsDataQuery(plugin: PanelPlugin | undefined | null): boolean { | 
					
						
							| 
									
										
										
										
											2020-05-04 19:58:05 +08:00
										 |  |  |   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 }; | 
					
						
							| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  |   const remove = value == null || value === ''; | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (isCustom) { | 
					
						
							|  |  |  |     if (defaults.custom) { | 
					
						
							|  |  |  |       if (remove) { | 
					
						
							|  |  |  |         defaults.custom = omit(defaults.custom, name); | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  |         defaults.custom = setOptionImmutably(defaults.custom, name, value); | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } else if (!remove) { | 
					
						
							| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  |       defaults.custom = setOptionImmutably(defaults.custom, name, value); | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } else if (remove) { | 
					
						
							|  |  |  |     defaults = omit(defaults, name); | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  |     defaults = setOptionImmutably(defaults, name, value); | 
					
						
							| 
									
										
										
										
											2020-09-15 16:17:58 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     ...config, | 
					
						
							|  |  |  |     defaults, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function setOptionImmutably<T extends object>(options: T, path: string | string[], value: any): T { | 
					
						
							|  |  |  |   const splat = !Array.isArray(path) ? path.split('.') : path; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const key = splat.shift()!; | 
					
						
							| 
									
										
										
										
											2021-09-24 00:28:16 +08:00
										 |  |  |   if (key.endsWith(']')) { | 
					
						
							|  |  |  |     const idx = key.lastIndexOf('['); | 
					
						
							|  |  |  |     const index = +key.substring(idx + 1, key.length - 1); | 
					
						
							| 
									
										
										
										
											2022-04-04 17:08:06 +08:00
										 |  |  |     const propKey = key.substring(0, idx); | 
					
						
							| 
									
										
										
										
											2021-09-24 00:28:16 +08:00
										 |  |  |     let current = (options as Record<string, any>)[propKey]; | 
					
						
							|  |  |  |     const arr = Array.isArray(current) ? [...current] : []; | 
					
						
							|  |  |  |     if (splat.length) { | 
					
						
							| 
									
										
										
										
											2021-09-25 23:22:52 +08:00
										 |  |  |       current = arr[index]; | 
					
						
							| 
									
										
										
										
											2021-09-24 00:28:16 +08:00
										 |  |  |       if (current == null || typeof current !== 'object') { | 
					
						
							|  |  |  |         current = {}; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       value = setOptionImmutably(current, splat, value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     arr[index] = value; | 
					
						
							|  |  |  |     return { ...options, [propKey]: arr }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-07-07 14:58:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!splat.length) { | 
					
						
							|  |  |  |     return { ...options, [key]: value }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let current = (options as Record<string, any>)[key]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (current == null || typeof current !== 'object') { | 
					
						
							|  |  |  |     current = {}; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { ...options, [key]: setOptionImmutably(current, splat, value) }; | 
					
						
							|  |  |  | } |