mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: format
This commit is contained in:
@@ -1,130 +1,168 @@
|
||||
import {
|
||||
DatePickerBase, yearProperty, monthProperty, dayProperty,
|
||||
dateProperty, maxDateProperty, minDateProperty
|
||||
DatePickerBase,
|
||||
yearProperty,
|
||||
monthProperty,
|
||||
dayProperty,
|
||||
dateProperty,
|
||||
maxDateProperty,
|
||||
minDateProperty,
|
||||
} from "./date-picker-common";
|
||||
import { colorProperty } from '../styling/style-properties'
|
||||
import { Color } from '../../color'
|
||||
import { colorProperty } from "../styling/style-properties";
|
||||
import { Color } from "../../color";
|
||||
|
||||
export * from "./date-picker-common";
|
||||
|
||||
export class DatePicker extends DatePickerBase {
|
||||
private _changeHandler: NSObject;
|
||||
public nativeViewProtected: UIDatePicker;
|
||||
private _changeHandler: NSObject;
|
||||
public nativeViewProtected: UIDatePicker;
|
||||
|
||||
public createNativeView() {
|
||||
const picker = UIDatePicker.new();
|
||||
picker.datePickerMode = UIDatePickerMode.Date;
|
||||
public createNativeView() {
|
||||
const picker = UIDatePicker.new();
|
||||
picker.datePickerMode = UIDatePickerMode.Date;
|
||||
|
||||
return picker;
|
||||
}
|
||||
return picker;
|
||||
}
|
||||
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
this._changeHandler = UIDatePickerChangeHandlerImpl.initWithOwner(new WeakRef(this));
|
||||
nativeView.addTargetActionForControlEvents(this._changeHandler, "valueChanged", UIControlEvents.ValueChanged);
|
||||
}
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
this._changeHandler = UIDatePickerChangeHandlerImpl.initWithOwner(
|
||||
new WeakRef(this)
|
||||
);
|
||||
nativeView.addTargetActionForControlEvents(
|
||||
this._changeHandler,
|
||||
"valueChanged",
|
||||
UIControlEvents.ValueChanged
|
||||
);
|
||||
}
|
||||
|
||||
public disposeNativeView() {
|
||||
this._changeHandler = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
public disposeNativeView() {
|
||||
this._changeHandler = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
|
||||
get ios(): UIDatePicker {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
get ios(): UIDatePicker {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
|
||||
[yearProperty.setNative](value: number) {
|
||||
this.date = new Date(value, this.month - 1, this.day);
|
||||
}
|
||||
[yearProperty.setNative](value: number) {
|
||||
this.date = new Date(value, this.month - 1, this.day);
|
||||
}
|
||||
|
||||
[monthProperty.setNative](value: number) {
|
||||
this.date = new Date(this.year, value - 1, this.day);
|
||||
}
|
||||
[monthProperty.setNative](value: number) {
|
||||
this.date = new Date(this.year, value - 1, this.day);
|
||||
}
|
||||
|
||||
[dayProperty.setNative](value: number) {
|
||||
this.date = new Date(this.year, this.month - 1, value);
|
||||
}
|
||||
[dayProperty.setNative](value: number) {
|
||||
this.date = new Date(this.year, this.month - 1, value);
|
||||
}
|
||||
|
||||
[dateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
|
||||
comps.year = value.getFullYear();
|
||||
comps.month = value.getMonth() + 1;
|
||||
comps.day = value.getDate();
|
||||
this.year = comps.year;
|
||||
this.month = comps.month;
|
||||
this.day = comps.day;
|
||||
picker.setDateAnimated(NSCalendar.currentCalendar.dateFromComponents(comps), false);
|
||||
}
|
||||
[dateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const comps = NSCalendar.currentCalendar.componentsFromDate(
|
||||
NSCalendarUnit.CalendarUnitYear |
|
||||
NSCalendarUnit.CalendarUnitMonth |
|
||||
NSCalendarUnit.CalendarUnitDay,
|
||||
picker.date
|
||||
);
|
||||
comps.year = value.getFullYear();
|
||||
comps.month = value.getMonth() + 1;
|
||||
comps.day = value.getDate();
|
||||
this.year = comps.year;
|
||||
this.month = comps.month;
|
||||
this.day = comps.day;
|
||||
picker.setDateAnimated(
|
||||
NSCalendar.currentCalendar.dateFromComponents(comps),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
[maxDateProperty.getDefault](): Date {
|
||||
return this.nativeViewProtected.maximumDate;
|
||||
}
|
||||
[maxDateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
|
||||
picker.maximumDate = <any>nsDate;
|
||||
}
|
||||
[maxDateProperty.getDefault](): Date {
|
||||
return this.nativeViewProtected.maximumDate;
|
||||
}
|
||||
[maxDateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const nsDate = NSDate.dateWithTimeIntervalSince1970(
|
||||
value.getTime() / 1000
|
||||
);
|
||||
picker.maximumDate = <any>nsDate;
|
||||
}
|
||||
|
||||
[minDateProperty.getDefault](): Date {
|
||||
return this.nativeViewProtected.minimumDate;
|
||||
}
|
||||
[minDateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
|
||||
picker.minimumDate = <any>nsDate;
|
||||
}
|
||||
[minDateProperty.getDefault](): Date {
|
||||
return this.nativeViewProtected.minimumDate;
|
||||
}
|
||||
[minDateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const nsDate = NSDate.dateWithTimeIntervalSince1970(
|
||||
value.getTime() / 1000
|
||||
);
|
||||
picker.minimumDate = <any>nsDate;
|
||||
}
|
||||
|
||||
[colorProperty.getDefault](): UIColor {
|
||||
return this.nativeViewProtected.valueForKey("textColor");
|
||||
}
|
||||
[colorProperty.setNative](value: Color | UIColor) {
|
||||
const picker = this.nativeViewProtected;
|
||||
picker.setValueForKey(value instanceof Color ? value.ios : value, "textColor");
|
||||
}
|
||||
[colorProperty.getDefault](): UIColor {
|
||||
return this.nativeViewProtected.valueForKey("textColor");
|
||||
}
|
||||
[colorProperty.setNative](value: Color | UIColor) {
|
||||
const picker = this.nativeViewProtected;
|
||||
picker.setValueForKey(
|
||||
value instanceof Color ? value.ios : value,
|
||||
"textColor"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UIDatePickerChangeHandlerImpl extends NSObject {
|
||||
private _owner: WeakRef<DatePicker>;
|
||||
private _owner: WeakRef<DatePicker>;
|
||||
|
||||
public static initWithOwner(owner: WeakRef<DatePicker>): UIDatePickerChangeHandlerImpl {
|
||||
const impl = <UIDatePickerChangeHandlerImpl>UIDatePickerChangeHandlerImpl.new();
|
||||
impl._owner = owner;
|
||||
public static initWithOwner(
|
||||
owner: WeakRef<DatePicker>
|
||||
): UIDatePickerChangeHandlerImpl {
|
||||
const impl = <UIDatePickerChangeHandlerImpl>(
|
||||
UIDatePickerChangeHandlerImpl.new()
|
||||
);
|
||||
impl._owner = owner;
|
||||
|
||||
return impl;
|
||||
}
|
||||
return impl;
|
||||
}
|
||||
|
||||
public valueChanged(sender: UIDatePicker) {
|
||||
const comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, sender.date);
|
||||
public valueChanged(sender: UIDatePicker) {
|
||||
const comps = NSCalendar.currentCalendar.componentsFromDate(
|
||||
NSCalendarUnit.CalendarUnitYear |
|
||||
NSCalendarUnit.CalendarUnitMonth |
|
||||
NSCalendarUnit.CalendarUnitDay,
|
||||
sender.date
|
||||
);
|
||||
|
||||
const owner = this._owner.get();
|
||||
if (!owner) {
|
||||
return;
|
||||
}
|
||||
const owner = this._owner.get();
|
||||
if (!owner) {
|
||||
return;
|
||||
}
|
||||
|
||||
let dateChanged = false;
|
||||
if (comps.year !== owner.year) {
|
||||
yearProperty.nativeValueChange(owner, comps.year);
|
||||
dateChanged = true;
|
||||
}
|
||||
let dateChanged = false;
|
||||
if (comps.year !== owner.year) {
|
||||
yearProperty.nativeValueChange(owner, comps.year);
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
if (comps.month !== owner.month) {
|
||||
monthProperty.nativeValueChange(owner, comps.month);
|
||||
dateChanged = true;
|
||||
}
|
||||
if (comps.month !== owner.month) {
|
||||
monthProperty.nativeValueChange(owner, comps.month);
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
if (comps.day !== owner.day) {
|
||||
dayProperty.nativeValueChange(owner, comps.day);
|
||||
dateChanged = true;
|
||||
}
|
||||
if (comps.day !== owner.day) {
|
||||
dayProperty.nativeValueChange(owner, comps.day);
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
if (dateChanged) {
|
||||
dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day));
|
||||
}
|
||||
}
|
||||
if (dateChanged) {
|
||||
dateProperty.nativeValueChange(
|
||||
owner,
|
||||
new Date(comps.year, comps.month - 1, comps.day)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
"valueChanged": { returns: interop.types.void, params: [UIDatePicker] }
|
||||
};
|
||||
public static ObjCExposedMethods = {
|
||||
valueChanged: { returns: interop.types.void, params: [UIDatePicker] },
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user