Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b602bd8

Browse files
committed
refactor(Location): out of router and into platform/common
closes angular#4943 BREAKING CHANGE: `Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change. Before: ``` import { PlatformLocation, Location, LocationStrategy, HashLocationStrategy, PathLocationStrategy, APP_BASE_HREF} from 'angular2/router'; import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location'; ``` After: ``` import { PlatformLocation, Location, LocationStrategy, HashLocationStrategy, PathLocationStrategy, APP_BASE_HREF} from 'angular2/platform/common'; import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location'; ``` Closes angular#7962
1 parent 30c4352 commit b602bd8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+187
-178
lines changed

modules/angular2/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './src/common/pipes';
22
export * from './src/common/directives';
33
export * from './src/common/forms';
4-
export * from './src/common/common_directives';
4+
export * from './src/common/common_directives';

modules/angular2/examples/router/ts/can_activate/can_activate_example.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import {provide, Component} from 'angular2/core';
22
import {bootstrap} from 'angular2/platform/browser';
3-
import {
4-
CanActivate,
5-
RouteConfig,
6-
ComponentInstruction,
7-
APP_BASE_HREF,
8-
ROUTER_DIRECTIVES
9-
} from 'angular2/router';
3+
import {CanActivate, RouteConfig, ComponentInstruction, ROUTER_DIRECTIVES} from 'angular2/router';
4+
import {APP_BASE_HREF} from 'angular2/platform/common';
105

116
function checkIfWeHavePermission(instruction: ComponentInstruction) {
127
return instruction.params['id'] == '1';

modules/angular2/examples/router/ts/can_deactivate/can_deactivate_example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
RouteConfig,
66
RouteParams,
77
ComponentInstruction,
8-
ROUTER_DIRECTIVES,
9-
APP_BASE_HREF
8+
ROUTER_DIRECTIVES
109
} from 'angular2/router';
10+
import {APP_BASE_HREF} from 'angular2/platform/common';
1111

