panel_model.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { Emitter } from 'app/core/utils/emitter';
  2. import _ from 'lodash';
  3. export interface GridPos {
  4. x: number;
  5. y: number;
  6. w: number;
  7. h: number;
  8. static?: boolean;
  9. }
  10. const notPersistedProperties: { [str: string]: boolean } = {
  11. events: true,
  12. fullscreen: true,
  13. isEditing: true,
  14. hasRefreshed: true,
  15. };
  16. const defaults: any = {
  17. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  18. datasource: null,
  19. targets: [{}],
  20. };
  21. export class PanelModel {
  22. id: number;
  23. gridPos: GridPos;
  24. type: string;
  25. title: string;
  26. alert?: any;
  27. scopedVars?: any;
  28. repeat?: string;
  29. repeatIteration?: number;
  30. repeatPanelId?: number;
  31. repeatDirection?: string;
  32. repeatedByRow?: boolean;
  33. minSpan?: number;
  34. collapsed?: boolean;
  35. panels?: any;
  36. soloMode?: boolean;
  37. targets: any[];
  38. datasource: string;
  39. thresholds?: any;
  40. snapshotData?: any;
  41. timeFrom?: any;
  42. timeShift?: any;
  43. hideTimeOverride?: any;
  44. maxDataPoints?: number;
  45. interval?: string;
  46. description?: string;
  47. links?: [];
  48. // non persisted
  49. fullscreen: boolean;
  50. isEditing: boolean;
  51. hasRefreshed: boolean;
  52. events: Emitter;
  53. constructor(model) {
  54. this.events = new Emitter();
  55. // copy properties from persisted model
  56. for (const property in model) {
  57. this[property] = model[property];
  58. }
  59. // defaults
  60. _.defaultsDeep(this, _.cloneDeep(defaults));
  61. }
  62. getOptions() {
  63. return this[this.getOptionsKey()] || {};
  64. }
  65. updateOptions(options: object) {
  66. const update: any = {};
  67. update[this.getOptionsKey()] = options;
  68. Object.assign(this, update);
  69. this.render();
  70. }
  71. private getOptionsKey() {
  72. return this.type + 'Options';
  73. }
  74. getSaveModel() {
  75. const model: any = {};
  76. for (const property in this) {
  77. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  78. continue;
  79. }
  80. if (_.isEqual(this[property], defaults[property])) {
  81. continue;
  82. }
  83. model[property] = _.cloneDeep(this[property]);
  84. }
  85. return model;
  86. }
  87. setViewMode(fullscreen: boolean, isEditing: boolean) {
  88. this.fullscreen = fullscreen;
  89. this.isEditing = isEditing;
  90. this.events.emit('panel-size-changed');
  91. }
  92. updateGridPos(newPos: GridPos) {
  93. let sizeChanged = false;
  94. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  95. sizeChanged = true;
  96. }
  97. this.gridPos.x = newPos.x;
  98. this.gridPos.y = newPos.y;
  99. this.gridPos.w = newPos.w;
  100. this.gridPos.h = newPos.h;
  101. if (sizeChanged) {
  102. this.events.emit('panel-size-changed');
  103. }
  104. }
  105. resizeDone() {
  106. this.events.emit('panel-size-changed');
  107. }
  108. refresh() {
  109. this.hasRefreshed = true;
  110. this.events.emit('refresh');
  111. }
  112. render() {
  113. if (!this.hasRefreshed) {
  114. this.refresh();
  115. } else {
  116. this.events.emit('render');
  117. }
  118. }
  119. panelInitialized() {
  120. this.events.emit('panel-initialized');
  121. }
  122. changeType(pluginId: string) {
  123. this.type = pluginId;
  124. delete this.thresholds;
  125. delete this.alert;
  126. }
  127. destroy() {
  128. this.events.emit('panel-teardown');
  129. this.events.removeAllListeners();
  130. }
  131. }