Skip to content

Commit 33ae6a8

Browse files
fix(location): make initial otherwise('/') activate state (perform url sync)
refactor(location) move location code to /location/* refactor(plugins): Switch to plugins API feat(UrlService): Add UrlService injectable Closes #17
1 parent cb7f51f commit 33ae6a8

File tree

7 files changed

+131
-132
lines changed

7 files changed

+131
-132
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "1.0.0-beta.4",
55
"scripts": {
66
"clean": "shx rm -rf lib lib-esm _bundles _doc",
7-
"build": "npm run clean && node_modules/.bin/ngc && node_modules/.bin/ngc -p tsconfig.esm.json && webpack",
7+
"build": "npm run clean && ngc && ngc -m es6 -outDir lib-esm && webpack",
88
"test": "karma start config/karma.ng2.js",
99
"docs": "typedoc --tsconfig tsconfig.typedoc.json --readme README.md --name 'ui-router-ng2' --theme node_modules/ui-router-typedoc-themes/bin/default --out _doc --internal-aliases internal,coreapi,ng2api --external-aliases internalapi,external --navigation-label-globals ui-router-ng2"
1010
},
@@ -49,11 +49,11 @@
4949
"main": "lib/ng2.js",
5050
"typings": "lib/ng2.d.ts",
5151
"dependencies": {
52-
"ui-router-core": "=1.0.1"
52+
"ui-router-core": "=3.1.0"
5353
},
5454
"peerDependencies": {
55-
"@angular/core": "^2.0.0",
56-
"@angular/common": "^2.0.0"
55+
"@angular/common": "^2.0.0",
56+
"@angular/core": "^2.0.0"
5757
},
5858
"devDependencies": {
5959
"@angular/common": "^2.3.1",

src/ng2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/** @ng2api @module ng2 */ /** for typedoc */
22
export * from "ui-router-core";
3-
import "ui-router-core/lib/justjs";
43

54
import 'rxjs/add/observable/of';
65
import 'rxjs/add/observable/combineLatest';
@@ -14,7 +13,7 @@ export * from "./ng2/interface";
1413
export * from "./ng2/lazyLoadNgModule";
1514
export * from "./ng2/rx";
1615
export * from "./ng2/providers";
17-
export * from "./ng2/location";
16+
export * from "./ng2/location/uiRouterLocation";
1817
export * from "./ng2/directives/directives";
1918
export * from "./ng2/statebuilders/views";
2019
export * from "./ng2/uiRouterNgModule";

src/ng2/location.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/ng2/location/locationConfig.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** @module ng2 */
2+
/** */
3+
4+
import { UIRouter, is, isDefined } from "ui-router-core";
5+
import { PlatformLocation, LocationStrategy, PathLocationStrategy } from "@angular/common";
6+
7+
export class Ng2LocationConfig {
8+
private _isHtml5: boolean;
9+
private _hashPrefix: string = "";
10+
11+
constructor(router: UIRouter, locationStrategy: LocationStrategy, public platformLocation: PlatformLocation) {
12+
this._isHtml5 = is(PathLocationStrategy)(locationStrategy);
13+
}
14+
15+
dispose() {}
16+
port = () => null as number;
17+
protocol = () => null as string;
18+
host = () => null as string;
19+
baseHref = () => this.platformLocation.getBaseHrefFromDOM();
20+
html5Mode = () => this._isHtml5;
21+
hashPrefix = (newprefix?: string): string => {
22+
if(isDefined(newprefix)) {
23+
this._hashPrefix = newprefix;
24+
}
25+
return this._hashPrefix;
26+
};
27+
}

src/ng2/location/locationService.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/** @module ng2 */
2+
/** */
3+
import { UIRouter } from "ui-router-core";
4+
import { BaseLocationServices } from "ui-router-core/lib/vanilla";
5+
import { parseUrl } from "ui-router-core/lib/vanilla/utils";
6+
import { PlatformLocation, LocationStrategy } from "@angular/common";
7+
8+
/** A `LocationServices` that uses the browser hash "#" to get/set the current location */
9+
export class Ng2LocationServices extends BaseLocationServices {
10+
constructor(router: UIRouter, private _locationStrategy: LocationStrategy, private _platform: PlatformLocation) {
11+
super(router, true);
12+
this._locationStrategy.onPopState(this._listener)
13+
}
14+
15+
_get() {
16+
return this._locationStrategy.path(true);
17+
}
18+
19+
_set(state: any, title: string, url: string, replace: boolean): any {
20+
let { path, search, hash } = parseUrl(url);
21+
let urlWithHash = path + (hash ? "#" + hash : "");
22+
23+
if (replace) {
24+
this._locationStrategy.replaceState(state, title, urlWithHash, search);
25+
} else {
26+
this._locationStrategy.pushState(state, title, urlWithHash, search);
27+
}
28+
}
29+
30+
dispose (router: UIRouter) {
31+
super.dispose(router);
32+
}
33+
}

