vendor: 66,262 bytes, lines 1-1582
1/** 2 * @version: 3.1 3 * @author: Dan Grossman http://www.dangrossman.info/ 4 * @copyright: Copyright (c) 2012-2019 Dan Grossman. All rights reserved. 5 * @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php 6 * @website: http://www.daterangepicker.com/ 7 */ 8// Following the UMD template https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js 9(function (root, factory) { 10 if(typeof define === 'function' && define.amd) { 11 // AMD. Make globaly available as well 12 define(['moment', 'jquery'], function (moment, jquery) { 13 if(!jquery.fn) jquery.fn = {}; // webpack server rendering 14 if(typeof moment !== 'function' && moment.hasOwnProperty('default')) moment = moment['default'] 15 return factory(moment, jquery); 16 }); 17 } else if(typeof module === 'object' && module.exports) { 18 // Node / Browserify 19 //isomorphic issue 20 var jQuery = (typeof window != 'undefined') ? window.jQuery : undefined; 21 if(!jQuery) { 22 jQuery = require('jquery'); 23 if(!jQuery.fn) jQuery.fn = {}; 24 } 25 var moment = (typeof window != 'undefined' && typeof window.moment != 'undefined') ? window.moment : require('moment'); 26 module.exports = factory(moment, jQuery); 27 } else { 28 // Browser globals 29 root.daterangepicker = factory(root.moment, root.jQuery); 30 } 31}(this, function(moment, $) { 32 var DateRangePicker = function(element, options, cb) { 33 34 //default settings for options 35 this.parentEl = 'body'; 36 this.element = $(element); 37 this.startDate = moment().startOf('day'); 38 this.endDate = moment().endOf('day'); 39 this.minDate = false; 40 this.maxDate = false; 41 this.maxSpan = false; 42 this.autoApply = false; 43 this.singleDatePicker = false; 44 this.singleCalendar = false; 45 this.showDropdowns = false; 46 this.minYear = moment().subtract(100, 'year').format('YYYY'); 47 this.maxYear = moment().add(100, 'year').format('YYYY'); 48 this.showWeekNumbers = false; 49 this.showISOWeekNumbers = false; 50 this.showCustomRangeLabel = true; 51 this.timePicker = false; 52 this.timePicker24Hour = false; 53 this.timePickerIncrement = 1; 54 this.timePickerSeconds = false; 55 this.linkedCalendars = true; 56 this.autoUpdateInput = true; 57 this.alwaysShowCalendars = false; 58 this.ranges = {}; 59 60 this.opens = 'right'; 61 if(this.element.hasClass('pull-right')) 62 this.opens = 'left'; 63 64 this.drops = 'down'; 65 if(this.element.hasClass('dropup')) 66 this.drops = 'up'; 67 68 this.buttonClasses = 'btn btn-sm'; 69 this.applyButtonClasses = 'btn-primary'; 70 this.cancelButtonClasses = 'btn-default'; 71 72 this.locale = { 73 direction: 'ltr', 74 format: moment.localeData().longDateFormat('L'), 75 separator: ' - ', 76 applyLabel: 'Apply', 77 cancelLabel: 'Cancel', 78 weekLabel: 'W', 79 customRangeLabel: 'Custom Range', 80 daysOfWeek: moment.weekdaysMin(), 81 monthNames: moment.monthsShort(), 82 firstDay: moment.localeData().firstDayOfWeek() 83 }; 84 85 this.callback = function() { }; 86 87 //some state information 88 this.isShowing = false; 89 this.leftCalendar = {}; 90 this.rightCalendar = {}; 91 92 //custom options from user 93 if(typeof options !== 'object' || options === null) 94 options = {}; 95 96 //allow setting options with data attributes 97 //data-api options will be overwritten with custom javascript options 98 options = $.extend(this.element.data(), options); 99 100 //html template for the picker UI 101 if(typeof options.template !== 'string' && !(options.template instanceof $)) 102 options.template = 103 '<div class="daterangepicker">' + 104 '<div class="ranges"></div>' + 105 '<div class="drp-calendar left">' + 106 '<div class="calendar-table"></div>' + 107 '<div class="calendar-time"></div>' + 108 '</div>' + 109 '<div class="drp-calendar right">' + 110 '<div class="calendar-table"></div>' + 111 '<div class="calendar-time"></div>' + 112 '</div>' + 113 '<div class="drp-buttons">' + 114 '<span class="drp-selected"></span>' + 115 '<button class="cancelBtn" type="button"></button>' + 116 '<button class="applyBtn" disabled="disabled" type="button"></button> ' + 117 '</div>' + 118 '</div>'; 119 120 this.parentEl = (options.parentEl && $(options.parentEl).length) ? $(options.parentEl) : $(this.parentEl); 121 this.container = $(options.template).appendTo(this.parentEl); 122 123 // 124 // handle all the possible options overriding defaults 125 // 126 127 if(typeof options.locale === 'object') { 128 129 if(typeof options.locale.direction === 'string') 130 this.locale.direction = options.locale.direction; 131 132 if(typeof options.locale.format === 'string') 133 this.locale.format = options.locale.format; 134 135 if(typeof options.locale.separator === 'string') 136 this.locale.separator = options.locale.separator; 137 138 if(typeof options.locale.daysOfWeek === 'object') 139 this.locale.daysOfWeek = options.locale.daysOfWeek.slice(); 140 141 if(typeof options.locale.monthNames === 'object') 142 this.locale.monthNames = options.locale.monthNames.slice(); 143 144 if(typeof options.locale.firstDay === 'number') 145 this.locale.firstDay = options.locale.firstDay; 146 147 if(typeof options.locale.applyLabel === 'string') 148 this.locale.applyLabel = options.locale.applyLabel; 149 150 if(typeof options.locale.cancelLabel === 'string') 151 this.locale.cancelLabel = options.locale.cancelLabel; 152 153 if(typeof options.locale.weekLabel === 'string') 154 this.locale.weekLabel = options.locale.weekLabel; 155 156 if(typeof options.locale.customRangeLabel === 'string'){ 157 //Support unicode chars in the custom range name. 158 var elem = document.createElement('textarea'); 159 elem.innerHTML = options.locale.customRangeLabel; 160 var rangeHtml = elem.value; 161 this.locale.customRangeLabel = rangeHtml; 162 } 163 } 164 this.container.addClass(this.locale.direction); 165 166 if(typeof options.startDate === 'string') 167 this.startDate = moment(options.startDate, this.locale.format); 168 169 if(typeof options.endDate === 'string') 170 this.endDate = moment(options.endDate, this.locale.format); 171 172 if(typeof options.minDate === 'string') 173 this.minDate = moment(options.minDate, this.locale.format); 174 175 if(typeof options.maxDate === 'string') 176 this.maxDate = moment(options.maxDate, this.locale.format); 177 178 if(typeof options.startDate === 'object') 179 this.startDate = moment(options.startDate); 180 181 if(typeof options.endDate === 'object') 182 this.endDate = moment(options.endDate); 183 184 if(typeof options.minDate === 'object') 185 this.minDate = moment(options.minDate); 186 187 if(typeof options.maxDate === 'object') 188 this.maxDate = moment(options.maxDate); 189 190 // sanity check for bad options 191 if(this.minDate && this.startDate.isBefore(this.minDate)) 192 this.startDate = this.minDate.clone(); 193 194 // sanity check for bad options 195 if(this.maxDate && this.endDate.isAfter(this.maxDate)) 196 this.endDate = this.maxDate.clone(); 197 198 if(typeof options.applyButtonClasses === 'string') 199 this.applyButtonClasses = options.applyButtonClasses; 200 201 if(typeof options.applyClass === 'string') //backwards compat 202 this.applyButtonClasses = options.applyClass; 203 204 if(typeof options.cancelButtonClasses === 'string') 205 this.cancelButtonClasses = options.cancelButtonClasses; 206 207 if(typeof options.cancelClass === 'string') //backwards compat 208 this.cancelButtonClasses = options.cancelClass; 209 210 if(typeof options.maxSpan === 'object') 211 this.maxSpan = options.maxSpan; 212 213 if(typeof options.dateLimit === 'object') //backwards compat 214 this.maxSpan = options.dateLimit; 215 216 if(typeof options.opens === 'string') 217 this.opens = options.opens; 218 219 if(typeof options.drops === 'string') 220 this.drops = options.drops; 221 222 if(typeof options.showWeekNumbers === 'boolean') 223 this.showWeekNumbers = options.showWeekNumbers; 224 225 if(typeof options.showISOWeekNumbers === 'boolean') 226 this.showISOWeekNumbers = options.showISOWeekNumbers; 227 228 if(typeof options.buttonClasses === 'string') 229 this.buttonClasses = options.buttonClasses; 230 231 if(typeof options.buttonClasses === 'object') 232 this.buttonClasses = options.buttonClasses.join(' '); 233 234 if(typeof options.showDropdowns === 'boolean') 235 this.showDropdowns = options.showDropdowns; 236 237 if(typeof options.minYear === 'number') 238 this.minYear = options.minYear; 239 240 if(typeof options.maxYear === 'number') 241 this.maxYear = options.maxYear; 242 243 if(typeof options.showCustomRangeLabel === 'boolean') 244 this.showCustomRangeLabel = options.showCustomRangeLabel; 245 246 if(typeof options.singleDatePicker === 'boolean') { 247 this.singleDatePicker = options.singleDatePicker; 248 if(this.singleDatePicker) 249 this.endDate = this.startDate.clone(); 250 } 251 252 if(typeof options.singleCalendar === 'boolean') 253 this.singleCalendar = options.singleCalendar; 254 255 if(typeof options.timePicker === 'boolean') 256 this.timePicker = options.timePicker; 257 258 if(typeof options.timePickerSeconds === 'boolean') 259 this.timePickerSeconds = options.timePickerSeconds; 260 261 if(typeof options.timePickerIncrement === 'number') 262 this.timePickerIncrement = options.timePickerIncrement; 263 264 if(typeof options.timePicker24Hour === 'boolean') 265 this.timePicker24Hour = options.timePicker24Hour; 266 267 if(typeof options.autoApply === 'boolean') 268 this.autoApply = options.autoApply; 269 270 if(typeof options.autoUpdateInput === 'boolean') 271 this.autoUpdateInput = options.autoUpdateInput; 272 273 if(typeof options.linkedCalendars === 'boolean') 274 this.linkedCalendars = options.linkedCalendars; 275 276 if(typeof options.isInvalidDate === 'function') 277 this.isInvalidDate = options.isInvalidDate; 278 279 if(typeof options.isCustomDate === 'function') 280 this.isCustomDate = options.isCustomDate; 281 282 if(typeof options.alwaysShowCalendars === 'boolean') 283 this.alwaysShowCalendars = options.alwaysShowCalendars; 284 285 // update day names order to firstDay 286 if(this.locale.firstDay != 0) { 287 var iterator = this.locale.firstDay; 288 while (iterator > 0) { 289 this.locale.daysOfWeek.push(this.locale.daysOfWeek.shift()); 290 iterator--; 291 } 292 } 293 294 var start, end, range; 295 296 //if no start/end dates set, check if an input element contains initial values 297 if(typeof options.startDate === 'undefined' && typeof options.endDate === 'undefined') { 298 if($(this.element).is(':text')) { 299 var val = $(this.element).val(), 300 split = val.split(this.locale.separator); 301 302 start = end = null; 303 304 if(split.length == 2) { 305 start = moment(split[0], this.locale.format); 306 end = moment(split[1], this.locale.format); 307 } else if(this.singleDatePicker && val !== "") { 308 start = moment(val, this.locale.format); 309 end = moment(val, this.locale.format); 310 } 311 if(start !== null && end !== null) { 312 this.setStartDate(start); 313 this.setEndDate(end); 314 } 315 } 316 } 317 318 if(typeof options.ranges === 'object') { 319 for (range in options.ranges) { 320 321 if(typeof options.ranges[range][0] === 'string') 322 start = moment(options.ranges[range][0], this.locale.format); 323 else 324 start = moment(options.ranges[range][0]); 325 326 if(typeof options.ranges[range][1] === 'string') 327 end = moment(options.ranges[range][1], this.locale.format); 328 else 329 end = moment(options.ranges[range][1]); 330 331 // If the start or end date exceed those allowed by the minDate or maxSpan 332 // options, shorten the range to the allowable period. 333 if(this.minDate && start.isBefore(this.minDate)) 334 start = this.minDate.clone(); 335 336 var maxDate = this.maxDate; 337 if(this.maxSpan && maxDate && start.clone().add(this.maxSpan).isAfter(maxDate)) 338 maxDate = start.clone().add(this.maxSpan); 339 if(maxDate && end.isAfter(maxDate)) 340 end = maxDate.clone(); 341 342 // If the end of the range is before the minimum or the start of the range is 343 // after the maximum, don't display this range option at all. 344 if((this.minDate && end.isBefore(this.minDate, this.timepicker ? 'minute' : 'day')) 345 || (maxDate && start.isAfter(maxDate, this.timepicker ? 'minute' : 'day'))) 346 continue; 347 348 //Support unicode chars in the range names. 349 var elem = document.createElement('textarea'); 350 elem.innerHTML = range; 351 var rangeHtml = elem.value; 352 353 this.ranges[rangeHtml] = [start, end]; 354 } 355 356 var list = '<ul>'; 357 for (range in this.ranges) { 358 list += '<li data-range-key="' + range + '">' + range + '</li>'; 359 } 360 if(this.showCustomRangeLabel) { 361 list += '<li data-range-key="' + this.locale.customRangeLabel + '">' + this.locale.customRangeLabel + '</li>'; 362 } 363 list += '</ul>'; 364 this.container.find('.ranges').prepend(list); 365 } 366 367 if(typeof cb === 'function') { 368 this.callback = cb; 369 } 370 371 if(!this.timePicker) { 372 this.startDate = this.startDate.startOf('day'); 373 this.endDate = this.endDate.endOf('day'); 374 this.container.find('.calendar-time').hide(); 375 } 376 377 //can't be used together for now 378 if(this.timePicker && this.autoApply) 379 this.autoApply = false; 380 381 if(this.autoApply) { 382 this.container.addClass('auto-apply'); 383 } 384 385 if(typeof options.ranges === 'object') 386 this.container.addClass('show-ranges'); 387 388 if(this.singleDatePicker || this.singleCalendar) { 389 this.container.addClass('single'); 390 this.container.find('.drp-calendar.left').addClass('single'); 391 this.container.find('.drp-calendar.left').show(); 392 this.container.find('.drp-calendar.right').hide(); 393 if(!this.timePicker && this.autoApply) { 394 this.container.addClass('auto-apply'); 395 } 396 } 397 398 if((typeof options.ranges === 'undefined' && !this.singleDatePicker) || this.alwaysShowCalendars) { 399 this.container.addClass('show-calendar'); 400 } 401 402 this.container.addClass('opens' + this.opens); 403 404 //apply CSS classes and labels to buttons 405 this.container.find('.applyBtn, .cancelBtn').addClass(this.buttonClasses); 406 if(this.applyButtonClasses.length) 407 this.container.find('.applyBtn').addClass(this.applyButtonClasses); 408 if(this.cancelButtonClasses.length) 409 this.container.find('.cancelBtn').addClass(this.cancelButtonClasses); 410 this.container.find('.applyBtn').html(this.locale.applyLabel); 411 this.container.find('.cancelBtn').html(this.locale.cancelLabel); 412 413 // 414 // event listeners 415 // 416 417 this.container.find('.drp-calendar') 418 .on('click.daterangepicker', '.prev', $.proxy(this.clickPrev, this)) 419 .on('click.daterangepicker', '.next', $.proxy(this.clickNext, this)) 420 .on('mousedown.daterangepicker', 'td.available', $.proxy(this.clickDate, this)) 421 .on('mouseenter.daterangepicker', 'td.available', $.proxy(this.hoverDate, this)) 422 .on('change.daterangepicker', 'select.yearselect', $.proxy(this.monthOrYearChanged, this)) 423 .on('change.daterangepicker', 'select.monthselect', $.proxy(this.monthOrYearChanged, this)) 424 .on('change.daterangepicker', 'select.hourselect,select.minuteselect,select.secondselect,select.ampmselect', $.proxy(this.timeChanged, this)); 425 426 this.container.find('.ranges') 427 .on('click.daterangepicker', 'li', $.proxy(this.clickRange, this)); 428 429 this.container.find('.drp-buttons') 430 .on('click.daterangepicker', 'button.applyBtn', $.proxy(this.clickApply, this)) 431 .on('click.daterangepicker', 'button.cancelBtn', $.proxy(this.clickCancel, this)); 432 433 if(this.element.is('input') || this.element.is('button')) { 434 this.element.on({ 435 'click.daterangepicker': $.proxy(this.show, this), 436 'focus.daterangepicker': $.proxy(this.show, this), 437 'keyup.daterangepicker': $.proxy(this.elementChanged, this), 438 'keydown.daterangepicker': $.proxy(this.keydown, this) //IE 11 compatibility 439 }); 440 } else { 441 this.element.on('click.daterangepicker', $.proxy(this.toggle, this)); 442 this.element.on('keydown.daterangepicker', $.proxy(this.toggle, this)); 443 } 444 445 // 446 // if attached to a text input, set the initial value 447 // 448 449 this.updateElement(); 450 451 }; 452 453 DateRangePicker.prototype = { 454 455 constructor: DateRangePicker, 456 457 setStartDate: function(startDate) { 458 if(typeof startDate === 'string') 459 this.startDate = moment(startDate, this.locale.format); 460 461 if(typeof startDate === 'object') 462 this.startDate = moment(startDate); 463 464 if(!this.timePicker) 465 this.startDate = this.startDate.startOf('day'); 466 467 if(this.timePicker && this.timePickerIncrement) 468 this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement); 469 470 if(this.minDate && this.startDate.isBefore(this.minDate)) { 471 this.startDate = this.minDate.clone(); 472 if(this.timePicker && this.timePickerIncrement) 473 this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement); 474 } 475 476 if(this.maxDate && this.startDate.isAfter(this.maxDate)) { 477 this.startDate = this.maxDate.clone(); 478 if(this.timePicker && this.timePickerIncrement) 479 this.startDate.minute(Math.floor(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement); 480 } 481 482 if(!this.isShowing) 483 this.updateElement(); 484 485 this.updateMonthsInView(); 486 }, 487 488 setEndDate: function(endDate) { 489 if(typeof endDate === 'string') 490 this.endDate = moment(endDate, this.locale.format); 491 492 if(typeof endDate === 'object') 493 this.endDate = moment(endDate); 494 495 if(!this.timePicker) 496 this.endDate = this.endDate.endOf('day'); 497 498 if(this.timePicker && this.timePickerIncrement) 499 this.endDate.minute(Math.round(this.endDate.minute() / this.timePickerIncrement) * this.timePickerIncrement); 500 501 if(this.endDate.isBefore(this.startDate)) 502 this.endDate = this.startDate.clone(); 503 504 if(this.maxDate && this.endDate.isAfter(this.maxDate)) 505 this.endDate = this.maxDate.clone(); 506 507 if(this.maxSpan && this.startDate.clone().add(this.maxSpan).isBefore(this.endDate)) 508 this.endDate = this.startDate.clone().add(this.maxSpan); 509 510 this.previousRightTime = this.endDate.clone(); 511 512 this.container.find('.drp-selected').html(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format)); 513 514 if(!this.isShowing) 515 this.updateElement(); 516 517 this.updateMonthsInView(); 518 }, 519 520 isInvalidDate: function() { 521 return false; 522 }, 523 524 isCustomDate: function() { 525 return false; 526 }, 527 528 updateView: function() { 529 if(this.timePicker) { 530 this.renderTimePicker('left'); 531 this.renderTimePicker('right'); 532 if(!this.endDate) { 533 this.container.find('.right .calendar-time select').prop('disabled', true).addClass('disabled'); 534 } else { 535 this.container.find('.right .calendar-time select').prop('disabled', false).removeClass('disabled'); 536 } 537 } 538 if(this.endDate) 539 this.container.find('.drp-selected').html(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format)); 540 this.updateMonthsInView(); 541 this.updateCalendars(); 542 this.updateFormInputs(); 543 }, 544 545 updateMonthsInView: function() { 546 if(this.endDate) { 547 548 //if both dates are visible already, do nothing 549 if(!this.singleDatePicker && this.leftCalendar.month && this.rightCalendar.month && 550 (this.startDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.startDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM')) 551 && 552 (this.endDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.endDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM')) 553 ) { 554 return; 555 } 556 557 this.leftCalendar.month = this.startDate.clone().date(2); 558 if(!this.linkedCalendars && (this.endDate.month() != this.startDate.month() || this.endDate.year() != this.startDate.year())) { 559 this.rightCalendar.month = this.endDate.clone().date(2); 560 } else { 561 this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month'); 562 } 563 564 } else { 565 if(this.leftCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM') && this.rightCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM')) { 566 this.leftCalendar.month = this.startDate.clone().date(2); 567 this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month'); 568 } 569 } 570 if(this.maxDate && this.linkedCalendars && !this.singleDatePicker && this.rightCalendar.month > this.maxDate) { 571 this.rightCalendar.month = this.maxDate.clone().date(2); 572 this.leftCalendar.month = this.maxDate.clone().date(2).subtract(1, 'month'); 573 } 574 }, 575 576 updateCalendars: function() { 577 578 if(this.timePicker) { 579 var hour, minute, second; 580 if(this.endDate) { 581 hour = parseInt(this.container.find('.left .hourselect').val(), 10); 582 minute = parseInt(this.container.find('.left .minuteselect').val(), 10); 583 if(isNaN(minute)) { 584 minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10); 585 } 586 second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0; 587 if(!this.timePicker24Hour) { 588 var ampm = this.container.find('.left .ampmselect').val(); 589 if(ampm === 'PM' && hour < 12) 590 hour += 12; 591 if(ampm === 'AM' && hour === 12) 592 hour = 0; 593 } 594 } else { 595 hour = parseInt(this.container.find('.right .hourselect').val(), 10); 596 minute = parseInt(this.container.find('.right .minuteselect').val(), 10); 597 if(isNaN(minute)) { 598 minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10); 599 } 600 second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0; 601 if(!this.timePicker24Hour) { 602 var ampm = this.container.find('.right .ampmselect').val(); 603 if(ampm === 'PM' && hour < 12) 604 hour += 12; 605 if(ampm === 'AM' && hour === 12) 606 hour = 0; 607 } 608 } 609 this.leftCalendar.month.hour(hour).minute(minute).second(second); 610 this.rightCalendar.month.hour(hour).minute(minute).second(second); 611 } 612 613 this.renderCalendar('left'); 614 this.renderCalendar('right'); 615 616 //highlight any predefined range matching the current start and end dates 617 this.container.find('.ranges li').removeClass('active'); 618 if(this.endDate == null) return; 619 620 this.calculateChosenLabel(); 621 }, 622 623 renderCalendar: function(side) { 624 625 // 626 // Build the matrix of dates that will populate the calendar 627 // 628 629 var calendar = side == 'left' ? this.leftCalendar : this.rightCalendar; 630 var month = calendar.month.month(); 631 var year = calendar.month.year(); 632 var hour = calendar.month.hour(); 633 var minute = calendar.month.minute(); 634 var second = calendar.month.second(); 635 var daysInMonth = moment([year, month]).daysInMonth(); 636 var firstDay = moment([year, month, 1]); 637 var lastDay = moment([year, month, daysInMonth]); 638 var lastMonth = moment(firstDay).subtract(1, 'month').month(); 639 var lastYear = moment(firstDay).subtract(1, 'month').year(); 640 var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth(); 641 var dayOfWeek = firstDay.day(); 642 643 //initialize a 6 rows x 7 columns array for the calendar 644 var calendar = []; 645 calendar.firstDay = firstDay; 646 calendar.lastDay = lastDay; 647 648 for (var i = 0; i < 6; i++) { 649 calendar[i] = []; 650 } 651 652 //populate the calendar with date objects 653 var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1; 654 if(startDay > daysInLastMonth) 655 startDay -= 7; 656 657 if(dayOfWeek == this.locale.firstDay) 658 startDay = daysInLastMonth - 6; 659 660 var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]); 661 662 var col, row; 663 for (var i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) { 664 if(i > 0 && col % 7 === 0) { 665 col = 0; 666 row++; 667 } 668 calendar[row][col] = curDate.clone().hour(hour).minute(minute).second(second); 669 curDate.hour(12); 670 671 if(this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') { 672 calendar[row][col] = this.minDate.clone(); 673 } 674 675 if(this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') { 676 calendar[row][col] = this.maxDate.clone(); 677 } 678 679 } 680 681 //make the calendar object available to hoverDate/clickDate 682 if(side == 'left') { 683 this.leftCalendar.calendar = calendar; 684 } else { 685 this.rightCalendar.calendar = calendar; 686 } 687 688 // 689 // Display the calendar 690 // 691 692 var minDate = side == 'left' ? this.minDate : this.startDate; 693 var maxDate = this.maxDate; 694 var selected = side == 'left' ? this.startDate : this.endDate; 695 var arrow = this.locale.direction == 'ltr' ? {left: 'chevron-left', right: 'chevron-right'} : {left: 'chevron-right', right: 'chevron-left'}; 696 697 var html = '<table class="table-condensed">'; 698 html += '<thead>'; 699 html += '<tr>'; 700 701 // add empty cell for week number 702 if(this.showWeekNumbers || this.showISOWeekNumbers) 703 html += '<th></th>'; 704 705 if((!minDate || minDate.isBefore(calendar.firstDay)) && (!this.linkedCalendars || side == 'left')) { 706 html += '<th class="prev available"><span></span></th>'; 707 } else { 708 html += '<th></th>'; 709 } 710 711 var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY"); 712 713 if(this.showDropdowns) { 714 var currentMonth = calendar[1][1].month(); 715 var currentYear = calendar[1][1].year(); 716 var maxYear = (maxDate && maxDate.year()) || (this.maxYear); 717 var minYear = (minDate && minDate.year()) || (this.minYear); 718 var inMinYear = currentYear == minYear; 719 var inMaxYear = currentYear == maxYear; 720 721 var monthHtml = '<select class="monthselect">'; 722 for (var m = 0; m < 12; m++) { 723 if((!inMinYear || (minDate && m >= minDate.month())) && (!inMaxYear || (maxDate && m <= maxDate.month()))) { 724 monthHtml += "<option value='" + m + "'" + 725 (m === currentMonth ? " selected='selected'" : "") + 726 ">" + this.locale.monthNames[m] + "</option>"; 727 } else { 728 monthHtml += "<option value='" + m + "'" + 729 (m === currentMonth ? " selected='selected'" : "") + 730 " disabled='disabled'>" + this.locale.monthNames[m] + "</option>"; 731 } 732 } 733 monthHtml += "</select>"; 734 735 var yearHtml = '<select class="yearselect">'; 736 for (var y = minYear; y <= maxYear; y++) { 737 yearHtml += '<option value="' + y + '"' + 738 (y === currentYear ? ' selected="selected"' : '') + 739 '>' + y + '</option>'; 740 } 741 yearHtml += '</select>'; 742 743 dateHtml = monthHtml + yearHtml; 744 } 745 746 html += '<th colspan="5" class="month">' + dateHtml + '</th>'; 747 if((!maxDate || maxDate.isAfter(calendar.lastDay)) && (!this.linkedCalendars || side == 'right' || this.singleDatePicker || this.singleCalendar)) { 748 html += '<th class="next available"><span></span></th>'; 749 } else { 750 html += '<th></th>'; 751 } 752 753 html += '</tr>'; 754 html += '<tr>'; 755 756 // add week number label 757 if(this.showWeekNumbers || this.showISOWeekNumbers) 758 html += '<th class="week">' + this.locale.weekLabel + '</th>'; 759 760 $.each(this.locale.daysOfWeek, function(index, dayOfWeek) { 761 html += '<th>' + dayOfWeek + '</th>'; 762 }); 763 764 html += '</tr>'; 765 html += '</thead>'; 766 html += '<tbody>'; 767 768 //adjust maxDate to reflect the maxSpan setting in order to 769 //grey out end dates beyond the maxSpan 770 if(this.endDate == null && this.maxSpan) { 771 var maxLimit = this.startDate.clone().add(this.maxSpan).endOf('day'); 772 if(!maxDate || maxLimit.isBefore(maxDate)) { 773 maxDate = maxLimit; 774 } 775 } 776 777 for (var row = 0; row < 6; row++) { 778 html += '<tr>'; 779 780 // add week number 781 if(this.showWeekNumbers) 782 html += '<td class="week">' + calendar[row][0].week() + '</td>'; 783 else if(this.showISOWeekNumbers) 784 html += '<td class="week">' + calendar[row][0].isoWeek() + '</td>'; 785 786 for (var col = 0; col < 7; col++) { 787 788 var classes = []; 789 790 //highlight today's date 791 if(calendar[row][col].isSame(new Date(), "day")) 792 classes.push('today'); 793 794 //highlight weekends 795 if(calendar[row][col].isoWeekday() > 5) 796 classes.push('weekend'); 797 798 //grey out the dates in other months displayed at beginning and end of this calendar 799 if(calendar[row][col].month() != calendar[1][1].month()) 800 classes.push('off', 'ends'); 801 802 //don't allow selection of dates before the minimum date 803 if(this.minDate && calendar[row][col].isBefore(this.minDate, 'day')) 804 classes.push('off', 'disabled'); 805 806 //don't allow selection of dates after the maximum date 807 if(maxDate && calendar[row][col].isAfter(maxDate, 'day')) 808 classes.push('off', 'disabled'); 809 810 //don't allow selection of date if a custom function decides it's invalid 811 if(this.isInvalidDate(calendar[row][col])) 812 classes.push('off', 'disabled'); 813 814 //highlight the currently selected start date 815 if(calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD')) 816 classes.push('active', 'start-date'); 817 818 //highlight the currently selected end date 819 if(this.endDate != null && calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD')) 820 classes.push('active', 'end-date'); 821 822 //highlight dates in-between the selected dates 823 if(this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate) 824 classes.push('in-range'); 825 826 //apply custom classes for this date 827 var isCustom = this.isCustomDate(calendar[row][col]); 828 if(isCustom !== false) { 829 if(typeof isCustom === 'string') 830 classes.push(isCustom); 831 else 832 Array.prototype.push.apply(classes, isCustom); 833 } 834 835 var cname = '', disabled = false; 836 for (var i = 0; i < classes.length; i++) { 837 cname += classes[i] + ' '; 838 if(classes[i] == 'disabled') 839 disabled = true; 840 } 841 if(!disabled) 842 cname += 'available'; 843 844 html += '<td class="' + cname.replace(/^\s+|\s+$/g, '') + '" data-title="' + 'r' + row + 'c' + col + '">' + calendar[row][col].date() + '</td>'; 845 846 } 847 html += '</tr>'; 848 } 849 850 html += '</tbody>'; 851 html += '</table>'; 852 853 this.container.find('.drp-calendar.' + side + ' .calendar-table').html(html); 854 855 }, 856 857 renderTimePicker: function(side) { 858 859 // Don't bother updating the time picker if it's currently disabled 860 // because an end date hasn't been clicked yet 861 if(side == 'right' && !this.endDate) return; 862 863 var html, selected, minDate, maxDate = this.maxDate; 864 865 if(this.maxSpan && (!this.maxDate || this.startDate.clone().add(this.maxSpan).isBefore(this.maxDate))) 866 maxDate = this.startDate.clone().add(this.maxSpan); 867 868 if(side == 'left') { 869 selected = this.startDate.clone(); 870 minDate = this.minDate; 871 } else if(side == 'right') { 872 selected = this.endDate.clone(); 873 minDate = this.startDate; 874 875 //Preserve the time already selected 876 var timeSelector = this.container.find('.drp-calendar.right .calendar-time'); 877 if(timeSelector.html() != '') { 878 879 selected.hour(!isNaN(selected.hour()) ? selected.hour() : timeSelector.find('.hourselect option:selected').val()); 880 selected.minute(!isNaN(selected.minute()) ? selected.minute() : timeSelector.find('.minuteselect option:selected').val()); 881 selected.second(!isNaN(selected.second()) ? selected.second() : timeSelector.find('.secondselect option:selected').val()); 882 883 if(!this.timePicker24Hour) { 884 var ampm = timeSelector.find('.ampmselect option:selected').val(); 885 if(ampm === 'PM' && selected.hour() < 12) 886 selected.hour(selected.hour() + 12); 887 if(ampm === 'AM' && selected.hour() === 12) 888 selected.hour(0); 889 } 890 891 } 892 893 if(selected.isBefore(this.startDate)) 894 selected = this.startDate.clone(); 895 896 if(maxDate && selected.isAfter(maxDate)) 897 selected = maxDate.clone(); 898 899 } 900 901 // 902 // hours 903 // 904 905 html = '<select class="hourselect">'; 906 907 var start = this.timePicker24Hour ? 0 : 1; 908 var end = this.timePicker24Hour ? 23 : 12; 909 910 for (var i = start; i <= end; i++) { 911 var i_in_24 = i; 912 if(!this.timePicker24Hour) 913 i_in_24 = selected.hour() >= 12 ? (i == 12 ? 12 : i + 12) : (i == 12 ? 0 : i); 914 915 var time = selected.clone().hour(i_in_24); 916 var disabled = false; 917 if(minDate && time.minute(59).isBefore(minDate)) 918 disabled = true; 919 if(maxDate && time.minute(0).isAfter(maxDate)) 920 disabled = true; 921 922 if(i_in_24 == selected.hour() && !disabled) { 923 html += '<option value="' + i + '" selected="selected">' + i + '</option>'; 924 } else if(disabled) { 925 html += '<option value="' + i + '" disabled="disabled" class="disabled">' + i + '</option>'; 926 } else { 927 html += '<option value="' + i + '">' + i + '</option>'; 928 } 929 } 930 931 html += '</select> '; 932 933 // 934 // minutes 935 // 936 937 html += ': <select class="minuteselect">'; 938 939 for (var i = 0; i < 60; i += this.timePickerIncrement) { 940 var padded = i < 10 ? '0' + i : i; 941 var time = selected.clone().minute(i); 942 943 var disabled = false; 944 if(minDate && time.second(59).isBefore(minDate)) 945 disabled = true; 946 if(maxDate && time.second(0).isAfter(maxDate)) 947 disabled = true; 948 949 if(selected.minute() == i && !disabled) { 950 html += '<option value="' + i + '" selected="selected">' + padded + '</option>'; 951 } else if(disabled) { 952 html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>'; 953 } else { 954 html += '<option value="' + i + '">' + padded + '</option>'; 955 } 956 } 957 958 html += '</select> '; 959 960 // 961 // seconds 962 // 963 964 if(this.timePickerSeconds) { 965 html += ': <select class="secondselect">'; 966 967 for (var i = 0; i < 60; i++) { 968 var padded = i < 10 ? '0' + i : i; 969 var time = selected.clone().second(i); 970 971 var disabled = false; 972 if(minDate && time.isBefore(minDate)) 973 disabled = true; 974 if(maxDate && time.isAfter(maxDate)) 975 disabled = true; 976 977 if(selected.second() == i && !disabled) { 978 html += '<option value="' + i + '" selected="selected">' + padded + '</option>'; 979 } else if(disabled) { 980 html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>'; 981 } else { 982 html += '<option value="' + i + '">' + padded + '</option>'; 983 } 984 } 985 986 html += '</select> '; 987 } 988 989 // 990 // AM/PM 991 // 992 993 if(!this.timePicker24Hour) { 994 html += '<select class="ampmselect">'; 995 996 var am_html = ''; 997 var pm_html = ''; 998 999 if(minDate && selected.clone().hour(12).minute(0).second(0).isBefore(minDate)) 1000 am_html = ' disabled="disabled" class="disabled"'; 1001 1002 if(maxDate && selected.clone().hour(0).minute(0).second(0).isAfter(maxDate)) 1003 pm_html = ' disabled="disabled" class="disabled"'; 1004 1005 if(selected.hour() >= 12) { 1006 html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>'; 1007 } else { 1008 html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>'; 1009 } 1010 1011 html += '</select>'; 1012 } 1013 1014 this.container.find('.drp-calendar.' + side + ' .calendar-time').html(html); 1015 1016 }, 1017 1018 updateFormInputs: function() { 1019 1020 if(this.singleDatePicker || (this.endDate && (this.startDate.isBefore(this.endDate) || this.startDate.isSame(this.endDate)))) { 1021 this.container.find('button.applyBtn').prop('disabled', false); 1022 } else { 1023 this.container.find('button.applyBtn').prop('disabled', true); 1024 } 1025 1026 }, 1027 1028 move: function() { 1029 var parentOffset = { top: 0, left: 0 }, 1030 containerTop, 1031 drops = this.drops; 1032 1033 var parentRightEdge = $(window).width(); 1034 if(!this.parentEl.is('body')) { 1035 parentOffset = { 1036 top: this.parentEl.offset().top - this.parentEl.scrollTop(), 1037 left: this.parentEl.offset().left - this.parentEl.scrollLeft() 1038 }; 1039 parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left; 1040 } 1041 1042 switch (drops) { 1043 case 'auto': 1044 containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top; 1045 if(containerTop + this.container.outerHeight() >= this.parentEl[0].scrollHeight) { 1046 containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top; 1047 drops = 'up'; 1048 } 1049 break; 1050 case 'up': 1051 containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top; 1052 break; 1053 default: 1054 containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top; 1055 break; 1056 } 1057 1058 // Force the container to it's actual width 1059 this.container.css({ 1060 top: 0, 1061 left: 0, 1062 right: 'auto' 1063 }); 1064 var containerWidth = this.container.outerWidth(); 1065 1066 this.container.toggleClass('drop-up', drops == 'up'); 1067 1068 if(this.opens == 'left') { 1069 var containerRight = parentRightEdge - this.element.offset().left - this.element.outerWidth(); 1070 if(containerWidth + containerRight > $(window).width()) { 1071 this.container.css({ 1072 top: containerTop, 1073 right: 'auto', 1074 left: 9 1075 }); 1076 } else { 1077 this.container.css({ 1078 top: containerTop, 1079 right: containerRight, 1080 left: 'auto' 1081 }); 1082 } 1083 } else if(this.opens == 'center') { 1084 var containerLeft = this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2 1085 - containerWidth / 2; 1086 if(containerLeft < 0) { 1087 this.container.css({ 1088 top: containerTop, 1089 right: 'auto', 1090 left: 9 1091 }); 1092 } else if(containerLeft + containerWidth > $(window).width()) { 1093 this.container.css({ 1094 top: containerTop, 1095 left: 'auto', 1096 right: 0 1097 }); 1098 } else { 1099 this.container.css({ 1100 top: containerTop, 1101 left: containerLeft, 1102 right: 'auto' 1103 }); 1104 } 1105 } else { 1106 var containerLeft = this.element.offset().left - parentOffset.left; 1107 if(containerLeft + containerWidth > $(window).width()) { 1108 this.container.css({ 1109 top: containerTop, 1110 left: 'auto', 1111 right: 0 1112 }); 1113 } else { 1114 this.container.css({ 1115 top: containerTop, 1116 left: containerLeft, 1117 right: 'auto' 1118 }); 1119 } 1120 } 1121 }, 1122 1123 show: function(e) { 1124 if(this.isShowing) return; 1125 1126 // Create a click proxy that is private to this instance of datepicker, for unbinding 1127 this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this); 1128 1129 // Bind global datepicker mousedown for hiding and 1130 $(document) 1131 .on('mousedown.daterangepicker', this._outsideClickProxy) 1132 // also support mobile devices 1133 .on('touchend.daterangepicker', this._outsideClickProxy) 1134 // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them 1135 .on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy) 1136 // and also close when focus changes to outside the picker (eg. tabbing between controls) 1137 .on('focusin.daterangepicker', this._outsideClickProxy); 1138 1139 // Reposition the picker if the window is resized while it's open 1140 $(window).on('resize.daterangepicker', $.proxy(function(e) { this.move(e); }, this)); 1141 1142 this.oldStartDate = this.startDate.clone(); 1143 this.oldEndDate = this.endDate.clone(); 1144 this.previousRightTime = this.endDate.clone(); 1145 1146 this.updateView(); 1147 this.container.show(); 1148 this.move(); 1149 this.element.trigger('show.daterangepicker', this); 1150 this.isShowing = true; 1151 }, 1152 1153 hide: function(e) { 1154 if(!this.isShowing) return; 1155 1156 //incomplete date selection, revert to last values 1157 if(!this.endDate) { 1158 this.startDate = this.oldStartDate.clone(); 1159 this.endDate = this.oldEndDate.clone(); 1160 } 1161 1162 //if a new date range was selected, invoke the user callback function 1163 if(!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate)) 1164 this.callback(this.startDate.clone(), this.endDate.clone(), this.chosenLabel); 1165 1166 //if picker is attached to a text input, update it 1167 this.updateElement(); 1168 1169 $(document).off('.daterangepicker'); 1170 $(window).off('.daterangepicker'); 1171 this.container.hide(); 1172 this.element.trigger('hide.daterangepicker', this); 1173 this.isShowing = false; 1174 }, 1175 1176 toggle: function(e) { 1177 if(this.isShowing) { 1178 this.hide(); 1179 } else { 1180 this.show(); 1181 } 1182 }, 1183 1184 outsideClick: function(e) { 1185 var target = $(e.target); 1186 // if the page is clicked anywhere except within the daterangerpicker/button 1187 // itself then call this.hide() 1188 if( 1189 // ie modal dialog fix 1190 e.type == "focusin" || 1191 target.closest(this.element).length || 1192 target.closest(this.container).length || 1193 target.closest('.calendar-table').length 1194 ) return; 1195 this.hide(); 1196 this.element.trigger('outsideClick.daterangepicker', this); 1197 }, 1198 1199 showCalendars: function() { 1200 this.container.addClass('show-calendar'); 1201 this.move(); 1202 this.element.trigger('showCalendar.daterangepicker', this); 1203 }, 1204 1205 hideCalendars: function() { 1206 this.container.removeClass('show-calendar'); 1207 this.element.trigger('hideCalendar.daterangepicker', this); 1208 }, 1209 1210 clickRange: function(e) { 1211 var label = e.target.getAttribute('data-range-key'); 1212 this.chosenLabel = label; 1213 if(label == this.locale.customRangeLabel) { 1214 this.showCalendars(); 1215 } else { 1216 var dates = this.ranges[label]; 1217 this.startDate = dates[0]; 1218 this.endDate = dates[1]; 1219 1220 if(!this.timePicker) { 1221 this.startDate.startOf('day'); 1222 this.endDate.endOf('day'); 1223 } 1224 1225 if(!this.alwaysShowCalendars) 1226 this.hideCalendars(); 1227 this.clickApply(); 1228 } 1229 }, 1230 1231 clickPrev: function(e) { 1232 var cal = $(e.target).parents('.drp-calendar'); 1233 if(cal.hasClass('left')) { 1234 this.leftCalendar.month.subtract(1, 'month'); 1235 if(this.linkedCalendars) 1236 this.rightCalendar.month.subtract(1, 'month'); 1237 } else { 1238 this.rightCalendar.month.subtract(1, 'month'); 1239 } 1240 this.updateCalendars(); 1241 }, 1242 1243 clickNext: function(e) { 1244 var cal = $(e.target).parents('.drp-calendar'); 1245 if(cal.hasClass('left')) { 1246 this.leftCalendar.month.add(1, 'month'); 1247 } else { 1248 this.rightCalendar.month.add(1, 'month'); 1249 if(this.linkedCalendars) 1250 this.leftCalendar.month.add(1, 'month'); 1251 } 1252 this.updateCalendars(); 1253 }, 1254 1255 hoverDate: function(e) { 1256 1257 //ignore dates that can't be selected 1258 if(!$(e.target).hasClass('available')) return; 1259 1260 var title = $(e.target).attr('data-title'); 1261 var row = title.substr(1, 1); 1262 var col = title.substr(3, 1); 1263 var cal = $(e.target).parents('.drp-calendar'); 1264 var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col]; 1265 1266 //highlight the dates between the start date and the date being hovered as a potential end date 1267 var leftCalendar = this.leftCalendar; 1268 var rightCalendar = this.rightCalendar; 1269 var startDate = this.startDate; 1270 if(!this.endDate) { 1271 this.container.find('.drp-calendar tbody td').each(function(index, el) { 1272 1273 //skip week numbers, only look at dates 1274 if($(el).hasClass('week')) return; 1275 1276 var title = $(el).attr('data-title'); 1277 var row = title.substr(1, 1); 1278 var col = title.substr(3, 1); 1279 var cal = $(el).parents('.drp-calendar'); 1280 var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col]; 1281 1282 if((dt.isAfter(startDate) && dt.isBefore(date)) || dt.isSame(date, 'day')) { 1283 $(el).addClass('in-range'); 1284 } else { 1285 $(el).removeClass('in-range'); 1286 } 1287 1288 }); 1289 } 1290 1291 }, 1292 1293 clickDate: function(e) { 1294 1295 if(!$(e.target).hasClass('available')) return; 1296 1297 var title = $(e.target).attr('data-title'); 1298 var row = title.substr(1, 1); 1299 var col = title.substr(3, 1); 1300 var cal = $(e.target).parents('.drp-calendar'); 1301 var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col]; 1302 1303 // 1304 // this function needs to do a few things: 1305 // * alternate between selecting a start and end date for the range, 1306 // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date 1307 // * if autoapply is enabled, and an end date was chosen, apply the selection 1308 // * if single date picker mode, and time picker isn't enabled, apply the selection immediately 1309 // * if one of the inputs above the calendars was focused, cancel that manual input 1310 // 1311 1312 if(this.endDate || date.isBefore(this.startDate, 'day')) { //picking start 1313 if(this.timePicker) { 1314 var hour = parseInt(this.container.find('.left .hourselect').val(), 10); 1315 if(!this.timePicker24Hour) { 1316 var ampm = this.container.find('.left .ampmselect').val(); 1317 if(ampm === 'PM' && hour < 12) 1318 hour += 12; 1319 if(ampm === 'AM' && hour === 12) 1320 hour = 0; 1321 } 1322 var minute = parseInt(this.container.find('.left .minuteselect').val(), 10); 1323 if(isNaN(minute)) { 1324 minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10); 1325 } 1326 var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0; 1327 date = date.clone().hour(hour).minute(minute).second(second); 1328 } 1329 this.endDate = null; 1330 this.setStartDate(date.clone()); 1331 } else if(!this.endDate && date.isBefore(this.startDate)) { 1332 //special case: clicking the same date for start/end, 1333 //but the time of the end date is before the start date 1334 this.setEndDate(this.startDate.clone()); 1335 } else { // picking end 1336 if(this.timePicker) { 1337 var hour = parseInt(this.container.find('.right .hourselect').val(), 10); 1338 if(!this.timePicker24Hour) { 1339 var ampm = this.container.find('.right .ampmselect').val(); 1340 if(ampm === 'PM' && hour < 12) 1341 hour += 12; 1342 if(ampm === 'AM' && hour === 12) 1343 hour = 0; 1344 } 1345 var minute = parseInt(this.container.find('.right .minuteselect').val(), 10); 1346 if(isNaN(minute)) { 1347 minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10); 1348 } 1349 var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0; 1350 date = date.clone().hour(hour).minute(minute).second(second); 1351 } 1352 this.setEndDate(date.clone()); 1353 if(this.autoApply) { 1354 this.calculateChosenLabel(); 1355 this.clickApply(); 1356 } 1357 } 1358 1359 if(this.singleDatePicker) { 1360 this.setEndDate(this.startDate); 1361 if(!this.timePicker && this.autoApply) 1362 this.clickApply(); 1363 } 1364 1365 this.updateView(); 1366 1367 //This is to cancel the blur event handler if the mouse was in one of the inputs 1368 e.stopPropagation(); 1369 1370 }, 1371 1372 calculateChosenLabel: function () { 1373 var customRange = true; 1374 var i = 0; 1375 for (var range in this.ranges) { 1376 if(this.timePicker) { 1377 var format = this.timePickerSeconds ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD HH:mm"; 1378 //ignore times when comparing dates if time picker seconds is not enabled 1379 if(this.startDate.format(format) == this.ranges[range][0].format(format) && this.endDate.format(format) == this.ranges[range][1].format(format)) { 1380 customRange = false; 1381 this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key'); 1382 break; 1383 } 1384 } else { 1385 //ignore times when comparing dates if time picker is not enabled 1386 if(this.startDate.format('YYYY-MM-DD') == this.ranges[range][0].format('YYYY-MM-DD') && this.endDate.format('YYYY-MM-DD') == this.ranges[range][1].format('YYYY-MM-DD')) { 1387 customRange = false; 1388 this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key'); 1389 break; 1390 } 1391 } 1392 i++; 1393 } 1394 if(customRange) { 1395 if(this.showCustomRangeLabel) { 1396 this.chosenLabel = this.container.find('.ranges li:last').addClass('active').attr('data-range-key'); 1397 } else { 1398 this.chosenLabel = null; 1399 } 1400 this.showCalendars(); 1401 } 1402 }, 1403 1404 clickApply: function(e) { 1405 this.hide(); 1406 this.element.trigger('apply.daterangepicker', this); 1407 }, 1408 1409 clickCancel: function(e) { 1410 this.startDate = this.oldStartDate; 1411 this.endDate = this.oldEndDate; 1412 this.hide(); 1413 this.element.trigger('cancel.daterangepicker', this); 1414 }, 1415 1416 monthOrYearChanged: function(e) { 1417 var isLeft = $(e.target).closest('.drp-calendar').hasClass('left'), 1418 leftOrRight = isLeft ? 'left' : 'right', 1419 cal = this.container.find('.drp-calendar.'+leftOrRight); 1420 1421 // Month must be Number for new moment versions 1422 var month = parseInt(cal.find('.monthselect').val(), 10); 1423 var year = cal.find('.yearselect').val(); 1424 1425 if(!isLeft) { 1426 if(year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) { 1427 month = this.startDate.month(); 1428 year = this.startDate.year(); 1429 } 1430 } 1431 1432 if(this.minDate) { 1433 if(year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) { 1434 month = this.minDate.month(); 1435 year = this.minDate.year(); 1436 } 1437 } 1438 1439 if(this.maxDate) { 1440 if(year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) { 1441 month = this.maxDate.month(); 1442 year = this.maxDate.year(); 1443 } 1444 } 1445 1446 if(isLeft) { 1447 this.leftCalendar.month.month(month).year(year); 1448 if(this.linkedCalendars) 1449 this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month'); 1450 } else { 1451 this.rightCalendar.month.month(month).year(year); 1452 if(this.linkedCalendars) 1453 this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month'); 1454 } 1455 this.updateCalendars(); 1456 }, 1457 1458 timeChanged: function(e) { 1459 1460 var cal = $(e.target).closest('.drp-calendar'), 1461 isLeft = cal.hasClass('left'); 1462 1463 var hour = parseInt(cal.find('.hourselect').val(), 10); 1464 var minute = parseInt(cal.find('.minuteselect').val(), 10); 1465 if(isNaN(minute)) { 1466 minute = parseInt(cal.find('.minuteselect option:last').val(), 10); 1467 } 1468 var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0; 1469 1470 if(!this.timePicker24Hour) { 1471 var ampm = cal.find('.ampmselect').val(); 1472 if(ampm === 'PM' && hour < 12) 1473 hour += 12; 1474 if(ampm === 'AM' && hour === 12) 1475 hour = 0; 1476 } 1477 1478 if(isLeft) { 1479 var start = this.startDate.clone(); 1480 start.hour(hour); 1481 start.minute(minute); 1482 start.second(second); 1483 this.setStartDate(start); 1484 if(this.singleDatePicker) { 1485 this.endDate = this.startDate.clone(); 1486 } else if(this.endDate && this.endDate.format('YYYY-MM-DD') == start.format('YYYY-MM-DD') && this.endDate.isBefore(start)) { 1487 this.setEndDate(start.clone()); 1488 } 1489 } else if(this.endDate) { 1490 var end = this.endDate.clone(); 1491 end.hour(hour); 1492 end.minute(minute); 1493 end.second(second); 1494 this.setEndDate(end); 1495 } 1496 1497 //update the calendars so all clickable dates reflect the new time component 1498 this.updateCalendars(); 1499 1500 //update the form inputs above the calendars with the new time 1501 this.updateFormInputs(); 1502 1503 //re-render the time pickers because changing one selection can affect what's enabled in another 1504 this.renderTimePicker('left'); 1505 this.renderTimePicker('right'); 1506 1507 }, 1508 1509 elementChanged: function() { 1510 if(!this.element.is('input')) return; 1511 if(!this.element.val().length) return; 1512 1513 var dateString = this.element.val().split(this.locale.separator), 1514 start = null, 1515 end = null; 1516 1517 if(dateString.length === 2) { 1518 start = moment(dateString[0], this.locale.format); 1519 end = moment(dateString[1], this.locale.format); 1520 } 1521 1522 if(this.singleDatePicker || start === null || end === null) { 1523 start = moment(this.element.val(), this.locale.format); 1524 end = start; 1525 } 1526 1527 if(!start.isValid() || !end.isValid()) return; 1528 1529 this.setStartDate(start); 1530 this.setEndDate(end); 1531 this.updateView(); 1532 }, 1533 1534 keydown: function(e) { 1535 //hide on tab or enter 1536 if((e.keyCode === 9) || (e.keyCode === 13)) { 1537 this.hide(); 1538 } 1539 1540 //hide on esc and prevent propagation 1541 if(e.keyCode === 27) { 1542 e.preventDefault(); 1543 e.stopPropagation(); 1544 1545 this.hide(); 1546 } 1547 }, 1548 1549 updateElement: function() { 1550 if(this.element.is('input') && this.autoUpdateInput) { 1551 var newValue = this.startDate.format(this.locale.format); 1552 if(!this.singleDatePicker) { 1553 newValue += this.locale.separator + this.endDate.format(this.locale.format); 1554 } 1555 if(newValue !== this.element.val()) { 1556 this.element.val(newValue).trigger('change'); 1557 } 1558 } 1559 }, 1560 1561 remove: function() { 1562 this.container.remove(); 1563 this.element.off('.daterangepicker'); 1564 this.element.removeData(); 1565 } 1566 1567 }; 1568 1569 $.fn.daterangepicker = function(options, callback) { 1570 var implementOptions = $.extend(true, {}, $.fn.daterangepicker.defaultOptions, options); 1571 this.each(function() { 1572 var el = $(this); 1573 if(el.data('daterangepicker')) 1574 el.data('daterangepicker').remove(); 1575 el.data('daterangepicker', new DateRangePicker(el, implementOptions, callback)); 1576 }); 1577 return this; 1578 }; 1579 1580 return DateRangePicker; 1581 1582}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.