UNPKG

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