2017-12-31 15:31:11 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
2017-12-20 05:33:33 -06:00
|
|
|
import config from 'app/core/config';
|
|
|
|
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';
|
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-18 09:22:58 -06:00
|
|
|
private appSubUrl;
|
2017-12-19 06:39:11 -06:00
|
|
|
private fullPageReloadRoutes;
|
2017-12-06 10:36:44 -06:00
|
|
|
|
|
|
|
/** @ngInject */
|
2017-12-31 15:31:11 -06:00
|
|
|
constructor(private $location, private $timeout, private $window, private $rootScope) {
|
2017-12-18 09:22:58 -06:00
|
|
|
this.appSubUrl = config.appSubUrl;
|
2017-12-20 05:33:33 -06:00
|
|
|
this.fullPageReloadRoutes = ['/logout'];
|
2017-12-18 09:22:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Angular's $location does not like <base href...> and absolute urls
|
2017-12-20 05:33:33 -06:00
|
|
|
stripBaseFromUrl(url = '') {
|
2017-12-18 09:22:58 -06:00
|
|
|
const appSubUrl = this.appSubUrl;
|
2017-12-20 05:33:33 -06:00
|
|
|
const stripExtraChars = appSubUrl.endsWith('/') ? 1 : 0;
|
2017-12-19 09:06:54 -06:00
|
|
|
const urlWithoutBase =
|
2017-12-21 01:39:31 -06:00
|
|
|
url.length > 0 && url.indexOf(appSubUrl) === 0 ? url.slice(appSubUrl.length - stripExtraChars) : url;
|
2017-12-18 09:22:58 -06:00
|
|
|
|
|
|
|
return urlWithoutBase;
|
2017-12-06 10:36:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2017-12-31 15:31:11 -06:00
|
|
|
this.$rootScope.$on('$routeUpdate', (evt, data) => {
|
|
|
|
let angularUrl = this.$location.url();
|
|
|
|
if (store.view.currentUrl !== angularUrl) {
|
|
|
|
store.view.updatePathAndQuery(this.$location.path(), this.$location.search());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
|
|
|
|
let angularUrl = this.$location.url();
|
|
|
|
if (store.view.currentUrl !== angularUrl) {
|
|
|
|
store.view.updatePathAndQuery(this.$location.path(), this.$location.search());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reaction(
|
|
|
|
() => store.view.currentUrl,
|
|
|
|
currentUrl => {
|
|
|
|
let angularUrl = this.$location.url();
|
|
|
|
if (angularUrl !== currentUrl) {
|
|
|
|
this.$location.url(currentUrl);
|
|
|
|
console.log('store updating angular $location.url', currentUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.on('location-change', payload => {
|
2017-12-18 09:22:58 -06:00
|
|
|
const urlWithoutBase = this.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);
|