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

231 lines
6.6 KiB
TypeScript

import { Font } from "../styling/font";
import {
SearchBarBase, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, fontInternalProperty,
textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty, isEnabledProperty
} from "./search-bar-common";
import { ios as iosUtils } from "../../utils/utils";
export * from "./search-bar-common";
const majorVersion = iosUtils.MajorVersion;
class UISearchBarDelegateImpl extends NSObject implements UISearchBarDelegate {
public static ObjCProtocols = [UISearchBarDelegate];
private _owner: WeakRef<SearchBar>;
public static initWithOwner(owner: WeakRef<SearchBar>): UISearchBarDelegateImpl {
let delegate = <UISearchBarDelegateImpl>UISearchBarDelegateImpl.new();
delegate._owner = owner;
return delegate;
}
public searchBarTextDidChange(searchBar: UISearchBar, searchText: string) {
let owner = this._owner.get();
if (!owner) {
return;
}
textProperty.nativeValueChange(owner, searchText);
// This code is needed since sometimes searchBarCancelButtonClicked is not called!
if (searchText === "") {
owner._emit(SearchBarBase.clearEvent);
}
}
public searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder();
let owner = this._owner.get();
if (!owner) {
return;
}
owner._emit(SearchBarBase.clearEvent);
}
public searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder();
let owner = this._owner.get();
if (!owner) {
return;
}
owner._emit(SearchBarBase.submitEvent);
}
}
class UISearchBarImpl extends UISearchBar {
sizeThatFits(size: CGSize): CGSize {
// iOS11 SDK does not support passing sizeThatFits(...) non-finite width value;
// iOS layout system will take care to size the element properly when passed 0
if (majorVersion >= 11 && size.width === Number.POSITIVE_INFINITY) {
size.width = 0;
}
return super.sizeThatFits(size);
}
}
export class SearchBar extends SearchBarBase {
nativeViewProtected: UISearchBar;
private _delegate;
private __textField: UITextField;
createNativeView() {
return UISearchBarImpl.new();
}
initNativeView() {
super.initNativeView();
this._delegate = UISearchBarDelegateImpl.initWithOwner(new WeakRef(this));
}
disposeNativeView() {
this._delegate = null;
super.disposeNativeView();
}
public onLoaded() {
super.onLoaded();
this.ios.delegate = this._delegate;
}
public onUnloaded() {
this.ios.delegate = null;
super.onUnloaded();
}
public dismissSoftInput() {
(<UIResponder>this.ios).resignFirstResponder();
}
get ios(): UISearchBar {
return this.nativeViewProtected;
}
get _textField(): UITextField {
if (!this.__textField) {
this.__textField = this.ios.valueForKey("searchField");
}
return this.__textField;
}
[isEnabledProperty.setNative](value: boolean) {
const nativeView = this.nativeViewProtected;
if (nativeView instanceof UIControl) {
nativeView.enabled = value;
}
const textField = this._textField;
if (textField) {
textField.enabled = value;
}
}
[backgroundColorProperty.getDefault](): UIColor {
return this.ios.barTintColor;
}
[backgroundColorProperty.setNative](value: UIColor | Color) {
let color: UIColor = value instanceof Color ? value.ios : value;
this.ios.barTintColor = color;
}
[colorProperty.getDefault](): UIColor {
let sf = this._textField;
if (sf) {
return sf.textColor;
}
return null;
}
[colorProperty.setNative](value: UIColor | Color) {
let sf = this._textField;
let color = value instanceof Color ? value.ios : value;
if (sf) {
sf.textColor = color;
sf.tintColor = color;
}
}
[fontInternalProperty.getDefault](): UIFont {
let sf = this._textField;
return sf ? sf.font : null;
}
[fontInternalProperty.setNative](value: UIFont | Font) {
let sf = this._textField;
if (sf) {
sf.font = value instanceof Font ? value.getUIFont(sf.font) : value;
}
}
[backgroundInternalProperty.getDefault](): any {
return null;
}
[backgroundInternalProperty.setNative](value: any) {
//
}
[textProperty.getDefault](): string {
return "";
}
[textProperty.setNative](value: string) {
const text = (value === null || value === undefined) ? "" : value.toString();
this.ios.text = text;
}
[hintProperty.getDefault](): string {
return "";
}
[hintProperty.setNative](value: string) {
this._updateAttributedPlaceholder();
}
[textFieldBackgroundColorProperty.getDefault](): UIColor {
const textField = this._textField;
if (textField) {
return textField.backgroundColor;
}
return null;
}
[textFieldBackgroundColorProperty.setNative](value: Color | UIColor) {
const color = value instanceof Color ? value.ios : value;
const textField = this._textField;
if (textField) {
textField.backgroundColor = color;
}
}
[textFieldHintColorProperty.getDefault](): UIColor {
return null;
}
[textFieldHintColorProperty.setNative](value: Color | UIColor) {
this._updateAttributedPlaceholder();
}
// Very similar to text-field.ios.ts implementation. Maybe unify APIs and base classes?
_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.textFieldHintColor) {
attributes[NSForegroundColorAttributeName] = this.textFieldHintColor.ios;
}
const attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(stringValue, attributes);
this._textField.attributedPlaceholder = attributedPlaceholder;
}
}