UNPKG

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