implement menuOptions

This commit is contained in:
hshristov
2015-03-24 17:47:10 +02:00
parent 70756fb115
commit 23b1825fc7
20 changed files with 428 additions and 369 deletions

View File

@ -513,10 +513,6 @@
</TypeScriptCompile>
<TypeScriptCompile Include="ui\text-view\text-view.d.ts" />
<TypeScriptCompile Include="utils\android_constants.ts" />
<TypeScriptCompile Include="utils\containers.d.ts" />
<TypeScriptCompile Include="utils\containers.ts">
<DependentUpon>containers.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="utils\module-merge.ts" />
<TypeScriptCompile Include="utils\utils.android.ts">
<DependentUpon>utils.d.ts</DependentUpon>

View File

@ -1,7 +1,7 @@
<Page loaded="pageLoaded">
<Page.optionsMenu>
<MenuItem text="test" tap="optionTap"/>
<MenuItem text="with icon" tap="optionTap" icon="~/app/test-icon.png"/>
<MenuItem text="test" tap="optionTap" android.position="popup" />
<MenuItem text="with icon" tap="optionTap" icon="~/app/test-icon.png" android.position="actionBar" />
</Page.optionsMenu>
<StackLayout>
<Button text="button" />

View File

@ -34,6 +34,73 @@ export function addLabelToPage(page: PageModule.Page, text?: string) {
page.content = label;
}
export function test_menuItem_inherit_bindingContext() {
var page: PageModule.Page;
var label: LabelModule.Label;
var context = { text: "item" };
var pageFactory = function (): PageModule.Page {
page = new PageModule.Page();
page.bindingContext = context;
var menuItem = new PageModule.MenuItem();
menuItem.bind({
sourceProperty: "text",
targetProperty: "text"
});
page.optionsMenu.addItem(menuItem);
label = new LabelModule.Label();
label.text = "Text";
page.content = label;
return page;
};
helper.navigate(pageFactory);
try {
TKUnit.assertEqual(page.optionsMenu.getItemAt(0).text, "item", "menuItem.text should equal to 'item'");
}
finally {
helper.goBack();
}
}
export function test_Setting_OptionsMenu_doesnt_thrown() {
var page: PageModule.Page;
var label: LabelModule.Label;
var gotException = false;
var pageFactory = function (): PageModule.Page {
page = new PageModule.Page();
var menuItem = new PageModule.MenuItem();
menuItem.text = "Item";
page.optionsMenu.addItem(menuItem);
label = new LabelModule.Label();
label.text = "Text";
page.content = label;
return page;
};
try {
helper.navigate(pageFactory);
}
catch (e) {
gotException = true;
}
try {
TKUnit.assert(!gotException, "Expected: false, Actual: " + gotException);
}
finally {
helper.goBack();
}
}
export function test_AfterPageLoaded_is_called_NativeInstance_is_created() {
var page: PageModule.Page;

View File

@ -3,11 +3,12 @@ import PageModule = require("ui/page");
import TKUnit = require("../../TKUnit");
import LabelModule = require("ui/label");
import helper = require("../helper");
import view = require("ui/core/view");
declare var exports;
require("utils/module-merge").merge(PageTestCommon, exports);
export var test_NavigateToNewPage_InnerControl = function () {
export function test_NavigateToNewPage_InnerControl() {
var testPage: PageModule.Page;
var pageFactory = function (): PageModule.Page {
testPage = new PageModule.Page();
@ -24,3 +25,38 @@ export var test_NavigateToNewPage_InnerControl = function () {
TKUnit.assert(label.android === undefined, "InnerControl.android should be undefined after navigate back.");
TKUnit.assert(label.isLoaded === false, "InnerControl.isLoaded should become false after navigating back");
}
export function test_NavBar_isVisible_when_MenuItems_areSet() {
var page: PageModule.Page;
var label: LabelModule.Label;
var navBarIsVisible = false;
var handler = function (data) {
page.off(view.knownEvents.loaded, handler);
navBarIsVisible = (<any>page.frame.ios).showNavigationBar;
}
var pageFactory = function (): PageModule.Page {
page = new PageModule.Page();
page.on(view.knownEvents.loaded, handler);
var mi = new PageModule.MenuItem();
mi.text = "B";
page.optionsMenu.addItem(mi);
label = new LabelModule.Label();
label.text = "Text";
page.content = label;
return page;
};
helper.navigate(pageFactory);
try {
TKUnit.assert(navBarIsVisible, "Expected: true, Actual: " + navBarIsVisible);
}
finally {
page.off(view.knownEvents.loaded, handler);
helper.goBack();
}
}

View File

@ -66,7 +66,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
// Create instance of the component.
instance = new instanceType();
} catch (ex) {
throw new Error("Cannot create module " + moduleId + ". " + ex);
throw new Error("Cannot create module " + moduleId + ". " + ex + ". StackTrace: " + ex.stack);
}
if (instance && instanceModule) {

View File

@ -20,7 +20,6 @@ class TapHandlerImpl extends NSObject {
public static ObjCExposedMethods = {
"tap": { returns: interop.types.void, params: [interop.types.id] }
};
}
// merge the exports of the common file with the exports of this file

56
ui/enums/enums.d.ts vendored
View File

@ -9,25 +9,25 @@
* iOS: [UIKeyboardTypeNumbersAndPunctuation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
*/
export var datetime: string;
/**
* Android: [TYPE_CLASS_PHONE](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_PHONE)
* iOS: [UIKeyboardTypePhonePad](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
*/
export var phone: string;
/**
* 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)
* iOS: [UIKeyboardTypeNumbersAndPunctuation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
*/
export var number: string;
/**
* 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)
* iOS: [UIKeyboardTypeURL](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
*/
export var url: string;
/**
* 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)
* iOS: [UIKeyboardTypeEmailAddress](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
@ -56,13 +56,13 @@
* iOS: [UIReturnKeyGo](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType)
*/
export var go: string;
/**
* Android: [IME_ACTION_SEARCH](http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_SEARCH)
* iOS: [UIReturnKeySearch](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType)
*/
export var search: string;
/**
* Android: [IME_ACTION_SEND](http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_SEND)
* iOS: [UIReturnKeySend](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType)
@ -304,7 +304,7 @@
* Capitalize the first letter of each sentence automatically.
*/
export var sentences: string;
/**
* Capitalize all characters automatically.
*/
@ -325,4 +325,44 @@
*/
export var jpeg: string;
}
}
/**
* Specifies NavigationBar visibility mode.
*/
module NavigationBarVisibility {
/**
* NavigationBar will be visible if there if frame backstack canGoBack is true or Page have optionsMenu with menuItems.
*/
export var auto: string;
/**
* NavigationBar will be hidden.
*/
export var never: string;
/**
* NavigationBar will be visible.
*/
export var always: string;
}
/**
* Specifies android MenuItem position.
*/
module MenuItemPosition {
/**
* Always show this item as a button in an Action Bar.
*/
export var actionBar: string;
/**
* Show this item as a button in an Action Bar if the system decides there is room for it.
*/
export var actionBarIfRoom: string;
/**
* Never show this item as a button in an Action Bar.
*/
export var popup: string;
}
}

View File

@ -86,7 +86,19 @@ export module AutocapitalizationType {
export var allCharacters: string = "allCharacters";
}
export module NavigationBarVisibility {
export var auto: string = "auto";
export var never: string = "never";
export var always: string = "always";
}
export module MenuItemPosition {
export var actionBar: string = "actionBar";
export var actionBarIfRoom: string = "actionBarIfRoom";
export var popup: string = "popup";
}
export module ImageFormat {
export var png: string = "png";
export var jpeg: string = "jpeg";
}
}

View File

@ -270,7 +270,7 @@ export class Frame extends view.CustomLayoutView implements definition.Frame {
}
get backStack(): Array<definition.BackstackEntry> {
return this._backStack;
return this._backStack.slice();
}
get currentPage(): pages.Page {

View File

@ -7,6 +7,7 @@ import utils = require("utils/utils");
import view = require("ui/core/view");
import application = require("application");
import imageSource = require("image-source");
import enums = require("ui/enums");
declare var exports;
require("utils/module-merge").merge(frameCommon, exports);
@ -143,14 +144,29 @@ class PageFragmentBody extends android.app.Fragment {
for (var i = 0; i < items.length; i++) {
var item = items[i];
var menuItem = menu.add(android.view.Menu.NONE, i, item.priority, item.text);
var menuItem = menu.add(android.view.Menu.NONE, i, android.view.Menu.NONE, item.text);
if (item.icon) {
var img = imageSource.fromFile(item.icon);
var img = imageSource.fromResource(item.icon);
var drawable = new android.graphics.drawable.BitmapDrawable(img.android);
menuItem.setIcon(drawable);
}
menuItem.setShowAsAction(android.view.MenuItem.SHOW_AS_ACTION_ALWAYS);
var showAsAction = PageFragmentBody.getShowAsAction(item);
menuItem.setShowAsAction(showAsAction);
}
}
private static getShowAsAction(menuItem: pages.MenuItem): number {
switch (menuItem.android.position) {
case enums.MenuItemPosition.actionBarIfRoom:
return android.view.MenuItem.SHOW_AS_ACTION_IF_ROOM;
case enums.MenuItemPosition.popup:
return android.view.MenuItem.SHOW_AS_ACTION_NEVER;
case enums.MenuItemPosition.actionBar:
default:
return android.view.MenuItem.SHOW_AS_ACTION_ALWAYS;
}
}
@ -462,7 +478,7 @@ class NativeActivity extends com.tns.NativeScriptActivity {
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
}
onOptionsItemSelected(menuItem) {
onOptionsItemSelected(menuItem: android.view.IMenuItem) {
if (!this.androidFrame.hasListeners(frameCommon.knownEvents.android.optionSelected)) {
return false;
}
@ -638,10 +654,11 @@ function findPageForFragment(fragment: android.app.Fragment, frame: Frame) {
trace.write("Current page matches fragment: " + fragmentTag, trace.categories.NativeLifecycle);
}
else {
for (var i = 0; i < frame.backStack.length; i++) {
entry = frame.backStack[i];
if (frame.backStack[i].resolvedPage[TAG] === fragmentTag) {
entry = frame.backStack[i];
var backStack = frame.backStack;
for (var i = 0; i < backStack.length; i++) {
entry = backStack[i];
if (backStack[i].resolvedPage[TAG] === fragmentTag) {
entry = backStack[i];
break;
}
}

8
ui/frame/frame.d.ts vendored
View File

@ -145,7 +145,7 @@ declare module "ui/frame" {
/**
* Gets the Android-specific menu item that has been selected.
*/
item: android.view.MenuItem;
item: android.view.IMenuItem;
/**
* True to mark the event as handled (that is to prevent the default processing).
@ -207,6 +207,12 @@ declare module "ui/frame" {
* Gets the native [UINavigationController](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html) instance associated with this Frame.
*/
controller: UINavigationController;
/**
* Gets or sets the visibility of navigationBar.
* Use NavBarVisibility enumeration - auto, never, always
*/
navBarVisibility: string;
}
/**

View File

@ -1,6 +1,9 @@
import frameCommon = require("ui/frame/frame-common");
import definition = require("ui/frame");
import trace = require("trace");
import imageSource = require("image-source");
import pages = require("ui/page");
import enums = require("ui/enums");
declare var exports;
require("utils/module-merge").merge(frameCommon, exports);
@ -12,6 +15,7 @@ var navDepth = 0;
export class Frame extends frameCommon.Frame {
private _ios: iOSFrame;
private _paramToNavigate: any;
public shouldSkipNativePop: boolean = false;
constructor() {
super();
@ -35,7 +39,7 @@ export class Frame extends frameCommon.Frame {
this._paramToNavigate = param;
}
}
public _navigateCore(backstackEntry: definition.BackstackEntry) {
var viewController = backstackEntry.resolvedPage.ios;
if (!viewController) {
@ -47,9 +51,7 @@ export class Frame extends frameCommon.Frame {
animated = this._getIsAnimatedNavigation(backstackEntry.entry);
}
if (this.backStack.length > 0) {
this._ios.showNavigationBar = true;
}
this.updateNavigationBar();
viewController[ENTRY] = backstackEntry;
@ -61,17 +63,29 @@ export class Frame extends frameCommon.Frame {
public _goBackCore(entry: definition.NavigationEntry) {
navDepth--;
trace.write("Frame<" + this._domId + ">.popViewControllerAnimated depth = " + navDepth, trace.categories.Navigation);
this._ios.controller.allowPop = true;
this._ios.controller.popViewControllerAnimated(this._getIsAnimatedNavigation(entry));
this._ios.controller.allowPop = false;
if (this.backStack.length === 0) {
this._ios.showNavigationBar = false;
if (!this.shouldSkipNativePop) {
this._ios.controller.popViewControllerAnimated(this._getIsAnimatedNavigation(entry));
}
}
public get ios(): any {
public updateNavigationBar(page?: pages.Page): void {
switch (this._ios.navBarVisibility) {
case enums.NavigationBarVisibility.always:
this._ios.showNavigationBar = true;
break;
case enums.NavigationBarVisibility.never:
this._ios.showNavigationBar = false;
break;
case enums.NavigationBarVisibility.auto:
var pageInstance: pages.Page = page || this.currentPage;
this._ios.showNavigationBar = this.backStack.length > 0 || (pageInstance && pageInstance.optionsMenu.getItems().length > 0);
break;
}
}
public get ios(): iOSFrame {
return this._ios;
}
@ -122,9 +136,41 @@ export class Frame extends frameCommon.Frame {
var navigationBar = this._ios.controller.navigationBar;
return (navigationBar && !this._ios.controller.navigationBarHidden) ? navigationBar.frame.size.height : 0;
}
public _invalidateOptionsMenu() {
this.populateMenuItems(this.currentPage);
}
populateMenuItems(page: pages.Page) {
var items = page.optionsMenu.getItems();
var navigationItem: UINavigationItem = (<UIViewController>page.ios).navigationItem;
var array: NSMutableArray = items.length > 0 ? NSMutableArray.new() : null;
for (var i = 0; i < items.length; i++) {
var item = items[i];
var tapHandler = TapBarItemHandlerImpl.new().initWithOwner(item);
// associate handler with menuItem or it will get collected by JSC.
(<any>item).handler = tapHandler;
var barButtonItem: UIBarButtonItem;
if (item.icon) {
var img = imageSource.fromResource(item.icon);
barButtonItem = UIBarButtonItem.alloc().initWithImageStyleTargetAction(img.ios, UIBarButtonItemStyle.UIBarButtonItemStylePlain, tapHandler, "tap");
}
else {
barButtonItem = UIBarButtonItem.alloc().initWithTitleStyleTargetAction(item.text, UIBarButtonItemStyle.UIBarButtonItemStylePlain, tapHandler, "tap");
}
array.addObject(barButtonItem);
}
if (array) {
navigationItem.setRightBarButtonItemsAnimated(array, true);
}
}
}
/* tslint:disable */
class UINavigationControllerImpl extends UINavigationController implements UINavigationControllerDelegate {
public static ObjCProtocols = [UINavigationControllerDelegate];
@ -132,21 +178,13 @@ class UINavigationControllerImpl extends UINavigationController implements UINav
return <UINavigationControllerImpl>super.new();
}
private _owner: frameCommon.Frame;
public initWithOwner(owner: frameCommon.Frame): UINavigationControllerImpl {
private _owner: Frame;
public initWithOwner(owner: Frame): UINavigationControllerImpl {
this._owner = owner;
return this;
}
private _allowPop: boolean;
public get allowPop(): boolean {
return this._allowPop;
}
public set allowPop(value: boolean) {
this._allowPop = value;
}
public viewDidLoad(): void {
this.view.autoresizesSubviews = false;
this.view.autoresizingMask = UIViewAutoresizing.UIViewAutoresizingNone;
@ -158,39 +196,37 @@ class UINavigationControllerImpl extends UINavigationController implements UINav
this._owner._updateLayout();
}
public popViewControllerAnimated(animated: boolean): UIViewController {
if (this.allowPop) {
return super.popViewControllerAnimated(animated);
}
else {
var currentControler = this._owner.currentPage.ios;
this._owner.goBack();
return currentControler;
}
}
public navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
var frame: Frame = this._owner;
var backStack = frame.backStack;
var currentEntry = backStack.length > 0 ? backStack[backStack.length - 1] : null;
var newEntry: definition.BackstackEntry = viewController[ENTRY];
if (newEntry === currentEntry && currentEntry) {
try {
frame.shouldSkipNativePop = true;
frame.goBack();
}
finally {
frame.shouldSkipNativePop = false;
}
}
public navigationControllerWillShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
var frame = this._owner;
var page = frame.currentPage;
if (page) {
frame._removeView(page);
}
var newEntry: definition.BackstackEntry = viewController[ENTRY];
var newPage = newEntry.resolvedPage;
frame._currentEntry = newEntry;
frame._addView(newPage);
frame.populateMenuItems(newPage);
frame.updateNavigationBar();
// notify the page
newPage.onNavigatedTo(newEntry.entry.context);
}
public navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
var frame: frameCommon.Frame = this._owner;
var newEntry: definition.BackstackEntry = viewController[ENTRY];
var newPage = newEntry.resolvedPage;
frame._processNavigationQueue(newPage);
}
@ -199,16 +235,19 @@ class UINavigationControllerImpl extends UINavigationController implements UINav
}
}
/* tslint:disable */
class iOSFrame implements definition.iOSFrame {
/* tslint:enable */
/* tslint:enable */
private _controller: UINavigationControllerImpl;
private _showNavigationBar: boolean;
private _navBarVisibility: string;
constructor(owner: frameCommon.Frame) {
constructor(owner: Frame) {
this._controller = UINavigationControllerImpl.new().initWithOwner(owner);
this._controller.delegate = this._controller;
this._controller.automaticallyAdjustsScrollViewInsets = false;
this.showNavigationBar = false;
this._navBarVisibility = enums.NavigationBarVisibility.auto;
}
public get controller() {
@ -222,4 +261,32 @@ class iOSFrame implements definition.iOSFrame {
this._showNavigationBar = value;
this._controller.navigationBarHidden = !value;
}
}
public get navBarVisibility(): string {
return this._navBarVisibility;
}
public set navBarVisibility(value: string) {
this._navBarVisibility = value;
}
}
class TapBarItemHandlerImpl extends NSObject {
static new(): TapBarItemHandlerImpl {
return <TapBarItemHandlerImpl>super.new();
}
private _owner: pages.MenuItem;
public initWithOwner(owner: pages.MenuItem): TapBarItemHandlerImpl {
this._owner = owner;
return this;
}
public tap(args) {
this._owner._raiseTap();
}
public static ObjCExposedMethods = {
"tap": { returns: interop.types.void, params: [interop.types.id] }
};
}

View File

@ -1,12 +1,23 @@
declare module "ui/layouts/absolute-layout" {
import layout = require("ui/layouts/layout");
import view = require("ui/core/view");
import dependencyObservable = require("ui/core/dependency-observable");
/**
* A layout that lets you specify exact locations (left/top coordinates) of its children.
*/
class AbsoluteLayout extends layout.Layout {
/**
* Represents the observable property backing the left property.
*/
public static leftProperty: dependencyObservable.Property;
/**
* Represents the observable property backing the top property.
*/
public static topProperty: dependencyObservable.Property;
/**
* Gets the value of the Left property from a given View.
*/

View File

@ -17,10 +17,10 @@ function onPropertyChanged(data: dependencyObservable.PropertyChangeData) {
export class AbsoluteLayout extends layouts.Layout implements definition.AbsoluteLayout {
public static LeftProperty = new dependencyObservable.Property("Left", "AbsoluteLayout",
public static leftProperty = new dependencyObservable.Property("left", "AbsoluteLayout",
new dependencyObservable.PropertyMetadata(0, undefined, onPropertyChanged, numberUtils.isFiniteNumber));
public static TopProperty = new dependencyObservable.Property("Top", "AbsoluteLayout",
public static topProperty = new dependencyObservable.Property("top", "AbsoluteLayout",
new dependencyObservable.PropertyMetadata(0, undefined, onPropertyChanged, numberUtils.isFiniteNumber));
public static getLeft(element: view.View): number {
@ -28,14 +28,14 @@ export class AbsoluteLayout extends layouts.Layout implements definition.Absolut
throw new Error("element cannot be null or undefinied.");
}
return element._getValue(AbsoluteLayout.LeftProperty);
return element._getValue(AbsoluteLayout.leftProperty);
}
public static setLeft(element: view.View, value: number): void {
if (!element) {
throw new Error("element cannot be null or undefinied.");
}
element._setValue(AbsoluteLayout.LeftProperty, value);
element._setValue(AbsoluteLayout.leftProperty, value);
}
public static getTop(element: view.View): number {
@ -43,14 +43,14 @@ export class AbsoluteLayout extends layouts.Layout implements definition.Absolut
throw new Error("element cannot be null or undefinied.");
}
return element._getValue(AbsoluteLayout.TopProperty);
return element._getValue(AbsoluteLayout.topProperty);
}
public static setTop(element: view.View, value: number): void {
if (!element) {
throw new Error("element cannot be null or undefinied.");
}
element._setValue(AbsoluteLayout.TopProperty, value);
element._setValue(AbsoluteLayout.topProperty, value);
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {

View File

@ -6,7 +6,9 @@ import styleScope = require("ui/styling/style-scope");
import fs = require("file-system");
import fileSystemAccess = require("file-system/file-system-access");
import trace = require("trace");
import observable = require("data/observable");
import bindable = require("ui/core/bindable");
import dependencyObservable = require("ui/core/dependency-observable");
import enums = require("ui/enums");
var OPTIONS_MENU = "optionsMenu";
@ -171,6 +173,12 @@ export class OptionsMenu implements dts.OptionsMenu {
}
this._items.push(item);
item.menu = this;
item.bind({
sourceProperty: "bindingContext",
targetProperty: "bindingContext"
}, this._page);
this.invalidate();
}
@ -184,6 +192,8 @@ export class OptionsMenu implements dts.OptionsMenu {
throw new Error("Cannot find item to remove");
}
item.menu = undefined;
item.unbind("bindingContext");
this._items.splice(itemIndex, 1);
this.invalidate();
}
@ -201,19 +211,60 @@ export class OptionsMenu implements dts.OptionsMenu {
this.invalidate();
}
private invalidate() {
invalidate() {
if (this._page.frame) {
this._page.frame._invalidateOptionsMenu();
}
}
}
export class MenuItem extends observable.Observable implements dts.MenuItem {
public text: string = "";
public icon: string;
public priority: number = 0;
export class MenuItem extends bindable.Bindable implements dts.MenuItem {
public static textProperty = new dependencyObservable.Property(
"text", "MenuItem", new dependencyObservable.PropertyMetadata("", null, MenuItem.onItemChanged));
public static iconProperty = new dependencyObservable.Property(
"icon", "MenuItem", new dependencyObservable.PropertyMetadata(null, null, MenuItem.onItemChanged));
private static onItemChanged(data: dependencyObservable.PropertyChangeData) {
var menuItem = <MenuItem>data.object;
if (menuItem.menu) {
menuItem.menu.invalidate();
}
}
private _android: dts.AndroidMenuItemOptions;
constructor() {
super();
if (global.android) {
this._android = {
position: enums.MenuItemPosition.actionBar
};
}
}
get android(): dts.AndroidMenuItemOptions {
return this._android;
}
get text(): string {
return this._getValue(MenuItem.textProperty);
}
set text(value: string) {
this._setValue(MenuItem.textProperty, value);
}
get icon(): string {
return this._getValue(MenuItem.iconProperty);
}
set icon(value: string) {
this._setValue(MenuItem.iconProperty, value);
}
public _raiseTap() {
this._emit(knownEvents.tap);
}
menu: OptionsMenu;
}

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

@ -6,6 +6,9 @@ declare module "ui/page" {
import view = require("ui/core/view");
import contentView = require("ui/content-view");
import frame = require("ui/frame");
import dependencyObservable = require("ui/core/dependency-observable");
import bindable = require("ui/core/bindable");
//@private
import styleScope = require("ui/styling/style-scope");
//@endprivate
@ -96,17 +99,20 @@ declare module "ui/page" {
*/
onNavigatedFrom(isBackNavigation: boolean): void;
//@private
_getStyleScope(): styleScope.StyleScope;
_addArrayFromBuilder(name: string, value: Array<any>): void;
//@endprivate
/**
* Raised when navigation to the page is finished.
*/
on(event: string, callback: (data: observable.EventData) => void);
/**
* Raised when navigation to the page is finished.
*/
on(event: "navigatedTo", callback: (args: NavigatedData) => void);
//@private
_getStyleScope(): styleScope.StyleScope;
_addArrayFromBuilder(name: string, value: Array<any>): void;
//@endprivate
}
/**
@ -136,17 +142,36 @@ declare module "ui/page" {
getItemAt(index: number): MenuItem;
}
export class MenuItem extends observable.Observable {
export class MenuItem extends bindable.Bindable {
/**
* Represents the observable property backing the text property.
*/
public static textProperty: dependencyObservable.Property;
/**
* Represents the observable property backing the icon property.
*/
public static iconProperty: dependencyObservable.Property;
text: string;
icon: string;
priority: number;
android: AndroidMenuItemOptions;
on(event: string, callback: (data: observable.EventData) => void);
on(event: "tap", callback: (args: observable.EventData) => void);
//@private
_raiseTap();
_raiseTap(): void;
//@endprivate
}
}
interface AndroidMenuItemOptions {
/**
* Specify if android menuItem should appear in the actionBar or in the popup.
* Use values from enums MenuItemPosition.
* Changes after menuItem is created are not supported.
*/
position: string;
}
}

View File

@ -3,7 +3,8 @@ import definition = require("ui/page");
import viewModule = require("ui/core/view");
import trace = require("trace");
module.exports.knownEvents = pageCommon.knownEvents;
declare var exports;
require("utils/module-merge").merge(pageCommon, exports);
class UIViewControllerImpl extends UIViewController {
static new(): UIViewControllerImpl {
@ -29,7 +30,6 @@ class UIViewControllerImpl extends UIViewController {
}
}
/* tslint:enable */
export class Page extends pageCommon.Page {
private _ios: UIViewController;
@ -65,7 +65,7 @@ export class Page extends pageCommon.Page {
(<UIViewController>view.ios).removeFromParentViewController();
(<UIViewController>view.ios).view.removeFromSuperview();
}
}
}
}
get ios(): UIViewController {

View File

@ -1,7 +1,6 @@
import common = require("ui/tool-bar/tool-bar-common");
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
import types = require("utils/types");
// merge the exports of the common file with the exports of this file
declare var exports;

101
utils/containers.d.ts vendored
View File

@ -1,101 +0,0 @@
declare module "utils/containers" {
/**
* An interface used to compare two instances of a same class.
*/
interface IEqualityComparer<T> {
/**
* Compares two instances of a same class.
* @param x - First object to compare.
* @param y - Second object to compare.
* Returns true if objects are equal, otherwise false.
*/
equals(x: T, y: T): boolean;
/**
* Generates an unique hash code for an object instance.
*/
getHashCode(obj: T): number;
}
/**
* Helper class used to sort arrays.
*/
class ArraySortHelper {
/**
* Sorts an array using a comparer function to order elements.
* @param keys - The array which will be sorted.
* @param index - Starting index for sorting
* @param length - How many items to sort.
* @param compareFn - A function that compares two array members.
*/
public static sort<T>(keys: Array<T>, index: number, length: number, compareFn: (a: T, b: T) => number);
}
/**
* Represents a collection of keys and values.
*/
class Dictionary<TKey, TValue> {
/**
* The size of the dictionary.
*/
count: number;
/**
* Creates an instance of a Dictionary.
*/
constructor(comparer: IEqualityComparer<TKey>);
/**
* Iterates through all items and executes a callback function.
* @param callbackfn - A function that will be executed for each item.
*/
public forEach(callbackfn: (key: TKey, value: TValue) => void);
/**
* Clears the entire Dictionary.
*/
public clear(): void;
/**
* Removes the item associated with a given key.
* @param key - A key to remove.
*/
public remove(key: TKey): boolean;
/**
* Returns the item associated with a given key.
* @param key - A lookup key.
*/
public get(key: TKey): TValue;
/**
* Returns if an item associated with a given key exist in the Dictionary.
* @param key - A lookup key.
*/
public has(key: TKey): boolean;
/**
* Associates a value with a key.
* @param key - A key for the value.
* @param value - The real value.
*/
public set(key: TKey, value: TValue): void;
}
/**
* An implementation of IEqualityComparer that works with strings.
*/
class StringComparer implements IEqualityComparer<string> {
/**
* Compares two strings.
* @param x - First string to compare.
* @param y - Second string to compare.
* Returns true if strings are equal, otherwise false.
*/
equals(x: string, y: string): boolean;
/**
* Generates an unique hash code for a string.
*/
getHashCode(str: string): number;
}
}

View File

@ -1,166 +0,0 @@
import definition = require("utils/containers");
export class ArraySortHelper implements definition.ArraySortHelper {
public static sort<T>(keys: Array<T>, index: number, length: number, compareFn: (a: T, b: T) => number) {
if (length < 2) {
return;
}
if (keys.length === length) {
keys.sort(compareFn);
return;
}
var sorted = keys.splice(index, length);
sorted.sort(compareFn);
var args: Array<Object> = [0, index];
keys.splice.apply(keys, args.concat(sorted));
}
}
export class StringComparer implements definition.IEqualityComparer<string> {
public equals(x: string, y: string): boolean {
return x === y;
}
public getHashCode(str: string): number {
var res = 0, len = str.length;
for (var i = 0; i < len; i++) {
res = res * 31 + str.charCodeAt(i);
}
return res;
}
}
interface Entry<TKey, TValue> {
hashCode: number;
next: Entry<TKey, TValue>;
key: TKey;
value: TValue;
}
export class Dictionary<TKey, TValue> implements definition.Dictionary<TKey, TValue> {
private _comparer: definition.IEqualityComparer<TKey>;
private _count: number = 0;
private _entries: Array<Entry<TKey, TValue>>;
private _version: number = 0;
constructor(comparer: definition.IEqualityComparer<TKey>) {
this._comparer = comparer;
this._entries = new Array<Entry<TKey, TValue>>();
}
get count(): number {
return this._count;
}
public forEach(callbackfn: (key: TKey, value: TValue) => void) {
var currentVersion = this._version;
for (var index in this._entries) {
var entry = this._entries[index];
callbackfn(entry.key, entry.value);
if (currentVersion !== this._version) {
throw new Error("Cannot modify Dictionary while enumerating.");
}
}
}
public clear(): void {
if (this.count > 0) {
this._entries = new Array<Entry<TKey, TValue>>();
this._count = 0;
this._version++;
}
}
public remove(key: TKey): boolean {
if (!key) {
throw new Error("key cannot be null/undefined.");
}
var hash = this._comparer.getHashCode(key);
var previousEntry: Entry<TKey, TValue> = null;
for (var entry = this._entries[hash]; entry; entry = entry.next) {
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
if (previousEntry) {
previousEntry.next = entry.next;
}
else {
this._entries[hash] = entry.next;
}
return true;
}
this._count--;
this._version++;
previousEntry = entry;
}
return false;
}
public get(key: TKey): TValue {
var entry = this.findEntry(key);
if (entry) {
return entry.value;
}
return undefined;
}
public has(key: TKey): boolean {
return (this.findEntry(key) ? true : false);
}
public set(key: TKey, value: TValue): void {
if (!key) {
throw new Error("key cannot be null or undefined.");
}
var hash = this._comparer.getHashCode(key);
var lastEntryForHash: Entry<TKey, TValue> = null;
for (var entry = this._entries[hash]; entry; entry = entry.next) {
lastEntryForHash = entry;
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
entry.value = value;
this._version++;
return;
}
}
this._count++;
var newEntry = <Entry<TKey, TValue>>{};
newEntry.hashCode = hash;
newEntry.key = key;
newEntry.value = value;
if (lastEntryForHash) {
lastEntryForHash.next = newEntry;
}
else {
this._entries[hash] = newEntry;
}
this._version++;
}
private findEntry(key: TKey): Entry<TKey, TValue> {
if (!key) {
throw new Error("key cannot be null or undefined.");
}
var hash = this._comparer.getHashCode(key);
for (var entry = this._entries[hash]; entry; entry = entry.next) {
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
return entry;
}
}
return null;
}
}