mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 08:35:43 -06:00
152 lines
4.5 KiB
JavaScript
152 lines
4.5 KiB
JavaScript
define([
|
|
'angular',
|
|
'jquery',
|
|
'config',
|
|
'lodash',
|
|
],
|
|
function (angular, $, config) {
|
|
"use strict";
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
module.controller('DashboardCtrl', function(
|
|
$scope,
|
|
$rootScope,
|
|
dashboardKeybindings,
|
|
timeSrv,
|
|
templateValuesSrv,
|
|
dynamicDashboardSrv,
|
|
dashboardSrv,
|
|
dashboardViewStateSrv,
|
|
contextSrv,
|
|
$timeout) {
|
|
|
|
$scope.editor = { index: 0 };
|
|
$scope.topNavPartial = 'app/features/dashboard/partials/dashboardTopNav.html';
|
|
$scope.panels = config.panels;
|
|
|
|
var resizeEventTimeout;
|
|
|
|
this.init = function(dashboard) {
|
|
$scope.reset_row();
|
|
$scope.registerWindowResizeEvent();
|
|
$scope.onAppEvent('show-json-editor', $scope.showJsonEditor);
|
|
$scope.setupDashboard(dashboard);
|
|
};
|
|
|
|
$scope.setupDashboard = function(data) {
|
|
$rootScope.performance.dashboardLoadStart = new Date().getTime();
|
|
$rootScope.performance.panelsInitialized = 0;
|
|
$rootScope.performance.panelsRendered = 0;
|
|
|
|
var dashboard = dashboardSrv.create(data.model, data.meta);
|
|
|
|
// init services
|
|
timeSrv.init(dashboard);
|
|
|
|
// template values service needs to initialize completely before
|
|
// the rest of the dashboard can load
|
|
templateValuesSrv.init(dashboard).finally(function() {
|
|
dynamicDashboardSrv.init(dashboard);
|
|
|
|
$scope.dashboard = dashboard;
|
|
$scope.dashboardMeta = dashboard.meta;
|
|
$scope.dashboardViewState = dashboardViewStateSrv.create($scope);
|
|
|
|
dashboardKeybindings.shortcuts($scope);
|
|
|
|
$scope.updateTopNavPartial();
|
|
$scope.updateSubmenuVisibility();
|
|
$scope.setWindowTitleAndTheme();
|
|
|
|
$scope.appEvent("dashboard-loaded", $scope.dashboard);
|
|
}).catch(function(err) {
|
|
console.log('Failed to initialize dashboard template variables, error: ', err);
|
|
$scope.appEvent("alert-error", ['Dashboard init failed', 'Template variables could not be initialized: ' + err.message]);
|
|
});
|
|
};
|
|
|
|
$scope.updateTopNavPartial = function() {
|
|
if ($scope.dashboard.meta.isSnapshot) {
|
|
$scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html';
|
|
}
|
|
};
|
|
|
|
$scope.updateSubmenuVisibility = function() {
|
|
$scope.submenuEnabled = $scope.dashboard.hasTemplateVarsOrAnnotations();
|
|
};
|
|
|
|
$scope.setWindowTitleAndTheme = function() {
|
|
window.document.title = config.window_title_prefix + $scope.dashboard.title;
|
|
};
|
|
|
|
$scope.broadcastRefresh = function() {
|
|
$rootScope.$broadcast('refresh');
|
|
};
|
|
|
|
$scope.add_row = function(dash, row) {
|
|
dash.rows.push(row);
|
|
};
|
|
|
|
$scope.add_row_default = function() {
|
|
$scope.reset_row();
|
|
$scope.row.title = 'New row';
|
|
$scope.add_row($scope.dashboard, $scope.row);
|
|
};
|
|
|
|
$scope.reset_row = function() {
|
|
$scope.row = {
|
|
title: '',
|
|
height: '250px',
|
|
editable: true,
|
|
};
|
|
};
|
|
|
|
$scope.panelEditorPath = function(type) {
|
|
return 'app/' + config.panels[type].path + '/editor.html';
|
|
};
|
|
|
|
$scope.pulldownEditorPath = function(type) {
|
|
return 'app/panels/'+type+'/editor.html';
|
|
};
|
|
|
|
$scope.showJsonEditor = function(evt, options) {
|
|
var editScope = $rootScope.$new();
|
|
editScope.object = options.object;
|
|
editScope.updateHandler = options.updateHandler;
|
|
$scope.appEvent('show-dash-editor', { src: 'app/partials/edit_json.html', scope: editScope });
|
|
};
|
|
|
|
$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');
|
|
};
|
|
|
|
$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');
|
|
});
|
|
};
|
|
|
|
});
|
|
|
|
});
|