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

Fixed template variable value changed handling

Torkel Ödegaard 7 лет назад
Родитель
Сommit
7634e04231

+ 4 - 2
public/app/features/dashboard/components/AdHocFilters/AdHocFiltersCtrl.ts

@@ -1,10 +1,12 @@
 import _ from 'lodash';
 import _ from 'lodash';
 import angular from 'angular';
 import angular from 'angular';
 import coreModule from 'app/core/core_module';
 import coreModule from 'app/core/core_module';
+import { DashboardModel } from 'app/features/dashboard/state';
 
 
 export class AdHocFiltersCtrl {
 export class AdHocFiltersCtrl {
   segments: any;
   segments: any;
   variable: any;
   variable: any;
+  dashboard: DashboardModel;
   removeTagFilterSegment: any;
   removeTagFilterSegment: any;
 
 
   /** @ngInject */
   /** @ngInject */
@@ -14,14 +16,13 @@ export class AdHocFiltersCtrl {
     private $q,
     private $q,
     private variableSrv,
     private variableSrv,
     $scope,
     $scope,
-    private $rootScope
   ) {
   ) {
     this.removeTagFilterSegment = uiSegmentSrv.newSegment({
     this.removeTagFilterSegment = uiSegmentSrv.newSegment({
       fake: true,
       fake: true,
       value: '-- remove filter --',
       value: '-- remove filter --',
     });
     });
     this.buildSegmentModel();
     this.buildSegmentModel();
-    this.$rootScope.onAppEvent('template-variable-value-updated', this.buildSegmentModel.bind(this), $scope);
+    this.dashboard.events.on('template-variable-value-updated', this.buildSegmentModel.bind(this), $scope);
   }
   }
 
 
   buildSegmentModel() {
   buildSegmentModel() {
@@ -171,6 +172,7 @@ export function adHocFiltersComponent() {
     controllerAs: 'ctrl',
     controllerAs: 'ctrl',
     scope: {
     scope: {
       variable: '=',
       variable: '=',
+      dashboard: '=',
     },
     },
   };
   };
 }
 }

+ 2 - 2
public/app/features/dashboard/components/DashboardRow/DashboardRow.tsx

@@ -18,11 +18,11 @@ export class DashboardRow extends React.Component<DashboardRowProps, any> {
       collapsed: this.props.panel.collapsed,
       collapsed: this.props.panel.collapsed,
     };
     };
 
 
-    appEvents.on('template-variable-value-updated', this.onVariableUpdated);
+    this.props.dashboard.on('template-variable-value-updated', this.onVariableUpdated);
   }
   }
 
 
   componentWillUnmount() {
   componentWillUnmount() {
-    appEvents.off('template-variable-value-updated', this.onVariableUpdated);
+    this.props.dashboard.off('template-variable-value-updated', this.onVariableUpdated);
   }
   }
 
 
   onVariableUpdated = () => {
   onVariableUpdated = () => {

+ 1 - 1
public/app/features/dashboard/components/SubMenu/template.html

@@ -7,7 +7,7 @@
       <value-select-dropdown ng-if="variable.type !== 'adhoc' && variable.type !== 'textbox'" variable="variable" on-updated="ctrl.variableUpdated(variable)"></value-select-dropdown>
       <value-select-dropdown ng-if="variable.type !== 'adhoc' && variable.type !== 'textbox'" variable="variable" on-updated="ctrl.variableUpdated(variable)"></value-select-dropdown>
       <input type="text" ng-if="variable.type === 'textbox'" ng-model="variable.query" class="gf-form-input width-12"  ng-blur="variable.current.value != variable.query && variable.updateOptions() && ctrl.variableUpdated(variable);" ng-keydown="$event.keyCode === 13 && variable.current.value != variable.query && variable.updateOptions() && ctrl.variableUpdated(variable);" ></input>
       <input type="text" ng-if="variable.type === 'textbox'" ng-model="variable.query" class="gf-form-input width-12"  ng-blur="variable.current.value != variable.query && variable.updateOptions() && ctrl.variableUpdated(variable);" ng-keydown="$event.keyCode === 13 && variable.current.value != variable.query && variable.updateOptions() && ctrl.variableUpdated(variable);" ></input>
     </div>
     </div>
-    <ad-hoc-filters ng-if="variable.type === 'adhoc'" variable="variable"></ad-hoc-filters>
+    <ad-hoc-filters ng-if="variable.type === 'adhoc'" variable="variable" dashboard="ctrl.dashboard"></ad-hoc-filters>
   </div>
   </div>
 
 
   <div ng-if="ctrl.dashboard.annotations.list.length > 0">
   <div ng-if="ctrl.dashboard.annotations.list.length > 0">

+ 5 - 0
public/app/features/dashboard/state/DashboardModel.ts

@@ -900,4 +900,9 @@ export class DashboardModel {
       panel.gridPos.h = Math.round(panel.gridPos.h / scaleFactor) || 1;
       panel.gridPos.h = Math.round(panel.gridPos.h / scaleFactor) || 1;
     });
     });
   }
   }
