-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
home.component.ts
142 lines (127 loc) · 5.06 KB
/
home.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
import { DateTimePicker } from "nativescript-datetimepicker";
import { EventData } from "tns-core-modules/data/observable";
import { Button } from "tns-core-modules/ui/button";
@Component({
selector: "Home",
moduleId: module.id,
templateUrl: "./home.component.html",
styleUrls: ['home.component.css']
})
export class HomeComponent implements OnInit {
public dateText: string = "tap to select date";
public timeText: string = "tap to select time";
public dateTimeText: string = "tap to select date and time";
public dateTime1: Date = new Date();
public dateTime2: Date = new Date();
public dateTime3: Date = new Date();
public dateOpacity: number;
public timeOpacity: number;
public dateTimeOpacity: number;
public customOpacity: number;
public dateVisibility: string;
public timeVisibility: string;
public dateTimeVisibility: string;
public customVisibility: string;
private _expandedId: string;
@ViewChild("scrollView", { static: false }) scrollView: ElementRef;
constructor() {
// Use the component constructor to inject providers.
this.expandCollapse(null);
}
ngOnInit(): void {
// Init your component properties here.
}
onPickDateTap(args: EventData): void {
const dateToday = new Date();
const dateTomorrow = new Date(dateToday.getFullYear(), dateToday.getMonth(), dateToday.getDate() + 1);
const dateNextWeek = new Date(dateToday.getFullYear(), dateToday.getMonth(), dateToday.getDate() + 7);
DateTimePicker.pickDate({
context: (<Button>args.object)._context,
date: dateTomorrow,
minDate: dateTomorrow,
maxDate: dateNextWeek,
okButtonText: "OK",
cancelButtonText: "Cancel",
title: "choose date",
locale: "en_GB"
}).then((selectedDate: Date) => {
if (selectedDate) {
this.dateText = this.getFormattedDate(selectedDate);
}
});
}
onPickTimeTap(args: EventData): void {
const dateToday = new Date();
const dateTomorrow = new Date(dateToday.getFullYear(), dateToday.getMonth(), dateToday.getDate() + 1);
dateTomorrow.setHours(8);
dateTomorrow.setMinutes(0);
DateTimePicker.pickTime({
context: (<Button>args.object)._context,
time: dateTomorrow,
okButtonText: "OK",
cancelButtonText: "Cancel",
title: "choose time",
locale: "en_GB",
is24Hours: true
}).then((selectedTime: Date) => {
if (selectedTime) {
this.timeText = this.getFormattedTime(selectedTime);
}
});
}
onPickDateTimeTap(args: EventData): void {
const dateToday = new Date();
DateTimePicker.pickDate({
context: (<Button>args.object)._context,
date: dateToday,
title: "choose date",
locale: "en_GB",
}).then((selectedDate: Date) => {
if (selectedDate) {
DateTimePicker.pickTime({
context: (<Button>args.object)._context,
time: selectedDate,
title: "choose time",
locale: "en_GB",
}).then((selectedDateTime: Date) => {
if (selectedDateTime) {
this.dateTimeText = this.getFormattedDate(selectedDateTime) + ' ' + this.getFormattedTime(selectedDateTime);
}
});
}
});
}
onHeaderTap(args: EventData): void {
const buttonId = (<Button>args.object).id;
const isCurrentlyExpanded = buttonId === this._expandedId;
if (isCurrentlyExpanded) {
this.expandCollapse(null);
} else {
this.expandCollapse(buttonId);
}
this.scrollView.nativeElement.scrollToVerticalOffset(0, false);
}
expandCollapse(expandId: string): void {
this.dateOpacity = expandId === 'date' ? 0.7 : 1;
this.dateVisibility = expandId === 'date' ? 'visible' : 'collapse';
this.timeOpacity = expandId === 'time' ? 0.7 : 1;
this.timeVisibility = expandId === 'time' ? 'visible' : 'collapse';
this.dateTimeOpacity = expandId === 'dateTime' ? 0.7 : 1;
this.dateTimeVisibility = expandId === 'dateTime' ? 'visible' : 'collapse';
this.customOpacity = expandId === 'custom' ? 0.7 : 1;
this.customVisibility = expandId === 'custom' ? 'visible' : 'collapse';
this._expandedId = expandId;
}
getFormattedDate(date: Date): string {
const d = date.getDate();
const m = date.getMonth() + 1;
const y = date.getFullYear();
return (d < 10 ? '0' : '') + d + '.' + (m < 10 ? '0' : '') + m + '.' + y;
}
getFormattedTime(date: Date): string {
const h = date.getHours();
const m = date.getMinutes();
return (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m;
}
}