bridge_srv.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 { store as reduxStore } from 'app/stores/configureStore';
  5. import { reaction } from 'mobx';
  6. import locationUtil from 'app/core/utils/location_util';
  7. import { updateLocation } from 'app/core/actions';
  8. // Services that handles angular -> mobx store sync & other react <-> angular sync
  9. export class BridgeSrv {
  10. private fullPageReloadRoutes;
  11. /** @ngInject */
  12. constructor(private $location, private $timeout, private $window, private $rootScope, private $route) {
  13. this.fullPageReloadRoutes = ['/logout'];
  14. }
  15. init() {
  16. this.$rootScope.$on('$routeUpdate', (evt, data) => {
  17. const angularUrl = this.$location.url();
  18. if (store.view.currentUrl !== angularUrl) {
  19. store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
  20. }
  21. const state = reduxStore.getState();
  22. if (state.location.url !== angularUrl) {
  23. reduxStore.dispatch(
  24. updateLocation({
  25. path: this.$location.path(),
  26. query: this.$location.search(),
  27. routeParams: this.$route.current.params,
  28. })
  29. );
  30. }
  31. });
  32. this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
  33. store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
  34. reduxStore.dispatch(
  35. updateLocation({
  36. path: this.$location.path(),
  37. query: this.$location.search(),
  38. routeParams: this.$route.current.params,
  39. })
  40. );
  41. });
  42. // listen for mobx store changes and update angular
  43. reaction(
  44. () => store.view.currentUrl,
  45. currentUrl => {
  46. const angularUrl = this.$location.url();
  47. const url = locationUtil.stripBaseFromUrl(currentUrl);
  48. if (angularUrl !== url) {
  49. this.$timeout(() => {
  50. this.$location.url(url);
  51. });
  52. console.log('store updating angular $location.url', url);
  53. }
  54. }
  55. );
  56. // Listen for changes in redux location -> update angular location
  57. reduxStore.subscribe(() => {
  58. const state = reduxStore.getState();
  59. const angularUrl = this.$location.url();
  60. const url = locationUtil.stripBaseFromUrl(state.location.url);
  61. if (angularUrl !== url) {
  62. this.$timeout(() => {
  63. this.$location.url(url);
  64. });
  65. console.log('store updating angular $location.url', url);
  66. }
  67. });
  68. appEvents.on('location-change', payload => {
  69. const urlWithoutBase = locationUtil.stripBaseFromUrl(payload.href);
  70. if (this.fullPageReloadRoutes.indexOf(urlWithoutBase) > -1) {
  71. this.$window.location.href = payload.href;
  72. return;
  73. }
  74. this.$timeout(() => {
  75. // A hack to use timeout when we're changing things (in this case the url) from outside of Angular.
  76. this.$location.url(urlWithoutBase);
  77. });
  78. });
  79. }
  80. }
  81. coreModule.service('bridgeSrv', BridgeSrv);