+
+  templateVariableValueUpdated() {
+    this.processRepeats();
+    this.events.emit('template-variable-value-updated');
+  }
 }
 }

+ 0 - 1
public/app/features/dashboard/state/initDashboard.ts

@@ -91,7 +91,6 @@ export function initDashboard({
             dispatch(updateLocation({path: dashboardUrl, partial: true, replace: true}));
             dispatch(updateLocation({path: dashboardUrl, partial: true, replace: true}));
             return;
             return;
           }
           }
-
           break;
           break;
         }
         }
         case DashboardRouteInfo.New: {
         case DashboardRouteInfo.New: {

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

@@ -48,7 +48,6 @@ describe('VariableSrv', function(this: any) {
         ds.metricFindQuery = () => Promise.resolve(scenario.queryResult);
         ds.metricFindQuery = () => Promise.resolve(scenario.queryResult);
 
 
         ctx.variableSrv = new VariableSrv(
         ctx.variableSrv = new VariableSrv(
-          ctx.$rootScope,
           $q,
           $q,
           ctx.$location,
           ctx.$location,
           ctx.$injector,
           ctx.$injector,

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

@@ -25,9 +25,6 @@ describe('VariableSrv init', function(this: any) {
   };
   };
 
 
   const $injector = {} as any;
   const $injector = {} as any;
-  const $rootscope = {
-    $on: () => {},
-  };
 
 
   let ctx = {} as any;
   let ctx = {} as any;
 
 
@@ -54,7 +51,7 @@ describe('VariableSrv init', function(this: any) {
         };
         };
 
 
         // @ts-ignore
         // @ts-ignore
-        ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv);
+        ctx.variableSrv = new VariableSrv($q, {}, $injector, templateSrv, timeSrv);
 
 
         $injector.instantiate = (variable, model) => {
         $injector.instantiate = (variable, model) => {
           return getVarMockConstructor(variable, model, ctx);
           return getVarMockConstructor(variable, model, ctx);

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

@@ -18,18 +18,18 @@ export class VariableSrv {
   variables: any[];
   variables: any[];
 
 
   /** @ngInject */
   /** @ngInject */
-  constructor(private $rootScope,
-              private $q,
+  constructor(private $q,
               private $location,
               private $location,
               private $injector,
               private $injector,
               private templateSrv: TemplateSrv,
               private templateSrv: TemplateSrv,
               private timeSrv: TimeSrv) {
               private timeSrv: TimeSrv) {
-    $rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope);
+
   }
   }
 
 
   init(dashboard: DashboardModel) {
   init(dashboard: DashboardModel) {
     this.dashboard = dashboard;
     this.dashboard = dashboard;
     this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this));
     this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this));
+    this.dashboard.events.on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this));
 
 
     // create working class models representing variables
     // create working class models representing variables
     this.variables = dashboard.templating.list = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
     this.variables = dashboard.templating.list = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
@@ -59,7 +59,7 @@ export class VariableSrv {
 
 
       return variable.updateOptions().then(() => {
       return variable.updateOptions().then(() => {
         if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
         if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
-          this.$rootScope.$emit('template-variable-value-updated');
+          this.dashboard.templateVariableValueUpdated();
         }
         }
       });
       });
     });
     });
@@ -144,7 +144,7 @@ export class VariableSrv {
 
 
     return this.$q.all(promises).then(() => {
     return this.$q.all(promises).then(() => {
       if (emitChangeEvents) {
       if (emitChangeEvents) {
-        this.$rootScope.appEvent('template-variable-value-updated');
+        this.dashboard.templateVariableValueUpdated();
         this.dashboard.startRefresh();
         this.dashboard.startRefresh();
       }
       }
     });
     });