~pkunal-parmar/ubuntu-calendar-app/RecurranceField

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.Popups 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem

import "dataService.js" as DataService

Popover {
    id: popover
    property var defaultDate;
    property alias errorText: errorPopupDialog.text;
    property var startDate: new Date()
    property var endDate: new Date()

    Column {
        id: containerLayout
        anchors {
            left: parent.left
            top: parent.top
            right: parent.right
        }

        ListItem.Header { text: i18n.tr("Create event") }
        ListItem.Empty {
            highlightWhenPressed: false
            TextField {
                objectName: "newEventName"
                id: titleEdit
                placeholderText: i18n.tr("Add event name")
                anchors {
                    fill: parent
                    margins: units.gu(1)
                }
            }
        }

        ListItem.Empty {
            id: dateItem

            height: column.height
            width: parent.width

            Column {
                id: column

                anchors {
                    left: parent.left
                    right: parent.right
                }

                Item {
                    width: popover.width
                    height: dateLabel.height
                    Label {
                        id: dateLabel
                        text: Qt.formatDateTime(defaultDate, "ddd, d MMMM yyyy");
                        anchors {
                            left: parent.left
                            right: parent.right
                            margins: units.gu(1)
                        }
                    }
                }

                Component {
                    id: timePicker
                    TimePicker {
                    }
                }

                Item {
                    id: timeContainer
                    width: parent.width
                    height: startTimeItem.height

                    ListItem.Empty {
                        id: startTimeItem
                        anchors.left: timeContainer.left
                        width: units.gu(12)
                        Button {
                            objectName: "startTimeInput"
                            id: startTimeButton
                            text: Qt.formatDateTime(startDate,"hh:mm")
                            anchors {
                                fill: parent
                                margins: units.gu(1)
                            }
                            onClicked: {
                                internal.clearFocus()
                                var popupObj = PopupUtils.open(timePicker);
                                popupObj.accepted.connect(function(startHour, startMinute) {
                                    var newDate = startDate;
                                    newDate.setHours(startHour, startMinute);
                                    startDate = newDate;
                                })
                            }
                        }
                    }

                    Label {
                        id: endTimeLabel
                        text: i18n.tr("to");
                        anchors {
                            horizontalCenter: parent.horizontalCenter;
                            verticalCenter: startTimeItem.verticalCenter;
                        }
                    }

                   ListItem.Empty {
                        id: endTimeItem
                        highlightWhenPressed: false
                        anchors.right: timeContainer.right
                        width: units.gu(12)
                        Button {
                            objectName: "endTimeInput"
                            id: endTimeButton
                            text: Qt.formatDateTime(endDate,"hh:mm")
                            anchors {
                                fill: parent
                                margins: units.gu(1)
                            }
                            onClicked: {
                                internal.clearFocus()
                                var popupObj = PopupUtils.open(timePicker);
                                popupObj.accepted.connect(function(endHour, endMinute) {
                                    var newDate = endDate;
                                    newDate.setHours(endHour, endMinute);
                                    endDate = newDate;
                                })
                            }
                        }
                    }
                }
            }
        }

        ListItem.Header { text: i18n.tr("Location & People") }
        ListItem.Empty {
            highlightWhenPressed: false
            TextField {
                objectName: "eventLocationInput"
                id: locationEdit
                placeholderText: i18n.tr("Add Location")
                anchors {
                    fill: parent
                    margins: units.gu(1)
                }
            }
        }

        ListItem.Empty {
            highlightWhenPressed: false
            TextField {
                objectName: "eventPeopleInput"
                id: personEdit
                placeholderText: i18n.tr("Invite People")
                anchors {
                    fill: parent
                    margins: units.gu(1)
                }
            }
        }

        ListItem.Empty {
            highlightWhenPressed: false
            Dialog {
                id: errorPopupDialog
                title: i18n.tr("Error")
                text: ""
                Button {
                    text: i18n.tr("Ok")
                    onClicked: PopupUtils.close(errorPopupDialog)
                }
            }
            Button {
                objectName: "eventSaveButton"
                text: i18n.tr("Save")
                anchors {
                    fill: parent
                    margins: units.gu(1)
                }

                onClicked: {
                    internal.clearFocus()

                    var error = 0;

                    if (startDate > endDate)
                        error = 2;

                    startDate.setDate(defaultDate.getDate());
                    endDate.setDate(defaultDate.getDate());

                    var event = {
                        title: titleEdit.text,
                        message: null,
                        startTime: startDate.getTime(),
                        endTime: endDate.getTime()
                    }

                    if (!error) {
                        DataService.addEvent(event);
                        PopupUtils.close(popover);
                    } else {
                        errorText = i18n.tr("End time can't be before start time");
                        errorPopupDialog.show();
                    }

                    error = 0;
                }
            }
        }
    }

    QtObject {
        id: internal

        function clearFocus() {
            Qt.inputMethod.hide()
            titleEdit.focus = false
            locationEdit.focus = false
            personEdit.focus = false
        }
    }
}