Added text-field.ios implementation

This commit is contained in:
atanasovg
2014-06-13 15:45:07 +03:00
parent 6c60c83512
commit 7f381c4bbd
7 changed files with 145 additions and 46 deletions

View File

@ -4,7 +4,7 @@ import application = require("application");
export class View extends proxy.ProxyObject { export class View extends proxy.ProxyObject {
private _parent: Panel; private _parent: Panel;
public onInitialized(content: android.content.Context) { public onInitialized(context: android.content.Context) {
// TODO: This is used by Android, rethink this routine // TODO: This is used by Android, rethink this routine
} }
@ -12,6 +12,7 @@ export class View extends proxy.ProxyObject {
// TODO: Temporary // TODO: Temporary
if (application.ios && this.ios) { if (application.ios && this.ios) {
native.addSubview(this.ios); native.addSubview(this.ios);
this.ios.sizeToFit();
} else if (application.android && this.android) { } else if (application.android && this.android) {
native.addView(this.android); native.addView(this.android);
} }
@ -62,6 +63,16 @@ export class Panel extends View {
this._children = new Array<View>(); this._children = new Array<View>();
} }
public onInitialized(context: android.content.Context) {
super.onInitialized(context);
// delegate the initialized event to the children
var i;
for (i = 0; i < this._children.length; i++) {
this._children[i].onInitialized(context);
}
}
public addChild(child: View) { public addChild(child: View) {
// Validate child is not parented // Validate child is not parented
if (child.parent) { if (child.parent) {
@ -96,4 +107,8 @@ export class Panel extends View {
this._children.splice(index, 1); this._children.splice(index, 1);
child.onRemovedFromParent(); child.onRemovedFromParent();
} }
}
export class StackPanel extends Panel {
} }

View File

@ -5,7 +5,7 @@ import application = require("application");
export class Frame extends frameCommon.Frame { export class Frame extends frameCommon.Frame {
public navigateCore(context: any) { public navigateCore(context: any) {
if (this.backStack.length === 0) { if (!this.currentPage) {
// When navigating for the very first time we do not want to start an activity // When navigating for the very first time we do not want to start an activity
// TODO: Revisit/polish this behavior // TODO: Revisit/polish this behavior
return; return;

View File

@ -41,8 +41,8 @@ class AndroidPage implements definition.AndroidPage {
} }
public getActivityExtends(): any { public getActivityExtends(): any {
if (!this._body) { if (!this._activityExtends) {
this.rebuildBody(); this.rebuildExtends();
} }
return this._activityExtends; return this._activityExtends;
@ -53,26 +53,29 @@ class AndroidPage implements definition.AndroidPage {
this._activityExtends = null; this._activityExtends = null;
} }
private rebuildBody() { private rebuildExtends() {
var that = this; // we may have a body set externally
this._body = { if (!this._body) {
onCreate: function () { var that = this;
that._activity = this; this._body = {
this.super.onCreate(null); onCreate: function () {
that._activity = this;
this.super.onCreate(null);
var view = that._ownerPage.contentView; var view = that._ownerPage.contentView;
if (view) { if (view) {
// TODO: Notify the entire visual tree for being initialized // TODO: Notify the entire visual tree for being initialized
view.onInitialized(that._activity); view.onInitialized(that._activity);
that._activity.setContentView(view.android); that._activity.setContentView(view.android);
}
} }
} }
}
if (this._ownerPage.onLoaded) { if (this._ownerPage.onLoaded) {
this._body.onStart = function () { this._body.onStart = function () {
this.super.onStart(); this.super.onStart();
that._ownerPage.onLoaded(); that._ownerPage.onLoaded();
}
} }
} }

View File

@ -1,2 +1,2 @@
declare var module, require; declare var module, require;
module.exports = require("ui/text-input/text-input"); module.exports = require("ui/text-field/text-field");

View File

@ -1,16 +1,58 @@
import observable = require("ui/core/observable"); import observable = require("ui/core/observable");
import view = require("ui/core/view"); import view = require("ui/core/view");
import application = require("application"); import application = require("application");
import definition = require("ui/text-field");
export class TextInput extends view.View { var TEXT = "text";
// this is the name of the property to store text locally until attached to a valid Context
var TEXTPRIVATE = "_text";
export class TextField extends view.View implements definition.TextField {
private _android: android.widget.EditText; private _android: android.widget.EditText;
private static textProperty = "text";
constructor() { constructor() {
super(); super();
}
var context = application.android.currentContext; public onInitialized(context: android.content.Context) {
if (!this._android) {
// TODO: We need to decide whether we will support context switching and if yes - to implement it.
this.createUI(context);
}
}
get android(): android.widget.EditText {
return this._android;
}
get text(): string {
if (!this._android) {
return this[TEXTPRIVATE];
}
return this._android.getText().toString();
}
set text(value: string) {
this.setProperty(TEXT, value);
}
public setNativeProperty(data: observable.PropertyChangeData) {
// TODO: Will this be a gigantic if-else switch?
if (data.propertyName === TEXT) {
if (this._android) {
this._android.setText(data.value, android.widget.TextView.BufferType.EDITABLE);
} else {
this[TEXTPRIVATE] = data.value;
}
} else if (true) {
}
}
private createUI(context: android.content.Context) {
this._android = new android.widget.EditText(context); this._android = new android.widget.EditText(context);
if (this[TEXTPRIVATE]) {
this._android.setText(this[TEXTPRIVATE]);
delete this[TEXTPRIVATE];
}
// TODO: This code is same for Label, extract base class or derive from Label // TODO: This code is same for Label, extract base class or derive from Label
var that = this; var that = this;
@ -20,28 +62,9 @@ export class TextInput extends view.View {
onTextChanged: function (text: string, start: number, before: number, count: number) { onTextChanged: function (text: string, start: number, before: number, count: number) {
}, },
afterTextChanged: function (editable: android.text.IEditable) { afterTextChanged: function (editable: android.text.IEditable) {
that.updateTwoWayBinding(TextInput.textProperty, editable.toString()); that.updateTwoWayBinding(TEXT, editable.toString());
} }
}); });
this._android.addTextChangedListener(textWatcher); this._android.addTextChangedListener(textWatcher);
} }
get android(): android.widget.EditText {
return this._android;
}
get text(): string {
return this._android.getText().toString();
}
set text(value: string) {
this.setProperty(TextInput.textProperty, value);
}
public setNativeProperty(data: observable.PropertyChangeData) {
// TODO: Will this be a gigantic if-else switch?
if (data.propertyName === TextInput.textProperty) {
this._android.setText(data.value, android.widget.TextView.BufferType.EDITABLE);
} else if (true) {
}
}
} }

View File

@ -1 +1,7 @@
 declare module "ui/text-field" {
import view = require("ui/core/view");
export class TextField extends view.View {
public text: string;
}
}

View File

@ -1 +1,53 @@
 import observable = require("ui/core/observable");
import view = require("ui/core/view");
import application = require("application");
import definition = require("ui/text-field");
var TEXT = "text";
export class TextField extends view.View implements definition.TextField {
private _ios: UIKit.UITextField;
private _delegate: any;
constructor() {
super();
this._ios = new UIKit.UITextField();
var that = this;
var protocolImplementation = Foundation.NSObject.extends({}, {}).implements({
protocol: "UITextFieldDelegate",
implementation: {
textFieldDidEndEditing: function (field: UIKit.UITextField) {
that.updateTwoWayBinding(TEXT, field.text);
console.log("TextField end edit");
}
}
});
this._delegate = new protocolImplementation();
this._ios.delegate = this._delegate;
var frame = CoreGraphics.CGRectMake(100, 100, 50, 30);
}
get ios(): UIKit.UITextField {
return this._ios;
}
get text(): string {
return this.ios.text;
}
set text(value: string) {
this.setProperty(TEXT, value);
}
public setNativeProperty(data: observable.PropertyChangeData) {
// TODO: Will this be a gigantic if-else switch?
if (data.propertyName === TEXT) {
this._ios.text = data.value;
this._ios.sizeToFit();
} else if (true) {
}
}
}