Merge pull request #178 from NativeScript/feature/page-navigation-events

Page navigation events
This commit is contained in:
Alexander Vakrilov
2015-05-19 13:39:47 +03:00
9 changed files with 113 additions and 59 deletions

View File

@ -39,7 +39,7 @@ export class ControlsPage extends pagesModule.Page implements definition.Control
this.content = this._mainLayout; this.content = this._mainLayout;
} }
public onNavigatedTo(context: any) { public onNavigatedTo() {
trace.write("Creating " + this._count + " controls...", trace.categories.Test, trace.messageType.info); trace.write("Creating " + this._count + " controls...", trace.categories.Test, trace.messageType.info);
this._infoLabel.text = "Creating " + this._count + " controls..."; this._infoLabel.text = "Creating " + this._count + " controls...";
var startTime = new Date().getMilliseconds(); var startTime = new Date().getMilliseconds();

View File

@ -152,7 +152,7 @@ export function testChangeItemWidth() {
wrapLayout.itemWidth = 50; wrapLayout.itemWidth = 50;
TKUnit.waitUntilReady(() => { TKUnit.waitUntilReady(() => {
return wrapLayout.getChildAt(wrapLayout.getChildrenCount() - 1).isLayoutValid; return wrapLayout.isLayoutValid;
}); });
var actualValue = viewModule.getViewById(wrapLayout, "1")._getCurrentLayoutBounds().left; var actualValue = viewModule.getViewById(wrapLayout, "1")._getCurrentLayoutBounds().left;
@ -185,7 +185,7 @@ export function testChangeItemHeight() {
wrapLayout.itemHeight = 50; wrapLayout.itemHeight = 50;
TKUnit.waitUntilReady(() => { TKUnit.waitUntilReady(() => {
return wrapLayout.getChildAt(wrapLayout.getChildrenCount() - 1).isLayoutValid; return wrapLayout.isLayoutValid;
}); });
var actualValue = viewModule.getViewById(wrapLayout, "1")._getCurrentLayoutBounds().top; var actualValue = viewModule.getViewById(wrapLayout, "1")._getCurrentLayoutBounds().top;
@ -236,7 +236,7 @@ export function testPaddingRight() {
layoutHelper.assertMeasure(btn2, 80, 50); layoutHelper.assertMeasure(btn2, 80, 50);
// There should be no space left for the button on the first row, // There should be no space left for the button on the first row,
// because fo the padding (200 - 100 - 30) = 70 button wants 80 // because for the padding (200 - 100 - 30) = 70 button wants 80
layoutHelper.assertLayout(btn1, 0, 0, 100, 50, "button1"); layoutHelper.assertLayout(btn1, 0, 0, 100, 50, "button1");
layoutHelper.assertLayout(btn2, 0, 50, 80, 50, "button2"); layoutHelper.assertLayout(btn2, 0, 50, 80, 50, "button2");
}); });

View File

@ -23,6 +23,7 @@ function isRunningOnEmulator(): boolean {
} }
export var allTests = {}; export var allTests = {};
allTests["APPLICATION"] = require("./application-tests");
allTests["DOCKLAYOUT"] = require("./layouts/dock-layout-tests"); allTests["DOCKLAYOUT"] = require("./layouts/dock-layout-tests");
allTests["WRAPLAYOUT"] = require("./layouts/wrap-layout-tests"); allTests["WRAPLAYOUT"] = require("./layouts/wrap-layout-tests");
allTests["ABSOLUTELAYOUT"] = require("./layouts/absolute-layout-tests"); allTests["ABSOLUTELAYOUT"] = require("./layouts/absolute-layout-tests");
@ -31,7 +32,6 @@ allTests["STACKLAYOUT"] = require("./layouts/stack-layout-tests");
allTests["PLATFORM"] = require("./platform-tests"); allTests["PLATFORM"] = require("./platform-tests");
allTests["STYLE-PROPERTIES"] = require("./ui/style/style-properties-tests"); allTests["STYLE-PROPERTIES"] = require("./ui/style/style-properties-tests");
allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests"); allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests");
allTests["APPLICATION"] = require("./application-tests");
allTests["FILE SYSTEM"] = require("./file-system-tests"); allTests["FILE SYSTEM"] = require("./file-system-tests");
allTests["HTTP"] = require("./http-tests"); allTests["HTTP"] = require("./http-tests");
allTests["APPLICATION SETTINGS"] = require("./application-settings-tests"); allTests["APPLICATION SETTINGS"] = require("./application-settings-tests");

View File

@ -198,9 +198,9 @@ export function buildUIWithWeakRefAndInteract<T extends view.View>(createFunc: (
} }
} }
export function navigate(pageFactory: () => page.Page) { export function navigate(pageFactory: () => page.Page, navigationContext?: any) {
var currentPage = frame.topmost().currentPage; var currentPage = frame.topmost().currentPage;
frame.topmost().navigate({ create: pageFactory, animated: false }); frame.topmost().navigate({ create: pageFactory, animated: false, context: navigationContext });
TKUnit.waitUntilReady(() => { return frame.topmost().currentPage !== currentPage; }); TKUnit.waitUntilReady(() => { return frame.topmost().currentPage !== currentPage; });
} }

