Skip to content

Commit 23b1825

Browse files
author
hshristov
committed
implement menuOptions
1 parent 70756fb commit 23b1825

File tree

20 files changed

+425
-366
lines changed

20 files changed

+425
-366
lines changed

CrossPlatformModules.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,6 @@
513513
</TypeScriptCompile>
514514
<TypeScriptCompile Include="ui\text-view\text-view.d.ts" />
515515
<TypeScriptCompile Include="utils\android_constants.ts" />
516-
<TypeScriptCompile Include="utils\containers.d.ts" />
517-
<TypeScriptCompile Include="utils\containers.ts">
518-
<DependentUpon>containers.d.ts</DependentUpon>
519-
</TypeScriptCompile>
520516
<TypeScriptCompile Include="utils\module-merge.ts" />
521517
<TypeScriptCompile Include="utils\utils.android.ts">
522518
<DependentUpon>utils.d.ts</DependentUpon>

apps/tests/pages/page17.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Page loaded="pageLoaded">
22
<Page.optionsMenu>
3-
<MenuItem text="test" tap="optionTap"/>
4-
<MenuItem text="with icon" tap="optionTap" icon="~/app/test-icon.png"/>
3+
<MenuItem text="test" tap="optionTap" android.position="popup" />
4+
<MenuItem text="with icon" tap="optionTap" icon="~/app/test-icon.png" android.position="actionBar" />
55
</Page.optionsMenu>
66
<StackLayout>
77
<Button text="button" />

apps/tests/ui/page/page-tests-common.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,73 @@ export function addLabelToPage(page: PageModule.Page, text?: string) {
3434
page.content = label;
3535
}
3636

