فهرست منبع

fix(templating): fixed scoped vars issue when generating urls for panel links, fixes #2410

Torkel Ödegaard 10 سال پیش
والد
کامیت
2cc53f328b

+ 7 - 7
public/app/features/panellinks/linkSrv.js

@@ -59,21 +59,21 @@ function (angular, kbn, _) {
         return info;
         return info;
       };
       };
 
 
-      this.getPanelLinkAnchorInfo = function(link) {
+      this.getPanelLinkAnchorInfo = function(link, scopedVars) {
         var info = {};
         var info = {};
         if (link.type === 'absolute') {
         if (link.type === 'absolute') {
           info.target = link.targetBlank ? '_blank' : '_self';
           info.target = link.targetBlank ? '_blank' : '_self';
-          info.href = templateSrv.replace(link.url || '');
-          info.title = templateSrv.replace(link.title || '');
+          info.href = templateSrv.replace(link.url || '', scopedVars);
+          info.title = templateSrv.replace(link.title || '', scopedVars);
           info.href += '?';
           info.href += '?';
         }
         }
         else if (link.dashUri) {
         else if (link.dashUri) {
           info.href = 'dashboard/' + link.dashUri + '?';
           info.href = 'dashboard/' + link.dashUri + '?';
-          info.title = templateSrv.replace(link.title || '');
+          info.title = templateSrv.replace(link.title || '', scopedVars);
           info.target = link.targetBlank ? '_blank' : '';
           info.target = link.targetBlank ? '_blank' : '';
         }
         }
         else {
         else {
-          info.title = templateSrv.replace(link.title || '');
+          info.title = templateSrv.replace(link.title || '', scopedVars);
           var slug = kbn.slugifyForUrl(link.dashboard || '');
           var slug = kbn.slugifyForUrl(link.dashboard || '');
           info.href = 'dashboard/db/' + slug + '?';
           info.href = 'dashboard/db/' + slug + '?';
         }
         }
@@ -87,12 +87,12 @@ function (angular, kbn, _) {
         }
         }
 
 
         if (link.includeVars) {
         if (link.includeVars) {
-          templateSrv.fillVariableValuesForUrl(params);
+          templateSrv.fillVariableValuesForUrl(params, scopedVars);
         }
         }
 
 
         info.href = this.addParamsToUrl(info.href, params);
         info.href = this.addParamsToUrl(info.href, params);
         if (link.params) {
         if (link.params) {
-          info.href += "&" + templateSrv.replace(link.params);
+          info.href += "&" + templateSrv.replace(link.params, scopedVars);
         }
         }
 
 
         return info;
         return info;

+ 11 - 8
public/app/features/templating/templateSrv.js

@@ -115,17 +115,20 @@ function (angular, _) {
       });
       });
     };
     };
 
 
-    this.fillVariableValuesForUrl = function(params) {
-      var toUrlVal = function(current) {
+    this.fillVariableValuesForUrl = function(params, scopedVars) {
+      _.each(this.variables, function(variable) {
+        var current = variable.current;
+        var value = current.value;
+
         if (current.text === 'All') {
         if (current.text === 'All') {
-          return 'All';
-        } else {
-          return current.value;
+          value = 'All';
         }
         }
-      };
 
 
-      _.each(this.variables, function(variable) {
-        params['var-' + variable.name] = toUrlVal(variable.current);
+        if (scopedVars && scopedVars[variable.name] !== void 0) {
+          value = scopedVars[variable.name].value;
+        }
+
+        params['var-' + variable.name] = value;
       });
       });
     };
     };
 
 

+ 1 - 1
public/app/panels/singlestat/singleStatPanel.js

@@ -183,7 +183,7 @@ function (angular, app, _, $) {
         elem.click(function() {
         elem.click(function() {
           if (panel.links.length === 0) { return; }
           if (panel.links.length === 0) { return; }
           var link = panel.links[0];
           var link = panel.links[0];
-          var linkInfo = linkSrv.getPanelLinkAnchorInfo(link);
+          var linkInfo = linkSrv.getPanelLinkAnchorInfo(link, scope.panel.scopedVars);
           if (panel.links[0].targetBlank) {
           if (panel.links[0].targetBlank) {
             var redirectWindow = window.open(linkInfo.href, '_blank');
             var redirectWindow = window.open(linkInfo.href, '_blank');
             redirectWindow.location;
             redirectWindow.location;

+ 24 - 2
public/test/specs/templateSrv-specs.js

@@ -109,7 +109,6 @@ define([
     describe('when checking if a string contains a variable', function() {
     describe('when checking if a string contains a variable', function() {
       beforeEach(function() {
       beforeEach(function() {
         _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
         _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
-        _templateSrv.updateTemplateData();
       });
       });
 
 
       it('should find it with $var syntax', function() {
       it('should find it with $var syntax', function() {
@@ -127,7 +126,6 @@ define([
     describe('updateTemplateData with simple value', function() {
     describe('updateTemplateData with simple value', function() {
       beforeEach(function() {
       beforeEach(function() {
         _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
         _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
-        _templateSrv.updateTemplateData();
       });
       });
 
 
       it('should set current value and update template data', function() {
       it('should set current value and update template data', function() {
@@ -136,6 +134,30 @@ define([
       });
       });
     });
     });
 
 
+    describe('fillVariableValuesForUrl with multi value', function() {
+      beforeEach(function() {
+        _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
+      });
+
+      it('should set multiple url params', function() {
+        var params = {};
+        _templateSrv.fillVariableValuesForUrl(params);
+        expect(params['var-test']).to.eql(['val1', 'val2']);
+      });
+    });
+
+    describe('fillVariableValuesForUrl with multi value and scopedVars', function() {
+      beforeEach(function() {
+        _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
+      });
+
+      it('should set multiple url params', function() {
+        var params = {};
+        _templateSrv.fillVariableValuesForUrl(params, {'test': {value: 'val1'}});
+        expect(params['var-test']).to.eql('val1');
+      });
+    });
+
     describe('replaceWithText', function() {
     describe('replaceWithText', function() {
       beforeEach(function() {
       beforeEach(function() {
         _templateSrv.init([
         _templateSrv.init([