UNPKG

4.96 kBJavaScriptView Raw
1/**
2 * @license Angular v8.2.0
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 * @fileoverview added by tsickle
14 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
15 */
16const ɵ0 = (locationSyncBootstrapListener);
17/**
18 * Creates an initializer that sets up `ngRoute` integration
19 * along with setting up the Angular router.
20 *
21 * \@usageNotes
22 *
23 * <code-example language="typescript">
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 * </code-example>
37 *
38 * \@publicApi
39 * @type {?}
40 */
41const RouterUpgradeInitializer = {
42 provide: APP_BOOTSTRAP_LISTENER,
43 multi: true,
44 useFactory: (/** @type {?} */0)),
45 deps: [UpgradeModule]
46};
47/**
48 * \@internal
49 * @param {?} ngUpgrade
50 * @return {?}
51 */
52function locationSyncBootstrapListener(ngUpgrade) {
53 return (/**
54 * @return {?}
55 */
56 () => { setUpLocationSync(ngUpgrade); });
57}
58/**
59 * Sets up a location change listener to trigger `history.pushState`.
60 * Works around the problem that `onPopState` does not trigger `history.pushState`.
61 * Must be called *after* calling `UpgradeModule.bootstrap`.
62 *
63 * @see `HashLocationStrategy` / `PathLocationStrategy`
64 *
65 * \@publicApi
66 * @param {?} ngUpgrade The upgrade NgModule.
67 * @param {?=} urlType The location strategy.
68 * @return {?}
69 */
70function setUpLocationSync(ngUpgrade, urlType = 'path') {
71 if (!ngUpgrade.$injector) {
72 throw new Error(`
73 RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
74 Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
75 `);
76 }
77 /** @type {?} */
78 const router = ngUpgrade.injector.get(Router);
79 /** @type {?} */
80 const location = ngUpgrade.injector.get(Location);
81 ngUpgrade.$injector.get('$rootScope')
82 .$on('$locationChangeStart', (/**
83 * @param {?} _
84 * @param {?} next
85 * @param {?} __
86 * @return {?}
87 */
88 (_, next, __) => {
89 /** @type {?} */
90 let url;
91 if (urlType === 'path') {
92 url = resolveUrl(next);
93 }
94 else if (urlType === 'hash') {
95 // Remove the first hash from the URL
96 /** @type {?} */
97 const hashIdx = next.indexOf('#');
98 url = resolveUrl(next.substring(0, hashIdx) + next.substring(hashIdx + 1));
99 }
100 else {
101 throw 'Invalid URLType passed to setUpLocationSync: ' + urlType;
102 }
103 /** @type {?} */
104 const path = location.normalize(url.pathname);
105 router.navigateByUrl(path + url.search + url.hash);
106 }));
107}
108/**
109 * Normalizes and parses a URL.
110 *
111 * - Normalizing means that a relative URL will be resolved into an absolute URL in the context of
112 * the application document.
113 * - Parsing means that the anchor's `protocol`, `hostname`, `port`, `pathname` and related
114 * properties are all populated to reflect the normalized URL.
115 *
116 * While this approach has wide compatibility, it doesn't work as expected on IE. On IE, normalizing
117 * happens similar to other browsers, but the parsed components will not be set. (E.g. if you assign
118 * `a.href = 'foo'`, then `a.protocol`, `a.host`, etc. will not be correctly updated.)
119 * We work around that by performing the parsing in a 2nd step by taking a previously normalized URL
120 * and assigning it again. This correctly populates all properties.
121 *
122 * See
123 * https://github.com/angular/angular.js/blob/2c7400e7d07b0f6cec1817dab40b9250ce8ebce6/src/ng/urlUtils.js#L26-L33
124 * for more info.
125 * @type {?}
126 */
127let anchor;
128/**
129 * @param {?} url
130 * @return {?}
131 */
132function resolveUrl(url) {
133 if (!anchor) {
134 anchor = document.createElement('a');
135 }
136 anchor.setAttribute('href', url);
137 anchor.setAttribute('href', anchor.href);
138 return {
139 // IE does not start `pathname` with `/` like other browsers.
140 pathname: `/${anchor.pathname.replace(/^\//, '')}`,
141 search: anchor.search,
142 hash: anchor.hash
143 };
144}
145
146/**
147 * @fileoverview added by tsickle
148 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
149 */
150
151/**
152 * @fileoverview added by tsickle
153 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
154 */
155
156/**
157 * Generated bundle index. Do not edit.
158 */
159
160export { locationSyncBootstrapListener, setUpLocationSync, RouterUpgradeInitializer };
161//# sourceMappingURL=upgrade.js.map