upgrade.js 4.5 KB

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