Files
grafana/public/app/features/dashboard/panel_model.ts

129 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import { Emitter } from 'app/core/utils/emitter';
import _ from 'lodash';
2017-10-10 09:34:14 +02:00
export interface GridPos {
2017-10-10 09:34:14 +02:00
x: number;
y: number;
w: number;
h: number;
2017-10-16 09:55:55 +02:00
static?: boolean;
}
const notPersistedProperties: { [str: string]: boolean } = {
events: true,
fullscreen: true,
2017-12-20 12:33:33 +01:00
isEditing: true,
hasRefreshed: true,
};
export class PanelModel {
id: number;
gridPos: GridPos;
2017-10-10 09:34:14 +02:00
type: string;
title: string;
alert?: any;
2017-10-12 19:01:02 +02:00
scopedVars?: any;
repeat?: string;
repeatIteration?: number;
repeatPanelId?: number;
repeatDirection?: string;
repeatedByRow?: boolean;
minSpan?: number;
2017-10-17 14:53:52 +02:00
collapsed?: boolean;
2017-10-17 12:04:18 +02:00
panels?: any;
2017-11-28 11:38:28 +03:00
soloMode?: boolean;
targets: any[];
2018-06-26 16:32:01 +02:00
datasource: string;
thresholds?: any;
// non persisted
fullscreen: boolean;
isEditing: boolean;
hasRefreshed: boolean;
events: Emitter;
2017-10-10 17:57:53 +02:00
constructor(model) {
this.events = new Emitter();
// copy properties from persisted model
2018-08-29 14:26:50 +02:00
for (const property in model) {
this[property] = model[property];
}
2017-10-12 21:37:27 +02:00
2018-06-26 16:32:01 +02:00
// defaults
this.gridPos = this.gridPos || { x: 0, y: 0, h: 3, w: 6 };
}
getSaveModel() {
2017-10-10 17:57:53 +02:00
const model: any = {};
2018-08-29 14:26:50 +02:00
for (const property in this) {
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
continue;
}
2017-10-12 21:37:27 +02:00
model[property] = _.cloneDeep(this[property]);
}
2017-10-10 17:57:53 +02:00
return model;
}
setViewMode(fullscreen: boolean, isEditing: boolean) {
this.fullscreen = fullscreen;
this.isEditing = isEditing;
2017-12-20 12:33:33 +01:00
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) {
2017-12-20 12:33:33 +01:00
this.events.emit('panel-size-changed');
}
}
2017-10-11 21:36:03 +02:00
resizeDone() {
2017-12-20 12:33:33 +01:00
this.events.emit('panel-size-changed');
2017-10-11 21:36:03 +02:00
}
2017-10-16 16:09:23 +02:00
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');
}
initEditMode() {
this.events.emit('panel-init-edit-mode');
}
changeType(pluginId: string) {
this.type = pluginId;
delete this.thresholds;
delete this.alert;
2018-07-09 18:17:51 +02:00
}
2017-10-16 16:09:23 +02:00
destroy() {
this.events.removeAllListeners();
}
2017-10-10 09:34:14 +02:00
}