UNPKG

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