bridge_srv.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import coreModule from 'app/core/core_module';
  2. import appEvents from 'app/core/app_events';
  3. import { store } from 'app/stores/store';
  4. import { reaction } from 'mobx';
  5. import locationUtil from 'app/core/utils/location_util';
  6. // Services that handles angular -> mobx store sync & other react <-> angular sync
  7. export class BridgeSrv {
  8. private fullPageReloadRoutes;
  9. /** @ngInject */
  10. constructor(private $location, private $timeout, private $window, private $rootScope, private $route) {
  11. this.fullPageReloadRoutes = ['/logout'];
  12. }
  13. init() {
  14. this.$rootScope.$on('$routeUpdate', (evt, data) => {
  15. let angularUrl = this.$location.url();
  16. if (store.view.currentUrl !== angularUrl) {
  17. store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
  18. }
  19. });
  20. this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
  21. store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
  22. });
  23. reaction(
  24. () => store.view.currentUrl,
  25. currentUrl => {
  26. let angularUrl = this.$location.url();
  27. const url = locationUtil.stripBaseFromUrl(currentUrl);
  28. if (angularUrl !== url) {
  29. this.$timeout(() => {
  30. this.$location.url(url);
  31. });
  32. console.log('store updating angular $location.url', url);
  33. }
  34. }
  35. );
  36. appEvents.on('location-change', payload => {
  37. const urlWithoutBase = locationUtil.stripBaseFromUrl(payload.href);
  38. if (this.fullPageReloadRoutes.indexOf(urlWithoutBase) > -1) {
  39. this.$window.location.href = payload.href;
  40. return;
  41. }
  42. this.$timeout(() => {
  43. // A hack to use timeout when we're changing things (in this case the url) from outside of Angular.
  44. this.$location.url(urlWithoutBase);
  45. });
  46. });
  47. }
  48. }
  49. coreModule.service('bridgeSrv', BridgeSrv);