upgrade.js 4.0 KB

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