Skip to content

Commit 875b9d0

Browse files
committed
fix(router): fix push() public interface
1 parent 9352661 commit 875b9d0

File tree

11 files changed

+64
-64
lines changed

11 files changed

+64
-64
lines changed

core/src/components.d.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
PopoverOptions,
5757
RangeInputChangeEvent,
5858
RouteID,
59+
RouterDirection,
60+
RouterEventDetail,
61+
RouterIntent,
5962
RouterOutletOptions,
6063
RouteWrite,
6164
SelectInputChangeEvent,
@@ -82,10 +85,6 @@ import {
8285
import {
8386
ViewController,
8487
} from './components/nav/view-controller';
85-
import {
86-
RouterDirection,
87-
RouterEventDetail,
88-
} from './components/router/utils/interface';
8988
import {
9089
ScrollBaseDetail,
9190
ScrollDetail,
@@ -492,7 +491,7 @@ declare global {
492491
/**
493492
* When using a router, it specifies the transition direction when navigating to another page using `href`.
494493
*/
495-
'routerDirection': 'forward' | 'back';
494+
'routerDirection': RouterDirection;
496495
}
497496
}
498497

@@ -522,7 +521,7 @@ declare global {
522521
/**
523522
* When using a router, it specifies the transition direction when navigating to another page using `href`.
524523
*/
525-
'routerDirection'?: 'forward' | 'back';
524+
'routerDirection'?: RouterDirection;
526525
}
527526
}
528527
}
@@ -839,7 +838,7 @@ declare global {
839838
/**
840839
* When using a router, it specifies the transition direction when navigating to another page using `href`.
841840
*/
842-
'routerDirection': 'forward' | 'back';
841+
'routerDirection': RouterDirection;
843842
/**
844843
* The button shape. Possible values are: `"round"`.
845844
*/
@@ -917,7 +916,7 @@ declare global {
917916
/**
918917
* When using a router, it specifies the transition direction when navigating to another page using `href`.
919918
*/
920-
'routerDirection'?: 'forward' | 'back';
919+
'routerDirection'?: RouterDirection;
921920
/**
922921
* The button shape. Possible values are: `"round"`.
923922
*/
@@ -2863,7 +2862,7 @@ declare global {
28632862
/**
28642863
* When using a router, it specifies the transition direction when navigating to another page using `href`.
28652864
*/
2866-
'routerDirection': 'forward' | 'back';
2865+
'routerDirection': RouterDirection;
28672866
}
28682867
}
28692868

@@ -2917,7 +2916,7 @@ declare global {
29172916
/**
29182917
* When using a router, it specifies the transition direction when navigating to another page using `href`.
29192918
*/
2920-
'routerDirection'?: 'forward' | 'back';
2919+
'routerDirection'?: RouterDirection;
29212920
}
29222921
}
29232922
}
@@ -3862,7 +3861,7 @@ declare global {
38623861
'rootParams': ComponentProps;
38633862
'setPages': (views: any[], opts?: NavOptions | null | undefined, done?: TransitionDoneFn | undefined) => Promise<boolean>;
38643863
'setRoot': (component: NavComponent, componentProps?: ComponentProps | null | undefined, opts?: NavOptions | null | undefined, done?: TransitionDoneFn | undefined) => Promise<boolean>;
3865-
'setRouteId': (id: string, params: any, direction: number) => Promise<RouteWrite>;
3864+
'setRouteId': (id: string, params: any, direction: RouterIntent) => Promise<RouteWrite>;
38663865
'swipeBackEnabled': boolean;
38673866
}
38683867
}
@@ -5153,7 +5152,7 @@ declare global {
51535152

51545153
namespace StencilComponents {
51555154
interface IonRouter {
5156-
'navChanged': (direction: RouterDirection) => Promise<boolean>;
5155+
'navChanged': (intent: RouterIntent) => Promise<boolean>;
51575156
'printDebug': () => void;
51585157
'push': (url: string, direction?: RouterDirection) => Promise<boolean>;
51595158
/**

core/src/components/anchor/anchor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, Prop } from '@stencil/core';
2-
import { RouterDirection, openURL } from '../../utils/theme';
2+
import { RouterDirection } from '../../interface';
3+
import { openURL } from '../../utils/theme';
34

45

56
@Component({

core/src/components/button/button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, Element, Event, EventEmitter, Prop, State } from '@stencil/core';
2-
import { CssClassMap, Mode } from '../../interface';
3-
import { RouterDirection, getButtonClassMap, getElementClassMap, openURL } from '../../utils/theme';
2+
import { CssClassMap, Mode, RouterDirection } from '../../interface';
3+
import { getButtonClassMap, getElementClassMap, openURL } from '../../utils/theme';
44

55

66
@Component({

core/src/components/item/item.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, Element, Listen, Prop } from '@stencil/core';
2-
import { CssClassMap, Mode } from '../../interface';
3-
import { RouterDirection, createThemedClasses, getElementClassMap, openURL } from '../../utils/theme';
2+
import { CssClassMap, Mode, RouterDirection } from '../../interface';
3+
import { createThemedClasses, getElementClassMap, openURL } from '../../utils/theme';
44

55

66
@Component({

core/src/components/nav/nav.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ViewLifecycle } from '../..';
33
import {
44
Animation, ComponentProps, Config, FrameworkDelegate, GestureDetail, Mode,
55
NavComponent, NavOptions, NavOutlet, NavResult, QueueController, RouteID,
6-
RouteWrite, RouterDirection, TransitionDoneFn, TransitionInstruction } from '../../interface';
6+
RouteWrite, RouterIntent, TransitionDoneFn, TransitionInstruction } from '../../interface';
77
import { assert } from '../../utils/helpers';
88
import { TransitionOptions, lifecycle, transition } from '../../utils/transition';
99
import { ViewController, ViewState, convertToViews, matches } from './view-controller';
@@ -172,7 +172,7 @@ export class Nav implements NavOutlet {
172172
}
173173

174174
@Method()
175-
setRouteId(id: string, params: any, direction: number): Promise<RouteWrite> {
175+
setRouteId(id: string, params: any, direction: RouterIntent): Promise<RouteWrite> {
176176
const active = this.getActive();
177177
if (matches(active, id, params)) {
178178
return Promise.resolve({
@@ -310,8 +310,8 @@ export class Nav implements NavOutlet {
310310
const router = this.win.document.querySelector('ion-router');
311311
if (router) {
312312
const direction = (result.direction === 'back')
313-
? RouterDirection.Back
314-
: RouterDirection.Forward;
313+
? RouterIntent.Back
314+
: RouterIntent.Forward;
315315

316316
router.navChanged(direction);
317317
}

core/src/components/router/router.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Component, Element, Event, EventEmitter, Listen, Method, Prop } from '@stencil/core';
2-
import { Config, QueueController } from '../../interface';
2+
import { Config, QueueController, RouteChain, RouterDirection, RouterEventDetail, RouterIntent } from '../../interface';
33
import { debounce } from '../../utils/helpers';
44
import { printRedirects, printRoutes } from './utils/debug';
55
import { readNavState, waitUntilNavNode, writeNavState } from './utils/dom';
6-
import { RouteChain, RouterDirection, RouterEventDetail } from './utils/interface';
76
import { routeRedirect, routerIDsToChain, routerPathToChain } from './utils/matching';
87
import { readRedirects, readRoutes } from './utils/parser';
98
import { chainToPath, generatePath, parsePath, readPath, writePath } from './utils/path';
@@ -73,12 +72,13 @@ export class Router {
7372
}
7473

7574
@Method()
76-
push(url: string, direction = RouterDirection.Forward) {
75+
push(url: string, direction: RouterDirection = 'forward') {
7776
const path = parsePath(url);
77+
const intent = DIRECTION_TO_INTENT[direction];
7878
console.debug('[ion-router] URL pushed -> updating nav', url, direction);
7979

80-
this.setPath(path, direction);
81-
return this.writeNavStateRoot(path, direction);
80+
this.setPath(path, intent);
81+
return this.writeNavStateRoot(path, intent);
8282
}
8383

8484
@Method()
@@ -90,7 +90,7 @@ export class Router {
9090
}
9191

9292
@Method()
93-
async navChanged(direction: RouterDirection): Promise<boolean> {
93+
async navChanged(intent: RouterIntent): Promise<boolean> {
9494
if (this.busy) {
9595
return false;
9696
}
@@ -109,21 +109,21 @@ export class Router {
109109
}
110110

111111
console.debug('[ion-router] nav changed -> update URL', ids, path);
112-
this.setPath(path, direction);
112+
this.setPath(path, intent);
113113

114-
await this.writeNavState(outlet, chain, RouterDirection.None, path, null, ids.length);
114+
await this.writeNavState(outlet, chain, RouterIntent.None, path, null, ids.length);
115115
return true;
116116
}
117117

118118
private onRedirectChanged() {
119119
const path = this.getPath();
120120
if (path && routeRedirect(path, readRedirects(this.el))) {
121-
this.writeNavStateRoot(path, RouterDirection.None);
121+
this.writeNavStateRoot(path, RouterIntent.None);
122122
}
123123
}
124124

125125
private onRoutesChanged() {
126-
return this.writeNavStateRoot(this.getPath(), RouterDirection.None);
126+
return this.writeNavStateRoot(this.getPath(), RouterIntent.None);
127127
}
128128

129129
private historyDirection() {
@@ -137,16 +137,16 @@ export class Router {
137137
this.lastState = state;
138138

139139
if (state > lastState) {
140-
return RouterDirection.Forward;
140+
return RouterIntent.Forward;
141141
} else if (state < lastState) {
142-
return RouterDirection.Back;
142+
return RouterIntent.Back;
143143
} else {
144-
return RouterDirection.None;
144+
return RouterIntent.None;
145145
}
146146
}
147147

148148

149-
private async writeNavStateRoot(path: string[]|null, direction: RouterDirection): Promise<boolean> {
149+
private async writeNavStateRoot(path: string[]|null, intent: RouterIntent): Promise<boolean> {
150150
if (this.busy) {
151151
return false;
152152
}
@@ -160,7 +160,7 @@ export class Router {
160160
const redirect = routeRedirect(path, redirects);
161161
let redirectFrom: string[]|null = null;
162162
if (redirect) {
163-
this.setPath(redirect.to, direction);
163+
this.setPath(redirect.to, intent);
164164
redirectFrom = redirect.from;
165165
path = redirect.to;
166166
}
@@ -174,11 +174,11 @@ export class Router {
174174
}
175175

176176
// write DOM give
177-
return this.writeNavState(this.win.document.body, chain, direction, path, redirectFrom);
177+
return this.writeNavState(this.win.document.body, chain, intent, path, redirectFrom);
178178
}
179179

180180
private async writeNavState(
181-
node: HTMLElement|undefined, chain: RouteChain, direction: RouterDirection,
181+
node: HTMLElement|undefined, chain: RouteChain, intent: RouterIntent,
182182
path: string[], redirectFrom: string[] | null,
183183
index = 0
184184
): Promise<boolean> {
@@ -191,7 +191,7 @@ export class Router {
191191
const routeEvent = this.routeChangeEvent(path, redirectFrom);
192192
routeEvent && this.ionRouteWillChange.emit(routeEvent);
193193

194-
const changed = await writeNavState(node, chain, direction, index);
194+
const changed = await writeNavState(node, chain, intent, index);
195195
this.busy = false;
196196

197197
if (changed) {
@@ -204,9 +204,9 @@ export class Router {
204204
return changed;
205205
}
206206

207-
private setPath(path: string[], direction: RouterDirection) {
207+
private setPath(path: string[], intent: RouterIntent) {
208208
this.state++;
209-
writePath(this.win.history, this.root, this.useHash, path, direction, this.state);
209+
writePath(this.win.history, this.root, this.useHash, path, intent, this.state);
210210
}
211211

212212
private getPath(): string[] | null {
@@ -228,3 +228,9 @@ export class Router {
228228
};
229229
}
230230
}
231+
232+
const DIRECTION_TO_INTENT = {
233+
'back': RouterIntent.Back,
234+
'root': RouterIntent.None,
235+
'forward': RouterIntent.Forward
236+
};

core/src/components/router/utils/dom.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { NavOutletElement, RouteChain, RouteID, RouterDirection } from './interface';
1+
import { NavOutletElement, RouteChain, RouteID, RouterIntent } from '../../../interface';
22

3-
export async function writeNavState(root: HTMLElement | undefined, chain: RouteChain, direction: RouterDirection, index: number, changed = false): Promise<boolean> {
3+
export async function writeNavState(root: HTMLElement | undefined, chain: RouteChain, intent: RouterIntent, index: number, changed = false): Promise<boolean> {
44
try {
55
// find next navigation outlet in the DOM
66
const outlet = searchNavNode(root);
@@ -12,17 +12,17 @@ export async function writeNavState(root: HTMLElement | undefined, chain: RouteC
1212
await outlet.componentOnReady();
1313

1414
const route = chain[index];
15-
const result = await outlet.setRouteId(route.id, route.params, direction);
15+
const result = await outlet.setRouteId(route.id, route.params, intent);
1616

1717
// if the outlet changed the page, reset navigation to neutral (no direction)
1818
// this means nested outlets will not animate
1919
if (result.changed) {
20-
direction = RouterDirection.None;
20+
intent = RouterIntent.None;
2121
changed = true;
2222
}
2323

2424
// recursivelly set nested outlets
25-
changed = await writeNavState(result.element, chain, direction, index + 1, changed);
25+
changed = await writeNavState(result.element, chain, intent, index + 1, changed);
2626

2727
// once all nested outlets are visible let's make the parent visible too,
2828
// using markVisible prevents flickering

core/src/components/router/utils/interface.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
export interface NavOutlet {
3-
setRouteId(id: string, data: any, direction: number): Promise<RouteWrite>;
3+
setRouteId(id: string, data: any, direction: RouterIntent): Promise<RouteWrite>;
44
getRouteId(): RouteID|undefined;
55
}
66

@@ -10,7 +10,9 @@ export interface RouterEventDetail {
1010
to: string;
1111
}
1212

13-
export const enum RouterDirection {
13+
export type RouterDirection = 'forward' | 'back' | 'root';
14+
15+
export const enum RouterIntent {
1416
None = 0,
1517
Forward = 1,
1618
Back = -1,

core/src/components/router/utils/path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RouteChain, RouterDirection } from './interface';
1+
import { RouteChain, RouterIntent } from '../../../interface';
22

33
export function generatePath(segments: string[]): string {
44
const path = segments
@@ -26,15 +26,15 @@ export function chainToPath(chain: RouteChain): string[]|null {
2626
return path;
2727
}
2828

29-
export function writePath(history: History, root: string, useHash: boolean, path: string[], direction: RouterDirection, state: number) {
29+
export function writePath(history: History, root: string, useHash: boolean, path: string[], intent: RouterIntent, state: number) {
3030
let url = generatePath([
3131
...parsePath(root),
3232
...path
3333
]);
3434
if (useHash) {
3535
url = '#' + url;
3636
}
37-
if (direction === RouterDirection.Forward) {
37+
if (intent === RouterIntent.Forward) {
3838
history.pushState(state, '', url);
3939
} else {
4040
history.replaceState(state, '', url);

core/src/components/tabs/tabs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Build, Component, Element, Event, EventEmitter, Listen, Method, Prop, State } from '@stencil/core';
2-
import { Config, NavOutlet, RouteID, RouteWrite, RouterDirection } from '../../interface';
2+
import { Config, NavOutlet, RouteID, RouteWrite, RouterIntent } from '../../interface';
33
import { TabbarLayout, TabbarPlacement } from '../tabbar/tabbar';
44

55

@@ -262,7 +262,7 @@ export class Tabs implements NavOutlet {
262262
if (this.useRouter) {
263263
const router = this.doc.querySelector('ion-router');
264264
if (router) {
265-
return router.navChanged(RouterDirection.Forward);
265+
return router.navChanged(RouterIntent.Forward);
266266
}
267267
}
268268
return Promise.resolve(false);

0 commit comments

Comments
 (0)