| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  | define([ | 
					
						
							|  |  |  |   'angular', | 
					
						
							|  |  |  |   'lodash', | 
					
						
							|  |  |  | ], | 
					
						
							|  |  |  | function (angular, _) { | 
					
						
							|  |  |  |   'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var module = angular.module('grafana.controllers'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-27 21:47:41 +02:00
										 |  |  |   module.controller('TemplateEditorCtrl', function($scope, datasourceSrv, templateSrv, templateValuesSrv) { | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     var replacementDefaults = { | 
					
						
							| 
									
										
										
										
											2014-08-27 15:54:30 +02:00
										 |  |  |       type: 'query', | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  |       datasource: null, | 
					
						
							|  |  |  |       refresh_on_load: false, | 
					
						
							|  |  |  |       name: '', | 
					
						
							|  |  |  |       options: [], | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $scope.init = function() { | 
					
						
							|  |  |  |       $scope.editor = { index: 0 }; | 
					
						
							|  |  |  |       $scope.datasources = datasourceSrv.getMetricSources(); | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |       $scope.variables = templateSrv.variables; | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  |       $scope.reset(); | 
					
						
							| 
									
										
										
										
											2014-08-27 10:41:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |       _.each($scope.variables, function(variable) { | 
					
						
							|  |  |  |         if (variable.datasource === void 0) { | 
					
						
							|  |  |  |           variable.datasource = null; | 
					
						
							|  |  |  |           variable.type = 'query'; | 
					
						
							| 
									
										
										
										
											2014-08-27 15:54:30 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $scope.$watch('editor.index', function(index) { | 
					
						
							|  |  |  |         if ($scope.currentIsNew === false && index === 1) { | 
					
						
							|  |  |  |           $scope.reset(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-27 10:41:27 +02:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $scope.add = function() { | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |       $scope.variables.push($scope.current); | 
					
						
							| 
									
										
										
										
											2014-08-29 10:17:00 +02:00
										 |  |  |       $scope.update(); | 
					
						
							| 
									
										
										
										
											2014-08-27 15:54:30 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $scope.runQuery = function() { | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |       templateValuesSrv.updateOptions($scope.current); | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |     $scope.edit = function(variable) { | 
					
						
							|  |  |  |       $scope.current = variable; | 
					
						
							| 
									
										
										
										
											2014-08-27 10:41:27 +02:00
										 |  |  |       $scope.currentIsNew = false; | 
					
						
							|  |  |  |       $scope.editor.index = 2; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-27 15:54:30 +02:00
										 |  |  |     $scope.update = function() { | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |       templateValuesSrv.updateOptions($scope.current); | 
					
						
							| 
									
										
										
										
											2014-08-27 15:54:30 +02:00
										 |  |  |       $scope.reset(); | 
					
						
							|  |  |  |       $scope.editor.index = 0; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  |     $scope.reset = function() { | 
					
						
							|  |  |  |       $scope.currentIsNew = true; | 
					
						
							|  |  |  |       $scope.current = angular.copy(replacementDefaults); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-27 21:47:41 +02:00
										 |  |  |     $scope.typeChanged = function () { | 
					
						
							|  |  |  |       if ($scope.current.type === 'time period') { | 
					
						
							| 
									
										
										
										
											2014-08-28 16:03:13 +02:00
										 |  |  |         $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         $scope.current.query = ''; | 
					
						
							| 
									
										
										
										
											2014-08-27 21:47:41 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 12:44:01 +02:00
										 |  |  |     $scope.removeVariable = function(variable) { | 
					
						
							|  |  |  |       var index = _.indexOf($scope.variables, variable); | 
					
						
							|  |  |  |       $scope.variables.splice(index, 1); | 
					
						
							| 
									
										
										
										
											2014-08-26 14:17:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }); |