2016-10-30 09:14:18 -05:00
|
|
|
///<reference path="../../../headers/common.d.ts" />
|
|
|
|
|
2016-10-30 12:02:46 -05:00
|
|
|
import _ from 'lodash';
|
2016-11-02 09:16:48 -05:00
|
|
|
import {Emitter, contextSrv, appEvents, assignModelProperties} from 'app/core/core';
|
2016-10-30 09:14:18 -05:00
|
|
|
|
|
|
|
export class DashboardRow {
|
|
|
|
panels: any;
|
|
|
|
title: any;
|
|
|
|
showTitle: any;
|
|
|
|
titleSize: any;
|
|
|
|
events: Emitter;
|
2016-10-30 12:02:46 -05:00
|
|
|
span: number;
|
2016-11-01 07:43:05 -05:00
|
|
|
height: number;
|
2016-11-04 05:43:44 -05:00
|
|
|
collapse: boolean;
|
2016-10-30 09:14:18 -05:00
|
|
|
|
|
|
|
defaults = {
|
|
|
|
title: 'Dashboard Row',
|
|
|
|
panels: [],
|
|
|
|
showTitle: false,
|
|
|
|
titleSize: 'h6',
|
|
|
|
height: 250,
|
|
|
|
isNew: false,
|
2016-11-01 07:43:05 -05:00
|
|
|
repeat: null,
|
|
|
|
repeatRowId: null,
|
|
|
|
repeatIteration: null,
|
2016-11-04 05:43:44 -05:00
|
|
|
collapse: false,
|
2016-10-30 09:14:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(private model) {
|
|
|
|
assignModelProperties(this, model, this.defaults);
|
|
|
|
this.events = new Emitter();
|
2016-10-30 12:02:46 -05:00
|
|
|
this.updateRowSpan();
|
2016-10-30 09:14:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getSaveModel() {
|
2016-11-17 05:32:11 -06:00
|
|
|
this.model = {};
|
2016-10-30 09:14:18 -05:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
2016-11-17 03:11:59 -06:00
|
|
|
|
|
|
|
// remove properties that dont server persisted purpose
|
|
|
|
delete this.model.isNew;
|
2016-10-30 09:14:18 -05:00
|
|
|
return this.model;
|
|
|
|
}
|
2016-10-30 12:02:46 -05:00
|
|
|
|
|
|
|
updateRowSpan() {
|
|
|
|
this.span = 0;
|
|
|
|
for (let panel of this.panels) {
|
|
|
|
this.span += panel.span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 10:41:17 -06:00
|
|
|
panelSpanChanged(alwaysSendEvent?) {
|
2016-10-30 12:02:46 -05:00
|
|
|
var oldSpan = this.span;
|
|
|
|
this.updateRowSpan();
|
|
|
|
|
2016-11-06 10:41:17 -06:00
|
|
|
if (alwaysSendEvent || oldSpan !== this.span) {
|
2016-10-30 12:02:46 -05:00
|
|
|
this.events.emit('span-changed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addPanel(panel) {
|
|
|
|
var rowSpan = this.span;
|
|
|
|
var panelCount = this.panels.length;
|
|
|
|
var space = (12 - rowSpan) - panel.span;
|
|
|
|
|
|
|
|
// try to make room of there is no space left
|
|
|
|
if (space <= 0) {
|
|
|
|
if (panelCount === 1) {
|
|
|
|
this.panels[0].span = 6;
|
|
|
|
panel.span = 6;
|
|
|
|
} else if (panelCount === 2) {
|
|
|
|
this.panels[0].span = 4;
|
|
|
|
this.panels[1].span = 4;
|
|
|
|
panel.span = 4;
|
2016-10-30 13:37:50 -05:00
|
|
|
} else if (panelCount === 3) {
|
|
|
|
this.panels[0].span = 3;
|
|
|
|
this.panels[1].span = 3;
|
|
|
|
this.panels[2].span = 3;
|
|
|
|
panel.span = 3;
|
2016-10-30 12:02:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.panels.push(panel);
|
|
|
|
this.events.emit('panel-added', panel);
|
|
|
|
this.panelSpanChanged();
|
|
|
|
}
|
|
|
|
|
2016-11-02 09:16:48 -05:00
|
|
|
removePanel(panel, ask?) {
|
|
|
|
if (ask !== false) {
|
2016-12-06 01:11:00 -06:00
|
|
|
var text2, confirmText;
|
|
|
|
if (panel.alert) {
|
|
|
|
text2 = "Panel includes an alert rule, removing panel will also remove alert rule";
|
|
|
|
confirmText = "YES";
|
|
|
|
}
|
|
|
|
|
2016-11-02 09:16:48 -05:00
|
|
|
appEvents.emit('confirm-modal', {
|
|
|
|
title: 'Remove Panel',
|
|
|
|
text: 'Are you sure you want to remove this panel?',
|
2016-12-06 01:11:00 -06:00
|
|
|
text2: text2,
|
2016-11-02 09:16:48 -05:00
|
|
|
icon: 'fa-trash',
|
2016-12-06 01:11:00 -06:00
|
|
|
confirmText: confirmText,
|
2016-11-02 09:16:48 -05:00
|
|
|
yesText: 'Remove',
|
|
|
|
onConfirm: () => {
|
|
|
|
this.removePanel(panel, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-30 12:02:46 -05:00
|
|
|
var index = _.indexOf(this.panels, panel);
|
|
|
|
this.panels.splice(index, 1);
|
|
|
|
this.events.emit('panel-removed', panel);
|
|
|
|
this.panelSpanChanged();
|
|
|
|
}
|
2016-10-30 13:37:50 -05:00
|
|
|
|
|
|
|
movePanel(fromIndex, toIndex) {
|
|
|
|
this.panels.splice(toIndex, 0, this.panels.splice(fromIndex, 1)[0]);
|
|
|
|
}
|
2016-11-01 07:43:05 -05:00
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.events.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
copyPropertiesFromRowSource(source) {
|
|
|
|
this.height = source.height;
|
|
|
|
this.title = source.title;
|
|
|
|
this.showTitle = source.showTitle;
|
|
|
|
this.titleSize = source.titleSize;
|
|
|
|
}
|
2016-11-04 05:43:44 -05:00
|
|
|
|
|
|
|
toggleCollapse() {
|
|
|
|
this.collapse = !this.collapse;
|
|
|
|
}
|
2016-10-30 09:14:18 -05:00
|
|
|
}
|
|
|
|
|