bridgeReactAngularRouting.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { ILocationService } from 'angular';
  2. import { RouteParamsProvider } from '../core/navigation/patch/RouteParamsProvider';
  3. import { RouteProvider } from '../core/navigation/patch/RouteProvider';
  4. import { AngularLocationWrapper } from './AngularLocationWrapper';
  5. import { coreModule } from './core_module';
  6. // Neutralizing Angular’s location tampering
  7. // https://stackoverflow.com/a/19825756
  8. const tamperAngularLocation = () => {
  9. coreModule.config([
  10. '$provide',
  11. ($provide: any) => {
  12. $provide.decorator('$browser', [
  13. '$delegate',
  14. ($delegate: any) => {
  15. $delegate.onUrlChange = () => {};
  16. $delegate.url = () => '';
  17. return $delegate;
  18. },
  19. ]);
  20. },
  21. ]);
  22. };
  23. // Intercepting $location service with implementation based on history
  24. const interceptAngularLocation = () => {
  25. coreModule.config([
  26. '$provide',
  27. ($provide: any) => {
  28. $provide.decorator('$location', [
  29. '$delegate',
  30. ($delegate: ILocationService) => {
  31. $delegate = new AngularLocationWrapper() as unknown as ILocationService;
  32. return $delegate;
  33. },
  34. ]);
  35. },
  36. ]);
  37. coreModule.provider('$route', RouteProvider);
  38. coreModule.provider('$routeParams', RouteParamsProvider);
  39. };
  40. export function initAngularRoutingBridge() {
  41. tamperAngularLocation();
  42. interceptAngularLocation();
  43. }