2013-09-13 15:52:13 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2013-12-29 04:46:36 -06:00
|
|
|
'jquery',
|
2013-09-13 15:52:13 -05:00
|
|
|
'config',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2013-09-13 15:52:13 -05:00
|
|
|
],
|
2015-02-04 04:35:19 -06:00
|
|
|
function (angular, $, config) {
|
2013-09-13 15:52:13 -05:00
|
|
|
"use strict";
|
|
|
|
|
2014-08-13 03:07:32 -05:00
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2014-08-16 01:55:05 -05:00
|
|
|
module.controller('DashboardCtrl', function(
|
2014-08-27 08:54:30 -05:00
|
|
|
$scope,
|
|
|
|
$rootScope,
|
|
|
|
dashboardKeybindings,
|
2014-08-27 09:29:48 -05:00
|
|
|
timeSrv,
|
2014-08-27 14:47:41 -05:00
|
|
|
templateValuesSrv,
|
2015-03-14 15:13:25 -05:00
|
|
|
dynamicDashboardSrv,
|
2014-08-27 08:54:30 -05:00
|
|
|
dashboardSrv,
|
2015-04-28 06:45:59 -05:00
|
|
|
unsavedChangesSrv,
|
2014-08-27 08:54:30 -05:00
|
|
|
dashboardViewStateSrv,
|
2015-03-25 09:48:51 -05:00
|
|
|
contextSrv,
|
2014-08-27 08:54:30 -05:00
|
|
|
$timeout) {
|
2013-10-12 10:08:16 -05:00
|
|
|
|
2014-06-08 07:40:44 -05:00
|
|
|
$scope.editor = { index: 0 };
|
2015-03-28 11:53:52 -05:00
|
|
|
$scope.topNavPartial = 'app/features/dashboard/partials/dashboardTopNav.html';
|
2015-02-04 04:35:19 -06:00
|
|
|
$scope.panels = config.panels;
|
|
|
|
|
2014-08-23 13:23:33 -05:00
|
|
|
var resizeEventTimeout;
|
2014-02-11 02:03:21 -06:00
|
|
|
|
2015-02-02 04:32:00 -06:00
|
|
|
this.init = function(dashboard) {
|
2015-10-26 08:33:55 -05:00
|
|
|
$scope.resetRow();
|
2014-08-23 13:23:33 -05:00
|
|
|
$scope.registerWindowResizeEvent();
|
2014-09-24 03:51:20 -05:00
|
|
|
$scope.onAppEvent('show-json-editor', $scope.showJsonEditor);
|
2015-02-02 04:32:00 -06:00
|
|
|
$scope.setupDashboard(dashboard);
|
2014-08-23 13:23:33 -05:00
|
|
|
};
|
2014-08-20 15:34:51 -05:00
|
|
|
|
2015-03-14 14:29:41 -05:00
|
|
|
$scope.setupDashboard = function(data) {
|
2014-08-14 05:26:06 -05:00
|
|
|
$rootScope.performance.dashboardLoadStart = new Date().getTime();
|
|
|
|
$rootScope.performance.panelsInitialized = 0;
|
2014-09-06 11:05:54 -05:00
|
|
|
$rootScope.performance.panelsRendered = 0;
|
2014-08-14 05:15:46 -05:00
|
|
|
|
2015-05-04 01:36:44 -05:00
|
|
|
var dashboard = dashboardSrv.create(data.dashboard, data.meta);
|
2015-05-07 02:35:39 -05:00
|
|
|
dashboardSrv.setCurrent(dashboard);
|
2014-08-13 03:07:32 -05:00
|
|
|
|
2014-08-28 09:44:16 -05:00
|
|
|
// init services
|
2015-03-14 14:29:41 -05:00
|
|
|
timeSrv.init(dashboard);
|
2014-06-08 08:28:50 -05:00
|
|
|
|
2015-03-14 14:29:41 -05:00
|
|
|
// template values service needs to initialize completely before
|
|
|
|
// the rest of the dashboard can load
|
2015-04-07 06:48:26 -05:00
|
|
|
templateValuesSrv.init(dashboard).finally(function() {
|
2015-03-14 15:13:25 -05:00
|
|
|
dynamicDashboardSrv.init(dashboard);
|
2015-04-28 06:45:59 -05:00
|
|
|
unsavedChangesSrv.init(dashboard, $scope);
|
2015-04-27 10:20:32 -05:00
|
|
|
|
2015-03-14 14:29:41 -05:00
|
|
|
$scope.dashboard = dashboard;
|
2015-04-06 04:22:35 -05:00
|
|
|
$scope.dashboardMeta = dashboard.meta;
|
2015-03-14 14:29:41 -05:00
|
|
|
$scope.dashboardViewState = dashboardViewStateSrv.create($scope);
|
2014-06-12 06:37:40 -05:00
|
|
|
|
2015-03-14 14:29:41 -05:00
|
|
|
dashboardKeybindings.shortcuts($scope);
|
2014-09-05 00:02:59 -05:00
|
|
|
|
2015-04-06 04:22:35 -05:00
|
|
|
$scope.updateTopNavPartial();
|
2015-03-14 14:29:41 -05:00
|
|
|
$scope.updateSubmenuVisibility();
|
|
|
|
$scope.setWindowTitleAndTheme();
|
|
|
|
|
|
|
|
$scope.appEvent("dashboard-loaded", $scope.dashboard);
|
2015-04-07 06:48:26 -05:00
|
|
|
}).catch(function(err) {
|
2015-10-02 02:04:46 -05:00
|
|
|
if (err.data && err.data.message) { err.message = err.data.message; }
|
2015-04-07 06:48:26 -05:00
|
|
|
$scope.appEvent("alert-error", ['Dashboard init failed', 'Template variables could not be initialized: ' + err.message]);
|
2015-03-14 14:29:41 -05:00
|
|
|
});
|
2013-12-29 04:46:36 -06:00
|
|
|
};
|
|
|
|
|
2015-04-06 04:22:35 -05:00
|
|
|
$scope.updateTopNavPartial = function() {
|
2015-05-13 03:45:53 -05:00
|
|
|
if ($scope.dashboard.meta.isSnapshot) {
|
2015-03-28 11:53:52 -05:00
|
|
|
$scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html';
|
2015-03-25 09:48:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-20 05:20:10 -06:00
|
|
|
$scope.updateSubmenuVisibility = function() {
|
2015-05-05 13:33:06 -05:00
|
|
|
$scope.submenuEnabled = $scope.dashboard.isSubmenuFeaturesEnabled();
|
2015-02-20 05:20:10 -06:00
|
|
|
};
|
|
|
|
|
2014-08-28 09:44:16 -05:00
|
|
|
$scope.setWindowTitleAndTheme = function() {
|
|
|
|
window.document.title = config.window_title_prefix + $scope.dashboard.title;
|
2013-09-26 17:42:43 -05:00
|
|
|
};
|
|
|
|
|
2015-02-22 01:09:58 -06:00
|
|
|
$scope.broadcastRefresh = function() {
|
2015-06-19 13:13:25 -05:00
|
|
|
$rootScope.performance.panelsRendered = 0;
|
2015-02-22 01:09:58 -06:00
|
|
|
$rootScope.$broadcast('refresh');
|
|
|
|
};
|
|
|
|
|
2015-10-26 08:33:55 -05:00
|
|
|
$scope.addRow = function(dash, row) {
|
2013-09-13 15:52:13 -05:00
|
|
|
dash.rows.push(row);
|
|
|
|
};
|
|
|
|
|
2015-10-26 08:33:55 -05:00
|
|
|
$scope.addRowDefault = function() {
|
|
|
|
$scope.resetRow();
|
2014-06-06 11:30:15 -05:00
|
|
|
$scope.row.title = 'New row';
|
2015-10-26 08:33:55 -05:00
|
|
|
$scope.addRow($scope.dashboard, $scope.row);
|
2014-06-06 11:30:15 -05:00
|
|
|
};
|
|
|
|
|
2015-10-26 08:33:55 -05:00
|
|
|
$scope.resetRow = function() {
|
2013-09-13 15:52:13 -05:00
|
|
|
$scope.row = {
|
|
|
|
title: '',
|
2014-06-06 09:05:56 -05:00
|
|
|
height: '250px',
|
2013-09-13 15:52:13 -05:00
|
|
|
editable: true,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-10-14 15:57:33 -05:00
|
|
|
$scope.panelEditorPath = function(type) {
|
|
|
|
return 'app/' + config.panels[type].path + '/editor.html';
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2014-10-14 15:57:33 -05:00
|
|
|
$scope.pulldownEditorPath = function(type) {
|
|
|
|
return 'app/panels/'+type+'/editor.html';
|
2014-09-09 04:35:29 -05:00
|
|
|
};
|
|
|
|
|
2014-09-03 04:15:01 -05:00
|
|
|
$scope.showJsonEditor = function(evt, options) {
|
|
|
|
var editScope = $rootScope.$new();
|
|
|
|
editScope.object = options.object;
|
|
|
|
editScope.updateHandler = options.updateHandler;
|
2014-09-24 09:26:39 -05:00
|
|
|
$scope.appEvent('show-dash-editor', { src: 'app/partials/edit_json.html', scope: editScope });
|
2014-09-03 04:15:01 -05:00
|
|
|
};
|
|
|
|
|
2014-10-09 19:30:23 -05:00
|
|
|
$scope.onDrop = function(panelId, row, dropTarget) {
|
|
|
|
var info = $scope.dashboard.getPanelInfoById(panelId);
|
|
|
|
if (dropTarget) {
|
|
|
|
var dropInfo = $scope.dashboard.getPanelInfoById(dropTarget.id);
|
|
|
|
dropInfo.row.panels[dropInfo.index] = info.panel;
|
|
|
|
info.row.panels[info.index] = dropTarget;
|
|
|
|
var dragSpan = info.panel.span;
|
|
|
|
info.panel.span = dropTarget.span;
|
|
|
|
dropTarget.span = dragSpan;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
info.row.panels.splice(info.index, 1);
|
|
|
|
info.panel.span = 12 - $scope.dashboard.rowSpan(row);
|
|
|
|
row.panels.push(info.panel);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rootScope.$broadcast('render');
|
|
|
|
};
|
|
|
|
|
2015-03-05 03:42:46 -06:00
|
|
|
$scope.registerWindowResizeEvent = function() {
|
|
|
|
angular.element(window).bind('resize', function() {
|
|
|
|
$timeout.cancel(resizeEventTimeout);
|
|
|
|
resizeEventTimeout = $timeout(function() { $scope.$broadcast('render'); }, 200);
|
|
|
|
});
|
|
|
|
$scope.$on('$destroy', function() {
|
|
|
|
angular.element(window).unbind('resize');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
});
|
2015-03-25 09:48:51 -05:00
|
|
|
|
2014-05-15 08:04:23 -05:00
|
|
|
});
|