UNPKG

@ionic/core

Version:
83 lines (82 loc) 2.44 kB
import { attachComponent } from '../../utils/framework-delegate'; import { assert } from '../../utils/helpers'; export const VIEW_STATE_NEW = 1; export const VIEW_STATE_ATTACHED = 2; export const VIEW_STATE_DESTROYED = 3; export class ViewController { constructor(component, params) { this.component = component; this.params = params; this.state = VIEW_STATE_NEW; } async init(container) { this.state = VIEW_STATE_ATTACHED; if (!this.element) { const component = this.component; this.element = await attachComponent(this.delegate, container, component, ['ion-page', 'ion-page-invisible'], this.params); } } _destroy() { assert(this.state !== VIEW_STATE_DESTROYED, 'view state must be ATTACHED'); const element = this.element; if (element) { if (this.delegate) { this.delegate.removeViewFromDom(element.parentElement, element); } else { element.remove(); } } this.nav = undefined; this.state = VIEW_STATE_DESTROYED; } } export function matches(view, id, params) { if (!view) { return false; } if (view.component !== id) { return false; } const currentParams = view.params; if (currentParams === params) { return true; } if (!currentParams && !params) { return true; } if (!currentParams || !params) { return false; } const keysA = Object.keys(currentParams); const keysB = Object.keys(params); if (keysA.length !== keysB.length) { return false; } for (const key of keysA) { if (currentParams[key] !== params[key]) { return false; } } return true; } export function convertToView(page, params) { if (!page) { return null; } if (page instanceof ViewController) { return page; } return new ViewController(page, params); } export function convertToViews(pages) { return pages.map(page => { if (page instanceof ViewController) { return page; } if ('page' in page) { return convertToView(page.page, page.params); } return convertToView(page, undefined); }).filter(v => v !== null); }