2017-12-31 15:31:11 -06:00
|
|
|
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';
|
2018-02-01 09:30:48 -06:00
|
|
|
import locationUtil from 'app/core/utils/location_util';
|
2018-08-31 15:16:20 -05:00
|
|
|
import { updateLocation } from 'app/core/actions';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { ITimeoutService, ILocationService, IWindowService } from 'angular';
|
|
|
|
import { CoreEvents } from 'app/types';
|
|
|
|
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
|
2017-12-06 10:36:44 -06:00
|
|
|
|
2018-10-25 06:57:23 -05:00
|
|
|
// Services that handles angular -> redux store sync & other react <-> angular sync
|
2017-12-31 15:31:11 -06:00
|
|
|
export class BridgeSrv {
|
2019-04-28 02:58:12 -05:00
|
|
|
private fullPageReloadRoutes: string[];
|
2017-12-06 10:36:44 -06:00
|
|
|
|
|
|
|
/** @ngInject */
|
2019-04-28 02:58:12 -05:00
|
|
|
constructor(
|
|
|
|
private $location: ILocationService,
|
|
|
|
private $timeout: ITimeoutService,
|
|
|
|
private $window: IWindowService,
|
2019-10-14 03:27:47 -05:00
|
|
|
private $rootScope: GrafanaRootScope,
|
2019-04-28 02:58:12 -05:00
|
|
|
private $route: any
|
|
|
|
) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.fullPageReloadRoutes = ['/logout'];
|
2017-12-18 09:22:58 -06:00
|
|
|
}
|
|
|
|
|
2017-12-06 10:36:44 -06:00
|
|
|
init() {
|
2017-12-31 15:31:11 -06:00
|
|
|
this.$rootScope.$on('$routeUpdate', (evt, data) => {
|
2018-08-26 10:14:40 -05:00
|
|
|
const angularUrl = this.$location.url();
|
2018-09-14 02:41:37 -05:00
|
|
|
const state = store.getState();
|
2018-08-31 15:16:20 -05:00
|
|
|
if (state.location.url !== angularUrl) {
|
2018-09-14 02:41:37 -05:00
|
|
|
store.dispatch(
|
2018-08-31 15:16:20 -05:00
|
|
|
updateLocation({
|
|
|
|
path: this.$location.path(),
|
|
|
|
query: this.$location.search(),
|
|
|
|
routeParams: this.$route.current.params,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-12-31 15:31:11 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
|
2018-09-14 02:41:37 -05:00
|
|
|
store.dispatch(
|
2018-08-31 15:16:20 -05:00
|
|
|
updateLocation({
|
|
|
|
path: this.$location.path(),
|
|
|
|
query: this.$location.search(),
|
|
|
|
routeParams: this.$route.current.params,
|
|
|
|
})
|
|
|
|
);
|
2017-12-31 15:31:11 -06:00
|
|
|
});
|
|
|
|
|
2018-08-31 15:16:20 -05:00
|
|
|
// Listen for changes in redux location -> update angular location
|
2018-09-14 02:41:37 -05:00
|
|
|
store.subscribe(() => {
|
|
|
|
const state = store.getState();
|
2018-08-31 15:16:20 -05:00
|
|
|
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();
|
|
|
|
}
|
2018-08-31 15:16:20 -05:00
|
|
|
});
|
|
|
|
console.log('store updating angular $location.url', url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.on(CoreEvents.locationChange, payload => {
|
2018-02-01 09:30:48 -06:00
|
|
|
const urlWithoutBase = locationUtil.stripBaseFromUrl(payload.href);
|
2017-12-19 06:39:11 -06:00
|
|
|
if (this.fullPageReloadRoutes.indexOf(urlWithoutBase) > -1) {
|
|
|
|
this.$window.location.href = payload.href;
|
|
|
|
return;
|
|
|
|
}
|
2017-12-18 09:22:58 -06:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
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);
|
2017-12-18 09:22:58 -06:00
|
|
|
});
|
2017-12-06 10:36:44 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 15:31:11 -06:00
|
|
|
coreModule.service('bridgeSrv', BridgeSrv);
|