panel_model.ts 3.0 KB

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