utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { omit } from 'lodash';
  2. import { FieldConfigSource, PanelPlugin } from '@grafana/data';
  3. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants';
  4. import { PanelModel } from '../../state/PanelModel';
  5. import { DisplayMode } from './types';
  6. export function calculatePanelSize(mode: DisplayMode, width: number, height: number, panel: PanelModel) {
  7. if (mode === DisplayMode.Fill) {
  8. return { width, height };
  9. }
  10. const panelPadding = 8 * 6;
  11. const sidebarWidth = 60;
  12. const colWidth = (window.innerWidth - sidebarWidth - GRID_CELL_VMARGIN * 4) / GRID_COLUMN_COUNT;
  13. const pWidth = colWidth * panel.gridPos.w;
  14. const pHeight = GRID_CELL_HEIGHT * panel.gridPos.h + panelPadding;
  15. const scale = Math.min(width / pWidth, height / pHeight);
  16. if (pWidth <= width && pHeight <= height) {
  17. return {
  18. width: pWidth,
  19. height: pHeight,
  20. };
  21. }
  22. return {
  23. width: pWidth * scale,
  24. height: pHeight * scale,
  25. };
  26. }
  27. export function supportsDataQuery(plugin: PanelPlugin | undefined | null): boolean {
  28. return plugin?.meta.skipDataQuery === false;
  29. }
  30. export const updateDefaultFieldConfigValue = (
  31. config: FieldConfigSource,
  32. name: string,
  33. value: any,
  34. isCustom?: boolean
  35. ) => {
  36. let defaults = { ...config.defaults };
  37. const remove = value == null || value === '';
  38. if (isCustom) {
  39. if (defaults.custom) {
  40. if (remove) {
  41. defaults.custom = omit(defaults.custom, name);
  42. } else {
  43. defaults.custom = setOptionImmutably(defaults.custom, name, value);
  44. }
  45. } else if (!remove) {
  46. defaults.custom = setOptionImmutably(defaults.custom, name, value);
  47. }
  48. } else if (remove) {
  49. defaults = omit(defaults, name);
  50. } else {
  51. defaults = setOptionImmutably(defaults, name, value);
  52. }
  53. return {
  54. ...config,
  55. defaults,
  56. };
  57. };
  58. export function setOptionImmutably<T extends object>(options: T, path: string | string[], value: any): T {
  59. const splat = !Array.isArray(path) ? path.split('.') : path;
  60. const key = splat.shift()!;
  61. if (key.endsWith(']')) {
  62. const idx = key.lastIndexOf('[');
  63. const index = +key.substring(idx + 1, key.length - 1);
  64. const propKey = key.substring(0, idx);
  65. let current = (options as Record<string, any>)[propKey];
  66. const arr = Array.isArray(current) ? [...current] : [];
  67. if (splat.length) {
  68. current = arr[index];
  69. if (current == null || typeof current !== 'object') {
  70. current = {};
  71. }
  72. value = setOptionImmutably(current, splat, value);
  73. }
  74. arr[index] = value;
  75. return { ...options, [propKey]: arr };
  76. }
  77. if (!splat.length) {
  78. return { ...options, [key]: value };
  79. }
  80. let current = (options as Record<string, any>)[key];
  81. if (current == null || typeof current !== 'object') {
  82. current = {};
  83. }
  84. return { ...options, [key]: setOptionImmutably(current, splat, value) };
  85. }