Browse Source

Add support for skipping variable value in URL, fixes #12174 (#12541)

* New rebase

Signed-off-by: Lukasz Gryglicki <lukaszgryglicki@o2.pl>

* Lint

Signed-off-by: Lukasz Gryglicki <lukaszgryglicki@o2.pl>
Łukasz Gryglicki 7 years ago
parent
commit
24f395f986

+ 2 - 0
public/app/features/templating/adhoc_variable.ts

@@ -3,6 +3,7 @@ import { Variable, assignModelProperties, variableTypes } from './variable';
 
 export class AdhocVariable implements Variable {
   filters: any[];
+  skipUrlSync: boolean;
 
   defaults = {
     type: 'adhoc',
@@ -11,6 +12,7 @@ export class AdhocVariable implements Variable {
     hide: 0,
     datasource: null,
     filters: [],
+    skipUrlSync: false,
   };
 
   /** @ngInject **/

+ 2 - 0
public/app/features/templating/constant_variable.ts

@@ -4,6 +4,7 @@ export class ConstantVariable implements Variable {
   query: string;
   options: any[];
   current: any;
+  skipUrlSync: boolean;
 
   defaults = {
     type: 'constant',
@@ -13,6 +14,7 @@ export class ConstantVariable implements Variable {
     query: '',
     current: {},
     options: [],
+    skipUrlSync: false,
   };
 
   /** @ngInject **/

+ 2 - 0
public/app/features/templating/custom_variable.ts

@@ -7,6 +7,7 @@ export class CustomVariable implements Variable {
   includeAll: boolean;
   multi: boolean;
   current: any;
+  skipUrlSync: boolean;
 
   defaults = {
     type: 'custom',
@@ -19,6 +20,7 @@ export class CustomVariable implements Variable {
     includeAll: false,
     multi: false,
     allValue: null,
+    skipUrlSync: false,
   };
 
   /** @ngInject **/

+ 2 - 0
public/app/features/templating/datasource_variable.ts

@@ -7,6 +7,7 @@ export class DatasourceVariable implements Variable {
   options: any;
   current: any;
   refresh: any;
+  skipUrlSync: boolean;
 
   defaults = {
     type: 'datasource',
@@ -18,6 +19,7 @@ export class DatasourceVariable implements Variable {
     options: [],
     query: '',
     refresh: 1,
+    skipUrlSync: false,
   };
 
   /** @ngInject **/

+ 2 - 0
public/app/features/templating/interval_variable.ts

@@ -11,6 +11,7 @@ export class IntervalVariable implements Variable {
   query: string;
   refresh: number;
   current: any;
+  skipUrlSync: boolean;
 
   defaults = {
     type: 'interval',
@@ -24,6 +25,7 @@ export class IntervalVariable implements Variable {
     auto: false,
     auto_min: '10s',
     auto_count: 30,
+    skipUrlSync: false,
   };
 
   /** @ngInject **/

+ 2 - 0
public/app/features/templating/query_variable.ts

@@ -22,6 +22,7 @@ export class QueryVariable implements Variable {
   tagsQuery: string;
   tagValuesQuery: string;
   tags: any[];
+  skipUrlSync: boolean;
 
   defaults = {
     type: 'query',
@@ -42,6 +43,7 @@ export class QueryVariable implements Variable {
     useTags: false,
     tagsQuery: '',
     tagValuesQuery: '',
+    skipUrlSync: false,
   };
 
   /** @ngInject **/

+ 57 - 0
public/app/features/templating/specs/template_srv.jest.ts

@@ -345,6 +345,49 @@ describe('templateSrv', function() {
     });
   });
 
+  describe('fillVariableValuesForUrl skip url sync', function() {
+    beforeEach(function() {
+      initTemplateSrv([
+        {
+          name: 'test',
+          skipUrlSync: true,
+          current: { value: 'value' },
+          getValueForUrl: function() {
+            return this.current.value;
+          },
+        },
+      ]);
+    });
+
+    it('should not include template variable value in url', function() {
+      var params = {};
+      _templateSrv.fillVariableValuesForUrl(params);
+      expect(params['var-test']).toBe(undefined);
+    });
+  });
+
+  describe('fillVariableValuesForUrl with multi value with skip url sync', function() {
+    beforeEach(function() {
+      initTemplateSrv([
+        {
+          type: 'query',
+          name: 'test',
+          skipUrlSync: true,
+          current: { value: ['val1', 'val2'] },
+          getValueForUrl: function() {
+            return this.current.value;
+          },
+        },
+      ]);
+    });
+
+    it('should not include template variable value in url', function() {
+      var params = {};
+      _templateSrv.fillVariableValuesForUrl(params);
+      expect(params['var-test']).toBe(undefined);
+    });
+  });
+
   describe('fillVariableValuesForUrl with multi value and scopedVars', function() {
     beforeEach(function() {
       initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
@@ -359,6 +402,20 @@ describe('templateSrv', function() {
     });
   });
 
+  describe('fillVariableValuesForUrl with multi value, scopedVars and skip url sync', function() {
+    beforeEach(function() {
+      initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
+    });
+
+    it('should not set scoped value as url params', function() {
+      var params = {};
+      _templateSrv.fillVariableValuesForUrl(params, {
+        test: { name: 'test', value: 'val1', skipUrlSync: true },
+      });
+      expect(params['var-test']).toBe(undefined);
+    });
+  });
+
   describe('replaceWithText', function() {
     beforeEach(function() {
       initTemplateSrv([

+ 6 - 0
public/app/features/templating/template_srv.ts

@@ -250,8 +250,14 @@ export class TemplateSrv {
   fillVariableValuesForUrl(params, scopedVars) {
     _.each(this.variables, function(variable) {
       if (scopedVars && scopedVars[variable.name] !== void 0) {
+        if (scopedVars[variable.name].skipUrlSync) {
+          return;
+        }
         params['var-' + variable.name] = scopedVars[variable.name].value;
       } else {
+        if (variable.skipUrlSync) {
+          return;
+        }
         params['var-' + variable.name] = variable.getValueForUrl();
       }
     });