Преглед на файлове

fix(templating): fixed issue with nested template variables and multi select, the child variable selection state is now updated like single select variables, so if none matches the first option is selected, fixes #4861

Torkel Ödegaard преди 9 години
родител
ревизия
6f094ef215
променени са 2 файла, в които са добавени 53 реда и са изтрити 2 реда
  1. 13 2
      public/app/features/templating/templateValuesSrv.js
  2. 40 0
      public/test/specs/templateValuesSrv-specs.js

+ 13 - 2
public/app/features/templating/templateValuesSrv.js

@@ -244,15 +244,26 @@ function (angular, _, kbn) {
     this.validateVariableSelectionState = function(variable) {
     this.validateVariableSelectionState = function(variable) {
       if (!variable.current) {
       if (!variable.current) {
         if (!variable.options.length) { return; }
         if (!variable.options.length) { return; }
-        return self.setVariableValue(variable, variable.options[0], true);
+        return self.setVariableValue(variable, variable.options[0], false);
       }
       }
 
 
       if (_.isArray(variable.current.value)) {
       if (_.isArray(variable.current.value)) {
         self.selectOptionsForCurrentValue(variable);
         self.selectOptionsForCurrentValue(variable);
+        // updated selected value
+        var selected = {
+          value: _.map(_.filter(variable.options, {selected: true}), function(op) {
+            return op.value;
+          })
+        };
+        // if none pick first
+        if (selected.value.length === 0) {
+          selected = variable.options[0];
+        }
+        return self.setVariableValue(variable, selected, false);
       } else {
       } else {
         var currentOption = _.findWhere(variable.options, {text: variable.current.text});
         var currentOption = _.findWhere(variable.options, {text: variable.current.text});
         if (currentOption) {
         if (currentOption) {
-          return self.setVariableValue(variable, currentOption, true);
+          return self.setVariableValue(variable, currentOption, false);
         } else {
         } else {
           if (!variable.options.length) { return; }
           if (!variable.options.length) { return; }
           return self.setVariableValue(variable, variable.options[0]);
           return self.setVariableValue(variable, variable.options[0]);

+ 40 - 0
public/test/specs/templateValuesSrv-specs.js

@@ -126,6 +126,46 @@ define([
       });
       });
     });
     });
 
 
+    describeUpdateVariable('query variable with multi select and new options does not contain some selected values', function(scenario) {
+      scenario.setup(function() {
+        scenario.variable = {
+          type: 'query',
+          query: '',
+          name: 'test',
+          current: {
+            value: ['val1', 'val2', 'val3'],
+            text: 'val1 + val2 + val3'
+          }
+        };
+        scenario.queryResult = [{text: 'val2'}, {text: 'val3'}];
+      });
+
+      it('should update current value', function() {
+        expect(scenario.variable.current.value).to.eql(['val2', 'val3']);
+        expect(scenario.variable.current.text).to.eql('val2 + val3');
+      });
+    });
+
+    describeUpdateVariable('query variable with multi select and new options does not contain any selected values', function(scenario) {
+      scenario.setup(function() {
+        scenario.variable = {
+          type: 'query',
+          query: '',
+          name: 'test',
+          current: {
+            value: ['val1', 'val2', 'val3'],
+            text: 'val1 + val2 + val3'
+          }
+        };
+        scenario.queryResult = [{text: 'val5'}, {text: 'val6'}];
+      });
+
+      it('should update current value with first one', function() {
+        expect(scenario.variable.current.value).to.eql('val5');
+        expect(scenario.variable.current.text).to.eql('val5');
+      });
+    });
+
     describeUpdateVariable('query variable with numeric results', function(scenario) {
     describeUpdateVariable('query variable with numeric results', function(scenario) {
       scenario.setup(function() {
       scenario.setup(function() {
         scenario.variable = { type: 'query', query: '', name: 'test', current: {} };
         scenario.variable = { type: 'query', query: '', name: 'test', current: {} };