mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-10 00:27:41 +08:00
refactor(nav): simplify ViewController
This commit is contained in:
@ -1,91 +1,31 @@
|
||||
|
||||
import { NavOptions, ViewState } from './nav-util';
|
||||
import { ViewState } from './nav-util';
|
||||
import { assert } from '../../utils/helpers';
|
||||
import { FrameworkDelegate, Nav } from '../..';
|
||||
import { ComponentProps, FrameworkDelegate, Nav } from '../..';
|
||||
import { attachComponent } from '../../utils/framework-delegate';
|
||||
|
||||
/**
|
||||
* @name ViewController
|
||||
* @description
|
||||
* Access various features and information about the current view.
|
||||
* @usage
|
||||
* ```ts
|
||||
* import { Component } from '@angular/core';
|
||||
* import { ViewController } from 'ionic-angular';
|
||||
*
|
||||
* @Component({...})
|
||||
* export class MyPage{
|
||||
*
|
||||
* constructor(public viewCtrl: ViewController) {}
|
||||
*
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class ViewController {
|
||||
|
||||
private _cntDir: any;
|
||||
private _leavingOpts: NavOptions;
|
||||
export class ViewController {
|
||||
|
||||
nav: Nav;
|
||||
_state: ViewState = ViewState.New;
|
||||
|
||||
/** @hidden */
|
||||
id: string;
|
||||
state: ViewState = ViewState.New;
|
||||
element: HTMLElement;
|
||||
delegate: FrameworkDelegate;
|
||||
|
||||
constructor(
|
||||
public component: any,
|
||||
public data: any
|
||||
public params: any
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @hidden
|
||||
*/
|
||||
async init(container: HTMLElement) {
|
||||
this._state = ViewState.Attached;
|
||||
this.state = ViewState.Attached;
|
||||
|
||||
if (!this.element) {
|
||||
const component = this.component;
|
||||
this.element = await attachComponent(this.delegate, container, component, ['ion-page', 'hide-page'], this.data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @hidden
|
||||
*/
|
||||
setLeavingOpts(opts: NavOptions) {
|
||||
this._leavingOpts = opts;
|
||||
}
|
||||
|
||||
matches(id: string, params: any): boolean {
|
||||
if (this.component !== id) {
|
||||
return false;
|
||||
}
|
||||
const currentParams = this.data;
|
||||
const null1 = (currentParams == null);
|
||||
const null2 = (params == null);
|
||||
if (null1 !== null2) {
|
||||
return false;
|
||||
}
|
||||
if (null1 && null2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const keysA = Object.keys(currentParams);
|
||||
const keysB = Object.keys(params);
|
||||
if (keysA.length !== keysB.length) {
|
||||
return false;
|
||||
this.element = await attachComponent(this.delegate, container, component, ['ion-page', 'hide-page'], this.params);
|
||||
}
|
||||
|
||||
// Test for A's keys different from B.
|
||||
for (let i = 0; i < keysA.length; i++) {
|
||||
const key = keysA[i];
|
||||
if (currentParams[key] !== params[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +33,7 @@ export class ViewController {
|
||||
* DOM WRITE
|
||||
*/
|
||||
_destroy() {
|
||||
assert(this._state !== ViewState.Destroyed, 'view state must be ATTACHED');
|
||||
assert(this.state !== ViewState.Destroyed, 'view state must be ATTACHED');
|
||||
|
||||
const element = this.element;
|
||||
if (element) {
|
||||
@ -103,19 +43,40 @@ export class ViewController {
|
||||
element.remove();
|
||||
}
|
||||
}
|
||||
this.nav = this._cntDir = this._leavingOpts = null;
|
||||
this._state = ViewState.Destroyed;
|
||||
this.nav = null;
|
||||
this.state = ViewState.Destroyed;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the index of the current component in the current navigation stack.
|
||||
* @returns {number} Returns the index of this page within its `NavController`.
|
||||
*/
|
||||
get index(): number {
|
||||
return (this.nav ? this.nav.indexOf(this) : -1);
|
||||
export function matches(view: ViewController|undefined, id: string, params: ComponentProps): boolean {
|
||||
if (!view) {
|
||||
return false;
|
||||
}
|
||||
if (view.component !== id) {
|
||||
return false;
|
||||
}
|
||||
const currentParams = view.params;
|
||||
const null1 = (currentParams == null);
|
||||
const null2 = (params == null);
|
||||
if (null1 !== null2) {
|
||||
return false;
|
||||
}
|
||||
if (null1 && null2) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function isViewController(viewCtrl: any): viewCtrl is ViewController {
|
||||
return viewCtrl instanceof ViewController;
|
||||
const keysA = Object.keys(currentParams);
|
||||
const keysB = Object.keys(params);
|
||||
if (keysA.length !== keysB.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Test for A's keys different from B.
|
||||
for (let i = 0; i < keysA.length; i++) {
|
||||
const key = keysA[i];
|
||||
if (currentParams[key] !== params[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user