@grafana/data#AppEvent TypeScript Examples

The following examples show how to use @grafana/data#AppEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
on<T>(event: AppEvent<T> | string, handler: (payload?: T | any) => void, scope?: any) {
    if (typeof event === 'string') {
      console.log(`Using strings as events is deprecated and will be removed in a future version. (${event})`);
      this.emitter.on(event, handler);

      if (scope) {
        const unbind = scope.$on('$destroy', () => {
          this.emitter.off(event, handler);
          unbind();
        });
      }
      return;
    }

    this.emitter.on(event.name, handler);

    if (scope) {
      const unbind = scope.$on('$destroy', () => {
        this.emitter.off(event.name, handler);
        unbind();
      });
    }
  }
Example #2
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
off<T>(event: AppEvent<T> | string, handler: (payload?: T | any) => void) {
    if (typeof event === 'string') {
      console.log(`Using strings as events is deprecated and will be removed in a future version. (${event})`);
      this.emitter.off(event, handler);
      return;
    }

    this.emitter.off(event.name, handler);
  }
Example #3
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
/**
   * Emits an `event` with `payload`.
   */
  emit<T extends undefined>(event: AppEvent<T>): void;
Example #4
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
emit<T extends Partial<T> extends T ? Partial<T> : never>(event: AppEvent<T>): void;
Example #5
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
emit<T>(event: AppEvent<T>, payload: T): void;
Example #6
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
emit<T>(event: AppEvent<T> | string, payload?: T | any): void {
    if (typeof event === 'string') {
      console.log(`Using strings as events is deprecated and will be removed in a future version. (${event})`);
      this.emitter.emit(event, payload);
    } else {
      this.emitter.emit(event.name, payload);
    }
  }
Example #7
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
/**
   * Handles `event` with `handler()` when emitted.
   */
  on<T extends undefined>(event: AppEvent<T>, handler: () => void, scope?: any): void;
Example #8
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
on<T extends Partial<T> extends T ? Partial<T> : never>(event: AppEvent<T>, handler: () => void, scope?: any): void;
Example #9
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
on<T>(event: AppEvent<T>, handler: (payload: T) => void, scope?: any): void;
Example #10
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
off<T extends undefined>(event: AppEvent<T>, handler: () => void): void;
Example #11
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
off<T extends Partial<T> extends T ? Partial<T> : never>(event: AppEvent<T>, handler: () => void, scope?: any): void;
Example #12
Source File: emitter.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
off<T>(event: AppEvent<T>, handler: (payload: T) => void): void;
Example #13
Source File: DashboardModel.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
on<T>(event: AppEvent<T>, callback: (payload?: T) => void) {
    this.events.on(event, callback);
  }
Example #14
Source File: DashboardModel.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
off<T>(event: AppEvent<T>, callback?: (payload?: T) => void) {
    this.events.off(event, callback);
  }
Example #15
Source File: panel_ctrl.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
publishAppEvent<T>(event: AppEvent<T>, payload?: T) {
    this.$scope.$root.appEvent(event, payload);
  }
Example #16
Source File: GrafanaCtrl.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
/** @ngInject */
  constructor(
    $scope: any,
    utilSrv: UtilSrv,
    $rootScope: GrafanaRootScope,
    contextSrv: ContextSrv,
    bridgeSrv: BridgeSrv,
    timeSrv: TimeSrv,
    linkSrv: LinkSrv,
    datasourceSrv: DatasourceSrv,
    keybindingSrv: KeybindingSrv,
    dashboardSrv: DashboardSrv,
    angularLoader: AngularLoader
  ) {
    // make angular loader service available to react components
    setAngularLoader(angularLoader);
    setBackendSrv(backendSrv);
    setDataSourceSrv(datasourceSrv);
    setTimeSrv(timeSrv);
    setLinkSrv(linkSrv);
    setKeybindingSrv(keybindingSrv);
    setDashboardSrv(dashboardSrv);

    const store = configureStore();
    setLocationSrv({
      update: (opt: LocationUpdate) => {
        store.dispatch(updateLocation(opt));
      },
    });

    $scope.init = () => {
      $scope.contextSrv = contextSrv;
      $scope.appSubUrl = config.appSubUrl;
      $scope._ = _;

      profiler.init(config, $rootScope);
      utilSrv.init();
      bridgeSrv.init();
    };

    $rootScope.colors = colors;

    $rootScope.onAppEvent = function<T>(
      event: AppEvent<T> | string,
      callback: (event: IAngularEvent, ...args: any[]) => void,
      localScope?: any
    ) {
      let unbind;
      if (typeof event === 'string') {
        unbind = $rootScope.$on(event, callback);
      } else {
        unbind = $rootScope.$on(event.name, callback);
      }

      let callerScope = this;
      if (callerScope.$id === 1 && !localScope) {
        console.log('warning rootScope onAppEvent called without localscope');
      }
      if (localScope) {
        callerScope = localScope;
      }
      callerScope.$on('$destroy', unbind);
    };

    $rootScope.appEvent = <T>(event: AppEvent<T> | string, payload?: T | any) => {
      if (typeof event === 'string') {
        $rootScope.$emit(event, payload);
        appEvents.emit(event, payload);
      } else {
        $rootScope.$emit(event.name, payload);
        appEvents.emit(event, payload);
      }
    };

    $scope.init();
  }