grafana/public/app/features/dashboard/dashboard_ctrl.ts

146 lines
3.9 KiB
TypeScript
Raw Normal View History

import config from "app/core/config";
import coreModule from "app/core/core_module";
import { PanelContainer } from "./dashgrid/PanelContainer";
import { DashboardModel } from "./dashboard_model";
2017-10-10 15:34:14 +08:00
export class DashboardCtrl implements PanelContainer {
dashboard: DashboardModel;
dashboardViewState: any;
loadedFallbackDashboard: boolean;
2017-10-10 23:57:53 +08:00
editTab: number;
/** @ngInject */
constructor(
private $scope,
2017-10-10 15:34:14 +08:00
private $rootScope,
private keybindingSrv,
private timeSrv,
private variableSrv,
private alertingSrv,
private dashboardSrv,
private unsavedChangesSrv,
private dashboardViewStateSrv,
public playlistSrv,
private panelLoader
) {
// 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) {
this.onInitFailed(err, "Dashboard init failed", true);
2017-10-10 15:34:14 +08:00
}
}
2017-10-10 15:34:14 +08:00
setupDashboardInternal(data) {
const dashboard = this.dashboardSrv.create(data.dashboard, data.meta);
this.dashboardSrv.setCurrent(dashboard);
2017-10-10 15:34:14 +08:00
// init services
this.timeSrv.init(dashboard);
this.alertingSrv.init(dashboard, data.alerts);
2017-10-10 15:34:14 +08:00
// template values service needs to initialize completely before
// the rest of the dashboard can load
this.variableSrv
.init(dashboard)
2017-10-10 15:34:14 +08:00
// template values failes are non fatal
.catch(this.onInitFailed.bind(this, "Templating init failed", false))
2017-10-10 15:34:14 +08:00
// continue
.finally(() => {
2017-10-13 03:37:27 +08:00
this.dashboard = dashboard;
this.dashboard.processRepeats();
2017-10-10 15:34:14 +08:00
this.unsavedChangesSrv.init(dashboard, this.$scope);
// TODO refactor ViewStateSrv
this.$scope.dashboard = dashboard;
this.dashboardViewState = this.dashboardViewStateSrv.create(
this.$scope
);
2017-10-10 15:34:14 +08:00
this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
this.dashboard.updateSubmenuVisibility();
this.setWindowTitleAndTheme();
this.$scope.appEvent("dashboard-initialized", dashboard);
})
.catch(this.onInitFailed.bind(this, "Dashboard init failed", true));
}
2017-10-10 15:34:14 +08:00
onInitFailed(msg, fatal, err) {
console.log(msg, err);
2017-10-10 15:34:14 +08:00
if (err.data && err.data.message) {
err.message = err.data.message;
} else if (!err.message) {
err = { message: err.toString() };
2017-10-10 15:34:14 +08:00
}
this.$scope.appEvent("alert-error", [msg, err.message]);
// protect against recursive fallbacks
if (fatal && !this.loadedFallbackDashboard) {
this.loadedFallbackDashboard = true;
this.setupDashboard({ dashboard: { title: "Dashboard Init failed" } });
}
}
templateVariableUpdated() {
this.dashboard.processRepeats();
}
setWindowTitleAndTheme() {
window.document.title = config.window_title_prefix + this.dashboard.title;
}
showJsonEditor(evt, options) {
var editScope = this.$rootScope.$new();
editScope.object = options.object;
editScope.updateHandler = options.updateHandler;
this.$scope.appEvent("show-dash-editor", {
src: "public/app/partials/edit_json.html",
scope: editScope
});
}
getDashboard() {
return this.dashboard;
}
getPanelLoader() {
return this.panelLoader;
}
timezoneChanged() {
this.$rootScope.$broadcast("refresh");
}
getPanelContainer() {
return this;
}
init(dashboard) {
this.$scope.onAppEvent("show-json-editor", this.showJsonEditor.bind(this));
this.$scope.onAppEvent(
"template-variable-value-updated",
this.templateVariableUpdated.bind(this)
);
this.setupDashboard(dashboard);
}
}
coreModule.controller("DashboardCtrl", DashboardCtrl);