| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  | define([ | 
					
						
							|  |  |  |   'angular', | 
					
						
							| 
									
										
										
										
											2013-12-29 18:46:36 +08:00
										 |  |  |   'jquery', | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  |   'config', | 
					
						
							| 
									
										
										
										
											2014-08-07 20:35:19 +08:00
										 |  |  |   'lodash', | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  | ], | 
					
						
							| 
									
										
										
										
											2015-02-04 18:35:19 +08:00
										 |  |  | function (angular, $, config) { | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  |   "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-13 16:07:32 +08:00
										 |  |  |   var module = angular.module('grafana.controllers'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 14:55:05 +08:00
										 |  |  |   module.controller('DashboardCtrl', function( | 
					
						
							| 
									
										
										
										
											2014-08-27 21:54:30 +08:00
										 |  |  |       $scope, | 
					
						
							|  |  |  |       $rootScope, | 
					
						
							|  |  |  |       dashboardKeybindings, | 
					
						
							| 
									
										
										
										
											2014-08-27 22:29:48 +08:00
										 |  |  |       timeSrv, | 
					
						
							| 
									
										
										
										
											2014-08-28 03:47:41 +08:00
										 |  |  |       templateValuesSrv, | 
					
						
							| 
									
										
										
										
											2015-03-15 04:13:25 +08:00
										 |  |  |       dynamicDashboardSrv, | 
					
						
							| 
									
										
										
										
											2014-08-27 21:54:30 +08:00
										 |  |  |       dashboardSrv, | 
					
						
							|  |  |  |       dashboardViewStateSrv, | 
					
						
							| 
									
										
										
										
											2015-03-25 22:48:51 +08:00
										 |  |  |       contextSrv, | 
					
						
							| 
									
										
										
										
											2014-08-27 21:54:30 +08:00
										 |  |  |       $timeout) { | 
					
						
							| 
									
										
										
										
											2013-10-12 23:08:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-08 20:40:44 +08:00
										 |  |  |     $scope.editor = { index: 0 }; | 
					
						
							| 
									
										
										
										
											2015-03-29 00:53:52 +08:00
										 |  |  |     $scope.topNavPartial = 'app/features/dashboard/partials/dashboardTopNav.html'; | 
					
						
							| 
									
										
										
										
											2015-02-04 18:35:19 +08:00
										 |  |  |     $scope.panels = config.panels; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-24 02:23:33 +08:00
										 |  |  |     var resizeEventTimeout; | 
					
						
							| 
									
										
										
										
											2014-02-11 16:03:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:00 +08:00
										 |  |  |     this.init = function(dashboard) { | 
					
						
							| 
									
										
										
										
											2014-08-24 02:23:33 +08:00
										 |  |  |       $scope.reset_row(); | 
					
						
							|  |  |  |       $scope.registerWindowResizeEvent(); | 
					
						
							| 
									
										
										
										
											2014-09-24 16:51:20 +08:00
										 |  |  |       $scope.onAppEvent('show-json-editor', $scope.showJsonEditor); | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:00 +08:00
										 |  |  |       $scope.setupDashboard(dashboard); | 
					
						
							| 
									
										
										
										
											2014-08-24 02:23:33 +08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2014-08-21 04:34:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |     $scope.setupDashboard = function(data) { | 
					
						
							| 
									
										
										
										
											2014-08-14 18:26:06 +08:00
										 |  |  |       $rootScope.performance.dashboardLoadStart = new Date().getTime(); | 
					
						
							|  |  |  |       $rootScope.performance.panelsInitialized = 0; | 
					
						
							| 
									
										
										
										
											2014-09-07 00:05:54 +08:00
										 |  |  |       $rootScope.performance.panelsRendered = 0; | 
					
						
							| 
									
										
										
										
											2014-08-14 18:15:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 17:22:35 +08:00
										 |  |  |       var dashboard = dashboardSrv.create(data.model, data.meta); | 
					
						
							| 
									
										
										
										
											2014-08-13 16:07:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 22:44:16 +08:00
										 |  |  |       // init services
 | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |       timeSrv.init(dashboard); | 
					
						
							| 
									
										
										
										
											2014-06-08 21:28:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |       // template values service needs to initialize completely before
 | 
					
						
							|  |  |  |       // the rest of the dashboard can load
 | 
					
						
							| 
									
										
										
										
											2015-04-07 19:48:26 +08:00
										 |  |  |       templateValuesSrv.init(dashboard).finally(function() { | 
					
						
							| 
									
										
										
										
											2015-03-15 04:13:25 +08:00
										 |  |  |         dynamicDashboardSrv.init(dashboard); | 
					
						
							| 
									
										
										
										
											2015-04-27 23:20:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |         $scope.dashboard = dashboard; | 
					
						
							| 
									
										
										
										
											2015-04-06 17:22:35 +08:00
										 |  |  |         $scope.dashboardMeta = dashboard.meta; | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |         $scope.dashboardViewState = dashboardViewStateSrv.create($scope); | 
					
						
							| 
									
										
										
										
											2014-06-12 19:37:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |         dashboardKeybindings.shortcuts($scope); | 
					
						
							| 
									
										
										
										
											2014-09-05 13:02:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 17:22:35 +08:00
										 |  |  |         $scope.updateTopNavPartial(); | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |         $scope.updateSubmenuVisibility(); | 
					
						
							|  |  |  |         $scope.setWindowTitleAndTheme(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $scope.appEvent("dashboard-loaded", $scope.dashboard); | 
					
						
							| 
									
										
										
										
											2015-04-07 19:48:26 +08:00
										 |  |  |       }).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]); | 
					
						
							| 
									
										
										
										
											2015-03-15 03:29:41 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2013-12-29 18:46:36 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 17:22:35 +08:00
										 |  |  |     $scope.updateTopNavPartial = function() { | 
					
						
							|  |  |  |       if ($scope.dashboard.meta.isSnapshot) { | 
					
						
							| 
									
										
										
										
											2015-03-29 00:53:52 +08:00
										 |  |  |         $scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html'; | 
					
						
							| 
									
										
										
										
											2015-03-25 22:48:51 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-20 19:20:10 +08:00
										 |  |  |     $scope.updateSubmenuVisibility = function() { | 
					
						
							|  |  |  |       $scope.submenuEnabled = $scope.dashboard.hasTemplateVarsOrAnnotations(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 22:44:16 +08:00
										 |  |  |     $scope.setWindowTitleAndTheme = function() { | 
					
						
							|  |  |  |       window.document.title = config.window_title_prefix + $scope.dashboard.title; | 
					
						
							| 
									
										
										
										
											2013-09-27 06:42:43 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 15:09:58 +08:00
										 |  |  |     $scope.broadcastRefresh = function() { | 
					
						
							|  |  |  |       $rootScope.$broadcast('refresh'); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 03:03:53 +08:00
										 |  |  |     $scope.add_row = function(dash, row) { | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  |       dash.rows.push(row); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-07 00:30:15 +08:00
										 |  |  |     $scope.add_row_default = function() { | 
					
						
							|  |  |  |       $scope.reset_row(); | 
					
						
							|  |  |  |       $scope.row.title = 'New row'; | 
					
						
							| 
									
										
										
										
											2014-06-08 03:00:05 +08:00
										 |  |  |       $scope.add_row($scope.dashboard, $scope.row); | 
					
						
							| 
									
										
										
										
											2014-06-07 00:30:15 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  |     $scope.reset_row = function() { | 
					
						
							|  |  |  |       $scope.row = { | 
					
						
							|  |  |  |         title: '', | 
					
						
							| 
									
										
										
										
											2014-06-06 22:05:56 +08:00
										 |  |  |         height: '250px', | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  |         editable: true, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 04:57:33 +08:00
										 |  |  |     $scope.panelEditorPath = function(type) { | 
					
						
							|  |  |  |       return 'app/' + config.panels[type].path + '/editor.html'; | 
					
						
							| 
									
										
										
										
											2013-09-14 04:52:13 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 04:57:33 +08:00
										 |  |  |     $scope.pulldownEditorPath = function(type) { | 
					
						
							|  |  |  |       return 'app/panels/'+type+'/editor.html'; | 
					
						
							| 
									
										
										
										
											2014-09-09 17:35:29 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 17:15:01 +08:00
										 |  |  |     $scope.showJsonEditor = function(evt, options) { | 
					
						
							|  |  |  |       var editScope = $rootScope.$new(); | 
					
						
							|  |  |  |       editScope.object = options.object; | 
					
						
							|  |  |  |       editScope.updateHandler = options.updateHandler; | 
					
						
							| 
									
										
										
										
											2014-09-24 22:26:39 +08:00
										 |  |  |       $scope.appEvent('show-dash-editor', { src: 'app/partials/edit_json.html', scope: editScope }); | 
					
						
							| 
									
										
										
										
											2014-09-03 17:15:01 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-10 08:30:23 +08: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 17:42:46 +08: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-14 04:52:13 +08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-03-25 22:48:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 21:04:23 +08:00
										 |  |  | }); |