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';
|
2017-12-31 15:31:11 -06:00
|
|
|
import { store } from 'app/stores/store';
|
|
|
|
import { reaction } from 'mobx';
|
2018-02-01 09:30:48 -06:00
|
|
|
import locationUtil from 'app/core/utils/location_util';
|
2017-12-06 10:36:44 -06:00
|
|
|
|
2017-12-31 15:31:11 -06:00
|
|
|
// Services that handles angular -> mobx store sync & other react <-> angular sync
|
|
|
|
export class BridgeSrv {
|
2017-12-19 06:39:11 -06:00
|
|
|
private fullPageReloadRoutes;
|
2017-12-06 10:36:44 -06:00
|
|
|
|
|
|
|
/** @ngInject */
|
2018-01-11 08:42:45 -06:00
|
|
|
constructor(private $location, private $timeout, private $window, private $rootScope, private $route) {
|
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();
|
2017-12-31 15:31:11 -06:00
|
|
|
if (store.view.currentUrl !== angularUrl) {
|
2018-01-11 08:42:45 -06:00
|
|
|
store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
|
2017-12-31 15:31:11 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
|
2018-02-01 06:28:04 -06:00
|
|
|
store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
|
2017-12-31 15:31:11 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
reaction(
|
|
|
|
() => store.view.currentUrl,
|
|
|
|
currentUrl => {
|
2018-08-26 10:14:40 -05:00
|
|
|
const angularUrl = this.$location.url();
|
2018-02-01 09:30:48 -06:00
|
|
|
const url = locationUtil.stripBaseFromUrl(currentUrl);
|
|
|
|
if (angularUrl !== url) {
|
2018-02-01 06:28:04 -06:00
|
|
|
this.$timeout(() => {
|
2018-02-01 09:30:48 -06:00
|
|
|
this.$location.url(url);
|
2018-02-01 06:28:04 -06:00
|
|
|
});
|
2018-02-01 09:30:48 -06:00
|
|
|
console.log('store updating angular $location.url', url);
|
2017-12-31 15:31:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.on('location-change', 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);
|