panel_model.ts 5.6 KB

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