View File

@ -225,29 +225,36 @@ export var test_NavigateToNewPage = function () {
export var test_PageNavigation_EventSequence = function () { export var test_PageNavigation_EventSequence = function () {
var testPage: PageModule.Page; var testPage: PageModule.Page;
var context = { property: "this is the context" };
var eventSequence = []; var eventSequence = [];
var pageFactory = function () { var pageFactory = function () {
testPage = new PageModule.Page(); testPage = new PageModule.Page();
addLabelToPage(testPage); addLabelToPage(testPage);
testPage.onNavigatingFrom = function () {
eventSequence.push("onNavigatingFrom");
}
testPage.onNavigatedFrom = function () { testPage.on(PageModule.Page.navigatingToEvent, function (data: PageModule.NavigatedData) {
eventSequence.push("onNavigatedFrom");
}
testPage.onNavigatingTo = function () {
eventSequence.push("onNavigatingTo"); eventSequence.push("onNavigatingTo");
} TKUnit.assertEqual(data.context, context, "onNavigatingTo: navigationContext");
});
testPage.onNavigatedTo = function () { testPage.on(PageModule.Page.navigatedToEvent, function (data: PageModule.NavigatedData) {
eventSequence.push("onNavigatedTo"); eventSequence.push("onNavigatedTo");
} TKUnit.assertEqual(data.context, context, "onNavigatedTo : navigationContext");
});
testPage.on(PageModule.Page.navigatingFromEvent, function (data: PageModule.NavigatedData) {
eventSequence.push("onNavigatingFrom");
TKUnit.assertEqual(data.context, context, "onNavigatingFrom: navigationContext");
});
testPage.on(PageModule.Page.navigatedFromEvent, function (data: PageModule.NavigatedData) {
eventSequence.push("onNavigatedFrom");
TKUnit.assertEqual(data.context, context, "onNavigatedFrom: navigationContext");
});
return testPage; return testPage;
}; };
helper.navigate(pageFactory); helper.navigate(pageFactory, context);
helper.goBack(); helper.goBack();
var expectedEventSequence = ["onNavigatingTo", "onNavigatedTo", "onNavigatingFrom", "onNavigatedFrom"]; var expectedEventSequence = ["onNavigatingTo", "onNavigatedTo", "onNavigatingFrom", "onNavigatedFrom"];
@ -263,9 +270,9 @@ export var test_NavigateTo_WithContext = function () {
var testPage: PageModule.Page; var testPage: PageModule.Page;
var pageFactory = function (): PageModule.Page { var pageFactory = function (): PageModule.Page {
testPage = new PageModule.Page(); testPage = new PageModule.Page();
testPage.onNavigatedTo = function (context) { testPage.on(PageModule.Page.navigatedToEvent, function () {
////console.log(JSON.stringify(context)); ////console.log(JSON.stringify(context));
} });
return testPage; return testPage;
}; };
var navEntry = { var navEntry = {

View File

@ -197,7 +197,7 @@ function onFragmentShown(fragment: PageFragmentBody) {
// notify the page // notify the page
frame._addView(page); frame._addView(page);
page.onNavigatedTo(entry.entry.context); page.onNavigatedTo();
frame._processNavigationQueue(page); frame._processNavigationQueue(page);
} }

View File

@ -222,7 +222,7 @@ class UINavigationControllerImpl extends UINavigationController implements UINav
var newPage = newEntry.resolvedPage; var newPage = newEntry.resolvedPage;
// notify the page // notify the page
newPage.onNavigatedTo(newEntry.entry.context); newPage.onNavigatedTo();
frame._processNavigationQueue(newPage); frame._processNavigationQueue(newPage);
} }

View File

@ -17,7 +17,10 @@ export module knownCollections {
} }
export class Page extends contentView.ContentView implements dts.Page, view.AddArrayFromBuilder { export class Page extends contentView.ContentView implements dts.Page, view.AddArrayFromBuilder {
public static navigatingToEvent = "navigatingTo";
public static navigatedToEvent = "navigatedTo"; public static navigatedToEvent = "navigatedTo";
public static navigatingFromEvent = "navigatingFrom";
public static navigatedFromEvent = "navigatedFrom";
public static shownModallyEvent = "shownModally"; public static shownModallyEvent = "shownModally";
private _navigationContext: any; private _navigationContext: any;
@ -96,23 +99,37 @@ export class Page extends contentView.ContentView implements dts.Page, view.AddA
public onNavigatingTo(context: any) { public onNavigatingTo(context: any) {
this._navigationContext = context; this._navigationContext = context;
this.notify({
eventName: Page.navigatingToEvent,
object: this,
context: this.navigationContext
});
} }
public onNavigatedTo(context: any) { public onNavigatedTo() {
this._navigationContext = context;
this.notify({ this.notify({
eventName: Page.navigatedToEvent, eventName: Page.navigatedToEvent,
object: this, object: this,
context: context context: this.navigationContext
}); });
} }
public onNavigatingFrom() { public onNavigatingFrom() {
// this.notify({
eventName: Page.navigatingFromEvent,
object: this,
context: this.navigationContext
});
} }
public onNavigatedFrom(isBackNavigation: boolean) { public onNavigatedFrom(isBackNavigation: boolean) {
// TODO: Should we clear navigation context here or somewhere else this.notify({
eventName: Page.navigatedFromEvent,
object: this,
context: this.navigationContext
});
this._navigationContext = undefined; this._navigationContext = undefined;
} }

