grafana/public/app/core/services/bridge_srv.ts

83 lines
2.6 KiB
TypeScript
Raw Normal View History

import coreModule from 'app/core/core_module';
2017-12-20 05:33:33 -06:00
import appEvents from 'app/core/app_events';
2018-11-05 09:54:48 -06:00
import { store } from 'app/store/store';
import locationUtil from 'app/core/utils/location_util';
import { updateLocation } from 'app/core/actions';
import { ITimeoutService, ILocationService, IWindowService } from 'angular';
import { CoreEvents } from 'app/types';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
2018-10-25 06:57:23 -05:00
// Services that handles angular -> redux store sync & other react <-> angular sync
export class BridgeSrv {
private fullPageReloadRoutes: string[];
/** @ngInject */
constructor(
private $location: ILocationService,
private $timeout: ITimeoutService,
private $window: IWindowService,
private $rootScope: GrafanaRootScope,
private $route: any
) {
2017-12-20 05:33:33 -06:00
this.fullPageReloadRoutes = ['/logout'];
}
init() {
this.$rootScope.$on('$routeUpdate', (evt, data) => {
const angularUrl = this.$location.url();
2018-09-14 02:41:37 -05:00
const state = store.getState();
if (state.location.url !== angularUrl) {
2018-09-14 02:41:37 -05:00
store.dispatch(
updateLocation({
path: this.$location.path(),
query: this.$location.search(),
routeParams: this.$route.current.params,
})
);
}
});
this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
2018-09-14 02:41:37 -05:00
store.dispatch(
updateLocation({
path: this.$location.path(),
query: this.$location.search(),
routeParams: this.$route.current.params,
})
);
});
// Listen for changes in redux location -> update angular location
2018-09-14 02:41:37 -05:00
store.subscribe(() => {
const state = store.getState();
const angularUrl = this.$location.url();
const url = locationUtil.stripBaseFromUrl(state.location.url);
if (angularUrl !== url) {
this.$timeout(() => {
this.$location.url(url);
2019-02-04 06:49:14 -06:00
// some state changes should not trigger new browser history
if (state.location.replace) {
this.$location.replace();
}
});
console.log('store updating angular $location.url', url);
}
});
appEvents.on(CoreEvents.locationChange, payload => {
const urlWithoutBase = locationUtil.stripBaseFromUrl(payload.href);
if (this.fullPageReloadRoutes.indexOf(urlWithoutBase) > -1) {
this.$window.location.href = payload.href;
return;
}
this.$timeout(() => {
// A hack to use timeout when we're changing things (in this case the url) from outside of Angular.
this.$location.url(urlWithoutBase);
});
});
}
}
coreModule.service('bridgeSrv', BridgeSrv);