Skip to content

Commit 7838cb8

Browse files
committed
Update jquery-datepair to version 0.2.2
1 parent cedcbc3 commit 7838cb8

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

vendor/assets/javascripts/jquery.datepair.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* datepair.js v0.2.1 - A javascript plugin for intelligently selecting date and time ranges inspired by Google Calendar.
2+
* datepair.js v0.2.2 - A javascript plugin for intelligently selecting date and time ranges inspired by Google Calendar.
33
* Copyright (c) 2014 Jon Thornton - http://jonthornton.github.com/Datepair.js
44
* License: MIT
55
*/
@@ -10,19 +10,19 @@
1010

1111
var _ONE_DAY = 86400000;
1212
var jq = window.Zepto || window.jQuery
13-
13+
1414
function simpleExtend(obj1, obj2) {
1515
var out = obj2 || {};
16-
16+
1717
for (var i in obj1) {
1818
if (!(i in out)) {
1919
out[i] = obj1[i]
2020
}
2121
}
22-
22+
2323
return out;
2424
}
25-
25+
2626
// IE's custom event support is totally borked.
2727
// Use jQuery if possible
2828
function triggerSimpleCustomEvent(el, eventName) {
@@ -34,7 +34,7 @@
3434
el.dispatchEvent(event);
3535
}
3636
}
37-
37+
3838
// el.classList not supported by < IE10
3939
// use jQuery if available
4040
function hasClass(el, className) {
@@ -44,7 +44,7 @@
4444
return el.classList.contains(className);
4545
}
4646
}
47-
47+
4848
function Datepair(container, options) {
4949
this.dateDelta = null;
5050
this.timeDelta = null;
@@ -55,7 +55,7 @@
5555
dateClass: 'date',
5656
defaultDateDelta: 0,
5757
defaultTimeDelta: 3600000,
58-
58+
5959
// defaults for jquery-timepicker; override when using other input widgets
6060
parseTime: function(input){
6161
return jq(input).timepicker('getTime');
@@ -66,7 +66,7 @@
6666
setMinTime: function(input, dateObj){
6767
jq(input).timepicker('option', 'minTime', dateObj);
6868
},
69-
69+
7070
// defaults for bootstrap datepicker; override when using other input widgets
7171
parseDate: function(input){
7272
return jq(input).datepicker('getDate');
@@ -75,22 +75,34 @@
7575
jq(input).datepicker('update', dateObj);
7676
}
7777
};
78-
78+
7979
this.container = container;
8080
this.settings = simpleExtend(this._defaults, options);
81-
81+
8282
this.startDateInput = this.container.querySelector('.'+this.settings.startClass+'.'+this.settings.dateClass);
8383
this.endDateInput = this.container.querySelector('.'+this.settings.endClass+'.'+this.settings.dateClass);
8484
this.startTimeInput = this.container.querySelector('.'+this.settings.startClass+'.'+this.settings.timeClass);
8585
this.endTimeInput = this.container.querySelector('.'+this.settings.endClass+'.'+this.settings.timeClass);
86-
86+
87+
// initialize date and time deltas
88+
if (this.startDateInput && this.startDateInput.value && this.startDateInput && this.endDateInput.value) {
89+
var startDate = this.settings.parseDate(this.startDateInput);
90+
var endDate = this.settings.parseDate(this.endDateInput);
91+
this.dateDelta = endDate.getTime() - startDate.getTime();
92+
}
93+
if (this.startTimeInput && this.startTimeInput.value && this.endTimeInput && this.endTimeInput.value) {
94+
var startTime = this.settings.parseTime(this.startTimeInput);
95+
var endTime = this.settings.parseTime(this.endTimeInput);
96+
this.timeDelta = endTime.getTime() - startTime.getTime();
97+
}
98+
8799
// init starts here
88100
this._bindChangeHandler();
89101
}
90-
102+
91103
Datepair.prototype = {
92104
constructor: Datepair,
93-
105+
94106
_bindChangeHandler: function(){
95107
// addEventListener doesn't work with synthetic "change" events
96108
// fired by jQuery's trigger() functioin. If jQuery is present,
@@ -101,70 +113,70 @@
101113
this.container.addEventListener('change', this, false);
102114
}
103115
},
104-
116+
105117
_unbindChangeHandler: function(){
106118
if (jq) {
107119
jq(this.container).off('change.datepair');
108120
} else {
109121
this.container.removeEventListener('change', this, false);
110122
}
111123
},
112-
124+
113125
// This function will be called when passing 'this' to addEventListener
114126
handleEvent: function(e){
115127
// temporarily unbind the change handler to prevent triggering this
116128
// if we update other inputs
117129
this._unbindChangeHandler();
118-
130+
119131
if (hasClass(e.target, this.settings.dateClass)) {
120132
if (e.target.value != '') {
121133
this._dateChanged(e.target);
122134
} else {
123135
this.dateDelta = null;
124136
}
125-
137+
126138
} else if (hasClass(e.target, this.settings.timeClass)) {
127139
if (e.target.value != '') {
128140
this._timeChanged(e.target);
129141
} else {
130142
this.timeDelta = null;
131143
}
132144
}
133-
145+
134146
this._validateRanges();
135147
this._updateEndMintime()
136148
this._bindChangeHandler();
137149
},
138-
150+
139151
_dateChanged: function(target){
140152
if (!this.startDateInput || !this.endDateInput) {
141153
return
142154
}
143-
155+
144156
if (!this.startDateInput.value || !this.endDateInput.value) {
145157
if (this.settings.defaultDateDelta !== null) {
146158
if (this.startDateInput.value) {
147159
var startDate = this.settings.parseDate(this.startDateInput);
148160
var newEnd = new Date(startDate.getTime() + this.settings.defaultDateDelta * _ONE_DAY);
149161
this.settings.updateDate(this.endDateInput, newEnd);
150-
162+
151163
} else if (this.endDateInput.value) {
152164
var endDate = this.settings.parseDate($endDateInput);
153165
var newStart = new Date(endDate.getTime() - this.settings.defaultDateDelta * _ONE_DAY);
154166
this.settings.updateDate(this.startDateInput, newStart);
155167
}
156-
168+
157169
this.dateDelta = this.settings.defaultDateDelta * _ONE_DAY;
158170
} else {
159171
this.dateDelta = null;
160172
}
161-
173+
162174
return;
163175
}
164-
176+
165177
var startDate = this.settings.parseDate(this.startDateInput);
166178
var endDate = this.settings.parseDate(this.endDateInput);
167-
179+
168180
if (hasClass(target, this.settings.startClass)) {
169181
var newEndDate = new Date(startDate.getTime() + this.dateDelta);
170182
this.settings.updateDate(this.endDateInput, newEndDate);
@@ -177,12 +189,12 @@
177189
}
178190
}
179191
},
180-
192+
181193
_timeChanged: function(target){
182194
if (!this.startTimeInput || !this.endTimeInput) {
183195
return
184196
}
185-
197+
186198
if (!this.startTimeInput.value || !this.endTimeInput.value) {
187199
if (this.settings.defaultTimeDelta !== null) {
188200
if (this.startTimeInput.value) {
@@ -194,56 +206,56 @@
194206
var newStart = new Date(endTime.getTime() - this.settings.defaultTimeDelta);
195207
this.settings.updateTime(this.startTimeInput, newStart);
196208
}
197-
209+
198210
this.timeDelta = this.settings.defaultTimeDelta;
199211
} else {
200212
this.timeDelta = null;
201213
}
202-
214+
203215
return;
204216
}
205-
217+
206218
var startTime = this.settings.parseTime(this.startTimeInput);
207219
var endTime = this.settings.parseTime(this.endTimeInput);
208-
220+
209221
if (hasClass(target, this.settings.startClass)) {
210222
var newEndTime = new Date(startTime.getTime() + this.timeDelta);
211223
this.settings.updateTime(this.endTimeInput, newEndTime);
212224
endTime = this.settings.parseTime(this.endTimeInput);
213225
}
214-
226+
215227
if (this.endDateInput && this.endDateInput.value && this.dateDelta + this.timeDelta < _ONE_DAY && (endTime.getTime() - startTime.getTime()) * this.timeDelta < 0) {
216228
var offset = (endTime < startTime) ? _ONE_DAY : -1 * _ONE_DAY;
217229
var endDate = this.settings.parseDate(this.endDateInput);
218230
this.settings.updateDate(this.endDateInput, new Date(endDate.getTime() + offset));
219231
this._dateChanged(this.endDateInput);
220232
}
221-
233+
222234
this.timeDelta = endTime.getTime() - startTime.getTime();
223235
},
224-
236+
225237
_updateEndMintime: function(){
226238
if (typeof this.settings.setMinTime != 'function') return;
227-
239+
228240
var startTime = null;
229241
if (!this.dateDelta || this.dateDelta < _ONE_DAY || (this.timeDelta && this.dateDelta + this.timeDelta < _ONE_DAY)) {
230242
startTime = this.settings.parseTime(this.startTimeInput);
231243
}
232-
244+
233245
this.settings.setMinTime(this.endTimeInput, startTime);
234246
},
235-
247+
236248
_validateRanges: function(){
237249
if (this.startTimeInput && this.endTimeInput && this.timeDelta === null) {
238250
triggerSimpleCustomEvent(this.container, 'rangeIncomplete');
239251
return;
240252
}
241-
253+
242254
if (this.startDateInput && this.endDateInput && this.dateDelta === null) {
243255
triggerSimpleCustomEvent(this.container, 'rangeIncomplete');
244256
return;
245257
}
246-
258+
247259
if (this.dateDelta + this.timeDelta >= 0) {
248260
triggerSimpleCustomEvent(this.container, 'rangeSelected');
249261
} else {
@@ -257,7 +269,7 @@
257269
}(window, document));
258270

259271
/*!
260-
* datepair.js v0.2.1 - A javascript plugin for intelligently selecting date and time ranges inspired by Google Calendar.
272+
* datepair.js v0.2.2 - A javascript plugin for intelligently selecting date and time ranges inspired by Google Calendar.
261273
* Copyright (c) 2014 Jon Thornton - http://jonthornton.github.com/Datepair.js
262274
* License: MIT
263275
*/

0 commit comments

Comments
 (0)