panel_model.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Libraries
  2. import _ from 'lodash';
  3. // Types
  4. import { Emitter } from 'app/core/utils/emitter';
  5. import { PANEL_OPTIONS_KEY_PREFIX } from 'app/core/constants';
  6. import { DataQuery } from '@grafana/ui/src/types';
  7. export interface GridPos {
  8. x: number;
  9. y: number;
  10. w: number;
  11. h: number;
  12. static?: boolean;
  13. }
  14. const notPersistedProperties: { [str: string]: boolean } = {
  15. events: true,
  16. fullscreen: true,
  17. isEditing: true,
  18. hasRefreshed: true,
  19. cachedPluginOptions: true,
  20. };
  21. // For angular panels we need to clean up properties when changing type
  22. // To make sure the change happens without strange bugs happening when panels use same
  23. // named property with different type / value expectations
  24. // This is not required for react panels
  25. const mustKeepProps: { [str: string]: boolean } = {
  26. id: true,
  27. gridPos: true,
  28. type: true,
  29. title: true,
  30. scopedVars: true,
  31. repeat: true,
  32. repeatIteration: true,
  33. repeatPanelId: true,
  34. repeatDirection: true,
  35. repeatedByRow: true,
  36. minSpan: true,
  37. collapsed: true,
  38. panels: true,
  39. targets: true,
  40. datasource: true,
  41. timeFrom: true,
  42. timeShift: true,
  43. hideTimeOverride: true,
  44. maxDataPoints: true,
  45. interval: true,
  46. description: true,
  47. links: true,
  48. fullscreen: true,
  49. isEditing: true,
  50. hasRefreshed: true,
  51. events: true,
  52. cacheTimeout: true,
  53. nullPointMode: true,
  54. cachedPluginOptions: true,
  55. transparent: true,
  56. };
  57. const defaults: any = {
  58. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  59. datasource: null,
  60. targets: [{}],
  61. cachedPluginOptions: {},
  62. transparent: false,
  63. };
  64. export class PanelModel {
  65. id: number;
  66. gridPos: GridPos;
  67. type: string;
  68. title: string;
  69. alert?: any;
  70. scopedVars?: any;
  71. repeat?: string;
  72. repeatIteration?: number;
  73. repeatPanelId?: number;
  74. repeatDirection?: string;
  75. repeatedByRow?: boolean;
  76. maxPerRow?: number;
  77. collapsed?: boolean;
  78. panels?: any;
  79. soloMode?: boolean;
  80. targets: any[];
  81. datasource: string;
  82. thresholds?: any;
  83. snapshotData?: any;
  84. timeFrom?: any;
  85. timeShift?: any;
  86. hideTimeOverride?: any;
  87. maxDataPoints?: number;
  88. interval?: string;
  89. description?: string;
  90. links?: [];
  91. transparent: boolean;
  92. // non persisted
  93. fullscreen: boolean;
  94. isEditing: boolean;
  95. hasRefreshed: boolean;
  96. events: Emitter;
  97. cacheTimeout?: any;
  98. // cache props between plugins
  99. cachedPluginOptions?: any;
  100. constructor(model) {
  101. this.events = new Emitter();
  102. // copy properties from persisted model
  103. for (const property in model) {
  104. this[property] = model[property];
  105. }
  106. // defaults
  107. _.defaultsDeep(this, _.cloneDeep(defaults));
  108. }
  109. getOptions(panelDefaults) {
  110. return _.defaultsDeep(this[this.getOptionsKey()] || {}, panelDefaults);
  111. }
  112. updateOptions(options: object) {
  113. const update: any = {};
  114. update[this.getOptionsKey()] = options;
  115. Object.assign(this, update);
  116. this.render();
  117. }
  118. private getOptionsKey() {
  119. return PANEL_OPTIONS_KEY_PREFIX + this.type;
  120. }
  121. getSaveModel() {
  122. const model: any = {};
  123. for (const property in this) {
  124. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  125. continue;
  126. }
  127. if (_.isEqual(this[property], defaults[property])) {
  128. continue;
  129. }
  130. model[property] = _.cloneDeep(this[property]);
  131. }
  132. return model;
  133. }
  134. setViewMode(fullscreen: boolean, isEditing: boolean) {
  135. this.fullscreen = fullscreen;
  136. this.isEditing = isEditing;
  137. this.events.emit('view-mode-changed');
  138. }
  139. updateGridPos(newPos: GridPos) {
  140. let sizeChanged = false;
  141. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  142. sizeChanged = true;
  143. }
  144. this.gridPos.x = newPos.x;
  145. this.gridPos.y = newPos.y;
  146. this.gridPos.w = newPos.w;
  147. this.gridPos.h = newPos.h;
  148. if (sizeChanged) {
  149. this.events.emit('panel-size-changed');
  150. }
  151. }
  152. resizeDone() {
  153. this.events.emit('panel-size-changed');
  154. }
  155. refresh() {
  156. this.hasRefreshed = true;
  157. this.events.emit('refresh');
  158. }
  159. render() {
  160. if (!this.hasRefreshed) {
  161. this.refresh();
  162. } else {
  163. this.events.emit('render');
  164. }
  165. }
  166. initialized() {
  167. this.events.emit('panel-initialized');
  168. }
  169. private getOptionsToRemember() {
  170. return Object.keys(this).reduce((acc, property) => {
  171. if (notPersistedProperties[property] || mustKeepProps[property]) {
  172. return acc;
  173. }
  174. return {
  175. ...acc,
  176. [property]: this[property],
  177. };
  178. }, {});
  179. }
  180. private saveCurrentPanelOptions() {
  181. this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
  182. }
  183. private restorePanelOptions(pluginId: string) {
  184. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  185. Object.keys(prevOptions).map(property => {
  186. this[property] = prevOptions[property];
  187. });
  188. }
  189. changeType(pluginId: string, fromAngularPanel: boolean) {
  190. this.saveCurrentPanelOptions();
  191. this.type = pluginId;
  192. // for angular panels only we need to remove all events and let angular panels do some cleanup
  193. if (fromAngularPanel) {
  194. this.destroy();
  195. for (const key of _.keys(this)) {
  196. if (mustKeepProps[key]) {
  197. continue;
  198. }
  199. delete this[key];
  200. }
  201. }
  202. this.restorePanelOptions(pluginId);
  203. }
  204. addQuery(query?: Partial<DataQuery>) {
  205. query = query || { refId: 'A' };
  206. query.refId = this.getNextQueryLetter();
  207. query.isNew = true;
  208. this.targets.push(query);
  209. }
  210. getNextQueryLetter(): string {
  211. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  212. return _.find(letters, refId => {
  213. return _.every(this.targets, other => {
  214. return other.refId !== refId;
  215. });
  216. });
  217. }
  218. destroy() {
  219. this.events.emit('panel-teardown');
  220. this.events.removeAllListeners();
  221. }
  222. }