2018-10-25 04:35:32 -05:00
|
|
|
// Utils
|
2017-12-20 05:33:33 -06:00
|
|
|
import config from 'app/core/config';
|
2018-10-25 04:35:32 -05:00
|
|
|
import appEvents from 'app/core/app_events';
|
2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
2018-10-25 04:35:32 -05:00
|
|
|
|
|
|
|
// Services
|
|
|
|
import { AnnotationsSrv } from '../annotations/annotations_srv';
|
|
|
|
|
|
|
|
// Types
|
2017-12-20 05:33:33 -06:00
|
|
|
import { DashboardModel } from './dashboard_model';
|
2018-02-22 11:02:29 -06:00
|
|
|
import { PanelModel } from './panel_model';
|
2016-04-14 15:53:19 -05:00
|
|
|
|
2018-06-19 14:25:57 -05:00
|
|
|
export class DashboardCtrl {
|
2017-10-10 02:34:14 -05:00
|
|
|
dashboard: DashboardModel;
|
|
|
|
dashboardViewState: any;
|
|
|
|
loadedFallbackDashboard: boolean;
|
2017-10-10 10:57:53 -05:00
|
|
|
editTab: number;
|
2016-04-14 15:53:19 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
|
|
|
constructor(
|
|
|
|
private $scope,
|
2017-10-10 02:34:14 -05:00
|
|
|
private $rootScope,
|
|
|
|
private keybindingSrv,
|
|
|
|
private timeSrv,
|
|
|
|
private variableSrv,
|
|
|
|
private alertingSrv,
|
|
|
|
private dashboardSrv,
|
|
|
|
private unsavedChangesSrv,
|
|
|
|
private dashboardViewStateSrv,
|
2018-10-25 04:35:32 -05:00
|
|
|
private annotationsSrv: AnnotationsSrv,
|
2018-06-19 14:25:57 -05:00
|
|
|
public playlistSrv
|
2017-12-19 09:06:54 -06:00
|
|
|
) {
|
|
|
|
// temp hack due to way dashboards are loaded
|
|
|
|
// can't use controllerAs on route yet
|
|
|
|
$scope.ctrl = this;
|
|
|
|
|
|
|
|
// TODO: break out settings view to separate view & controller
|
|
|
|
this.editTab = 0;
|
|
|
|
|
|
|
|
// funcs called from React component bindings and needs this binding
|
|
|
|
this.getPanelContainer = this.getPanelContainer.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
setupDashboard(data) {
|
|
|
|
try {
|
|
|
|
this.setupDashboardInternal(data);
|
|
|
|
} catch (err) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.onInitFailed(err, 'Dashboard init failed', true);
|
2017-10-10 02:34:14 -05:00
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
setupDashboardInternal(data) {
|
|
|
|
const dashboard = this.dashboardSrv.create(data.dashboard, data.meta);
|
|
|
|
this.dashboardSrv.setCurrent(dashboard);
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
// init services
|
|
|
|
this.timeSrv.init(dashboard);
|
|
|
|
this.alertingSrv.init(dashboard, data.alerts);
|
2018-10-25 04:35:32 -05:00
|
|
|
this.annotationsSrv.init(dashboard);
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
// template values service needs to initialize completely before
|
|
|
|
// the rest of the dashboard can load
|
|
|
|
this.variableSrv
|
|
|
|
.init(dashboard)
|
2017-10-10 02:34:14 -05:00
|
|
|
// template values failes are non fatal
|
2017-12-20 05:33:33 -06:00
|
|
|
.catch(this.onInitFailed.bind(this, 'Templating init failed', false))
|
2017-10-10 02:34:14 -05:00
|
|
|
// continue
|
|
|
|
.finally(() => {
|
2017-10-12 14:37:27 -05:00
|
|
|
this.dashboard = dashboard;
|
|
|
|
this.dashboard.processRepeats();
|
2018-08-05 04:27:02 -05:00
|
|
|
this.dashboard.updateSubmenuVisibility();
|
2018-08-05 04:04:12 -05:00
|
|
|
this.dashboard.autoFitPanels(window.innerHeight);
|
2017-10-10 02:34:14 -05:00
|
|
|
|
|
|
|
this.unsavedChangesSrv.init(dashboard, this.$scope);
|
|
|
|
|
|
|
|
// TODO refactor ViewStateSrv
|
|
|
|
this.$scope.dashboard = dashboard;
|
2017-12-21 01:39:31 -06:00
|
|
|
this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
|
2017-10-10 02:34:14 -05:00
|
|
|
|
|
|
|
this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
|
|
|
|
this.setWindowTitleAndTheme();
|
|
|
|
|
2018-10-25 04:35:32 -05:00
|
|
|
appEvents.emit('dashboard-initialized', dashboard);
|
2017-10-10 02:34:14 -05:00
|
|
|
})
|
2017-12-20 05:33:33 -06:00
|
|
|
.catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
onInitFailed(msg, fatal, err) {
|
|
|
|
console.log(msg, err);
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (err.data && err.data.message) {
|
|
|
|
err.message = err.data.message;
|
|
|
|
} else if (!err.message) {
|
|
|
|
err = { message: err.toString() };
|
2017-10-10 02:34:14 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$scope.appEvent('alert-error', [msg, err.message]);
|
2016-04-14 15:53:19 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
// protect against recursive fallbacks
|
|
|
|
if (fatal && !this.loadedFallbackDashboard) {
|
|
|
|
this.loadedFallbackDashboard = true;
|
2017-12-20 05:33:33 -06:00
|
|
|
this.setupDashboard({ dashboard: { title: 'Dashboard Init failed' } });
|
2016-04-14 15:53:19 -05:00
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
templateVariableUpdated() {
|
|
|
|
this.dashboard.processRepeats();
|
|
|
|
}
|
|
|
|
|
|
|
|
setWindowTitleAndTheme() {
|
2018-09-03 04:00:46 -05:00
|
|
|
window.document.title = config.windowTitlePrefix + this.dashboard.title;
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
showJsonEditor(evt, options) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const editScope = this.$rootScope.$new();
|
2017-12-19 09:06:54 -06:00
|
|
|
editScope.object = options.object;
|
|
|
|
editScope.updateHandler = options.updateHandler;
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$scope.appEvent('show-dash-editor', {
|
|
|
|
src: 'public/app/partials/edit_json.html',
|
|
|
|
scope: editScope,
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getDashboard() {
|
|
|
|
return this.dashboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPanelContainer() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:02:29 -06:00
|
|
|
onRemovingPanel(evt, options) {
|
|
|
|
options = options || {};
|
|
|
|
if (!options.panelId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const panelInfo = this.dashboard.getPanelInfoById(options.panelId);
|
2018-02-22 11:02:29 -06:00
|
|
|
this.removePanel(panelInfo.panel, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
removePanel(panel: PanelModel, ask: boolean) {
|
|
|
|
// confirm deletion
|
|
|
|
if (ask !== false) {
|
2018-08-30 01:58:43 -05:00
|
|
|
let text2, confirmText;
|
2018-02-22 11:02:29 -06:00
|
|
|
|
|
|
|
if (panel.alert) {
|
|
|
|
text2 = 'Panel includes an alert rule, removing panel will also remove alert rule';
|
|
|
|
confirmText = 'YES';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$scope.appEvent('confirm-modal', {
|
|
|
|
title: 'Remove Panel',
|
|
|
|
text: 'Are you sure you want to remove this panel?',
|
|
|
|
text2: text2,
|
|
|
|
icon: 'fa-trash',
|
|
|
|
confirmText: confirmText,
|
|
|
|
yesText: 'Remove',
|
|
|
|
onConfirm: () => {
|
|
|
|
this.removePanel(panel, false);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dashboard.removePanel(panel);
|
|
|
|
}
|
|
|
|
|
2018-10-19 02:43:54 -05:00
|
|
|
onDestroy() {
|
|
|
|
if (this.dashboard) {
|
|
|
|
this.dashboard.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
init(dashboard) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
|
2017-12-21 01:39:31 -06:00
|
|
|
this.$scope.onAppEvent('template-variable-value-updated', this.templateVariableUpdated.bind(this));
|
2018-02-22 11:02:29 -06:00
|
|
|
this.$scope.onAppEvent('panel-remove', this.onRemovingPanel.bind(this));
|
2018-10-19 02:43:54 -05:00
|
|
|
this.$scope.$on('$destroy', this.onDestroy.bind(this));
|
2017-12-19 09:06:54 -06:00
|
|
|
this.setupDashboard(dashboard);
|
|
|
|
}
|
2016-04-14 15:53:19 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.controller('DashboardCtrl', DashboardCtrl);
|