UNPKG

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