mirror of https://github.com/grafana/grafana.git
				
				
				
			
		
			
	
	
		
			163 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
		
		
			
		
	
	
			163 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, | ||
|  |       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); | ||
|  | 
 | ||
|  |       // init services
 | ||
|  |       timeSrv.init(dashboard); | ||
|  | 
 | ||
|  |       // template values service needs to initialize completely before
 | ||
|  |       // the rest of the dashboard can load
 | ||
|  |       templateValuesSrv.init(dashboard).then(function() { | ||
|  |         $scope.dashboard = dashboard; | ||
|  |         $scope.dashboardViewState = dashboardViewStateSrv.create($scope); | ||
|  |         $scope.initDashboardMeta(data.meta, $scope.dashboard); | ||
|  | 
 | ||
|  |         dashboardKeybindings.shortcuts($scope); | ||
|  | 
 | ||
|  |         $scope.updateSubmenuVisibility(); | ||
|  |         $scope.setWindowTitleAndTheme(); | ||
|  | 
 | ||
|  |         $scope.appEvent("dashboard-loaded", $scope.dashboard); | ||
|  |       }); | ||
|  |     }; | ||
|  | 
 | ||
|  |     $scope.initDashboardMeta = function(meta) { | ||
|  |       meta.canShare = true; | ||
|  |       meta.canSave = true; | ||
|  |       meta.canEdit = true; | ||
|  |       meta.canStar = true; | ||
|  | 
 | ||
|  |       if (contextSrv.hasRole('Viewer')) { | ||
|  |         meta.canSave = false; | ||
|  |       } | ||
|  | 
 | ||
|  |       if (meta.isHome) { | ||
|  |         meta.canShare = false; | ||
|  |         meta.canStar = false; | ||
|  |         meta.canSave = false; | ||
|  |         meta.canEdit = false; | ||
|  |       } | ||
|  | 
 | ||
|  |       if (meta.isSnapshot) { | ||
|  |         $scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html'; | ||
|  |       } | ||
|  | 
 | ||
|  |       $scope.dashboardMeta = meta; | ||
|  |     }; | ||
|  | 
 | ||
|  |     $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'); | ||
|  |       }); | ||
|  |     }; | ||
|  | 
 | ||
|  |   }); | ||
|  | 
 | ||
|  | }); |