panel_model.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // non persisted
  47. fullscreen: boolean;
  48. isEditing: boolean;
  49. hasRefreshed: boolean;
  50. events: Emitter;
  51. constructor(model) {
  52. this.events = new Emitter();
  53. // copy properties from persisted model
  54. for (const property in model) {
  55. this[property] = model[property];
  56. }
  57. // defaults
  58. _.defaultsDeep(this, _.cloneDeep(defaults));
  59. }
  60. getOptions() {
  61. return this[this.getOptionsKey()] || {};
  62. }
  63. updateOptions(options: object) {
  64. const update: any = {};
  65. update[this.getOptionsKey()] = options;
  66. Object.assign(this, update);
  67. this.render();
  68. }
  69. private getOptionsKey() {
  70. return this.type + 'Options';
  71. }
  72. getSaveModel() {
  73. const model: any = {};
  74. for (const property in this) {
  75. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  76. continue;
  77. }
  78. if (_.isEqual(this[property], defaults[property])) {
  79. continue;
  80. }
  81. model[property] = _.cloneDeep(this[property]);
  82. }
  83. return model;
  84. }
  85. setViewMode(fullscreen: boolean, isEditing: boolean) {
  86. this.fullscreen = fullscreen;
  87. this.isEditing = isEditing;
  88. this.events.emit('panel-size-changed');
  89. }
  90. updateGridPos(newPos: GridPos) {
  91. let sizeChanged = false;
  92. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  93. sizeChanged = true;
  94. }
  95. this.gridPos.x = newPos.x;
  96. this.gridPos.y = newPos.y;
  97. this.gridPos.w = newPos.w;
  98. this.gridPos.h = newPos.h;
  99. if (sizeChanged) {
  100. this.events.emit('panel-size-changed');
  101. }
  102. }
  103. resizeDone() {
  104. this.events.emit('panel-size-changed');
  105. }
  106. refresh() {
  107. this.hasRefreshed = true;
  108. this.events.emit('refresh');
  109. }
  110. render() {
  111. if (!this.hasRefreshed) {
  112. this.refresh();
  113. } else {
  114. this.events.emit('render');
  115. }
  116. }
  117. panelInitialized() {
  118. this.events.emit('panel-initialized');
  119. }
  120. changeType(pluginId: string) {
  121. this.type = pluginId;
  122. delete this.thresholds;
  123. delete this.alert;
  124. }
  125. destroy() {
  126. this.events.emit('panel-teardown');
  127. this.events.removeAllListeners();
  128. }
  129. }