1212
// #docregion routerCanDeactivate
1313
@Component({

modules/angular2/examples/router/ts/on_activate/on_activate_example.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import {Component, provide} from 'angular2/core';
22
import {bootstrap} from 'angular2/platform/browser';
3-
import {
4-
OnActivate,
5-
ComponentInstruction,
6-
RouteConfig,
7-
ROUTER_DIRECTIVES,
8-
APP_BASE_HREF
9-
} from 'angular2/router';
3+
import {OnActivate, ComponentInstruction, RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
4+
import {APP_BASE_HREF} from 'angular2/platform/common';
105

116
// #docregion routerOnActivate
127
@Component({template: `Child`})

modules/angular2/examples/router/ts/on_deactivate/on_deactivate_example.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import {Component, Injectable, provide} from 'angular2/core';
22
import {bootstrap} from 'angular2/platform/browser';
3-
import {
4-
OnDeactivate,
5-
ComponentInstruction,
6-
RouteConfig,
7-
ROUTER_DIRECTIVES,
8-
APP_BASE_HREF
9-
} from 'angular2/router';
3+
import {OnDeactivate, ComponentInstruction, RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
4+
import {APP_BASE_HREF} from 'angular2/platform/common';
105

116

127
@Injectable()

modules/angular2/examples/router/ts/reuse/reuse_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
RouteConfig,
66
ComponentInstruction,
77
ROUTER_DIRECTIVES,
8-
APP_BASE_HREF,
98
CanReuse,
109
RouteParams,
1110
OnReuse
1211
} from 'angular2/router';
12+
import {APP_BASE_HREF} from 'angular2/platform/common';
1313

1414

1515
// #docregion reuseCmp

modules/angular2/platform/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Platform agnostic services.
3+
* Can be used both in the browser and on the server.
4+
*/
5+
export * from 'angular2/src/platform/location';

modules/angular2/platform/common_dom.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* This is a set of classes and objects that can be used both in the browser and on the server.
2+
* This is a set of DOM related classes and objects that can be used both in the browser and on the
3+
* server.
34
*/
45
export {DOM, setRootDomAdapter, DomAdapter} from 'angular2/src/platform/dom/dom_adapter';
56
export {DomRenderer} from 'angular2/src/platform/dom/dom_renderer';

modules/angular2/platform/testing/browser_static.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
1414
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
1515
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
1616
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
17-
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
17+
import {LocationStrategy} from 'angular2/platform/common';
1818
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';
1919

2020
import {XHRImpl} from "angular2/src/platform/browser/xhr_impl";

modules/angular2/platform/testing/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
1616
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
1717
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
1818
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
19-
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
2019
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';
2120

2221
import {TestComponentBuilder} from 'angular2/src/testing/test_component_builder';
@@ -36,6 +35,7 @@ import {
3635
ELEMENT_PROBE_PROVIDERS
3736
} from 'angular2/platform/common_dom';
3837
import {DomEventsPlugin} from 'angular2/src/platform/dom/events/dom_events';
38+
import {LocationStrategy} from 'angular2/platform/common';
3939

4040
import {CONST_EXPR} from 'angular2/src/facade/lang';
4141

modules/angular2/router.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ export {Router} from './src/router/router';
88
export {RouterOutlet} from './src/router/directives/router_outlet';
99
export {RouterLink} from './src/router/directives/router_link';
1010
export {RouteParams, RouteData} from './src/router/instruction';
11-
export {PlatformLocation} from './src/router/location/platform_location';
1211
export {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './src/router/route_registry';
13-
export {LocationStrategy, APP_BASE_HREF} from './src/router/location/location_strategy';
14-
export {HashLocationStrategy} from './src/router/location/hash_location_strategy';
15-
export {PathLocationStrategy} from './src/router/location/path_location_strategy';
16-
export {Location} from './src/router/location/location';
1712
export * from './src/router/route_config/route_config_decorator';
1813
export * from './src/router/route_definition';
1914
export {OnActivate, OnDeactivate, OnReuse, CanDeactivate, CanReuse} from './src/router/interfaces';

modules/angular2/src/mock/location_mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Injectable} from 'angular2/src/core/di';
22
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
33
import {ListWrapper} from 'angular2/src/facade/collection';
4-
import {Location} from 'angular2/src/router/location/location';
4+
import {Location} from 'angular2/platform/common';
55

66
/**
77
* A spy for {@link Location} that allows tests to fire simulated location events.

modules/angular2/src/mock/mock_location_strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Injectable} from 'angular2/src/core/di';
22
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
3-
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
3+
import {LocationStrategy} from 'angular2/platform/common';
44

55

66
/**

modules/angular2/src/router/location/browser_platform_location.ts renamed to modules/angular2/src/platform/browser/location/browser_platform_location.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {Injectable} from 'angular2/core';
1+
import {Injectable} from 'angular2/src/core/di/decorators';
2+
import {UrlChangeListener, PlatformLocation} from './platform_location';
23
import {History, Location} from 'angular2/src/facade/browser';
3-
import {UrlChangeListener} from './platform_location';
4-
import {PlatformLocation} from './platform_location';
54
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
65

76
/**

modules/angular2/src/router/location/hash_location_strategy.ts renamed to modules/angular2/src/platform/browser/location/hash_location_strategy.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import {Injectable, Inject, Optional} from 'angular2/core';
2-
import {
3-
LocationStrategy,
4-
joinWithSlash,
5-
APP_BASE_HREF,
6-
normalizeQueryParams
7-
} from './location_strategy';
8-
import {UrlChangeListener} from './platform_location';
2+
import {LocationStrategy, APP_BASE_HREF} from './location_strategy';
3+
import {Location} from './location';
4+
import {UrlChangeListener, PlatformLocation} from './platform_location';
95
import {isPresent} from 'angular2/src/facade/lang';
10-
import {PlatformLocation} from './platform_location';
116

127
/**
138
* `HashLocationStrategy` is a {@link LocationStrategy} used to configure the
@@ -23,12 +18,14 @@ import {PlatformLocation} from './platform_location';
2318
* ```
2419
* import {Component, provide} from 'angular2/core';
2520
* import {
26-
* ROUTER_DIRECTIVES,
27-
* ROUTER_PROVIDERS,
28-
* RouteConfig,
2921
* Location,
3022
* LocationStrategy,
3123
* HashLocationStrategy
24+
* } from 'angular2/platform/common';
25+
* import {
26+
* ROUTER_DIRECTIVES,
27+
* ROUTER_PROVIDERS,
28+
* RouteConfig
3229
* } from 'angular2/router';
3330
*
3431
* @Component({directives: [ROUTER_DIRECTIVES]})
@@ -78,20 +75,20 @@ export class HashLocationStrategy extends LocationStrategy {
7875
}
7976

8077
prepareExternalUrl(internal: string): string {
81-
var url = joinWithSlash(this._baseHref, internal);
78+
var url = Location.joinWithSlash(this._baseHref, internal);
8279
return url.length > 0 ? ('#' + url) : url;
8380
}
8481

8582
pushState(state: any, title: string, path: string, queryParams: string) {
86-
var url = this.prepareExternalUrl(path + normalizeQueryParams(queryParams));
83+
var url = this.prepareExternalUrl(path + Location.normalizeQueryParams(queryParams));
8784
if (url.length == 0) {
8885
url = this._platformLocation.pathname;
8986
}
9087
this._platformLocation.pushState(state, title, url);
9188
}
9289

9390
replaceState(state: any, title: string, path: string, queryParams: string) {
94-
var url = this.prepareExternalUrl(path + normalizeQueryParams(queryParams));
91+
var url = this.prepareExternalUrl(path + Location.normalizeQueryParams(queryParams));
9592
if (url.length == 0) {
9693
url = this._platformLocation.pathname;
9794
}

modules/angular2/src/router/location/location.ts renamed to modules/angular2/src/platform/browser/location/location.ts

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {LocationStrategy} from './location_strategy';
21
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
32
import {Injectable, Inject} from 'angular2/core';
3+
import {LocationStrategy} from './location_strategy';
44

55
/**
66
* `Location` is a service that applications can use to interact with a browser's URL.
@@ -22,11 +22,11 @@ import {Injectable, Inject} from 'angular2/core';
2222
*
2323
* ```
2424
* import {Component} from 'angular2/core';
25+
* import {Location} from 'angular2/platform/common';
2526
* import {
2627
* ROUTER_DIRECTIVES,
2728
* ROUTER_PROVIDERS,
28-
* RouteConfig,
29-
* Location
29+
* RouteConfig
3030
* } from 'angular2/router';
3131
*
3232
* @Component({directives: [ROUTER_DIRECTIVES]})
@@ -51,7 +51,7 @@ export class Location {
5151

5252
constructor(public platformStrategy: LocationStrategy) {
5353
var browserBaseHref = this.platformStrategy.getBaseHref();
54-
this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
54+
this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref));
5555
this.platformStrategy.onPopState((ev) => {
5656
ObservableWrapper.callEmit(this._subject, {'url': this.path(), 'pop': true, 'type': ev.type});
5757
});
@@ -67,7 +67,7 @@ export class Location {
6767
* trailing slashes
6868
*/
6969
normalize(url: string): string {
70-
return stripTrailingSlash(_stripBaseHref(this._baseHref, stripIndexHtml(url)));
70+
return Location.stripTrailingSlash(_stripBaseHref(this._baseHref, _stripIndexHtml(url)));
7171
}
7272

7373
/**
@@ -117,6 +117,50 @@ export class Location {
117117
onReturn: () => void = null): Object {
118118
return ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
119119
}
120+
121+
/**
122+
* Given a string of url parameters, prepend with '?' if needed, otherwise return parameters as
123+
* is.
124+
*/
125+
public static normalizeQueryParams(params: string): string {
126+
return (params.length > 0 && params.substring(0, 1) != '?') ? ('?' + params) : params;
127+
}
128+
129+
/**
130+
* Given 2 parts of a url, join them with a slash if needed.
131+
*/
132+
public static joinWithSlash(start: string, end: string): string {
133+
if (start.length == 0) {
134+
return end;
135+
}
136+
if (end.length == 0) {
137+
return start;
138+
}
139+
var slashes = 0;
140+
if (start.endsWith('/')) {
141+
slashes++;
142+
}
143+
if (end.startsWith('/')) {
144+
slashes++;
145+
}
146+
if (slashes == 2) {
147+
return start + end.substring(1);
148+
}
149+
if (slashes == 1) {
150+
return start + end;
151+
}
152+
return start + '/' + end;
153+
}
154+
155+
/**
156+
* If url has a trailing slash, remove it, otherwise return url as is.
157+
*/
158+
public static stripTrailingSlash(url: string): string {
159+
if (/\/$/g.test(url)) {
160+
url = url.substring(0, url.length - 1);
161+
}
162+
return url;
163+
}
120164
}
121165

122166
function _stripBaseHref(baseHref: string, url: string): string {
@@ -126,17 +170,10 @@ function _stripBaseHref(baseHref: string, url: string): string {
126170
return url;
127171
}
128172

129-
function stripIndexHtml(url: string): string {
173+
function _stripIndexHtml(url: string): string {
130174
if (/\/index.html$/g.test(url)) {
131175
// '/index.html'.length == 11
132176
return url.substring(0, url.length - 11);
133177
}
134178
return url;
135179
}
136-
137-
function stripTrailingSlash(url: string): string {
138-
if (/\/$/g.test(url)) {
139-
url = url.substring(0, url.length - 1);
140-
}
141-
return url;
142-
}

modules/angular2/src/router/location/location_strategy.ts renamed to modules/angular2/src/platform/browser/location/location_strategy.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export abstract class LocationStrategy {
4343
* ```
4444
* import {Component} from 'angular2/core';
4545
* import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/router';
46+
* import {APP_BASE_HREF} from 'angular2/platform/common';
4647
*
4748
* @Component({directives: [ROUTER_DIRECTIVES]})
4849
* @RouteConfig([
@@ -59,30 +60,3 @@ export abstract class LocationStrategy {
5960
* ```
6061
*/
6162
export const APP_BASE_HREF: OpaqueToken = CONST_EXPR(new OpaqueToken('appBaseHref'));
62-
63-
export function normalizeQueryParams(params: string): string {
64-
return (params.length > 0 && params.substring(0, 1) != '?') ? ('?' + params) : params;
65-
}
66-
67-
export function joinWithSlash(start: string, end: string): string {
68-
if (start.length == 0) {
69-
return end;
70-
}
71-
if (end.length == 0) {
72-
return start;
73-
}
74-
var slashes = 0;
75-
if (start.endsWith('/')) {
76-
slashes++;
77-
}
78-
if (end.startsWith('/')) {
79-
slashes++;
80-
}
81-
if (slashes == 2) {
82-
return start + end.substring(1);
83-
}
84-
if (slashes == 1) {
85-
return start + end;
86-
}
87-
return start + '/' + end;
88-
}

0 commit comments

Comments
 (0)