upgrade.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @license Angular v7.0.3
  3. * (c) 2010-2018 Google, Inc. 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. * @fileoverview added by tsickle
  12. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  13. */
  14. /** *
  15. * \@description
  16. *
  17. * Creates an initializer that in addition to setting up the Angular
  18. * router sets up the ngRoute integration.
  19. *
  20. * ```
  21. * \@NgModule({
  22. * imports: [
  23. * RouterModule.forRoot(SOME_ROUTES),
  24. * UpgradeModule
  25. * ],
  26. * providers: [
  27. * RouterUpgradeInitializer
  28. * ]
  29. * })
  30. * export class AppModule {
  31. * ngDoBootstrap() {}
  32. * }
  33. * ```
  34. *
  35. * \@publicApi
  36. @type {?} */
  37. const RouterUpgradeInitializer = {
  38. provide: APP_BOOTSTRAP_LISTENER,
  39. multi: true,
  40. useFactory: /** @type {?} */ (locationSyncBootstrapListener),
  41. deps: [UpgradeModule]
  42. };
  43. /**
  44. * \@internal
  45. * @param {?} ngUpgrade
  46. * @return {?}
  47. */
  48. function locationSyncBootstrapListener(ngUpgrade) {
  49. return () => { setUpLocationSync(ngUpgrade); };
  50. }
  51. /**
  52. * \@description
  53. *
  54. * Sets up a location synchronization.
  55. *
  56. * History.pushState does not fire onPopState, so the Angular location
  57. * doesn't detect it. The workaround is to attach a location change listener
  58. *
  59. * \@publicApi
  60. * @param {?} ngUpgrade
  61. * @return {?}
  62. */
  63. function setUpLocationSync(ngUpgrade) {
  64. if (!ngUpgrade.$injector) {
  65. throw new Error(`
  66. RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
  67. Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
  68. `);
  69. }
  70. /** @type {?} */
  71. const router = ngUpgrade.injector.get(Router);
  72. /** @type {?} */
  73. const location = ngUpgrade.injector.get(Location);
  74. ngUpgrade.$injector.get('$rootScope')
  75. .$on('$locationChangeStart', (_, next, __) => {
  76. /** @type {?} */
  77. const url = resolveUrl(next);
  78. /** @type {?} */
  79. const path = location.normalize(url.pathname);
  80. router.navigateByUrl(path + url.search + url.hash);
  81. });
  82. }
  83. /** *
  84. * Normalize and parse a URL.
  85. *
  86. * - Normalizing means that a relative URL will be resolved into an absolute URL in the context of
  87. * the application document.
  88. * - Parsing means that the anchor's `protocol`, `hostname`, `port`, `pathname` and related
  89. * properties are all populated to reflect the normalized URL.
  90. *
  91. * While this approach has wide compatibility, it doesn't work as expected on IE. On IE, normalizing
  92. * happens similar to other browsers, but the parsed components will not be set. (E.g. if you assign
  93. * `a.href = 'foo'`, then `a.protocol`, `a.host`, etc. will not be correctly updated.)
  94. * We work around that by performing the parsing in a 2nd step by taking a previously normalized URL
  95. * and assigning it again. This correctly populates all properties.
  96. *
  97. * See
  98. * https://github.com/angular/angular.js/blob/2c7400e7d07b0f6cec1817dab40b9250ce8ebce6/src/ng/urlUtils.js#L26-L33
  99. * for more info.
  100. @type {?} */
  101. let anchor;
  102. /**
  103. * @param {?} url
  104. * @return {?}
  105. */
  106. function resolveUrl(url) {
  107. if (!anchor) {
  108. anchor = document.createElement('a');
  109. }
  110. anchor.setAttribute('href', url);
  111. anchor.setAttribute('href', anchor.href);
  112. return {
  113. // IE does not start `pathname` with `/` like other browsers.
  114. pathname: `/${anchor.pathname.replace(/^\//, '')}`,
  115. search: anchor.search,
  116. hash: anchor.hash
  117. };
  118. }
  119. /**
  120. * @fileoverview added by tsickle
  121. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  122. */
  123. // This file only reexports content of the `src` folder. Keep it that way.
  124. /**
  125. * @fileoverview added by tsickle
  126. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  127. */
  128. /**
  129. * Generated bundle index. Do not edit.
  130. */
  131. export { locationSyncBootstrapListener, setUpLocationSync, RouterUpgradeInitializer };
  132. //# sourceMappingURL=upgrade.js.map