FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Now passing options to showModal in one argument · NativeScript/NativeScript@fc4cb51 · GitHub

Commit fc4cb51

Browse files
committed
Now passing options to showModal in one argument
1 parent f4cc99c commit fc4cb51

4 files changed

Lines changed: 146 additions & 24 deletions

File tree

‎tests/app/ui/page/page-tests-common.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// >> article-require-page-module
1+
// >> article-require-page-module
22
import { Page, ShownModallyData, NavigatedData } from "tns-core-modules/ui/page";
33
// FrameModule is needed in order to have an option to navigate to the new page.
44
import { topmost, NavigationEntry } from "tns-core-modules/ui/frame";
@@ -556,6 +556,66 @@ export function test_WhenViewBaseCallsShowModal_WithArguments_ShouldOpenModal()
556556
TKUnit.waitUntilReady(() => modalClosed);
557557
}
558558

559+
export function test_WhenViewBaseCallsShowModal_WithShowModalOptionsArguments_ShouldOpenModal() {
560+
let modalClosed = false;
561+
562+
const modalCloseCallback = function (returnValue: any) {
563+
modalClosed = true;
564+
}
565+
566+
const createTabItems = function(count: number) {
567+
var items = new Array<TabViewItem>();
568+
569+
for (var i = 0; i < count; i++) {
570+
var label = new Label();
571+
label.text = "Tab " + i;
572+
var tabEntry = new TabViewItem();
573+
tabEntry.title = "Tab " + i;
574+
tabEntry.view = label;
575+
576+
items.push(tabEntry);
577+
}
578+
579+
return items;
580+
}
581+
582+
const modalPageShownModallyEventHandler = function(args: ShownModallyData) {
583+
const page = <Page>args.object;
584+
page.off(View.shownModallyEvent, modalPageShownModallyEventHandler);
585+
args.closeCallback();
586+
}
587+
588+
const hostNavigatedToEventHandler = function(args) {
589+
const page = <Page>args.object;
590+
page.off(Page.navigatedToEvent, hostNavigatedToEventHandler);
591+
592+
const modalPage = new Page();
593+
modalPage.id = "modalPage_test_WhenViewBaseCallsShowModal_WithShowModalOptionsArguments_ShouldOpenModal";
594+
modalPage.on(View.shownModallyEvent, modalPageShownModallyEventHandler);
595+
const tabViewItem = (<TabView>page.content).items[0];
596+
tabViewItem.showModal(modalPage, {}, modalCloseCallback, {
597+
fullscreen: false,
598+
animated: false
599+
});
600+
}
601+
602+
const masterPageFactory = function(): Page {
603+
const masterPage = new Page();
604+
masterPage.id = "masterPage_test_WhenViewBaseCallsShowModal_WithShowModalOptionsArguments_ShouldOpenModal";
605+
masterPage.on(Page.navigatedToEvent, hostNavigatedToEventHandler)
606+
607+
const tabView = new TabView();
608+
tabView.items = createTabItems(2);
609+
masterPage.content = tabView;
610+
611+
return masterPage;
612+
};
613+
614+
helper.navigate(masterPageFactory);
615+
616+
TKUnit.waitUntilReady(() => modalClosed);
617+
}
618+
559619
export function test_WhenViewBaseCallsShowModal_WithoutArguments_ShouldThrow() {
560620
let navigatedTo = false;
561621

‎tns-core-modules/ui/core/view-base/view-base.d.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ export function isEventOrGesture(name: string, view: ViewBase): boolean;
4646
*/
4747
export function getViewById(view: ViewBase, id: string): ViewBase;
4848

49+
export interface ShowModalOptions {
50+
/**
51+
* An optional parameter specifying whether to show the modal view in full-screen mode.
52+
*/
53+
fullscreen?: boolean;
54+
55+
/**
56+
* An optional parameter specifying whether to show the modal view with animation.
57+
*/
58+
animated?: boolean;
59+
60+
/**
61+
* An optional parameter specifying whether to stretch the modal view when not in full-screen mode.
62+
*/
63+
stretched?: boolean;
64+
65+
/**
66+
* An optional parameter that specify options specific to iOS as an object.
67+
* Supported properties:
68+
* presentationStyle - any value from the UIModalPresentationStyle
69+
*/
70+
ios?: any;
71+
}
72+
4973
export abstract class ViewBase extends Observable {
5074
// Dynamic properties.
5175
left: Length;
@@ -121,7 +145,18 @@ export abstract class ViewBase extends Observable {
121145
* @param stretched - An optional parameter specifying whether to stretch the modal view when not in full-screen mode.
122146
* @param iOSPresentationStyle - An optional, iOS only parameter specifying the way the modal view is covering the screen.
123147
*/
124-
showModal(moduleName: string, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean, iOSPresentationStyle?: UIModalPresentationStyle): ViewBase;
148+
showModal(moduleName: string, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean): ViewBase;
149+
150+
/**
151+
* Shows the View contained in moduleName as a modal view.
152+
* @param moduleName - The name of the module to load starting from the application root.
153+
* @param context - Any context you want to pass to the modally shown view.
154+
* This same context will be available in the arguments of the shownModally event handler.
155+
* @param closeCallback - A function that will be called when the view is closed.
156+
* Any arguments provided when calling ShownModallyData.closeCallback will be available here.
157+
* @param modalOptions - A ShowModalOptions instance
158+
*/
159+
showModal(moduleName: string, context: any, closeCallback: Function, modalOptions: ShowModalOptions): ViewBase;
125160

126161
/**
127162
* Shows the view passed as parameter as a modal view.
@@ -133,7 +168,16 @@ export abstract class ViewBase extends Observable {
133168
* @param stretched - An optional parameter specifying whether to stretch the modal view when not in full-screen mode.
134169
* @param iOSPresentationStyle - An optional, iOS only parameter specifying the way the modal view is covering the screen.
135170
*/
136-
showModal(view: ViewBase, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean, iOSPresentationStyle?: UIModalPresentationStyle): ViewBase;
171+
showModal(view: ViewBase, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean): ViewBase;
172+
173+
/**
174+
* Shows the view passed as parameter as a modal view.
175+
* @param view - View instance to be shown modally.
176+
* @param context - Any context you want to pass to the modally shown view. This same context will be available in the arguments of the shownModally event handler.
177+
* @param closeCallback - A function that will be called when the view is closed. Any arguments provided when calling ShownModallyData.closeCallback will be available here.
178+
* @param modalOptions - A ShowModalOptions instance
179+
*/
180+
showModal(view: ViewBase, context: any, closeCallback: Function, modalOptions: ShowModalOptions): ViewBase;
137181

138182
/**
139183
* Deprecated. Showing view as modal is deprecated.

‎tns-core-modules/ui/core/view/view-common.ts‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,40 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
215215
}
216216

217217
public showModal(): ViewDefinition {
218-
if (arguments.length === 0) {
219-
throw new Error("showModal without parameters is deprecated. Please call showModal on a view instance instead.");
218+
if (arguments.length === 0) {
219+
throw new Error("showModal without parameters is deprecated. Please call showModal on a view instance instead.");
220+
} else {
221+
var firstAgrument;
222+
var context: any;
223+
var closeCallback: Function;
224+
var fullscreen: boolean;
225+
var animated;
226+
var stretched;
227+
var iosOpts;
228+
229+
if (arguments.length === 4) {
230+
firstAgrument = arguments[0];
231+
context = arguments[1];
232+
closeCallback = arguments[2];
233+
fullscreen = arguments[3].fullscreen;
234+
animated = arguments[3].animated;
235+
stretched = arguments[3].stretched;
236+
iosOpts = arguments[3].ios;
220237
} else {
221-
const firstAgrument = arguments[0];
222-
const context: any = arguments[1];
223-
const closeCallback: Function = arguments[2];
224-
const fullscreen: boolean = arguments[3];
225-
const animated = arguments[4];
226-
const stretched = arguments[5];
227-
const presentationStyle = arguments[6];
228-
229-
const view: ViewDefinition = firstAgrument instanceof ViewCommon
230-
? firstAgrument : createViewFromEntry({ moduleName: firstAgrument });
231-
232-
(<ViewCommon>view)._showNativeModalView(this, context, closeCallback, fullscreen, animated, stretched, presentationStyle);
233-
return view;
238+
firstAgrument = arguments[0];
239+
context = arguments[1];
240+
closeCallback = arguments[2];
241+
fullscreen = arguments[3];
242+
animated = arguments[4];
243+
stretched = arguments[5];
234244
}
245+
246+
const view: ViewDefinition = firstAgrument instanceof ViewCommon
247+
? firstAgrument : createViewFromEntry({ moduleName: firstAgrument });
248+
249+
(<ViewCommon>view)._showNativeModalView(this, context, closeCallback, fullscreen, animated, stretched, iosOpts);
250+
return view;
251+
}
235252
}
236253

237254
public closeModal(...args) {
@@ -250,7 +267,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
250267
return this._modal;
251268
}
252269

253-
protected _showNativeModalView(parent: ViewCommon, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean, presentationStyle?: UIModalPresentationStyle) {
270+
protected _showNativeModalView(parent: ViewCommon, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean, iosOpts?: any) {
254271
_rootModalViews.push(this);
255272

256273
parent._modal = this;

‎tns-core-modules/ui/core/view/view.ios.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export class View extends ViewCommon {
361361
return this._suspendCATransaction || this._suspendNativeUpdatesCount;
362362
}
363363

364-
protected _showNativeModalView(parent: View, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean, presentationStyle?: UIModalPresentationStyle) {
364+
protected _showNativeModalView(parent: View, context: any, closeCallback: Function, fullscreen?: boolean, animated?: boolean, stretched?: boolean, iosOpts?: any) {
365365
const parentWithController = ios.getParentWithViewController(parent);
366366
if (!parentWithController) {
367367
traceWrite(`Could not find parent with viewController for ${parent} while showing modal view.`,
@@ -397,16 +397,17 @@ export class View extends ViewCommon {
397397
controller.modalPresentationStyle = UIModalPresentationStyle.FormSheet;
398398
}
399399

400-
if (presentationStyle !== undefined) {
401-
controller.modalPresentationStyle = presentationStyle;
400+
if (iosOpts && iosOpts.presentationStyle) {
401+
controller.modalPresentationStyle = iosOpts.presentationStyle;
402402

403-
if (presentationStyle === UIModalPresentationStyle.Popover) {
404-
// TODO: read the width and height of the page and apply it here ?
403+
if (iosOpts.presentationStyle === UIModalPresentationStyle.Popover) {
404+
// TODO: get the width and height of the page and apply it here ?
405405
// controller.preferredContentSize = CGSizeMake(400, 400);
406406
const popoverPresentationController = controller.popoverPresentationController;
407407

408408
const view = parent.nativeViewProtected;
409409
popoverPresentationController.sourceView = view;
410+
// popoverPresentationController.backgroundColor = this.ios.view.backgroundColor;
410411
popoverPresentationController.sourceRect = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
411412
}
412413
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL