Skip to content

Commit 9785e95

Browse files
author
Alexander Vakrilov
authored
Merge pull request #368 from NativeScript/clear-history
ClearHistory and PageTransitions
2 parents 071f471 + 125d9df commit 9785e95

16 files changed

+703
-100
lines changed

Diff for: nativescript-angular/application.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {topmost, NavigationEntry} from "ui/frame";
3030

3131
export type ProviderArray = Array<Type | Provider | any[]>;
3232

33-
import {defaultPageProvider, defaultDeviceProvider, defaultAnimationDriverProvider} from "./platform-providers";
33+
import {defaultPageProvider, defaultFrameProvider, defaultDeviceProvider, defaultAnimationDriverProvider} from "./platform-providers";
3434

3535
import * as nativescriptIntl from "nativescript-intl";
3636
global.Intl = nativescriptIntl;
@@ -91,6 +91,7 @@ export function bootstrap(appComponentType: any,
9191
}, deps: []
9292
}),
9393

94+
defaultFrameProvider,
9495
defaultPageProvider,
9596
defaultDeviceProvider,
9697
defaultAnimationDriverProvider,

Diff for: nativescript-angular/platform-providers.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {topmost} from 'ui/frame';
1+
import {topmost, Frame} from 'ui/frame';
22
import {Page} from 'ui/page';
33
import {provide, Provider, OpaqueToken} from '@angular/core/src/di';
44
import {device} from "platform";
@@ -19,6 +19,8 @@ export function getDefaultPage(): Page {
1919
}
2020
}
2121

22+
export const defaultFrameProvider = provide(Frame, { useFactory: topmost });
23+
2224
export const defaultDeviceProvider = provide(DEVICE, { useValue: device });
2325

2426
export const defaultAnimationDriverProvider = provide(AnimationDriver, { useClass: NativeScriptAnimationDriver });