src/ng2/location/uiRouterLocation.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** @module ng2 */
2+
/** */
3+
import { PlatformLocation, LocationStrategy } from "@angular/common";
4+
import { Injectable } from "@angular/core";
5+
import { UIRouter } from "ui-router-core";
6+
import { Ng2LocationConfig } from "./locationConfig";
7+
import { Ng2LocationServices } from "./locationService";
8+
9+
@Injectable()
10+
export class UIRouterLocation {
11+
constructor(public locationStrategy: LocationStrategy, public platformLocation: PlatformLocation ) { }
12+
13+
init(router: UIRouter) {
14+
router.locationService = new Ng2LocationServices(router, this.locationStrategy, this.platformLocation);
15+
router.locationConfig = new Ng2LocationConfig(router, this.locationStrategy, this.platformLocation)
16+
}
17+
}
18+
19+
20+

src/ng2/providers.ts

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,21 @@
8383
* ```
8484
*
8585
* @preferred @module ng2
86-
*/ /** */
87-
import {Injector, Provider} from "@angular/core";
88-
import {UIRouter} from "ui-router-core";
89-
import {PathNode} from "ui-router-core";
90-
import {StateRegistry} from "ui-router-core";
91-
import {StateService} from "ui-router-core";
92-
import {TransitionService} from "ui-router-core";
93-
import {UrlMatcherFactory} from "ui-router-core";
94-
import {UrlRouter} from "ui-router-core";
95-
import {ViewService} from "ui-router-core";
96-
import {UIView, ParentUIViewInject} from "./directives/uiView";
97-
import {ng2ViewsBuilder, Ng2ViewConfig} from "./statebuilders/views";
98-
import {Ng2ViewDeclaration} from "./interface";
99-
import {applyRootModuleConfig, applyModuleConfig} from "./uiRouterConfig";
100-
import {Globals} from "ui-router-core";
101-
import {UIRouterLocation} from "./location";
102-
import {services} from "ui-router-core";
103-
import {Resolvable} from "ui-router-core";
104-
import {RootModule, StatesModule, UIROUTER_ROOT_MODULE, UIROUTER_MODULE_TOKEN} from "./uiRouterNgModule";
105-
import {UIRouterRx} from "./rx";
106-
import {NATIVE_INJECTOR_TOKEN} from "ui-router-core";
86+
*/
87+
/** */
88+
import { Injector, Provider } from "@angular/core";
89+
import {
90+
UIRouter, PathNode, StateRegistry, StateService, TransitionService, UrlMatcherFactory, UrlRouter, ViewService,
91+
UrlService, Globals, services, Resolvable, NATIVE_INJECTOR_TOKEN
92+
} from "ui-router-core";
93+
import { UIView, ParentUIViewInject } from "./directives/uiView";
94+
import { ng2ViewsBuilder, Ng2ViewConfig } from "./statebuilders/views";
95+
import { Ng2ViewDeclaration } from "./interface";
96+
import { applyRootModuleConfig, applyModuleConfig } from "./uiRouterConfig";
97+
import { UIRouterLocation } from "./location/uiRouterLocation";
98+
import { RootModule, StatesModule, UIROUTER_ROOT_MODULE, UIROUTER_MODULE_TOKEN } from "./uiRouterNgModule";
99+
import { UIRouterRx } from "./rx";
100+
import { servicesPlugin } from "ui-router-core/lib/vanilla";
107101

108102
/**
109103
* This is a factory function for a UIRouter instance
@@ -120,50 +114,53 @@ export function uiRouterFactory(location: UIRouterLocation, injector: Injector)
120114
throw new Error("Exactly one UIRouterModule.forRoot() should be in the bootstrapped app module's imports: []");
121115
}
122116

123-
// ----------------- Monkey Patches ----------------
124-
// Monkey patch the services.$injector to the ng2 Injector
125-
services.$injector.get = injector.get.bind(injector);
126-
127-
// Monkey patch the services.$location with ng2 Location implementation
128-
location.init();
129-
130117

131118
// ----------------- Create router -----------------
132119
// Create a new ng2 UIRouter and configure it for ng2
133120
let router = new UIRouter();
134-
new UIRouterRx(router);
135-
let registry = router.stateRegistry;
121+
122+
// Add RxJS plugin
123+
router.plugin(UIRouterRx);
124+
125+
// Add $q-like and $injector-like service APIs
126+
router.plugin(servicesPlugin);
127+
128+
129+
// ----------------- Monkey Patches ----------------
130+
// Monkey patch the services.$injector to use the root ng2 Injector
131+
services.$injector.get = injector.get.bind(injector);
132+
136133

137134
// ----------------- Configure for ng2 -------------
135+
location.init(router);
136+
138137
// Apply ng2 ui-view handling code
139-
router.viewService.viewConfigFactory("ng2", (path: PathNode[], config: Ng2ViewDeclaration) => new Ng2ViewConfig(path, config));
140-
registry.decorator('views', ng2ViewsBuilder);
138+
let viewConfigFactory = (path: PathNode[], config: Ng2ViewDeclaration) => new Ng2ViewConfig(path, config);
139+
router.viewService._pluginapi._viewConfigFactory("ng2", viewConfigFactory);
141140

142141
// Apply statebuilder decorator for ng2 NgModule registration
143-
registry.stateQueue.flush(router.stateService);
142+
let registry = router.stateRegistry;
143+
registry.decorator('views', ng2ViewsBuilder);
144144

145145
// Prep the tree of NgModule by placing the root NgModule's Injector on the root state.
146146
let ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, injector);
147147
registry.root().resolvables.push(ng2InjectorResolvable);
148148

149149

150150
// ----------------- Initialize router -------------
151-
// Allow states to be registered
152-
registry.stateQueue.autoFlush(router.stateService);
153-
154151
setTimeout(() => {
155152
rootModules.forEach(moduleConfig => applyRootModuleConfig(router, injector, moduleConfig));
156153
modules.forEach(moduleConfig => applyModuleConfig(router, injector, moduleConfig));
157154

158155
// Start monitoring the URL
159-
if (!router.urlRouterProvider.interceptDeferred) {
160-
router.urlRouter.listen();
161-
router.urlRouter.sync();
156+
if (!router.urlRouter.interceptDeferred) {
157+
router.urlService.listen();
158+
router.urlService.sync();
162159
}
163160
});
164161

165162
return router;
166-
};
163+
}
167164

168165
export function parentUIViewInjectFactory(r: StateRegistry) { return { fqn: null, context: r.root() } as ParentUIViewInject; }
169166

@@ -177,18 +174,20 @@ export function fnStateService(r: UIRouter) { return r.stateService; }
177174
export function fnTransitionService(r: UIRouter) { return r.transitionService; }
178175
export function fnUrlMatcherFactory(r: UIRouter) { return r.urlMatcherFactory; }
179176
export function fnUrlRouter(r: UIRouter) { return r.urlRouter; }
177+
export function fnUrlService(r: UIRouter) { return r.urlService; }
180178
export function fnViewService(r: UIRouter) { return r.viewService; }
181179
export function fnStateRegistry(r: UIRouter) { return r.stateRegistry; }
182180
export function fnGlobals(r: any) { return r.globals; }
183181

184182
export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [
185-
{ provide: StateService, useFactory: fnStateService, deps: [UIRouter]},
186-
{ provide: TransitionService, useFactory: fnTransitionService, deps: [UIRouter]},
187-
{ provide: UrlMatcherFactory, useFactory: fnUrlMatcherFactory, deps: [UIRouter]},
188-
{ provide: UrlRouter, useFactory: fnUrlRouter, deps: [UIRouter]},
189-
{ provide: ViewService, useFactory: fnViewService, deps: [UIRouter]},
190-
{ provide: StateRegistry, useFactory: fnStateRegistry, deps: [UIRouter]},
191-
{ provide: Globals, useFactory: fnGlobals, deps: [UIRouter]},
183+
{ provide: StateService, useFactory: fnStateService, deps: [UIRouter]},
184+
{ provide: TransitionService, useFactory: fnTransitionService, deps: [UIRouter]},
185+
{ provide: UrlMatcherFactory, useFactory: fnUrlMatcherFactory, deps: [UIRouter]},
186+
{ provide: UrlRouter, useFactory: fnUrlRouter, deps: [UIRouter]},
187+
{ provide: UrlService, useFactory: fnUrlService, deps: [UIRouter]},
188+
{ provide: ViewService, useFactory: fnViewService, deps: [UIRouter]},
189+
{ provide: StateRegistry, useFactory: fnStateRegistry, deps: [UIRouter]},
190+
{ provide: Globals, useFactory: fnGlobals, deps: [UIRouter]},
192191
];
193192

194193
/**

0 commit comments

Comments
 (0)