37+
export function test_menuItem_inherit_bindingContext() {
38+
39+
var page: PageModule.Page;
40+
var label: LabelModule.Label;
41+
var context = { text: "item" };
42+
43+
var pageFactory = function (): PageModule.Page {
44+
page = new PageModule.Page();
45+
page.bindingContext = context;
46+
var menuItem = new PageModule.MenuItem();
47+
48+
menuItem.bind({
49+
sourceProperty: "text",
50+
targetProperty: "text"
51+
});
52+
53+
page.optionsMenu.addItem(menuItem);
54+
55+
label = new LabelModule.Label();
56+
label.text = "Text";
57+
page.content = label;
58+
return page;
59+
};
60+
61+
helper.navigate(pageFactory);
62+
63+
try {
64+
TKUnit.assertEqual(page.optionsMenu.getItemAt(0).text, "item", "menuItem.text should equal to 'item'");
65+
}
66+
finally {
67+
helper.goBack();
68+
}
69+
}
70+
71+
export function test_Setting_OptionsMenu_doesnt_thrown() {
72+
73+
var page: PageModule.Page;
74+
var label: LabelModule.Label;
75+
var gotException = false;
76+
77+
var pageFactory = function (): PageModule.Page {
78+
page = new PageModule.Page();
79+
var menuItem = new PageModule.MenuItem();
80+
menuItem.text = "Item";
81+
page.optionsMenu.addItem(menuItem);
82+
83+
label = new LabelModule.Label();
84+
label.text = "Text";
85+
page.content = label;
86+
return page;
87+
};
88+
89+
try {
90+
helper.navigate(pageFactory);
91+
}
92+
catch (e) {
93+
gotException = true;
94+
}
95+
96+
try {
97+
TKUnit.assert(!gotException, "Expected: false, Actual: " + gotException);
98+
}
99+
finally {
100+
helper.goBack();
101+
}
102+
}
103+
37104
export function test_AfterPageLoaded_is_called_NativeInstance_is_created() {
38105

39106
var page: PageModule.Page;

apps/tests/ui/page/page-tests.ios.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import PageModule = require("ui/page");
33
import TKUnit = require("../../TKUnit");
44
import LabelModule = require("ui/label");
55
import helper = require("../helper");
6+
import view = require("ui/core/view");
67

78
declare var exports;
89
require("utils/module-merge").merge(PageTestCommon, exports);
910

10-
export var test_NavigateToNewPage_InnerControl = function () {
11+
export function test_NavigateToNewPage_InnerControl() {
1112
var testPage: PageModule.Page;
1213
var pageFactory = function (): PageModule.Page {
1314
testPage = new PageModule.Page();
@@ -24,3 +25,38 @@ export var test_NavigateToNewPage_InnerControl = function () {
2425
TKUnit.assert(label.android === undefined, "InnerControl.android should be undefined after navigate back.");
2526
TKUnit.assert(label.isLoaded === false, "InnerControl.isLoaded should become false after navigating back");
2627
}
28+
29+
export function test_NavBar_isVisible_when_MenuItems_areSet() {
30+
31+
var page: PageModule.Page;
32+
var label: LabelModule.Label;
33+
var navBarIsVisible = false;
34+
35+
var handler = function (data) {
36+
page.off(view.knownEvents.loaded, handler);
37+
navBarIsVisible = (<any>page.frame.ios).showNavigationBar;
38+
}
39+
40+
var pageFactory = function (): PageModule.Page {
41+
page = new PageModule.Page();
42+
page.on(view.knownEvents.loaded, handler);
43+
44+
var mi = new PageModule.MenuItem();
45+
mi.text = "B";
46+
page.optionsMenu.addItem(mi);
47+
label = new LabelModule.Label();
48+
label.text = "Text";
49+
page.content = label;
50+
return page;
51+
};
52+
53+
helper.navigate(pageFactory);
54+
55+
try {
56+
TKUnit.assert(navBarIsVisible, "Expected: true, Actual: " + navBarIsVisible);
57+
}
58+
finally {
59+
page.off(view.knownEvents.loaded, handler);
60+
helper.goBack();
61+
}
62+
}

ui/builder/component-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
6666
// Create instance of the component.
6767
instance = new instanceType();
6868
} catch (ex) {
69-
throw new Error("Cannot create module " + moduleId + ". " + ex);
69+
throw new Error("Cannot create module " + moduleId + ". " + ex + ". StackTrace: " + ex.stack);
7070
}
7171

7272
if (instance && instanceModule) {

ui/button/button.ios.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class TapHandlerImpl extends NSObject {
2020
public static ObjCExposedMethods = {
2121
"tap": { returns: interop.types.void, params: [interop.types.id] }
2222
};
23-
2423
}
2524

2625
// merge the exports of the common file with the exports of this file

ui/enums/enums.d.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
* iOS: [UIKeyboardTypeNumbersAndPunctuation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
1010
*/
1111
export var datetime: string;
12-
12+
1313
/**
1414
* Android: [TYPE_CLASS_PHONE](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_PHONE)
1515
* iOS: [UIKeyboardTypePhonePad](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
1616
*/
1717
export var phone: string;
18-
18+
1919
/**
2020
* Android: [TYPE_CLASS_NUMBER](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER) | android.text.InputType.TYPE_NUMBER_VARIATION_NORMAL | [TYPE_NUMBER_FLAG_SIGNED](http://developer.android.com/reference/android/text/InputType.html#TYPE_NUMBER_FLAG_SIGNED) | [TYPE_NUMBER_FLAG_DECIMAL](http://developer.android.com/reference/android/text/InputType.html#TYPE_NUMBER_FLAG_DECIMAL)
2121
* iOS: [UIKeyboardTypeNumbersAndPunctuation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
2222
*/
2323
export var number: string;
24-
24+
2525
/**
2626
* Android: [TYPE_CLASS_TEXT](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_TEXT) | [TYPE_TEXT_VARIATION_URI](http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_VARIATION_URI)
2727
* iOS: [UIKeyboardTypeURL](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
2828
*/
2929
export var url: string;
30-
30+
3131
/**
3232
* Android: [TYPE_CLASS_TEXT](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_TEXT) | [TYPE_TEXT_VARIATION_EMAIL_ADDRESS](http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_VARIATION_EMAIL_ADDRESS)
3333
* iOS: [UIKeyboardTypeEmailAddress](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
@@ -56,13 +56,13 @@
5656
* iOS: [UIReturnKeyGo](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType)
5757
*/
5858
export var go: string;
59-
59+
6060
/**
6161
* Android: [IME_ACTION_SEARCH](http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_SEARCH)
6262
* iOS: [UIReturnKeySearch](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType)
6363
*/
6464
export var search: string;
65-
65+
6666
/**
6767
* Android: [IME_ACTION_SEND](http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_SEND)
6868
* iOS: [UIReturnKeySend](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType)
@@ -304,7 +304,7 @@
304304
* Capitalize the first letter of each sentence automatically.
305305
*/
306306
export var sentences: string;
307-
307+
308308
/**
309309
* Capitalize all characters automatically.
310310
*/
@@ -325,4 +325,44 @@
325325
*/
326326
export var jpeg: string;
327327
}
328-
}
328+
329+
/**
330+
* Specifies NavigationBar visibility mode.
331+
*/
332+
module NavigationBarVisibility {
333+
/**
334+
* NavigationBar will be visible if there if frame backstack canGoBack is true or Page have optionsMenu with menuItems.
335+
*/
336+
export var auto: string;
337+
338+
/**
339+
* NavigationBar will be hidden.
340+
*/
341+
export var never: string;
342+
343+
/**
344+
* NavigationBar will be visible.
345+
*/
346+
export var always: string;
347+
}
348+
349+
/**
350+
* Specifies android MenuItem position.
351+
*/
352+
module MenuItemPosition {
353+
/**
354+
* Always show this item as a button in an Action Bar.
355+
*/
356+
export var actionBar: string;
357+
358+
/**
359+
* Show this item as a button in an Action Bar if the system decides there is room for it.
360+
*/
361+
export var actionBarIfRoom: string;
362+
363+
/**
364+
* Never show this item as a button in an Action Bar.
365+
*/
366+
export var popup: string;
367+
}
368+
}

ui/enums/enums.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,19 @@ export module AutocapitalizationType {
8686
export var allCharacters: string = "allCharacters";
8787
}
8888

89+
export module NavigationBarVisibility {
90+
export var auto: string = "auto";
91+
export var never: string = "never";
92+
export var always: string = "always";
93+
}
94+
95+
export module MenuItemPosition {
96+
export var actionBar: string = "actionBar";
97+
export var actionBarIfRoom: string = "actionBarIfRoom";
98+
export var popup: string = "popup";
99+
}
100+
89101
export module ImageFormat {
90102
export var png: string = "png";
91103
export var jpeg: string = "jpeg";
92-
}
104+
}

ui/frame/frame-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export class Frame extends view.CustomLayoutView implements definition.Frame {
270270
}
271271

272272
get backStack(): Array<definition.BackstackEntry> {
273-
return this._backStack;
273+
return this._backStack.slice();
274274
}
275275

276276
get currentPage(): pages.Page {

ui/frame/frame.android.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import utils = require("utils/utils");
77
import view = require("ui/core/view");
88
import application = require("application");
99
import imageSource = require("image-source");
10+
import enums = require("ui/enums");
1011

1112
declare var exports;
1213
require("utils/module-merge").merge(frameCommon, exports);
@@ -143,14 +144,29 @@ class PageFragmentBody extends android.app.Fragment {
143144

144145
for (var i = 0; i < items.length; i++) {
145146
var item = items[i];
146-
var menuItem = menu.add(android.view.Menu.NONE, i, item.priority, item.text);
147+
var menuItem = menu.add(android.view.Menu.NONE, i, android.view.Menu.NONE, item.text);
147148
if (item.icon) {
148-
var img = imageSource.fromFile(item.icon);
149+
var img = imageSource.fromResource(item.icon);
149150
var drawable = new android.graphics.drawable.BitmapDrawable(img.android);
150151
menuItem.setIcon(drawable);
151152
}
152153

153-
menuItem.setShowAsAction(android.view.MenuItem.SHOW_AS_ACTION_ALWAYS);
154+
var showAsAction = PageFragmentBody.getShowAsAction(item);
155+
menuItem.setShowAsAction(showAsAction);
156+
}
157+
}
158+
159+
private static getShowAsAction(menuItem: pages.MenuItem): number {
160+
switch (menuItem.android.position) {
161+
case enums.MenuItemPosition.actionBarIfRoom:
162+
return android.view.MenuItem.SHOW_AS_ACTION_IF_ROOM;
163+
164+
case enums.MenuItemPosition.popup:
165+
return android.view.MenuItem.SHOW_AS_ACTION_NEVER;
166+
167+
case enums.MenuItemPosition.actionBar:
168+
default:
169+
return android.view.MenuItem.SHOW_AS_ACTION_ALWAYS;
154170
}
155171
}
156172

@@ -462,7 +478,7 @@ class NativeActivity extends com.tns.NativeScriptActivity {
462478
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
463479
}
464480

465-
onOptionsItemSelected(menuItem) {
481+
onOptionsItemSelected(menuItem: android.view.IMenuItem) {
466482
if (!this.androidFrame.hasListeners(frameCommon.knownEvents.android.optionSelected)) {
467483
return false;
468484
}
@@ -638,10 +654,11 @@ function findPageForFragment(fragment: android.app.Fragment, frame: Frame) {
638654
trace.write("Current page matches fragment: " + fragmentTag, trace.categories.NativeLifecycle);
639655
}
640656
else {
641-
for (var i = 0; i < frame.backStack.length; i++) {
642-
entry = frame.backStack[i];
643-
if (frame.backStack[i].resolvedPage[TAG] === fragmentTag) {
644-
entry = frame.backStack[i];
657+
var backStack = frame.backStack;
658+
for (var i = 0; i < backStack.length; i++) {
659+
entry = backStack[i];
660+
if (backStack[i].resolvedPage[TAG] === fragmentTag) {
661+
entry = backStack[i];
645662
break;
646663
}
647664
}

0 commit comments

Comments
 (0)