86
ui/page/page.d.ts vendored
View File

@ -14,11 +14,11 @@ declare module "ui/page" {
//@endprivate //@endprivate
/** /**
* Defines the data for the Page.navigatedTo event. * Defines the data for the page navigation events.
*/ */
export interface NavigatedData extends observable.EventData { export interface NavigatedData extends observable.EventData {
/** /**
* The navigation context (optional, may be undefined) passed to the Page.onNavigatedTo method. * The navigation context (optional, may be undefined) passed to the page navigation evetns method.
*/ */
context: any; context: any;
} }
@ -46,11 +46,31 @@ declare module "ui/page" {
* Represents a logical unit for navigation (inside Frame). * Represents a logical unit for navigation (inside Frame).
*/ */
export class Page extends contentView.ContentView implements view.AddArrayFromBuilder { export class Page extends contentView.ContentView implements view.AddArrayFromBuilder {
/**
* String value used when hooking to shownModally event.
*/
public static shownModallyEvent: string;
/**
* String value used when hooking to navigatingTo event.
*/
public static navigatingToEvent: string;
/** /**
* String value used when hooking to navigatedTo event. * String value used when hooking to navigatedTo event.
*/ */
public static navigatedToEvent: string; public static navigatedToEvent: string;
/**
* String value used when hooking to navigatingFrom event.
*/
public static navigatingFromEvent: string;
/**
* String value used when hooking to navigatedFrom event.
*/
public static navigatedFromEvent: string;
constructor(options?: Options) constructor(options?: Options)
/** /**
@ -85,29 +105,6 @@ declare module "ui/page" {
*/ */
optionsMenu: OptionsMenu; optionsMenu: OptionsMenu;
/**
* A method called before navigating to the page.
* @param context - The data passed to the page through the NavigationEntry.context property.
*/
onNavigatingTo(context: any): void;
/**
* A method called after navigated to the page.
* @param context - The data passed to the page through the NavigationEntry.context property.
*/
onNavigatedTo(context: any): void;
/**
* A method called before navigating from the page.
*/
onNavigatingFrom(): void;
/**
* A method called after navigated from the page.
* @param isBackNavigation - True if the Page is being navigated from using the Frame.goBack() method, false otherwise.
*/
onNavigatedFrom(isBackNavigation: boolean): void;
/** /**
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method). * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change"). * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
@ -117,14 +114,24 @@ declare module "ui/page" {
on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any); on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any);
/** /**
* Raised when navigation to the page is finished. * Raised when navigation to the page has started.
*/
on(event: "navigatingTo", callback: (args: NavigatedData) => void, thisArg?: any);
/**
* Raised when navigation to the page has finished.
*/ */
on(event: "navigatedTo", callback: (args: NavigatedData) => void, thisArg?: any); on(event: "navigatedTo", callback: (args: NavigatedData) => void, thisArg?: any);
/** /**
* String value used when hooking to shownModally event. * Raised when navigation from the page has started.
*/ */
public static shownModallyEvent: string; on(event: "navigatingFrom", callback: (args: NavigatedData) => void, thisArg?: any);
/**
* Raised when navigation from the page has finished.
*/
on(event: "navigatedFrom", callback: (args: NavigatedData) => void, thisArg?: any);
/** /**
* Raised when the page is shown as a modal dialog. * Raised when the page is shown as a modal dialog.
@ -142,6 +149,29 @@ declare module "ui/page" {
_addArrayFromBuilder(name: string, value: Array<any>): void; _addArrayFromBuilder(name: string, value: Array<any>): void;
//@private //@private
/**
* A method called before navigating to the page.
* @param context - The data passed to the page through the NavigationEntry.context property.
*/
onNavigatingTo(context: any): void;
/**
* A method called after navigated to the page.
*/
onNavigatedTo(): void;
/**
* A method called before navigating from the page.
*/
onNavigatingFrom(): void;
/**
* A method called after navigated from the page.
* @param isBackNavigation - True if the Page is being navigated from using the Frame.goBack() method, false otherwise.
*/
onNavigatedFrom(isBackNavigation: boolean): void;
_getStyleScope(): styleScope.StyleScope; _getStyleScope(): styleScope.StyleScope;
_invalidateOptionsMenu(); _invalidateOptionsMenu();
//@endprivate //@endprivate