Skip to content

Commit

Permalink
refactor(Location): out of router and into platform/common
Browse files Browse the repository at this point in the history
closes #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 #7962
  • Loading branch information
NathanWalker committed Apr 20, 2016
1 parent 30c4352 commit b602bd8
Show file tree
Hide file tree
Showing 55 changed files with 187 additions and 178 deletions.
2 changes: 1 addition & 1 deletion modules/angular2/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './src/common/pipes';
export * from './src/common/directives';
export * from './src/common/forms';
export * from './src/common/common_directives';
export * from './src/common/common_directives';
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {provide, Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {
CanActivate,
RouteConfig,
ComponentInstruction,
APP_BASE_HREF,
ROUTER_DIRECTIVES
} from 'angular2/router';
import {CanActivate, RouteConfig, ComponentInstruction, ROUTER_DIRECTIVES} from 'angular2/router';
import {APP_BASE_HREF} from 'angular2/platform/common';

function checkIfWeHavePermission(instruction: ComponentInstruction) {
return instruction.params['id'] == '1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
RouteConfig,
RouteParams,
ComponentInstruction,
ROUTER_DIRECTIVES,
APP_BASE_HREF
ROUTER_DIRECTIVES
} from 'angular2/router';
import {APP_BASE_HREF} from 'angular2/platform/common';

// #docregion routerCanDeactivate
@Component({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {Component, provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {
OnActivate,
ComponentInstruction,
RouteConfig,
ROUTER_DIRECTIVES,
APP_BASE_HREF
} from 'angular2/router';
import {OnActivate, ComponentInstruction, RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {APP_BASE_HREF} from 'angular2/platform/common';

// #docregion routerOnActivate
@Component({template: `Child`})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {Component, Injectable, provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {
OnDeactivate,
ComponentInstruction,
RouteConfig,
ROUTER_DIRECTIVES,
APP_BASE_HREF
} from 'angular2/router';
import {OnDeactivate, ComponentInstruction, RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {APP_BASE_HREF} from 'angular2/platform/common';


@Injectable()
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/examples/router/ts/reuse/reuse_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
RouteConfig,
ComponentInstruction,
ROUTER_DIRECTIVES,
APP_BASE_HREF,
CanReuse,
RouteParams,
OnReuse
} from 'angular2/router';
import {APP_BASE_HREF} from 'angular2/platform/common';


// #docregion reuseCmp
Expand Down
5 changes: 5 additions & 0 deletions modules/angular2/platform/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Platform agnostic services.
* Can be used both in the browser and on the server.
*/
export * from 'angular2/src/platform/location';
3 changes: 2 additions & 1 deletion modules/angular2/platform/common_dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* This is a set of classes and objects that can be used both in the browser and on the server.
* This is a set of DOM related classes and objects that can be used both in the browser and on the
* server.
*/
export {DOM, setRootDomAdapter, DomAdapter} from 'angular2/src/platform/dom/dom_adapter';
export {DomRenderer} from 'angular2/src/platform/dom/dom_renderer';
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/platform/testing/browser_static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
import {LocationStrategy} from 'angular2/platform/common';
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';

import {XHRImpl} from "angular2/src/platform/browser/xhr_impl";
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/platform/testing/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';

import {TestComponentBuilder} from 'angular2/src/testing/test_component_builder';
Expand All @@ -36,6 +35,7 @@ import {
ELEMENT_PROBE_PROVIDERS
} from 'angular2/platform/common_dom';
import {DomEventsPlugin} from 'angular2/src/platform/dom/events/dom_events';
import {LocationStrategy} from 'angular2/platform/common';

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

Expand Down
5 changes: 0 additions & 5 deletions modules/angular2/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ export {Router} from './src/router/router';
export {RouterOutlet} from './src/router/directives/router_outlet';
export {RouterLink} from './src/router/directives/router_link';
export {RouteParams, RouteData} from './src/router/instruction';
export {PlatformLocation} from './src/router/location/platform_location';
export {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './src/router/route_registry';
export {LocationStrategy, APP_BASE_HREF} from './src/router/location/location_strategy';
export {HashLocationStrategy} from './src/router/location/hash_location_strategy';
export {PathLocationStrategy} from './src/router/location/path_location_strategy';
export {Location} from './src/router/location/location';
export * from './src/router/route_config/route_config_decorator';
export * from './src/router/route_definition';
export {OnActivate, OnDeactivate, OnReuse, CanDeactivate, CanReuse} from './src/router/interfaces';
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/src/mock/location_mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Injectable} from 'angular2/src/core/di';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {ListWrapper} from 'angular2/src/facade/collection';
import {Location} from 'angular2/src/router/location/location';
import {Location} from 'angular2/platform/common';

/**
* A spy for {@link Location} that allows tests to fire simulated location events.
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/src/mock/mock_location_strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Injectable} from 'angular2/src/core/di';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
import {LocationStrategy} from 'angular2/platform/common';


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Injectable} from 'angular2/core';
import {Injectable} from 'angular2/src/core/di/decorators';
import {UrlChangeListener, PlatformLocation} from './platform_location';
import {History, Location} from 'angular2/src/facade/browser';
import {UrlChangeListener} from './platform_location';
import {PlatformLocation} from './platform_location';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import {Injectable, Inject, Optional} from 'angular2/core';
import {
LocationStrategy,
joinWithSlash,
APP_BASE_HREF,
normalizeQueryParams
} from './location_strategy';
import {UrlChangeListener} from './platform_location';
import {LocationStrategy, APP_BASE_HREF} from './location_strategy';
import {Location} from './location';
import {UrlChangeListener, PlatformLocation} from './platform_location';
import {isPresent} from 'angular2/src/facade/lang';
import {PlatformLocation} from './platform_location';

/**
* `HashLocationStrategy` is a {@link LocationStrategy} used to configure the
Expand All @@ -23,12 +18,14 @@ import {PlatformLocation} from './platform_location';
* ```
* import {Component, provide} from 'angular2/core';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_PROVIDERS,
* RouteConfig,
* Location,
* LocationStrategy,
* HashLocationStrategy
* } from 'angular2/platform/common';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_PROVIDERS,
* RouteConfig
* } from 'angular2/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
Expand Down Expand Up @@ -78,20 +75,20 @@ export class HashLocationStrategy extends LocationStrategy {
}

prepareExternalUrl(internal: string): string {
var url = joinWithSlash(this._baseHref, internal);
var url = Location.joinWithSlash(this._baseHref, internal);
return url.length > 0 ? ('#' + url) : url;
}

pushState(state: any, title: string, path: string, queryParams: string) {
var url = this.prepareExternalUrl(path + normalizeQueryParams(queryParams));
var url = this.prepareExternalUrl(path + Location.normalizeQueryParams(queryParams));
if (url.length == 0) {
url = this._platformLocation.pathname;
}
this._platformLocation.pushState(state, title, url);
}

replaceState(state: any, title: string, path: string, queryParams: string) {
var url = this.prepareExternalUrl(path + normalizeQueryParams(queryParams));
var url = this.prepareExternalUrl(path + Location.normalizeQueryParams(queryParams));
if (url.length == 0) {
url = this._platformLocation.pathname;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {LocationStrategy} from './location_strategy';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {Injectable, Inject} from 'angular2/core';
import {LocationStrategy} from './location_strategy';

/**
* `Location` is a service that applications can use to interact with a browser's URL.
Expand All @@ -22,11 +22,11 @@ import {Injectable, Inject} from 'angular2/core';
*
* ```
* import {Component} from 'angular2/core';
* import {Location} from 'angular2/platform/common';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_PROVIDERS,
* RouteConfig,
* Location
* RouteConfig
* } from 'angular2/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
Expand All @@ -51,7 +51,7 @@ export class Location {

constructor(public platformStrategy: LocationStrategy) {
var browserBaseHref = this.platformStrategy.getBaseHref();
this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref));
this.platformStrategy.onPopState((ev) => {
ObservableWrapper.callEmit(this._subject, {'url': this.path(), 'pop': true, 'type': ev.type});
});
Expand All @@ -67,7 +67,7 @@ export class Location {
* trailing slashes
*/
normalize(url: string): string {
return stripTrailingSlash(_stripBaseHref(this._baseHref, stripIndexHtml(url)));
return Location.stripTrailingSlash(_stripBaseHref(this._baseHref, _stripIndexHtml(url)));
}

/**
Expand Down Expand Up @@ -117,6 +117,50 @@ export class Location {
onReturn: () => void = null): Object {
return ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
}

/**
* Given a string of url parameters, prepend with '?' if needed, otherwise return parameters as
* is.
*/
public static normalizeQueryParams(params: string): string {
return (params.length > 0 && params.substring(0, 1) != '?') ? ('?' + params) : params;
}

/**
* Given 2 parts of a url, join them with a slash if needed.
*/
public static joinWithSlash(start: string, end: string): string {
if (start.length == 0) {
return end;
}
if (end.length == 0) {
return start;
}
var slashes = 0;
if (start.endsWith('/')) {
slashes++;
}
if (end.startsWith('/')) {
slashes++;
}
if (slashes == 2) {
return start + end.substring(1);
}
if (slashes == 1) {
return start + end;
}
return start + '/' + end;
}

/**
* If url has a trailing slash, remove it, otherwise return url as is.
*/
public static stripTrailingSlash(url: string): string {
if (/\/$/g.test(url)) {
url = url.substring(0, url.length - 1);
}
return url;
}
}

function _stripBaseHref(baseHref: string, url: string): string {
Expand All @@ -126,17 +170,10 @@ function _stripBaseHref(baseHref: string, url: string): string {
return url;
}

function stripIndexHtml(url: string): string {
function _stripIndexHtml(url: string): string {
if (/\/index.html$/g.test(url)) {
// '/index.html'.length == 11
return url.substring(0, url.length - 11);
}
return url;
}

function stripTrailingSlash(url: string): string {
if (/\/$/g.test(url)) {
url = url.substring(0, url.length - 1);
}
return url;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export abstract class LocationStrategy {
* ```
* import {Component} from 'angular2/core';
* import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/router';
* import {APP_BASE_HREF} from 'angular2/platform/common';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
Expand All @@ -59,30 +60,3 @@ export abstract class LocationStrategy {
* ```
*/
export const APP_BASE_HREF: OpaqueToken = CONST_EXPR(new OpaqueToken('appBaseHref'));

export function normalizeQueryParams(params: string): string {
return (params.length > 0 && params.substring(0, 1) != '?') ? ('?' + params) : params;
}

export function joinWithSlash(start: string, end: string): string {
if (start.length == 0) {
return end;
}
if (end.length == 0) {
return start;
}
var slashes = 0;
if (start.endsWith('/')) {
slashes++;
}
if (end.startsWith('/')) {
slashes++;
}
if (slashes == 2) {
return start + end.substring(1);
}
if (slashes == 1) {
return start + end;
}
return start + '/' + end;
}
Loading

0 comments on commit b602bd8

Please sign in to comment.