Browse Source

Minor refactoring and adding some typing

Torkel Ödegaard 7 years ago
parent
commit
8b68ba5cbb

+ 1 - 1
public/app/features/templating/editor_ctrl.ts

@@ -135,7 +135,7 @@ export class VariableEditorCtrl {
         $scope.runQuery().then(() => {
           $scope.reset();
           $scope.mode = 'list';
-          templateSrv.updateTemplateData();
+          templateSrv.updateIndex();
         });
       }
     };

+ 2 - 2
public/app/features/templating/specs/template_srv.test.ts

@@ -348,7 +348,7 @@ describe('templateSrv', () => {
     });
   });
 
-  describe('updateTemplateData with simple value', () => {
+  describe('updateIndex with simple value', () => {
     beforeEach(() => {
       initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
     });
@@ -476,7 +476,7 @@ describe('templateSrv', () => {
         }
       ]);
       _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
-      _templateSrv.updateTemplateData();
+      _templateSrv.updateIndex();
     });
 
     it('should replace with text except for grafanaVariables', () => {

+ 1 - 1
public/app/features/templating/specs/variable_srv.test.ts

@@ -23,7 +23,7 @@ describe('VariableSrv', function(this: any) {
       init: vars => {
         this.variables = vars;
       },
-      updateTemplateData: () => {},
+      updateIndex: () => {},
       replace: str =>
         str.replace(this.regex, match => {
           return match;

+ 2 - 1
public/app/features/templating/specs/variable_srv_init.test.ts

@@ -11,7 +11,7 @@ describe('VariableSrv init', function(this: any) {
       this.variables = vars;
     },
     variableInitialized: () => {},
-    updateTemplateData: () => {},
+    updateIndex: () => {},
     replace: str =>
       str.replace(this.regex, match => {
         return match;
@@ -53,6 +53,7 @@ describe('VariableSrv init', function(this: any) {
           templateSrv,
         };
 
+        // @ts-ignore
         ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv);
 
         $injector.instantiate = (variable, model) => {

+ 5 - 5
public/app/features/templating/template_srv.ts

@@ -25,10 +25,10 @@ export class TemplateSrv {
   init(variables, timeRange?: TimeRange) {
     this.variables = variables;
     this.timeRange = timeRange;
-    this.updateTemplateData();
+    this.updateIndex();
   }
 
-  updateTemplateData() {
+  updateIndex() {
     const existsOrEmpty = value => value || value === '';
 
     this.index = this.variables.reduce((acc, currentValue) => {
@@ -54,9 +54,9 @@ export class TemplateSrv {
     }
   }
 
-  updateTimeVariables(timeRange: TimeRange) {
+  updateTimeRange(timeRange: TimeRange) {
     this.timeRange = timeRange;
-    this.updateTemplateData();
+    this.updateIndex();
   }
 
   variableInitialized(variable) {
@@ -289,7 +289,7 @@ export class TemplateSrv {
     });
   }
 
-  fillVariableValuesForUrl(params, scopedVars) {
+  fillVariableValuesForUrl(params, scopedVars?) {
     _.each(this.variables, variable => {
       if (scopedVars && scopedVars[variable.name] !== void 0) {
         if (scopedVars[variable.name].skipUrlSync) {

+ 18 - 8
public/app/features/templating/variable_srv.ts

@@ -6,18 +6,28 @@ import _ from 'lodash';
 import coreModule from 'app/core/core_module';
 import { variableTypes } from './variable';
 import { Graph } from 'app/core/utils/dag';
+import { TemplateSrv } from 'app/features/templating/template_srv';
+import { TimeSrv } from 'app/features/dashboard/time_srv';
+import { DashboardModel } from 'app/features/dashboard/dashboard_model';
+
+// Types
 import { TimeRange } from '@grafana/ui/src';
 
 export class VariableSrv {
-  dashboard: any;
-  variables: any;
+  dashboard: DashboardModel;
+  variables: any[];
 
   /** @ngInject */
-  constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv, private timeSrv) {
+  constructor(private $rootScope,
+              private $q,
+              private $location,
+              private $injector,
+              private templateSrv: TemplateSrv,
+              private timeSrv: TimeSrv) {
     $rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope);
   }
 
-  init(dashboard) {
+  init(dashboard: DashboardModel) {
     this.dashboard = dashboard;
     this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this));
 
@@ -38,12 +48,12 @@ export class VariableSrv {
         })
       )
       .then(() => {
-        this.templateSrv.updateTemplateData();
+        this.templateSrv.updateIndex();
       });
   }
 
   onTimeRangeUpdated(timeRange: TimeRange) {
-    this.templateSrv.updateTimeVariables(timeRange);
+    this.templateSrv.updateTimeRange(timeRange);
     const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => {
       const previousOptions = variable.options.slice();
 
@@ -102,14 +112,14 @@ export class VariableSrv {
 
   addVariable(variable) {
     this.variables.push(variable);
-    this.templateSrv.updateTemplateData();
+    this.templateSrv.updateIndex();
     this.dashboard.updateSubmenuVisibility();
   }
 
   removeVariable(variable) {
     const index = _.indexOf(this.variables, variable);
     this.variables.splice(index, 1);
-    this.templateSrv.updateTemplateData();
+    this.templateSrv.updateIndex();
     this.dashboard.updateSubmenuVisibility();
   }
 

+ 1 - 1
public/test/specs/helpers.ts

@@ -182,7 +182,7 @@ export function TemplateSrvStub(this: any) {
     return [];
   };
   this.fillVariableValuesForUrl = () => {};
-  this.updateTemplateData = () => {};
+  this.updateIndex = () => {};
   this.variableExists = () => {
     return false;
   };