Files
Alexander Vakrilov cc97a16800 feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core

* chore: preparing compat generate script

* chore: add missing definitions

* chore: no need for http-request to be private

* chore: packages chore

* test: generate tests for tns-core-modules

* chore: add anroid module for consistency

* chore: add .npmignore

* chore: added privateModulesWhitelist

* chore(webpack): added bundle-entry-points

* chore: scripts

* chore: tests changed to use @ns/core

* test: add scoped-packages test project

* test: fix types

* test: update test project

* chore: build scripts

* chore: update build script

* chore: npm scripts cleanup

* chore: make the compat pgk work with old wp config

* test: generate diff friendly tests

* chore: create barrel exports

* chore: move files after rebase

* chore: typedoc config

* chore: compat mode

* chore: review of barrels

* chore: remove tns-core-modules import after rebase

* chore: dev workflow setup

* chore: update developer-workflow

* docs: experiment with API extractor

* chore: api-extractor and barrel exports

* chore: api-extractor configs

* chore: generate d.ts rollup with api-extractor

* refactor: move methods inside Frame

* chore: fic tests to use Frame static methods

* refactor: create Builder class

* refactor: use Builder class in tests

* refactor: include Style in ui barrel

* chore: separate compat build script

* chore: fix tslint errors

* chore: update NATIVESCRIPT_CORE_ARGS

* chore: fix compat pack

* chore: fix ui-test-app build with linked modules

* chore: Application, ApplicationSettings, Connectivity and Http

* chore: export Trace, Profiling and Utils

* refactor: Static create methods for ImageSource

* chore: fix deprecated usages of ImageSource

* chore: move Span and FormattedString to ui

* chore: add events-args and ImageSource to index files

* chore: check for CLI >= 6.2 when building for IOS

* chore: update travis build

* chore: copy Pod file to compat package

* chore: update error msg ui-tests-app

* refactor: Apply suggestions from code review

Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com>

* chore: typings and refs

* chore: add missing d.ts files for public API

* chore: adress code review FB

* chore: update api-report

* chore: dev-workflow for other apps

* chore: api update

* chore: update api-report
2019-10-17 00:45:33 +03:00

265 lines
8.7 KiB
TypeScript

