forked from jonthornton/jquery-timepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepair.js
executable file
·190 lines (143 loc) · 4.81 KB
/
datepair.js
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
$(function() {
$('.datepair input.date').each(function(){
var $this = $(this);
var opts = {};
if ($this.hasClass('future')) {
opts.minDate = new Date();
}
if ($this.hasClass('start') || $this.hasClass('end')) {
opts.onSelect = doDatepair;
}
$this.datepicker(opts);
});
$('.datepair input.time').each(function() {
var $this = $(this);
var opts = { 'showDuration': true, 'timeFormat': 'g:ia' };
if ($this.hasClass('start') || $this.hasClass('end')) {
opts.onSelect = doDatepair;
}
$this.timepicker(opts);
});
$('.datepair').each(initDatepair);
function initDatepair()
{
var container = $(this);
var startDateInput = container.find('input.start.date');
var endDateInput = container.find('input.end.date');
var dateDelta = 0;
if (startDateInput.length && endDateInput.length) {
var startDate = new Date(startDateInput.val());
var endDate = new Date(endDateInput.val());
dateDelta = endDate.getTime() - startDate.getTime();
container.data('dateDelta', dateDelta);
}
var startTimeInput = container.find('input.start.time');
var endTimeInput = container.find('input.end.time');
if (startTimeInput.length && endTimeInput.length) {
var startInt = startTimeInput.timepicker('getSecondsFromMidnight');
var endInt = endTimeInput.timepicker('getSecondsFromMidnight');
container.data('timeDelta', endInt - startInt);
if (dateDelta < 86400000) {
endTimeInput.timepicker('option', 'minTime', startInt);
}
}
}
function doDatepair()
{
var target = $(this);
var container = target.closest('.datepair');
if (target.hasClass('date')) {
updateDatePair(target, container);
} else if (target.hasClass('time')) {
updateTimePair(target, container);
}
}
function updateDatePair(target, container)
{
var start = container.find('input.start.date');
var end = container.find('input.end.date');
if (!start.length || !end.length) {
return;
}
var startDate = new Date(start.val());
var endDate = new Date(end.val());
var oldDelta = container.data('dateDelta');
if (oldDelta && target.hasClass('start')) {
var newEnd = new Date(startDate.getTime()+oldDelta);
end.datepicker('setDate', newEnd);
return;
} else {
var newDelta = endDate.getTime() - startDate.getTime();
if (newDelta < 0) {
newDelta = 0;
if (target.hasClass('start')) {
end.datepicker('setDate', startDate);
} else if (target.hasClass('end')) {
start.datepicker('setDate', endDate);
}
}
if (newDelta < 86400000) {
var startTimeVal = container.find('input.start.time').val();
if (startTimeVal) {
container.find('input.end.time').timepicker('option', {'minTime': startTimeVal});
}
} else {
container.find('input.end.time').timepicker('option', {'minTime': null});
}
container.data('dateDelta', newDelta);
}
}
function updateTimePair(target, container)
{
var start = container.find('input.start.time');
var end = container.find('input.end.time');
if (!start.length || !end.length) {
return;
}
var startInt = start.timepicker('getSecondsFromMidnight');
var endInt = end.timepicker('getSecondsFromMidnight');
var oldDelta = container.data('timeDelta');
var dateDelta = container.data('dateDelta');
if (target.hasClass('start') && (!dateDelta || dateDelta < 86400000)) {
end.timepicker('option', 'minTime', startInt);
}
var endDateAdvance = 0;
var newDelta;
if (oldDelta && target.hasClass('start')) {
// lock the duration and advance the end time
var newEnd = (startInt+oldDelta)%86400;
if (newEnd < 0) {
newEnd += 86400;
}
end.timepicker('setTime', newEnd);
newDelta = newEnd - startInt;
} else if (startInt !== null && endInt !== null) {
newDelta = endInt - startInt;
} else {
return;
}
container.data('timeDelta', newDelta);
if (newDelta < 0 && (!oldDelta || oldDelta > 0)) {
// overnight time span. advance the end date 1 day
var endDateAdvance = 86400000;
} else if (newDelta > 0 && oldDelta < 0) {
// switching from overnight to same-day time span. decrease the end date 1 day
var endDateAdvance = -86400000;
}
var startInput = container.find('.start.date');
var endInput = container.find('.end.date');
if (startInput.val() && !endInput.val()) {
endInput.datepicker('setDate', startInput.val());
dateDelta = 0;
container.data('dateDelta', 0);
}
if (endDateAdvance != 0) {
if (dateDelta || dateDelta === 0) {
var endDate = new Date(endInput.val());
var newEnd = new Date(endDate.getTime() + endDateAdvance);
endInput.datepicker('setDate', newEnd);
container.data('dateDelta', dateDelta + endDateAdvance);
}
}
}
});