UNPKG

4.77 kBJavaScriptView Raw
1/**
2 * @license Angular v8.1.3
3 * (c) 2010-2019 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 Inc. 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 */
19var ɵ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" linenums="false">
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 */
43var 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 function () { setUpLocationSync(ngUpgrade); };
54}
55/**
56 * Sets up a location change listener to trigger `history.pushState`.
57 * Works around the problem that `onPopState` does not trigger `history.pushState`.
58 * Must be called *after* calling `UpgradeModule.bootstrap`.
59 *
60 * @param ngUpgrade The upgrade NgModule.
61 * @param urlType The location strategy.
62 * @see `HashLocationStrategy`
63 * @see `PathLocationStrategy`
64 *
65 * @publicApi
66 */
67function setUpLocationSync(ngUpgrade, urlType) {
68 if (urlType === void 0) { urlType = 'path'; }
69 if (!ngUpgrade.$injector) {
70 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 ");
71 }
72 var router = ngUpgrade.injector.get(Router);
73 var location = ngUpgrade.injector.get(Location);
74 ngUpgrade.$injector.get('$rootScope')
75 .$on('$locationChangeStart', function (_, next, __) {
76 var url;
77 if (urlType === 'path') {
78 url = resolveUrl(next);
79 }
80 else if (urlType === 'hash') {
81 // Remove the first hash from the URL
82 var hashIdx = next.indexOf('#');
83 url = resolveUrl(next.substring(0, hashIdx) + next.substring(hashIdx + 1));
84 }
85 else {
86 throw 'Invalid URLType passed to setUpLocationSync: ' + urlType;
87 }
88 var path = location.normalize(url.pathname);
89 router.navigateByUrl(path + url.search + url.hash);
90 });
91}
92/**
93 * Normalizes and parses a URL.
94 *
95 * - Normalizing means that a relative URL will be resolved into an absolute URL in the context of
96 * the application document.
97 * - Parsing means that the anchor's `protocol`, `hostname`, `port`, `pathname` and related
98 * properties are all populated to reflect the normalized URL.
99 *
100 * While this approach has wide compatibility, it doesn't work as expected on IE. On IE, normalizing
101 * happens similar to other browsers, but the parsed components will not be set. (E.g. if you assign
102 * `a.href = 'foo'`, then `a.protocol`, `a.host`, etc. will not be correctly updated.)
103 * We work around that by performing the parsing in a 2nd step by taking a previously normalized URL
104 * and assigning it again. This correctly populates all properties.
105 *
106 * See
107 * https://github.com/angular/angular.js/blob/2c7400e7d07b0f6cec1817dab40b9250ce8ebce6/src/ng/urlUtils.js#L26-L33
108 * for more info.
109 */
110var anchor;
111function resolveUrl(url) {
112 if (!anchor) {
113 anchor = document.createElement('a');
114 }
115 anchor.setAttribute('href', url);
116 anchor.setAttribute('href', anchor.href);
117 return {
118 // IE does not start `pathname` with `/` like other browsers.
119 pathname: "/" + anchor.pathname.replace(/^\//, ''),
120 search: anchor.search,
121 hash: anchor.hash
122 };
123}
124
125/**
126 * @license
127 * Copyright Google Inc. All Rights Reserved.
128 *
129 * Use of this source code is governed by an MIT-style license that can be
130 * found in the LICENSE file at https://angular.io/license
131 */
132// This file only reexports content of the `src` folder. Keep it that way.
133
134/**
135 * @license
136 * Copyright Google Inc. All Rights Reserved.
137 *
138 * Use of this source code is governed by an MIT-style license that can be
139 * found in the LICENSE file at https://angular.io/license
140 */
141
142/**
143 * Generated bundle index. Do not edit.
144 */
145
146export { RouterUpgradeInitializer, locationSyncBootstrapListener, setUpLocationSync, ɵ0 };
147//# sourceMappingURL=upgrade.js.map