Diff for: nativescript-angular/router-deprecated/page-router-outlet.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class PageRouterOutlet extends RouterOutlet {
9898
let previousInstruction = this.currentInstruction;
9999
this.currentInstruction = nextInstruction;
100100

101-
if (this.location.isPageNavigatingBack()) {
101+
if (this.location._isPageNavigatingBack()) {
102102
return this.activateOnGoBack(nextInstruction, previousInstruction);
103103
} else {
104104
return this.activateOnGoForward(nextInstruction, previousInstruction);
@@ -108,7 +108,7 @@ export class PageRouterOutlet extends RouterOutlet {
108108
private activateOnGoBack(nextInstruction: ComponentInstruction, previousInstruction: ComponentInstruction): Promise<any> {
109109
routerLog("PageRouterOutlet.activate() - Back naviation, so load from cache: " + nextInstruction.componentType.name);
110110

111-
this.location.finishBackPageNavigation();
111+
this.location._finishBackPageNavigation();
112112

113113
// Get Component form ref and just call the activate hook
114114
let cacheItem = this.refCache.peek();
@@ -177,7 +177,7 @@ export class PageRouterOutlet extends RouterOutlet {
177177
//Add it to the new page
178178
page.content = componentView;
179179

180-
this.location.navigateToNewPage();
180+
this.location._beginPageNavigation();
181181
return new Promise((resolve, reject) => {
182182
page.on('navigatingTo', () => {
183183
// Finish activation when page navigation has started
@@ -186,7 +186,7 @@ export class PageRouterOutlet extends RouterOutlet {
186186

187187
page.on('navigatedFrom', (<any>global).Zone.current.wrap((args: NavigatedData) => {
188188
if (args.isBackNavigation) {
189-
this.location.beginBackPageNavigation();
189+
this.location._beginBackPageNavigation();
190190
this.location.back();
191191
}
192192
}));
@@ -214,7 +214,7 @@ export class PageRouterOutlet extends RouterOutlet {
214214
(<OnDeactivate>this.componentRef.instance).routerOnDeactivate(nextInstruction, this.currentInstruction));
215215
}
216216

217-
if (this.location.isPageNavigatingBack()) {
217+
if (this.location._isPageNavigatingBack()) {
218218
routerLog("PageRouterOutlet.deactivate() while going back - should destroy: " + instruction.componentType.name)
219219
return next.then((_) => {
220220
const popedItem = this.refCache.pop();
@@ -311,6 +311,6 @@ export class PageRouterOutlet extends RouterOutlet {
311311
}
312312

313313
private log(method: string, nextInstruction: ComponentInstruction) {
314-
routerLog("PageRouterOutlet." + method + " isBack: " + this.location.isPageNavigatingBack() + " nextUrl: " + nextInstruction.urlPath);
314+
routerLog("PageRouterOutlet." + method + " isBack: " + this.location._isPageNavigatingBack() + " nextUrl: " + nextInstruction.urlPath);
315315
}
316316
}

Diff for: nativescript-angular/router/ns-location-strategy.ts

+79-29
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
import application = require("application");
2+
import { Injectable } from '@angular/core';
23
import { LocationStrategy } from '@angular/common';
3-
import { NgZone, ApplicationRef, Inject, forwardRef } from '@angular/core';
44
import { routerLog } from "../trace";
5-
import { topmost } from "ui/frame";
5+
import { Frame, NavigationTransition } from "ui/frame";
6+
import {isPresent} from '@angular/core/src/facade/lang';
67

7-
interface LocationState {
8+
export interface NavigationOptions {
9+
clearHistory?: boolean;
10+
animated?: boolean;
11+
transition?: NavigationTransition;
12+
}
13+
14+
const defaultNavOptions: NavigationOptions = {
15+
clearHistory: false,
16+
animated: true
17+
};
18+
19+
export interface LocationState {
820
state: any,
921
title: string,
1022
url: string,
1123
queryParams: string,
1224
isPageNavigation: boolean
1325
}
1426

27+
@Injectable()
1528
export class NSLocationStrategy extends LocationStrategy {
1629
private states = new Array<LocationState>();
1730
private popStateCallbacks = new Array<(_: any) => any>();
1831

1932
private _isPageNavigationgBack = false;
2033
private _isPageNavigatingForward: boolean = false;
34+
private _currentNavigationOptions: NavigationOptions;
2135

22-
constructor() {
36+
constructor(private frame: Frame) {
2337
super();
2438
routerLog("NSLocationStrategy.constructor()");
2539
}
2640

2741
path(): string {
28-
routerLog("NSLocationStrategy.path()");
2942
let state = this.peekState();
30-
return state ? state.url : "/";
43+
const result = state ? state.url : "/";
44+
routerLog("NSLocationStrategy.path(): " + result);
45+
return result;
3146
}
3247

3348
prepareExternalUrl(internal: string): string {
@@ -42,7 +57,16 @@ export class NSLocationStrategy extends LocationStrategy {
4257
}
4358

4459
pushStateInternal(state: any, title: string, url: string, queryParams: string): void {
45-
let isNewPage = this._isPageNavigatingForward;
60+
let isNewPage = this._isPageNavigatingForward || this.states.length === 0;
61+
62+
const navOptions = this._currentNavigationOptions || defaultNavOptions;
63+
64+
if (navOptions.clearHistory) {
65+
routerLog("NSLocationStrategy.pushStateInternal clearing states history");
66+
this.states.length = 0;
67+
}
68+
69+
this._currentNavigationOptions = undefined;
4670

4771
this._isPageNavigatingForward = false;
4872
this.states.push({
@@ -55,19 +79,22 @@ export class NSLocationStrategy extends LocationStrategy {
5579
}
5680

5781
replaceState(state: any, title: string, url: string, queryParams: string): void {
58-
routerLog(`NSLocationStrategy.replaceState state: ${state}, title: ${title}, url: ${url}, queryParams: ${queryParams}`);
59-
6082
if (this.states.length > 0) {
61-
let oldState = this.states.pop();
62-
routerLog(`NSLocationStrategy.replaceState state poped: ${oldState.state}, title: ${oldState.title}, url: ${oldState.url}, queryParams: ${oldState.queryParams}`);
83+
routerLog(`NSLocationStrategy.replaceState changing exisitng state: ${state}, title: ${title}, url: ${url}, queryParams: ${queryParams}`);
84+
const topState = this.peekState();
85+
topState.state = state;
86+
topState.title = title;
87+
topState.url = url;
88+
topState.queryParams = queryParams;
89+
}
90+
else {
91+
routerLog(`NSLocationStrategy.replaceState pushing new state: ${state}, title: ${title}, url: ${url}, queryParams: ${queryParams}`);
92+
this.pushStateInternal(state, title, url, queryParams);
6393
}
64-
65-
this.pushStateInternal(state, title, url, queryParams);
6694
}
6795

6896
forward(): void {
69-
routerLog("NSLocationStrategy.forward");
70-
throw new Error("Not implemented");
97+
throw new Error("NSLocationStrategy.forward() - not implemented");
7198
}
7299

73100
back(): void {
@@ -76,21 +103,23 @@ export class NSLocationStrategy extends LocationStrategy {
76103
// clear the stack until we get to a page navigation state
77104
let state = this.states.pop();
78105
let count = 1;
79-
while (!state.isPageNavigation) {
106+
107+
while (!(state.isPageNavigation)) {
80108
state = this.states.pop();
81109
count++;
82110
}
83-
routerLog("NSLocationStrategy.back() while navigating back. States popped: " + count)
111+
112+
routerLog("NSLocationStrategy.back() while navigating back. States popped: " + count);
84113
this.callPopState(state, true);
85114
} else {
86115
let state = this.peekState();
87116
if (state.isPageNavigation) {
88117
// This was a page navigation - so navigate through frame.
89-
routerLog("NSLocationStrategy.back() while not navigating back but top state is page - will call frame.goback()")
90-
topmost().goBack();
118+
routerLog("NSLocationStrategy.back() while not navigating back but top state is page - will call frame.goback()");
119+
this.frame.goBack();
91120
} else {
92121
// Nested navigation - just pop the state
93-
routerLog("NSLocationStrategy.back() while not navigating back but top state is not page - just pop")
122+
routerLog("NSLocationStrategy.back() while not navigating back but top state is not page - just pop");
94123
this.callPopState(this.states.pop(), true);
95124
}
96125
}
@@ -108,8 +137,8 @@ export class NSLocationStrategy extends LocationStrategy {
108137
}
109138

110139
private callPopState(state: LocationState, pop: boolean = true) {
111-
var change = { url: state.url, pop: pop };
112-
for (var fn of this.popStateCallbacks) {
140+
const change = { url: state.url, pop: pop };
141+
for (let fn of this.popStateCallbacks) {
113142
fn(change);
114143
}
115144
}
@@ -121,32 +150,53 @@ export class NSLocationStrategy extends LocationStrategy {
121150
return null;
122151
}
123152

153+
public toString() {
154+
return this.states
155+
.map((v, i) => `${i}.[${v.isPageNavigation ? "PAGE" : "INTERNAL"}] "${v.url}"`)
156+
.reverse()
157+
.join("\n");
158+
}
159+
124160
// Methods for syncing with page navigation in PageRouterOutlet
125-
public beginBackPageNavigation() {
161+
public _beginBackPageNavigation() {
126162
routerLog("NSLocationStrategy.startGoBack()");
127163
if (this._isPageNavigationgBack) {
128-
throw new Error("Calling startGoBack while going back.")
164+
throw new Error("Calling startGoBack while going back.");
129165
}
130166
this._isPageNavigationgBack = true;
131167
}
132168

133-
public finishBackPageNavigation() {
169+
public _finishBackPageNavigation() {
134170
routerLog("NSLocationStrategy.finishBackPageNavigation()");
135171
if (!this._isPageNavigationgBack) {
136-
throw new Error("Calling endGoBack while not going back.")
172+
throw new Error("Calling endGoBack while not going back.");
137173
}
138174
this._isPageNavigationgBack = false;
139175
}
140176

141-
public isPageNavigatingBack() {
177+
public _isPageNavigatingBack() {
142178
return this._isPageNavigationgBack;
143179
}
144180

145-
public navigateToNewPage() {
181+
public _beginPageNavigation(): NavigationOptions {
146182
routerLog("NSLocationStrategy.navigateToNewPage()");
147183
if (this._isPageNavigatingForward) {
148-
throw new Error("Calling navigateToNewPage while already navigating to new page.")
184+
throw new Error("Calling navigateToNewPage while already navigating to new page.");
149185
}
186+
150187
this._isPageNavigatingForward = true;
188+
return this._currentNavigationOptions || defaultNavOptions;
189+
}
190+
191+
public _setNavigationOptions(options: NavigationOptions) {
192+
this._currentNavigationOptions = {
193+
clearHistory: isPresent(options.clearHistory) ? options.clearHistory : false,
194+
animated: isPresent(options.animated) ? options.animated : true,
195+
transition: options.transition
196+
};
197+
}
198+
199+
public _getSatates(): Array<LocationState> {
200+
return this.states.slice();
151201
}
152202
}

Diff for: nativescript-angular/router/ns-platform-location.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,38 @@ export class NativescriptPlatformLocation extends PlatformLocation {
1717
}
1818

1919
onPopState(fn: UrlChangeListener): void {
20-
routerLog("NativescriptPlatformLocation.onPopState()");
2120
this.locationStartegy.onPopState(fn);
2221
}
2322

2423
onHashChange(fn: UrlChangeListener): void {
25-
routerLog("NativescriptPlatformLocation.onHashChange()");
2624
}
2725

2826
get search(): string {
29-
routerLog("NativescriptPlatformLocation.get search()");
30-
3127
return "";
3228
}
3329
get hash(): string {
34-
routerLog("NativescriptPlatformLocation.get hash()");
35-
3630
return "";
3731
}
3832
get pathname(): string {
39-
routerLog("NativescriptPlatformLocation.get pathname()");
4033
return this.locationStartegy.path();
4134
}
4235
set pathname(newPath: string) {
43-
routerLog("NativescriptPlatformLocation.set pathname(): " + newPath);
36+
throw new Error("NativescriptPlatformLocation set pathname - not implemented")
4437
}
4538

4639
pushState(state: any, title: string, url: string): void {
47-
routerLog("NativescriptPlatformLocation.pushState()");
48-
4940
this.locationStartegy.pushState(state, title, url, null);
5041
}
5142

5243
replaceState(state: any, title: string, url: string): void {
53-
routerLog("NativescriptPlatformLocation.replaceState()");
5444
this.locationStartegy.replaceState(state, title, url, null);
5545
}
5646

5747
forward(): void {
58-
routerLog("NativescriptPlatformLocation.forward()");
59-
60-
throw new Error("NativescriptPlatformLocation.forward() not implemend");
48+
throw new Error("NativescriptPlatformLocation.forward() - not implemented");
6149
}
6250

6351
back(): void {
64-
routerLog("NativescriptPlatformLocation.back()");
65-
6652
this.locationStartegy.back();
6753
}
6854
}

0 commit comments

Comments
 (0)