import {
TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty,
Length, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, _updateCharactersInRangeReplacementString, Color, layout
} from "./text-field-common";
import { profile } from "../../profiling";
export * from "./text-field-common";
const zeroLength: Length = {
value: 0,
unit: "px"
};
class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
public static ObjCProtocols = [UITextFieldDelegate];
private _owner: WeakRef<TextField>;
private firstEdit: boolean;
public static initWithOwner(owner: WeakRef<TextField>): UITextFieldDelegateImpl {
const delegate = <UITextFieldDelegateImpl>UITextFieldDelegateImpl.new();
delegate._owner = owner;
return delegate;
}
public textFieldShouldBeginEditing(textField: UITextField): boolean {
this.firstEdit = true;
const owner = this._owner.get();
if (owner) {
return owner.editable;
}
return true;
}
public textFieldDidBeginEditing(textField: UITextField): void {
const owner = this._owner.get();
if (owner) {
owner.notify({ eventName: TextField.focusEvent, object: owner });
}
}
public textFieldDidEndEditing(textField: UITextField) {
const owner = this._owner.get();
if (owner) {
if (owner.updateTextTrigger === "focusLost") {
textProperty.nativeValueChange(owner, textField.text);
}
owner.dismissSoftInput();
}
}
public textFieldShouldClear(textField: UITextField) {
this.firstEdit = false;
const owner = this._owner.get();
if (owner) {
textProperty.nativeValueChange(owner, "");
}
return true;
}
public textFieldShouldReturn(textField: UITextField): boolean {
// Called when the user presses the return button.
const owner = this._owner.get();
if (owner) {
owner.dismissSoftInput();
owner.notify({ eventName: TextField.returnPressEvent, object: owner });
}
return true;
}
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
const owner = this._owner.get();
if (owner) {
const delta = replacementString.length - range.length;
if (delta > 0) {
if (textField.text.length + delta > owner.maxLength) {
return false;
}
}
if (owner.updateTextTrigger === "textChanged") {
if (textField.secureTextEntry && this.firstEdit) {
textProperty.nativeValueChange(owner, replacementString);
}
else {
if (range.location <= textField.text.length) {
const newText = NSString.stringWithString(textField.text).stringByReplacingCharactersInRangeWithString(range, replacementString);
textProperty.nativeValueChange(owner, newText);
}
}
}
if (owner.formattedText) {
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
}
}
this.firstEdit = false;
return true;
}
}
class UITextFieldImpl extends UITextField {
private _owner: WeakRef<TextField>;
public static initWithOwner(owner: WeakRef<TextField>): UITextFieldImpl {
const handler = <UITextFieldImpl>UITextFieldImpl.new();
handler._owner = owner;
return handler;
}
private _getTextRectForBounds(bounds: CGRect): CGRect {
const owner = this._owner ? this._owner.get() : null;
if (!owner) {
return bounds;
}
const size = bounds.size;
const x = layout.toDeviceIndependentPixels(owner.effectiveBorderLeftWidth + owner.effectivePaddingLeft);
const y = layout.toDeviceIndependentPixels(owner.effectiveBorderTopWidth + owner.effectivePaddingTop);
const width = layout.toDeviceIndependentPixels(layout.toDevicePixels(size.width) - (owner.effectiveBorderLeftWidth + owner.effectivePaddingLeft + owner.effectivePaddingRight + owner.effectiveBorderRightWidth));
const height = layout.toDeviceIndependentPixels(layout.toDevicePixels(size.height) - (owner.effectiveBorderTopWidth + owner.effectivePaddingTop + owner.effectivePaddingBottom + owner.effectiveBorderBottomWidth));
return CGRectMake(x, y, width, height);
}
public textRectForBounds(bounds: CGRect): CGRect {
return this._getTextRectForBounds(bounds);
}
public editingRectForBounds(bounds: CGRect): CGRect {
return this._getTextRectForBounds(bounds);
}
}
export class TextField extends TextFieldBase {
nativeViewProtected: UITextField;
private _delegate: UITextFieldDelegateImpl;
createNativeView() {
return UITextFieldImpl.initWithOwner(new WeakRef(this));
}
initNativeView() {
super.initNativeView();
this._delegate = UITextFieldDelegateImpl.initWithOwner(new WeakRef(this));
}
disposeNativeView() {
this._delegate = null;
super.disposeNativeView();
}
@profile
public onLoaded() {
super.onLoaded();
this.ios.delegate = this._delegate;
}
public onUnloaded() {
this.ios.delegate = null;
super.onUnloaded();
}
get ios(): UITextField {
return this.nativeViewProtected;
}
[hintProperty.getDefault](): string {
return this.nativeTextViewProtected.placeholder;
}
[hintProperty.setNative](value: string) {
this._updateAttributedPlaceholder();
}
[secureProperty.getDefault](): boolean {
return this.nativeTextViewProtected.secureTextEntry;
}
[secureProperty.setNative](value: boolean) {
this.nativeTextViewProtected.secureTextEntry = value;
}
[colorProperty.getDefault](): { textColor: UIColor, tintColor: UIColor } {
return {
textColor: this.nativeTextViewProtected.textColor,
tintColor: this.nativeTextViewProtected.tintColor
};
}
[colorProperty.setNative](value: Color | { textColor: UIColor, tintColor: UIColor }) {
if (value instanceof Color) {
let color = value instanceof Color ? value.ios : value;
this.nativeTextViewProtected.textColor = color;
this.nativeTextViewProtected.tintColor = color;
} else {
this.nativeTextViewProtected.textColor = value.textColor;
this.nativeTextViewProtected.tintColor = value.tintColor;
}
}
[placeholderColorProperty.getDefault](): UIColor {
return null;
}
[placeholderColorProperty.setNative](value: UIColor | Color) {
this._updateAttributedPlaceholder();
}
_updateAttributedPlaceholder(): void {
let stringValue = this.hint;
if (stringValue === null || stringValue === void 0) {
stringValue = "";
} else {
stringValue = stringValue + "";
}
if (stringValue === "") {
// we do not use empty string since initWithStringAttributes does not return proper value and
// nativeView.attributedPlaceholder will be null
stringValue = " ";
}
const attributes: any = {};
if (this.style.placeholderColor) {
attributes[NSForegroundColorAttributeName] = this.style.placeholderColor.ios;
}
const attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(stringValue, attributes);
this.nativeTextViewProtected.attributedPlaceholder = attributedPlaceholder;
}
[paddingTopProperty.getDefault](): Length {
return zeroLength;
}
[paddingTopProperty.setNative](value: Length) {
// Padding is realized via UITextFieldImpl.textRectForBounds method
}
[paddingRightProperty.getDefault](): Length {
return zeroLength;
}
[paddingRightProperty.setNative](value: Length) {
// Padding is realized via UITextFieldImpl.textRectForBounds method
}
[paddingBottomProperty.getDefault](): Length {
return zeroLength;
}
[paddingBottomProperty.setNative](value: Length) {
// Padding is realized via UITextFieldImpl.textRectForBounds method
}
[paddingLeftProperty.getDefault](): Length {
return zeroLength;
}
[paddingLeftProperty.setNative](value: Length) {
// Padding is realized via UITextFieldImpl.textRectForBounds method
}
}