-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-router-ng2): Initial angular2 support
- Loading branch information
1 parent
002e8d1
commit 217de70
Showing
16 changed files
with
1,008 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='../typings/es6-shim/es6-shim.d.ts' /> | ||
/** | ||
* Main entry point for angular 2.x build | ||
*/ | ||
/** for typedoc */ | ||
|
||
export * from "./ui-router"; | ||
import "./justjs"; | ||
export * from "./ng2/uiView"; | ||
export * from "./ng2/uiSref"; | ||
export * from "./ng2/uiSrefActive"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {UIRouter} from "../router"; | ||
import {Directive} from "angular2/core"; | ||
import {Optional} from "angular2/core"; | ||
import {Input} from "angular2/core"; | ||
import {ElementRef} from "angular2/core"; | ||
import {Renderer} from "angular2/core"; | ||
|
||
@Directive({ selector: 'a[uiSref]' }) | ||
export class AnchorUiSref { | ||
constructor( public _el: ElementRef, public _renderer: Renderer) { } | ||
update(href) { | ||
this._renderer.setElementProperty(this._el, 'href', href); | ||
} | ||
} | ||
|
||
@Directive({ | ||
selector: '[uiSref]', | ||
inputs: ['uiSref', 'uiParams', 'uiOptions'], | ||
host: { '(click)': 'go()' } | ||
}) | ||
export class UiSref { | ||
state: string; | ||
params: any; | ||
options: any; | ||
|
||
constructor( | ||
private _router: UIRouter, | ||
@Optional() private _anchorUiSref: AnchorUiSref | ||
) { } | ||
|
||
set "ui-sref"(val) { this.state = val; this.update(); } | ||
set "uiSref"(val) { this.state = val; this.update(); } | ||
set "uiParams"(val) { this.params = val; this.update(); } | ||
set "uiOptions"(val) { this.options = val; this.update(); } | ||
|
||
ngOnInit() { | ||
this.update(); | ||
} | ||
|
||
update() { | ||
if (this._anchorUiSref) { | ||
this._anchorUiSref.update(this._router.stateService.href(this.state, this.params)); | ||
} | ||
// TODO: process ui-sref-active | ||
} | ||
|
||
go() { | ||
this._router.stateService.go(this.state, this.params, this.options); | ||
return false; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {UIRouter} from "../router"; | ||
import {Directive} from "angular2/core"; | ||
import {UiSref} from "./uiSref"; | ||
|
||
@Directive({ | ||
selector: '[uiSrefClass]', | ||
inputs: ['uiSrefClass'] | ||
}) | ||
export class UiSrefClass { | ||
// current statuses of the bound uiSref directive | ||
active = false; | ||
exact = false; | ||
entering = false; | ||
exiting = false; | ||
inactive = true; | ||
|
||
patterns: any; | ||
classes: string; | ||
sref: UiSref; | ||
|
||
//constructor($transitions: TransitionService, public router: UIRouter) { | ||
constructor(public router: UIRouter) { | ||
this.ngOnDestroy = <any> router.transitionService.onSuccess({}, this._update.bind(this)); | ||
} | ||
|
||
ngOnDestroy() {} | ||
|
||
/** | ||
* e.g. | ||
* { | ||
* active: 'active && !exiting', | ||
* loading: 'entering', | ||
* active: matches('admin.*') | ||
* } | ||
*/ | ||
set uiSrefClass(val) { | ||
console.log(val); // [uiSrefClass]="{active: isActive}" logs as "{active: undefined}" | ||
this.patterns = val; | ||
} | ||
|
||
public provideUiSref(sref: UiSref) { | ||
this.sref = sref; | ||
this._update(); | ||
} | ||
|
||
private _update() { | ||
if (!this.sref) return; | ||
// update classes | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import {Component, ElementRef, DynamicComponentLoader} from 'angular2/core'; | ||
import {Injector} from "angular2/core"; | ||
import {provide} from "angular2/core"; | ||
import {Input} from "angular2/core"; | ||
import {ComponentRef} from "angular2/core"; | ||
import {Type} from "angular2/core"; | ||
|
||
import {UIRouter} from "../router"; | ||
import {trace} from "../common/trace"; | ||
import {ViewConfig} from "../view/view"; | ||
import {Inject} from "angular2/core"; | ||
import {ViewContext} from "../view/interface"; | ||
|
||
let id = 0; | ||
|
||
const getProviders = (injector) => { | ||
let providers = [], parentInj = injector.parent; | ||
for (let i = 0; i < parentInj._proto.numberOfProviders; i++) { | ||
providers.push(parentInj._proto.getProviderAtIndex(i)); | ||
} | ||
return providers; | ||
}; | ||
|
||
@Component({ | ||
selector: 'ui-view, [ui-view]', | ||
styles: [` | ||
.done-true { | ||
text-decoration: line-through; | ||
color: grey; | ||
}` | ||
], | ||
template: ` | ||
<div style="padding: 1em; border: 1px solid lightgrey;"> | ||
<div #content style="color: lightgrey; font-size: smaller;"> | ||
<div>ui-view #{{uiViewData.id}} created by '{{ parentContext.name || "(root)" }}' state</div> | ||
<div>name: (absolute) '{{uiViewData.fqn}}' (contextual) '{{uiViewData.name}}@{{parentContext.name}}' </div> | ||
<div>currently filled by: '{{(uiViewData.config && uiViewData.config.context) || 'empty...'}}' | ||
</div> | ||
</div> | ||
` | ||
}) | ||
export class UiView { | ||
@Input() name: string; | ||
@Input() set 'ui-view'(val) { this.name = val; } | ||
|
||
componentRef: ComponentRef; | ||
deregister: Function; | ||
uiViewData: any = {}; | ||
|
||
static INJECT = { | ||
fqn: "UiView.parentFQN", | ||
context: "UiView.parentContext" | ||
}; | ||
|
||
constructor( | ||
public router: UIRouter, | ||
@Inject(UiView.INJECT.context) public parentContext: ViewContext, | ||
@Inject(UiView.INJECT.fqn) public parentFqn: string, | ||
public dcl: DynamicComponentLoader, | ||
public elementRef: ElementRef, | ||
public injector: Injector | ||
) { } | ||
|
||
ngOnInit() { | ||
let parentFqn = this.parentFqn; | ||
let name = this.name || '$default'; | ||
console.log(`parentFqn: ${parentFqn}`); | ||
|
||
this.uiViewData = { | ||
id: id++, | ||
name: name, | ||
fqn: parentFqn ? parentFqn + "." + name : name, | ||
creationContext: this.parentContext, | ||
configUpdated: this.viewConfigUpdated.bind(this), | ||
config: undefined | ||
}; | ||
|
||
this.deregister = this.router.viewService.registerUiView(this.uiViewData); | ||
} | ||
|
||
disposeLast() { | ||
if (this.componentRef) this.componentRef.dispose(); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.deregister(); | ||
this.disposeLast(); | ||
} | ||
|
||
viewConfigUpdated(config: ViewConfig) { | ||
let {uiViewData, injector, dcl, elementRef} = this; | ||
|
||
// The "new" viewconfig is already applied, so exit early | ||
if (uiViewData.config === config) return; | ||
// This is a new viewconfig. Destroy the old component | ||
this.disposeLast(); | ||
trace.traceUiViewConfigUpdated(uiViewData, config && config.context); | ||
uiViewData.config = config; | ||
// The config may be undefined if there is nothing state currently targeting this UiView. | ||
if (!config) return; | ||
|
||
// Do some magic | ||
let rc = config.node.resolveContext; | ||
let resolvables = rc.getResolvables(); | ||
let rawProviders = Object.keys(resolvables).map(key => provide(key, { useValue: resolvables[key].data })); | ||
rawProviders.push(provide(UiView.INJECT.context, { useValue: config.context })); | ||
rawProviders.push(provide(UiView.INJECT.fqn, { useValue: uiViewData.fqn })); | ||
let providers = Injector.resolve(rawProviders); | ||
|
||
let exclusions = [UiView.INJECT.context, UiView.INJECT.fqn]; | ||
providers = getProviders(injector).filter(x => exclusions.indexOf(x.key.displayName) === -1).concat(providers); | ||
|
||
// The 'controller' should be a Component class | ||
// TODO: pull from 'component' declaration, do not require template. | ||
let component = <Type> config.viewDeclarationObj.controller; | ||
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => this.componentRef = ref); | ||
} | ||
} | ||
|
Oops, something went wrong.