mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
refactor(all): avoid using export const enum (#16614)
* refactor(all): avoid using export const enum fixes #16497 * add tslint
This commit is contained in:
5
core/src/components/nav/constants.ts
Normal file
5
core/src/components/nav/constants.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export const LIFECYCLE_WILL_ENTER = 'ionViewWillEnter';
|
||||
export const LIFECYCLE_DID_ENTER = 'ionViewDidEnter';
|
||||
export const LIFECYCLE_WILL_LEAVE = 'ionViewWillLeave';
|
||||
export const LIFECYCLE_DID_LEAVE = 'ionViewDidLeave';
|
||||
export const LIFECYCLE_WILL_UNLOAD = 'ionViewWillUnload';
|
@ -1,4 +1,6 @@
|
||||
import { Animation, AnimationBuilder, ComponentRef, FrameworkDelegate, Mode, ViewController } from '../../interface';
|
||||
import { Animation, AnimationBuilder, ComponentRef, FrameworkDelegate, Mode } from '../../interface';
|
||||
|
||||
import { ViewController } from './view-controller';
|
||||
|
||||
export type NavDirection = 'back' | 'forward';
|
||||
|
||||
@ -62,3 +64,5 @@ export interface TransitionInstruction {
|
||||
leavingRequiresTransition?: boolean;
|
||||
enteringRequiresTransition?: boolean;
|
||||
}
|
||||
|
||||
export { ViewController };
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { Build, Component, Element, Event, EventEmitter, Method, Prop, QueueApi, Watch } from '@stencil/core';
|
||||
|
||||
import { ViewLifecycle } from '../..';
|
||||
import { Animation, AnimationBuilder, ComponentProps, Config, FrameworkDelegate, Gesture, Mode, NavComponent, NavOptions, NavOutlet, NavResult, RouteID, RouteWrite, TransitionDoneFn, TransitionInstruction, ViewController } from '../../interface';
|
||||
import { Animation, AnimationBuilder, ComponentProps, Config, FrameworkDelegate, Gesture, Mode, NavComponent, NavOptions, NavOutlet, NavResult, RouteID, RouteWrite, RouterDirection, TransitionDoneFn, TransitionInstruction, ViewController } from '../../interface';
|
||||
import { assert } from '../../utils/helpers';
|
||||
import { TransitionOptions, lifecycle, setPageHidden, transition } from '../../utils/transition';
|
||||
|
||||
import { ViewState, convertToViews, matches } from './view-controller';
|
||||
import { LIFECYCLE_DID_LEAVE, LIFECYCLE_WILL_LEAVE, LIFECYCLE_WILL_UNLOAD } from './constants';
|
||||
import { VIEW_STATE_ATTACHED, VIEW_STATE_DESTROYED, VIEW_STATE_NEW, convertToViews, matches } from './view-controller';
|
||||
|
||||
@Component({
|
||||
tag: 'ion-nav',
|
||||
@ -124,7 +124,7 @@ export class Nav implements NavOutlet {
|
||||
|
||||
componentDidUnload() {
|
||||
for (const view of this.views) {
|
||||
lifecycle(view.element!, ViewLifecycle.WillUnload);
|
||||
lifecycle(view.element!, LIFECYCLE_WILL_UNLOAD);
|
||||
view._destroy();
|
||||
}
|
||||
|
||||
@ -324,7 +324,7 @@ export class Nav implements NavOutlet {
|
||||
setRouteId(
|
||||
id: string,
|
||||
params: ComponentProps | undefined,
|
||||
direction: number
|
||||
direction: RouterDirection
|
||||
): Promise<RouteWrite> {
|
||||
const active = this.getActiveSync();
|
||||
if (matches(active, id, params)) {
|
||||
@ -354,7 +354,7 @@ export class Nav implements NavOutlet {
|
||||
}
|
||||
};
|
||||
|
||||
if (direction === 0) {
|
||||
if (direction === 'root') {
|
||||
finish = this.setRoot(id, params, commonOpts);
|
||||
} else {
|
||||
const viewController = this.views.find(v => matches(v, id, params));
|
||||
@ -364,9 +364,9 @@ export class Nav implements NavOutlet {
|
||||
...commonOpts,
|
||||
direction: 'back'
|
||||
});
|
||||
} else if (direction === 1) {
|
||||
} else if (direction === 'forward') {
|
||||
finish = this.push(id, params, commonOpts);
|
||||
} else if (direction === -1) {
|
||||
} else if (direction === 'back') {
|
||||
finish = this.setRoot(id, params, {
|
||||
...commonOpts,
|
||||
direction: 'back',
|
||||
@ -502,7 +502,7 @@ export class Nav implements NavOutlet {
|
||||
if (ti.opts!.updateURL !== false && this.useRouter) {
|
||||
const router = this.win.document.querySelector('ion-router');
|
||||
if (router) {
|
||||
const direction = result.direction === 'back' ? -1 : 1;
|
||||
const direction = result.direction === 'back' ? 'back' : 'forward';
|
||||
router.navChanged(direction);
|
||||
}
|
||||
}
|
||||
@ -560,7 +560,7 @@ export class Nav implements NavOutlet {
|
||||
throw new Error('no views in the stack to be removed');
|
||||
}
|
||||
|
||||
if (enteringView && enteringView.state === ViewState.New) {
|
||||
if (enteringView && enteringView.state === VIEW_STATE_NEW) {
|
||||
await enteringView.init(this.el);
|
||||
}
|
||||
this.postViewInit(enteringView, leavingView, ti);
|
||||
@ -645,7 +645,7 @@ export class Nav implements NavOutlet {
|
||||
if (nav && nav !== this) {
|
||||
throw new Error('inserted view was already inserted');
|
||||
}
|
||||
if (view.state === ViewState.Destroyed) {
|
||||
if (view.state === VIEW_STATE_DESTROYED) {
|
||||
throw new Error('inserted view was already destroyed');
|
||||
}
|
||||
}
|
||||
@ -749,9 +749,9 @@ export class Nav implements NavOutlet {
|
||||
// let's make sure, callbacks are zoned
|
||||
if (destroyQueue && destroyQueue.length > 0) {
|
||||
for (const view of destroyQueue) {
|
||||
lifecycle(view.element, ViewLifecycle.WillLeave);
|
||||
lifecycle(view.element, ViewLifecycle.DidLeave);
|
||||
lifecycle(view.element, ViewLifecycle.WillUnload);
|
||||
lifecycle(view.element, LIFECYCLE_WILL_LEAVE);
|
||||
lifecycle(view.element, LIFECYCLE_DID_LEAVE);
|
||||
lifecycle(view.element, LIFECYCLE_WILL_UNLOAD);
|
||||
}
|
||||
|
||||
// once all lifecycle events has been delivered, we can safely detroy the views
|
||||
@ -837,7 +837,7 @@ export class Nav implements NavOutlet {
|
||||
|
||||
private removeView(view: ViewController) {
|
||||
assert(
|
||||
view.state === ViewState.Attached || view.state === ViewState.Destroyed,
|
||||
view.state === VIEW_STATE_ATTACHED || view.state === VIEW_STATE_DESTROYED,
|
||||
'view state should be loaded or destroyed'
|
||||
);
|
||||
|
||||
@ -873,7 +873,7 @@ export class Nav implements NavOutlet {
|
||||
if (i > activeViewIndex) {
|
||||
// this view comes after the active view
|
||||
// let's unload it
|
||||
lifecycle(element, ViewLifecycle.WillUnload);
|
||||
lifecycle(element, LIFECYCLE_WILL_UNLOAD);
|
||||
this.destroyView(view);
|
||||
} else if (i < activeViewIndex) {
|
||||
// this view comes before the active view
|
||||
|
@ -952,7 +952,7 @@ describe('NavController', () => {
|
||||
? win.document.createElement(enteringView.component)
|
||||
: enteringView.element = enteringView.component as HTMLElement;
|
||||
}
|
||||
enteringView.state = ViewState.Attached;
|
||||
enteringView.state = VIEW_STATE_ATTACHED;
|
||||
};
|
||||
return navI;
|
||||
}
|
||||
|
@ -2,15 +2,13 @@ import { ComponentProps, FrameworkDelegate } from '../../interface';
|
||||
import { attachComponent } from '../../utils/framework-delegate';
|
||||
import { assert } from '../../utils/helpers';
|
||||
|
||||
export const enum ViewState {
|
||||
New = 1,
|
||||
Attached,
|
||||
Destroyed
|
||||
}
|
||||
export const VIEW_STATE_NEW = 1;
|
||||
export const VIEW_STATE_ATTACHED = 2;
|
||||
export const VIEW_STATE_DESTROYED = 3;
|
||||
|
||||
export class ViewController {
|
||||
|
||||
state: ViewState = ViewState.New;
|
||||
state = VIEW_STATE_NEW;
|
||||
nav?: any;
|
||||
element?: HTMLElement;
|
||||
delegate?: FrameworkDelegate;
|
||||
@ -21,7 +19,7 @@ export class ViewController {
|
||||
) {}
|
||||
|
||||
async init(container: HTMLElement) {
|
||||
this.state = ViewState.Attached;
|
||||
this.state = VIEW_STATE_ATTACHED;
|
||||
|
||||
if (!this.element) {
|
||||
const component = this.component;
|
||||
@ -33,7 +31,7 @@ export class ViewController {
|
||||
* DOM WRITE
|
||||
*/
|
||||
_destroy() {
|
||||
assert(this.state !== ViewState.Destroyed, 'view state must be ATTACHED');
|
||||
assert(this.state !== VIEW_STATE_DESTROYED, 'view state must be ATTACHED');
|
||||
|
||||
const element = this.element;
|
||||
if (element) {
|
||||
@ -44,7 +42,7 @@ export class ViewController {
|
||||
}
|
||||
}
|
||||
this.nav = undefined;
|
||||
this.state = ViewState.Destroyed;
|
||||
this.state = VIEW_STATE_DESTROYED;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user