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