panel_model.ts 2.9 KB

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