upgrade.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @license Angular v8.1.0
  3. * (c) 2010-2019 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. * @fileoverview added by tsickle
  12. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  13. */
  14. const ɵ0 = (locationSyncBootstrapListener);
  15. /**
  16. * \@description
  17. *
  18. * Creates an initializer that in addition to setting up the Angular
  19. * router sets up the ngRoute integration.
  20. *
  21. * ```
  22. * \@NgModule({
  23. * imports: [
  24. * RouterModule.forRoot(SOME_ROUTES),
  25. * UpgradeModule
  26. * ],
  27. * providers: [
  28. * RouterUpgradeInitializer
  29. * ]
  30. * })
  31. * export class AppModule {
  32. * ngDoBootstrap() {}
  33. * }
  34. * ```
  35. *
  36. * \@publicApi
  37. * @type {?}
  38. */
  39. const RouterUpgradeInitializer = {
  40. provide: APP_BOOTSTRAP_LISTENER,
  41. multi: true,
  42. useFactory: (/** @type {?} */ (ɵ0)),
  43. deps: [UpgradeModule]
  44. };
  45. /**
  46. * \@internal
  47. * @param {?} ngUpgrade
  48. * @return {?}
  49. */
  50. function locationSyncBootstrapListener(ngUpgrade) {
  51. return (/**
  52. * @return {?}
  53. */
  54. () => { setUpLocationSync(ngUpgrade); });
  55. }
  56. /**
  57. * \@description
  58. *
  59. * Sets up a location synchronization.
  60. *
  61. * History.pushState does not fire onPopState, so the Angular location
  62. * doesn't detect it. The workaround is to attach a location change listener
  63. *
  64. * \@publicApi
  65. * @param {?} ngUpgrade
  66. * @param {?=} urlType
  67. * @return {?}
  68. */
  69. function setUpLocationSync(ngUpgrade, urlType = 'path') {
  70. if (!ngUpgrade.$injector) {
  71. throw new Error(`
  72. RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
  73. Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
  74. `);
  75. }
  76. /** @type {?} */
  77. const router = ngUpgrade.injector.get(Router);
  78. /** @type {?} */
  79. const location = ngUpgrade.injector.get(Location);
  80. ngUpgrade.$injector.get('$rootScope')
  81. .$on('$locationChangeStart', (/**
  82. * @param {?} _
  83. * @param {?} next
  84. * @param {?} __
  85. * @return {?}
  86. */
  87. (_, next, __) => {
  88. /** @type {?} */
  89. let url;
  90. if (urlType === 'path') {
  91. url = resolveUrl(next);
  92. }
  93. else if (urlType === 'hash') {
  94. // Remove the first hash from the URL
  95. /** @type {?} */
  96. const hashIdx = next.indexOf('#');
  97. url = resolveUrl(next.substring(0, hashIdx) + next.substring(hashIdx + 1));
  98. }
  99. else {
  100. throw 'Invalid URLType passed to setUpLocationSync: ' + urlType;
  101. }
  102. /** @type {?} */
  103. const path = location.normalize(url.pathname);
  104. router.navigateByUrl(path + url.search + url.hash);
  105. }));
  106. }
  107. /**
  108. * Normalize and parse 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. * @type {?}
  125. */
  126. let anchor;
  127. /**
  128. * @param {?} url
  129. * @return {?}
  130. */
  131. function resolveUrl(url) {
  132. if (!anchor) {
  133. anchor = document.createElement('a');
  134. }
  135. anchor.setAttribute('href', url);
  136. anchor.setAttribute('href', anchor.href);
  137. return {
  138. // IE does not start `pathname` with `/` like other browsers.
  139. pathname: `/${anchor.pathname.replace(/^\//, '')}`,
  140. search: anchor.search,
  141. hash: anchor.hash
  142. };
  143. }
  144. /**
  145. * @fileoverview added by tsickle
  146. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  147. */
  148. /**
  149. * @fileoverview added by tsickle
  150. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  151. */
  152. /**
  153. * Generated bundle index. Do not edit.
  154. */
  155. export { locationSyncBootstrapListener, setUpLocationSync, RouterUpgradeInitializer };
  156. //# sourceMappingURL=upgrade.js.map