mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 12:29:55 +08:00
@ -19,13 +19,13 @@ export class NavRouter extends RouterOutlet {
|
|||||||
private _lastUrl: string;
|
private _lastUrl: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
_elementRef: ElementRef,
|
elementRef: ElementRef,
|
||||||
_loader: DynamicComponentLoader,
|
loader: DynamicComponentLoader,
|
||||||
_parentRouter: Router,
|
private parentRouter: Router,
|
||||||
@Attribute('name') nameAttr: string,
|
@Attribute('name') nameAttr: string,
|
||||||
private _nav: Nav
|
private _nav: Nav
|
||||||
) {
|
) {
|
||||||
super(_elementRef, _loader, _parentRouter, nameAttr);
|
super(elementRef, loader, parentRouter, nameAttr);
|
||||||
|
|
||||||
// register this router with Ionic's NavController
|
// register this router with Ionic's NavController
|
||||||
// Ionic's NavController will call this NavRouter's "stateChange"
|
// Ionic's NavController will call this NavRouter's "stateChange"
|
||||||
@ -33,6 +33,37 @@ export class NavRouter extends RouterOutlet {
|
|||||||
_nav.registerRouter(this);
|
_nav.registerRouter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stateChange(direction: string, viewCtrl: ViewController) {
|
||||||
|
// stateChange is called by Ionic's NavController
|
||||||
|
// viewCtrl is Ionic's ViewController class, which has the properties "componentType" and "params"
|
||||||
|
|
||||||
|
// only do an update if there's an actual view change
|
||||||
|
if (!viewCtrl) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the best PathRecognizer for this view's componentType
|
||||||
|
let pathRecognizer = this.getPathRecognizerByComponent(viewCtrl.componentType);
|
||||||
|
if (pathRecognizer) {
|
||||||
|
|
||||||
|
// generate a componentInstruction from the view's PathRecognizer and params
|
||||||
|
let componentInstruction = pathRecognizer.generate(viewCtrl.data);
|
||||||
|
|
||||||
|
// create a ResolvedInstruction from the componentInstruction
|
||||||
|
let instruction = new ResolvedInstruction(componentInstruction, null, null);
|
||||||
|
if (instruction) {
|
||||||
|
let url = instruction.toRootUrl();
|
||||||
|
if (url === this._lastUrl) return;
|
||||||
|
|
||||||
|
this._lastUrl = url;
|
||||||
|
|
||||||
|
this['_parentRouter'].navigateByInstruction(instruction);
|
||||||
|
|
||||||
|
console.debug('NavRouter, stateChange, name:', viewCtrl.name, 'id:', viewCtrl.id, 'url:', url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
activate(nextInstruction: ComponentInstruction): Promise<any> {
|
activate(nextInstruction: ComponentInstruction): Promise<any> {
|
||||||
var previousInstruction = this['_currentInstruction'];
|
var previousInstruction = this['_currentInstruction'];
|
||||||
this['_currentInstruction'] = nextInstruction;
|
this['_currentInstruction'] = nextInstruction;
|
||||||
@ -59,47 +90,15 @@ export class NavRouter extends RouterOutlet {
|
|||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
stateChange(direction: string, viewCtrl: ViewController) {
|
|
||||||
// stateChange is called by Ionic's NavController
|
|
||||||
// type could be "push" or "pop"
|
|
||||||
// viewCtrl is Ionic's ViewController class, which has the properties "componentType" and "params"
|
|
||||||
|
|
||||||
// only do an update if there's an actual view change
|
|
||||||
if (!viewCtrl) return;
|
|
||||||
|
|
||||||
// get the best PathRecognizer for this view's componentType
|
|
||||||
let pathRecognizer = this.getPathRecognizerByComponent(viewCtrl.componentType);
|
|
||||||
if (pathRecognizer) {
|
|
||||||
|
|
||||||
// generate a componentInstruction from the view's PathRecognizer and params
|
|
||||||
let componentInstruction = pathRecognizer.generate(viewCtrl.data);
|
|
||||||
|
|
||||||
// create a ResolvedInstruction from the componentInstruction
|
|
||||||
let instruction = new ResolvedInstruction(componentInstruction, null, null);
|
|
||||||
if (instruction) {
|
|
||||||
let url = instruction.toRootUrl();
|
|
||||||
if (url === this._lastUrl) return;
|
|
||||||
|
|
||||||
this._lastUrl = url;
|
|
||||||
|
|
||||||
this['_parentRouter'].navigateByInstruction(instruction);
|
|
||||||
|
|
||||||
console.debug('NavRouter, stateChange, name:', viewCtrl.name, 'id:', viewCtrl.id, 'url:', url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getPathRecognizerByComponent(componentType) {
|
getPathRecognizerByComponent(componentType) {
|
||||||
// given a componentType, figure out the best PathRecognizer to use
|
// given a componentType, figure out the best PathRecognizer to use
|
||||||
let rules = this['_parentRouter'].registry._rules;
|
let rules = this.parentRouter.registry['_rules'];
|
||||||
|
|
||||||
let pathRecognizer = null;
|
let pathRecognizer = null;
|
||||||
rules.forEach((rule) => {
|
rules.forEach((rule) => {
|
||||||
|
pathRecognizer = rule.rules.find(function(routeRule) {
|
||||||
pathRecognizer = rule.matchers.find((matcherPathRecognizer) => {
|
return routeRule.handler.componentType === componentType;
|
||||||
return (matcherPathRecognizer.handler.componentType === componentType);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return pathRecognizer;
|
return pathRecognizer;
|
||||||
|
Reference in New Issue
Block a user