| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import { Emitter } from 'app/core/utils/emitter';
- import _ from 'lodash';
- export interface GridPos {
- x: number;
- y: number;
- w: number;
- h: number;
- static?: boolean;
- }
- const notPersistedProperties: { [str: string]: boolean } = {
- events: true,
- fullscreen: true,
- isEditing: true,
- hasRefreshed: true,
- };
- const defaults: any = {
- gridPos: { x: 0, y: 0, h: 3, w: 6 },
- datasource: null,
- targets: [{}],
- };
- export class PanelModel {
- id: number;
- gridPos: GridPos;
- type: string;
- title: string;
- alert?: any;
- scopedVars?: any;
- repeat?: string;
- repeatIteration?: number;
- repeatPanelId?: number;
- repeatDirection?: string;
- repeatedByRow?: boolean;
- minSpan?: number;
- collapsed?: boolean;
- panels?: any;
- soloMode?: boolean;
- targets: any[];
- datasource: string;
- thresholds?: any;
- snapshotData?: any;
- timeFrom?: any;
- timeShift?: any;
- hideTimeOverride?: any;
- maxDataPoints?: number;
- interval?: string;
- description?: string;
- // non persisted
- fullscreen: boolean;
- isEditing: boolean;
- hasRefreshed: boolean;
- events: Emitter;
- constructor(model) {
- this.events = new Emitter();
- // copy properties from persisted model
- for (const property in model) {
- this[property] = model[property];
- }
- // defaults
- _.defaultsDeep(this, _.cloneDeep(defaults));
- }
- getOptions() {
- return this[this.getOptionsKey()] || {};
- }
- updateOptions(options: object) {
- const update: any = {};
- update[this.getOptionsKey()] = options;
- Object.assign(this, update);
- this.render();
- }
- private getOptionsKey() {
- return this.type + 'Options';
- }
- getSaveModel() {
- const model: any = {};
- for (const property in this) {
- if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
- continue;
- }
- if (_.isEqual(this[property], defaults[property])) {
- continue;
- }
- model[property] = _.cloneDeep(this[property]);
- }
- return model;
- }
- setViewMode(fullscreen: boolean, isEditing: boolean) {
- this.fullscreen = fullscreen;
- this.isEditing = isEditing;
- this.events.emit('panel-size-changed');
- }
- updateGridPos(newPos: GridPos) {
- let sizeChanged = false;
- if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
- sizeChanged = true;
- }
- this.gridPos.x = newPos.x;
- this.gridPos.y = newPos.y;
- this.gridPos.w = newPos.w;
- this.gridPos.h = newPos.h;
- if (sizeChanged) {
- this.events.emit('panel-size-changed');
- }
- }
- resizeDone() {
- this.events.emit('panel-size-changed');
- }
- refresh() {
- this.hasRefreshed = true;
- this.events.emit('refresh');
- }
- render() {
- if (!this.hasRefreshed) {
- this.refresh();
- } else {
- this.events.emit('render');
- }
- }
- panelInitialized() {
- this.events.emit('panel-initialized');
- }
- changeType(pluginId: string) {
- this.type = pluginId;
- delete this.thresholds;
- delete this.alert;
- }
- destroy() {
- this.events.emit('panel-teardown');
- this.events.removeAllListeners();
- }
- }
|