upgrade.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Angular v19.2.4
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { Location } from '@angular/common';
  7. import { APP_BOOTSTRAP_LISTENER } from '@angular/core';
  8. import { Router } from '@angular/router';
  9. import { UpgradeModule } from '@angular/upgrade/static';
  10. /**
  11. * Creates an initializer that sets up `ngRoute` integration
  12. * along with setting up the Angular router.
  13. *
  14. * @usageNotes
  15. *
  16. * <code-example language="typescript">
  17. * @NgModule({
  18. * imports: [
  19. * RouterModule.forRoot(SOME_ROUTES),
  20. * UpgradeModule
  21. * ],
  22. * providers: [
  23. * RouterUpgradeInitializer
  24. * ]
  25. * })
  26. * export class AppModule {
  27. * ngDoBootstrap() {}
  28. * }
  29. * </code-example>
  30. *
  31. * @publicApi
  32. */
  33. const RouterUpgradeInitializer = {
  34. provide: APP_BOOTSTRAP_LISTENER,
  35. multi: true,
  36. useFactory: locationSyncBootstrapListener,
  37. deps: [UpgradeModule],
  38. };
  39. /**
  40. * @internal
  41. */
  42. function locationSyncBootstrapListener(ngUpgrade) {
  43. return () => {
  44. setUpLocationSync(ngUpgrade);
  45. };
  46. }
  47. /**
  48. * Sets up a location change listener to trigger `history.pushState`.
  49. * Works around the problem that `onPopState` does not trigger `history.pushState`.
  50. * Must be called *after* calling `UpgradeModule.bootstrap`.
  51. *
  52. * @param ngUpgrade The upgrade NgModule.
  53. * @param urlType The location strategy.
  54. * @see {@link /api/common/HashLocationStrategy HashLocationStrategy}
  55. * @see {@link /api/common/PathLocationStrategy PathLocationStrategy}
  56. *
  57. * @publicApi
  58. */
  59. function setUpLocationSync(ngUpgrade, urlType = 'path') {
  60. if (!ngUpgrade.$injector) {
  61. throw new Error(`
  62. RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
  63. Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
  64. `);
  65. }
  66. const router = ngUpgrade.injector.get(Router);
  67. const location = ngUpgrade.injector.get(Location);
  68. ngUpgrade.$injector
  69. .get('$rootScope')
  70. .$on('$locationChangeStart', (event, newUrl, oldUrl, newState, oldState) => {
  71. // Navigations coming from Angular router have a navigationId state
  72. // property. Don't trigger Angular router navigation again if it is
  73. // caused by a URL change from the current Angular router
  74. // navigation.
  75. const currentNavigationId = router.getCurrentNavigation()?.id;
  76. const newStateNavigationId = newState?.navigationId;
  77. if (newStateNavigationId !== undefined && newStateNavigationId === currentNavigationId) {
  78. return;
  79. }
  80. let url;
  81. if (urlType === 'path') {
  82. url = resolveUrl(newUrl);
  83. }
  84. else if (urlType === 'hash') {
  85. // Remove the first hash from the URL
  86. const hashIdx = newUrl.indexOf('#');
  87. url = resolveUrl(newUrl.substring(0, hashIdx) + newUrl.substring(hashIdx + 1));
  88. }
  89. else {
  90. throw 'Invalid URLType passed to setUpLocationSync: ' + urlType;
  91. }
  92. const path = location.normalize(url.pathname);
  93. router.navigateByUrl(path + url.search + url.hash);
  94. });
  95. }
  96. /**
  97. * Normalizes and parses a URL.
  98. *
  99. * - Normalizing means that a relative URL will be resolved into an absolute URL in the context of
  100. * the application document.
  101. * - Parsing means that the anchor's `protocol`, `hostname`, `port`, `pathname` and related
  102. * properties are all populated to reflect the normalized URL.
  103. *
  104. * While this approach has wide compatibility, it doesn't work as expected on IE. On IE, normalizing
  105. * happens similar to other browsers, but the parsed components will not be set. (E.g. if you assign
  106. * `a.href = 'foo'`, then `a.protocol`, `a.host`, etc. will not be correctly updated.)
  107. * We work around that by performing the parsing in a 2nd step by taking a previously normalized URL
  108. * and assigning it again. This correctly populates all properties.
  109. *
  110. * See
  111. * https://github.com/angular/angular.js/blob/2c7400e7d07b0f6cec1817dab40b9250ce8ebce6/src/ng/urlUtils.js#L26-L33
  112. * for more info.
  113. */
  114. let anchor;
  115. function resolveUrl(url) {
  116. anchor ??= document.createElement('a');
  117. anchor.setAttribute('href', url);
  118. anchor.setAttribute('href', anchor.href);
  119. return {
  120. // IE does not start `pathname` with `/` like other browsers.
  121. pathname: `/${anchor.pathname.replace(/^\//, '')}`,
  122. search: anchor.search,
  123. hash: anchor.hash,
  124. };
  125. }
  126. export { RouterUpgradeInitializer, locationSyncBootstrapListener, setUpLocationSync };
  127. //# sourceMappingURL=upgrade.mjs.map