diff --git a/public/app/directives/valueSelectDropdown.js b/public/app/directives/valueSelectDropdown.js index db62b6684f6..8c963bcf1eb 100644 --- a/public/app/directives/valueSelectDropdown.js +++ b/public/app/directives/valueSelectDropdown.js @@ -215,7 +215,7 @@ function (angular, app, _) { angular .module('grafana.directives') - .directive('valueSelectDropdown', function($compile, $window, $timeout) { + .directive('valueSelectDropdown', function($compile, $window, $timeout, $rootScope) { return { scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" }, @@ -260,6 +260,14 @@ function (angular, app, _) { } }); + var cleanUp = $rootScope.$on('template-variable-value-updated', function() { + scope.vm.updateLinkText(); + }); + + scope.$on("$destroy", function() { + cleanUp(); + }); + scope.vm.init(); }, }; diff --git a/public/app/features/dashboard/submenuCtrl.js b/public/app/features/dashboard/submenuCtrl.js index b8e609c061e..b1d0dc3ae32 100644 --- a/public/app/features/dashboard/submenuCtrl.js +++ b/public/app/features/dashboard/submenuCtrl.js @@ -1,18 +1,12 @@ define([ 'angular', - 'lodash' ], -function (angular, _) { +function (angular) { 'use strict'; var module = angular.module('grafana.controllers'); module.controller('SubmenuCtrl', function($scope, $q, $rootScope, templateValuesSrv, dynamicDashboardSrv) { - var _d = { - enable: true - }; - - _.defaults($scope.pulldown,_d); $scope.init = function() { $scope.panel = $scope.pulldown; @@ -33,6 +27,7 @@ function (angular, _) { $scope.variableUpdated = function(variable) { templateValuesSrv.variableUpdated(variable).then(function() { dynamicDashboardSrv.update($scope.dashboard); + $rootScope.$emit('template-variable-value-updated'); $rootScope.$broadcast('refresh'); }); }; diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index b2bed74a4ce..4192279d74a 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -146,6 +146,9 @@ function (angular, _, kbn) { var currentOption = _.findWhere(variable.options, { text: variable.current.text }); if (currentOption) { return self.setVariableValue(variable, currentOption); + } else { + if (!variable.options.length) { return; } + return self.setVariableValue(variable, variable.options[0]); } } }; diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 359ccc98540..3c75d91bbb1 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -106,6 +106,31 @@ define([ }); }); + describeUpdateVariable('query variable with empty current object and refresh', function(scenario) { + scenario.setup(function() { + scenario.variable = { type: 'query', query: '', name: 'test', current: {} }; + scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}]; + }); + + it('should set current value to first option', function() { + expect(scenario.variable.options.length).to.be(2); + expect(scenario.variable.current.value).to.be('backend1'); + }); + }); + + describeUpdateVariable('interval variable without auto', function(scenario) { + scenario.setup(function() { + scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' }; + }); + + it('should update options array', function() { + expect(scenario.variable.options.length).to.be(4); + expect(scenario.variable.options[0].text).to.be('1s'); + expect(scenario.variable.options[0].value).to.be('1s'); + }); + }); + + describeUpdateVariable('interval variable with auto', function(scenario) { scenario.setup(function() { scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 }; @@ -171,7 +196,7 @@ define([ describeUpdateVariable('and existing value still exists in options', function(scenario) { scenario.setup(function() { scenario.variable = { type: 'query', query: 'apps.*', name: 'test' }; - scenario.variable.current = { text: 'backend2'}; + scenario.variable.current = { value: 'backend2', text: 'backend2'}; scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}]; });