Просмотр исходного кода

Fixed issue with new variable value select dropdown and parent -> child updates, Fixes #2215

Torkel Ödegaard 10 лет назад
Родитель
Сommit
ec98f9181b

+ 9 - 1
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();
         },
       };

+ 2 - 7
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');
       });
     };

+ 3 - 0
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]);
         }
       }
     };

+ 26 - 1
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'}];
       });