|
| 1 | +var _ONE_DAY = 86400000; |
| 2 | +var jq = window.Zepto || window.jQuery; |
| 3 | + |
| 4 | +function simpleExtend(obj1, obj2) { |
| 5 | + var out = obj2 || {}; |
| 6 | + |
| 7 | + for (var i in obj1) { |
| 8 | + if (!(i in out)) { |
| 9 | + out[i] = obj1[i]; |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + return out; |
| 14 | +} |
| 15 | + |
| 16 | +// IE's custom event support is totally borked. |
| 17 | +// Use jQuery if possible |
| 18 | +function triggerSimpleCustomEvent(el, eventName) { |
| 19 | + if (jq) { |
| 20 | + jq(el).trigger(eventName); |
| 21 | + } else { |
| 22 | + var event = document.createEvent('CustomEvent'); |
| 23 | + event.initCustomEvent(eventName, true, true, {}); |
| 24 | + el.dispatchEvent(event); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// el.classList not supported by < IE10 |
| 29 | +// use jQuery if available |
| 30 | +function hasClass(el, className) { |
| 31 | + if (jq) { |
| 32 | + return jq(el).hasClass(className); |
| 33 | + } else { |
| 34 | + return el.classList.contains(className); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function Datepair(container, options) { |
| 39 | + this.dateDelta = null; |
| 40 | + this.timeDelta = null; |
| 41 | + this._defaults = { |
| 42 | + startClass: 'start', |
| 43 | + endClass: 'end', |
| 44 | + timeClass: 'time', |
| 45 | + dateClass: 'date', |
| 46 | + defaultDateDelta: 0, |
| 47 | + defaultTimeDelta: 3600000, |
| 48 | + anchor: 'start', |
| 49 | + |
| 50 | + // defaults for jquery-timepicker; override when using other input widgets |
| 51 | + parseTime: function(input){ |
| 52 | + return jq(input).timepicker('getTime'); |
| 53 | + }, |
| 54 | + updateTime: function(input, dateObj){ |
| 55 | + jq(input).timepicker('setTime', dateObj); |
| 56 | + }, |
| 57 | + setMinTime: function(input, dateObj){ |
| 58 | + jq(input).timepicker('option', 'minTime', dateObj); |
| 59 | + }, |
| 60 | + |
| 61 | + // defaults for bootstrap datepicker; override when using other input widgets |
| 62 | + parseDate: function(input){ |
| 63 | + return input.value && jq(input).datepicker('getDate'); |
| 64 | + }, |
| 65 | + updateDate: function(input, dateObj){ |
| 66 | + jq(input).datepicker('update', dateObj); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + this.container = container; |
| 71 | + this.settings = simpleExtend(this._defaults, options); |
| 72 | + |
| 73 | + this.startDateInput = this.container.querySelector('.'+this.settings.startClass+'.'+this.settings.dateClass); |
| 74 | + this.endDateInput = this.container.querySelector('.'+this.settings.endClass+'.'+this.settings.dateClass); |
| 75 | + this.startTimeInput = this.container.querySelector('.'+this.settings.startClass+'.'+this.settings.timeClass); |
| 76 | + this.endTimeInput = this.container.querySelector('.'+this.settings.endClass+'.'+this.settings.timeClass); |
| 77 | + |
| 78 | + // initialize date and time deltas |
| 79 | + this.refresh(); |
| 80 | + |
| 81 | + // init starts here |
| 82 | + this._bindChangeHandler(); |
| 83 | +} |
| 84 | + |
| 85 | +Datepair.prototype = { |
| 86 | + constructor: Datepair, |
| 87 | + |
| 88 | + option: function(key, value) |
| 89 | + { |
| 90 | + if (typeof key == 'object') { |
| 91 | + this.settings = simpleExtend(this.settings, key); |
| 92 | + |
| 93 | + } else if (typeof key == 'string' && typeof value != 'undefined') { |
| 94 | + this.settings[key] = value; |
| 95 | + |
| 96 | + } else if (typeof key == 'string') { |
| 97 | + return this.settings[key]; |
| 98 | + } |
| 99 | + |
| 100 | + this._updateEndMintime(); |
| 101 | + }, |
| 102 | + |
| 103 | + getTimeDiff: function() |
| 104 | + { |
| 105 | + // due to the fact that times can wrap around, timeDiff for any |
| 106 | + // time-only pair will always be >= 0 |
| 107 | + var delta = this.dateDelta + this.timeDelta; |
| 108 | + if (delta < 0 && (!this.startDateInput || !this.endDateInput) ) { |
| 109 | + delta += _ONE_DAY; |
| 110 | + } |
| 111 | + |
| 112 | + return delta; |
| 113 | + }, |
| 114 | + |
| 115 | + refresh: function() |
| 116 | + { |
| 117 | + if (this.startDateInput && this.startDateInput.value && this.endDateInput && this.endDateInput.value) { |
| 118 | + var startDate = this.settings.parseDate(this.startDateInput); |
| 119 | + var endDate = this.settings.parseDate(this.endDateInput); |
| 120 | + if (startDate && endDate) { |
| 121 | + this.dateDelta = endDate.getTime() - startDate.getTime(); |
| 122 | + } |
| 123 | + } |
| 124 | + if (this.startTimeInput && this.startTimeInput.value && this.endTimeInput && this.endTimeInput.value) { |
| 125 | + var startTime = this.settings.parseTime(this.startTimeInput); |
| 126 | + var endTime = this.settings.parseTime(this.endTimeInput); |
| 127 | + if (startTime && endTime) { |
| 128 | + this.timeDelta = endTime.getTime() - startTime.getTime(); |
| 129 | + this._updateEndMintime(); |
| 130 | + } |
| 131 | + } |
| 132 | + }, |
| 133 | + |
| 134 | + remove: function() |
| 135 | + { |
| 136 | + this._unbindChangeHandler(); |
| 137 | + }, |
| 138 | + |
| 139 | + _bindChangeHandler: function(){ |
| 140 | + // addEventListener doesn't work with synthetic "change" events |
| 141 | + // fired by jQuery's trigger() functioin. If jQuery is present, |
| 142 | + // use that for event binding |
| 143 | + if (jq) { |
| 144 | + jq(this.container).on('change.datepair', jq.proxy(this.handleEvent, this)); |
| 145 | + } else { |
| 146 | + this.container.addEventListener('change', this, false); |
| 147 | + } |
| 148 | + }, |
| 149 | + |
| 150 | + _unbindChangeHandler: function(){ |
| 151 | + if (jq) { |
| 152 | + jq(this.container).off('change.datepair'); |
| 153 | + } else { |
| 154 | + this.container.removeEventListener('change', this, false); |
| 155 | + } |
| 156 | + }, |
| 157 | + |
| 158 | + // This function will be called when passing 'this' to addEventListener |
| 159 | + handleEvent: function(e){ |
| 160 | + // temporarily unbind the change handler to prevent triggering this |
| 161 | + // if we update other inputs |
| 162 | + this._unbindChangeHandler(); |
| 163 | + |
| 164 | + if (hasClass(e.target, this.settings.dateClass)) { |
| 165 | + if (e.target.value != '') { |
| 166 | + this._dateChanged(e.target); |
| 167 | + this._timeChanged(e.target); |
| 168 | + } else { |
| 169 | + this.dateDelta = null; |
| 170 | + } |
| 171 | + |
| 172 | + } else if (hasClass(e.target, this.settings.timeClass)) { |
| 173 | + if (e.target.value != '') { |
| 174 | + this._timeChanged(e.target); |
| 175 | + } else { |
| 176 | + this.timeDelta = null; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + this._validateRanges(); |
| 181 | + this._updateEndMintime(); |
| 182 | + this._bindChangeHandler(); |
| 183 | + }, |
| 184 | + |
| 185 | + _dateChanged: function(target){ |
| 186 | + if (!this.startDateInput || !this.endDateInput) { |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + var startDate = this.settings.parseDate(this.startDateInput); |
| 191 | + var endDate = this.settings.parseDate(this.endDateInput); |
| 192 | + |
| 193 | + if (!startDate || !endDate) { |
| 194 | + if (this.settings.defaultDateDelta !== null) { |
| 195 | + if (startDate) { |
| 196 | + var newEnd = new Date(startDate.getTime() + this.settings.defaultDateDelta * _ONE_DAY); |
| 197 | + this.settings.updateDate(this.endDateInput, newEnd); |
| 198 | + |
| 199 | + } else if (endDate) { |
| 200 | + var newStart = new Date(endDate.getTime() - this.settings.defaultDateDelta * _ONE_DAY); |
| 201 | + this.settings.updateDate(this.startDateInput, newStart); |
| 202 | + } |
| 203 | + |
| 204 | + this.dateDelta = this.settings.defaultDateDelta * _ONE_DAY; |
| 205 | + } else { |
| 206 | + this.dateDelta = null; |
| 207 | + } |
| 208 | + |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + if (this.settings.anchor == 'start' && hasClass(target, this.settings.startClass)) { |
| 213 | + var newDate = new Date(startDate.getTime() + this.dateDelta); |
| 214 | + this.settings.updateDate(this.endDateInput, newDate); |
| 215 | + } else if (this.settings.anchor == 'end' && hasClass(target, this.settings.endClass)) { |
| 216 | + var newDate = new Date(endDate.getTime() - this.dateDelta); |
| 217 | + this.settings.updateDate(this.startDateInput, newDate); |
| 218 | + } else { |
| 219 | + if (endDate < startDate) { |
| 220 | + var otherInput = hasClass(target, this.settings.startClass) ? this.endDateInput : this.startDateInput; |
| 221 | + var selectedDate = this.settings.parseDate(target); |
| 222 | + this.dateDelta = 0; |
| 223 | + this.settings.updateDate(otherInput, selectedDate); |
| 224 | + } else { |
| 225 | + this.dateDelta = endDate.getTime() - startDate.getTime(); |
| 226 | + } |
| 227 | + } |
| 228 | + }, |
| 229 | + |
| 230 | + _timeChanged: function(target){ |
| 231 | + if (!this.startTimeInput || !this.endTimeInput) { |
| 232 | + return; |
| 233 | + } |
| 234 | + |
| 235 | + var startTime = this.settings.parseTime(this.startTimeInput); |
| 236 | + var endTime = this.settings.parseTime(this.endTimeInput); |
| 237 | + |
| 238 | + if (!startTime || !endTime) { |
| 239 | + if (this.settings.defaultTimeDelta !== null) { |
| 240 | + if (startTime) { |
| 241 | + var newEnd = new Date(startTime.getTime() + this.settings.defaultTimeDelta); |
| 242 | + this.settings.updateTime(this.endTimeInput, newEnd); |
| 243 | + } else if (endTime) { |
| 244 | + var newStart = new Date(endTime.getTime() - this.settings.defaultTimeDelta); |
| 245 | + this.settings.updateTime(this.startTimeInput, newStart); |
| 246 | + } |
| 247 | + |
| 248 | + this.timeDelta = this.settings.defaultTimeDelta; |
| 249 | + } else { |
| 250 | + this.timeDelta = null; |
| 251 | + } |
| 252 | + |
| 253 | + return; |
| 254 | + } |
| 255 | + |
| 256 | + if (this.settings.anchor == 'start' && hasClass(target, this.settings.startClass)) { |
| 257 | + var newTime = new Date(startTime.getTime() + this.timeDelta); |
| 258 | + this.settings.updateTime(this.endTimeInput, newTime); |
| 259 | + endTime = this.settings.parseTime(this.endTimeInput); |
| 260 | + |
| 261 | + this._doMidnightRollover(startTime, endTime); |
| 262 | + } else if (this.settings.anchor == 'end' && hasClass(target, this.settings.endClass)) { |
| 263 | + var newTime = new Date(endTime.getTime() - this.timeDelta); |
| 264 | + this.settings.updateTime(this.startTimeInput, newTime); |
| 265 | + startTime = this.settings.parseTime(this.startTimeInput); |
| 266 | + |
| 267 | + this._doMidnightRollover(startTime, endTime); |
| 268 | + } else { |
| 269 | + this._doMidnightRollover(startTime, endTime); |
| 270 | + |
| 271 | + var startDate, endDate; |
| 272 | + if (this.startDateInput && this.endDateInput) { |
| 273 | + startDate = this.settings.parseDate(this.startDateInput); |
| 274 | + endDate = this.settings.parseDate(this.endDateInput); |
| 275 | + } |
| 276 | + |
| 277 | + if ((+startDate == +endDate) && (endTime < startTime)) { |
| 278 | + var thisInput = hasClass(target, this.settings.endClass) ? this.endTimeInput : this.startTimeInput; |
| 279 | + var otherInput = hasClass(target, this.settings.startClass) ? this.endTimeInput : this.startTimeInput; |
| 280 | + var selectedTime = this.settings.parseTime(thisInput); |
| 281 | + this.timeDelta = 0; |
| 282 | + this.settings.updateTime(otherInput, selectedTime); |
| 283 | + } else { |
| 284 | + this.timeDelta = endTime.getTime() - startTime.getTime(); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + |
| 289 | + }, |
| 290 | + |
| 291 | + _doMidnightRollover: function(startTime, endTime) { |
| 292 | + if (!this.startDateInput || !this.endDateInput) { |
| 293 | + return; |
| 294 | + } |
| 295 | + |
| 296 | + var endDate = this.settings.parseDate(this.endDateInput); |
| 297 | + var startDate = this.settings.parseDate(this.startDateInput); |
| 298 | + var newDelta = endTime.getTime() - startTime.getTime(); |
| 299 | + var offset = (endTime < startTime) ? _ONE_DAY : -1 * _ONE_DAY; |
| 300 | + |
| 301 | + if (this.dateDelta !== null |
| 302 | + && this.dateDelta + this.timeDelta <= _ONE_DAY |
| 303 | + && this.dateDelta + newDelta != 0 |
| 304 | + && (offset > 0 || this.dateDelta != 0) |
| 305 | + && ((newDelta >= 0 && this.timeDelta < 0) || (newDelta < 0 && this.timeDelta >= 0))) { |
| 306 | + |
| 307 | + if (this.settings.anchor == 'start') { |
| 308 | + this.settings.updateDate(this.endDateInput, new Date(endDate.getTime() + offset)); |
| 309 | + this._dateChanged(this.endDateInput); |
| 310 | + } else if (this.settings.anchor == 'end') { |
| 311 | + this.settings.updateDate(this.startDateInput, new Date(startDate.getTime() - offset)); |
| 312 | + this._dateChanged(this.startDateInput); |
| 313 | + } |
| 314 | + } |
| 315 | + this.timeDelta = newDelta; |
| 316 | + }, |
| 317 | + |
| 318 | + _updateEndMintime: function(){ |
| 319 | + if (typeof this.settings.setMinTime != 'function') return; |
| 320 | + |
| 321 | + var baseTime = null; |
| 322 | + if (this.settings.anchor == 'start' && (!this.dateDelta || this.dateDelta < _ONE_DAY || (this.timeDelta && this.dateDelta + this.timeDelta < _ONE_DAY))) { |
| 323 | + baseTime = this.settings.parseTime(this.startTimeInput); |
| 324 | + } |
| 325 | + |
| 326 | + this.settings.setMinTime(this.endTimeInput, baseTime); |
| 327 | + }, |
| 328 | + |
| 329 | + _validateRanges: function(){ |
| 330 | + if (this.startTimeInput && this.endTimeInput && this.timeDelta === null) { |
| 331 | + triggerSimpleCustomEvent(this.container, 'rangeIncomplete'); |
| 332 | + return; |
| 333 | + } |
| 334 | + |
| 335 | + if (this.startDateInput && this.endDateInput && this.dateDelta === null) { |
| 336 | + triggerSimpleCustomEvent(this.container, 'rangeIncomplete'); |
| 337 | + return; |
| 338 | + } |
| 339 | + |
| 340 | + // due to the fact that times can wrap around, any time-only pair will be considered valid |
| 341 | + if (!this.startDateInput || !this.endDateInput || this.dateDelta + this.timeDelta >= 0) { |
| 342 | + triggerSimpleCustomEvent(this.container, 'rangeSelected'); |
| 343 | + } else { |
| 344 | + triggerSimpleCustomEvent(this.container, 'rangeError'); |
| 345 | + } |
| 346 | + } |
| 347 | +}; |
0 commit comments