upgrade.mjs 4.7 KB

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