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