9
(function (root, factory) {
10
if (typeof define === 'function' && define.amd) {
12
define(['moment', 'jquery'], function (moment, jquery) {
13
if (!jquery.fn) jquery.fn = {};
14
if (typeof moment !== 'function' && moment.hasOwnProperty('default')) moment = moment['default']
15
return factory(moment, jquery);
17
} else if (typeof module === 'object' && module.exports) {
20
var jQuery = (typeof window != 'undefined') ? window.jQuery : undefined;
22
jQuery = require('jquery');
23
if (!jQuery.fn) jQuery.fn = {};
25
var moment = (typeof window != 'undefined' && typeof window.moment != 'undefined') ? window.moment : require('moment');
26
module.exports = factory(moment, jQuery);
29
root.daterangepicker = factory(root.moment, root.jQuery);
31
}(this, function(moment, $) {
32
var DateRangePicker = function(element, options, cb) {
35
this.parentEl = 'body';
36
this.element = $(element);
37
this.startDate = moment().startOf('day');
38
this.endDate = moment().endOf('day');
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;
60
if (this.element.hasClass('pull-right'))
64
if (this.element.hasClass('dropup'))
67
this.buttonClasses = 'btn btn-sm';
68
this.applyButtonClasses = 'btn-primary';
69
this.cancelButtonClasses = 'btn-default';
73
format: moment.localeData().longDateFormat('L'),
76
cancelLabel: 'Cancel',
78
customRangeLabel: 'Custom Range',
79
daysOfWeek: moment.weekdaysMin(),
80
monthNames: moment.monthsShort(),
81
firstDay: moment.localeData().firstDayOfWeek()
84
this.callback = function() { };
87
this.isShowing = false;
88
this.leftCalendar = {};
89
this.rightCalendar = {};
92
if (typeof options !== 'object' || options === null)
97
options = $.extend(this.element.data(), options);
100
if (typeof options.template !== 'string' && !(options.template instanceof $))
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>' +
108
'<div class="drp-calendar right">' +
109
'<div class="calendar-table"></div>' +
110
'<div class="calendar-time"></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> ' +
119
this.parentEl = (options.parentEl && $(options.parentEl).length) ? $(options.parentEl) : $(this.parentEl);
120
this.container = $(options.template).appendTo(this.parentEl);
126
if (typeof options.locale === 'object') {
128
if (typeof options.locale.direction === 'string')
129
this.locale.direction = options.locale.direction;
131
if (typeof options.locale.format === 'string')
132
this.locale.format = options.locale.format;
134
if (typeof options.locale.separator === 'string')
135
this.locale.separator = options.locale.separator;
137
if (typeof options.locale.daysOfWeek === 'object')
138
this.locale.daysOfWeek = options.locale.daysOfWeek.slice();
140
if (typeof options.locale.monthNames === 'object')
141
this.locale.monthNames = options.locale.monthNames.slice();
143
if (typeof options.locale.firstDay === 'number')
144
this.locale.firstDay = options.locale.firstDay;
146
if (typeof options.locale.applyLabel === 'string')
147
this.locale.applyLabel = options.locale.applyLabel;
149
if (typeof options.locale.cancelLabel === 'string')
150
this.locale.cancelLabel = options.locale.cancelLabel;
152
if (typeof options.locale.weekLabel === 'string')
153
this.locale.weekLabel = options.locale.weekLabel;
155
if (typeof options.locale.customRangeLabel === 'string'){
157
var elem = document.createElement('textarea');
158
elem.innerHTML = options.locale.customRangeLabel;
159
var rangeHtml = elem.value;
160
this.locale.customRangeLabel = rangeHtml;
163
this.container.addClass(this.locale.direction);
165
if (typeof options.startDate === 'string')
166
this.startDate = moment(options.startDate, this.locale.format);
168
if (typeof options.endDate === 'string')
169
this.endDate = moment(options.endDate, this.locale.format);
171
if (typeof options.minDate === 'string')
172
this.minDate = moment(options.minDate, this.locale.format);
174
if (typeof options.maxDate === 'string')
175
this.maxDate = moment(options.maxDate, this.locale.format);
177
if (typeof options.startDate === 'object')
178
this.startDate = moment(options.startDate);
180
if (typeof options.endDate === 'object')
181
this.endDate = moment(options.endDate);
183
if (typeof options.minDate === 'object')
184
this.minDate = moment(options.minDate);
186
if (typeof options.maxDate === 'object')
187
this.maxDate = moment(options.maxDate);
190
if (this.minDate && this.startDate.isBefore(this.minDate))
191
this.startDate = this.minDate.clone();
194
if (this.maxDate && this.endDate.isAfter(this.maxDate))
195
this.endDate = this.maxDate.clone();
197
if (typeof options.applyButtonClasses === 'string')
198
this.applyButtonClasses = options.applyButtonClasses;
200
if (typeof options.applyClass === 'string')
201
this.applyButtonClasses = options.applyClass;
203
if (typeof options.cancelButtonClasses === 'string')
204
this.cancelButtonClasses = options.cancelButtonClasses;
206
if (typeof options.cancelClass === 'string')
207
this.cancelButtonClasses = options.cancelClass;
209
if (typeof options.maxSpan === 'object')
210
this.maxSpan = options.maxSpan;
212
if (typeof options.dateLimit === 'object')
213
this.maxSpan = options.dateLimit;
215
if (typeof options.opens === 'string')
216
this.opens = options.opens;
218
if (typeof options.drops === 'string')
219
this.drops = options.drops;
221
if (typeof options.showWeekNumbers === 'boolean')
222
this.showWeekNumbers = options.showWeekNumbers;
224
if (typeof options.showISOWeekNumbers === 'boolean')
225
this.showISOWeekNumbers = options.showISOWeekNumbers;
227
if (typeof options.buttonClasses === 'string')
228
this.buttonClasses = options.buttonClasses;
230
if (typeof options.buttonClasses === 'object')
231
this.buttonClasses = options.buttonClasses.join(' ');
233
if (typeof options.showDropdowns === 'boolean')
234
this.showDropdowns = options.showDropdowns;
236
if (typeof options.minYear === 'number')
237
this.minYear = options.minYear;
239
if (typeof options.maxYear === 'number')
240
this.maxYear = options.maxYear;
242
if (typeof options.showCustomRangeLabel === 'boolean')
243
this.showCustomRangeLabel = options.showCustomRangeLabel;
245
if (typeof options.singleDatePicker === 'boolean') {
246
this.singleDatePicker = options.singleDatePicker;
247
if (this.singleDatePicker)
248
this.endDate = this.startDate.clone();
251
if (typeof options.timePicker === 'boolean')
252
this.timePicker = options.timePicker;
254
if (typeof options.timePickerSeconds === 'boolean')
255
this.timePickerSeconds = options.timePickerSeconds;
257
if (typeof options.timePickerIncrement === 'number')
258
this.timePickerIncrement = options.timePickerIncrement;
260
if (typeof options.timePicker24Hour === 'boolean')
261
this.timePicker24Hour = options.timePicker24Hour;
263
if (typeof options.autoApply === 'boolean')
264
this.autoApply = options.autoApply;
266
if (typeof options.autoUpdateInput === 'boolean')
267
this.autoUpdateInput = options.autoUpdateInput;
269
if (typeof options.linkedCalendars === 'boolean')
270
this.linkedCalendars = options.linkedCalendars;
272
if (typeof options.isInvalidDate === 'function')
273
this.isInvalidDate = options.isInvalidDate;
275
if (typeof options.isCustomDate === 'function')
276
this.isCustomDate = options.isCustomDate;
278
if (typeof options.alwaysShowCalendars === 'boolean')
279
this.alwaysShowCalendars = options.alwaysShowCalendars;
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());
290
var start, end, range;
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);
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);
307
if (start !== null && end !== null) {
308
this.setStartDate(start);
309
this.setEndDate(end);
314
if (typeof options.ranges === 'object') {
315
for (range in options.ranges) {
317
if (typeof options.ranges[range][0] === 'string')
318
start = moment(options.ranges[range][0], this.locale.format);
320
start = moment(options.ranges[range][0]);
322
if (typeof options.ranges[range][1] === 'string')
323
end = moment(options.ranges[range][1], this.locale.format);
325
end = moment(options.ranges[range][1]);
329
if (this.minDate && start.isBefore(this.minDate))
330
start = this.minDate.clone();
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();
340
if ((this.minDate && end.isBefore(this.minDate, this.timepicker ? 'minute' : 'day'))
341
|| (maxDate && start.isAfter(maxDate, this.timepicker ? 'minute' : 'day')))
345
var elem = document.createElement('textarea');
346
elem.innerHTML = range;
347
var rangeHtml = elem.value;
349
this.ranges[rangeHtml] = [start, end];
353
for (range in this.ranges) {
354
list += '<li data-range-key="' + range + '">' + range + '</li>';
356
if (this.showCustomRangeLabel) {
357
list += '<li data-range-key="' + this.locale.customRangeLabel + '">' + this.locale.customRangeLabel + '</li>';
360
this.container.find('.ranges').prepend(list);
363
if (typeof cb === 'function') {
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();
374
if (this.timePicker && this.autoApply)
375
this.autoApply = false;
377
if (this.autoApply) {
378
this.container.addClass('auto-apply');
381
if (typeof options.ranges === 'object')
382
this.container.addClass('show-ranges');
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 && this.autoApply) {
390
this.container.addClass('auto-apply');
394
if ((typeof options.ranges === 'undefined' && !this.singleDatePicker) || this.alwaysShowCalendars) {
395
this.container.addClass('show-calendar');
398
this.container.addClass('opens' + this.opens);
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);
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));
422
this.container.find('.ranges')
423
.on('click.daterangepicker', 'li', $.proxy(this.clickRange, this));
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));
429
if (this.element.is('input') || this.element.is('button')) {
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)
437
this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
438
this.element.on('keydown.daterangepicker', $.proxy(this.toggle, this));
445
this.updateElement();
449
DateRangePicker.prototype = {
451
constructor: DateRangePicker,
453
setStartDate: function(startDate) {
454
if (typeof startDate === 'string')
455
this.startDate = moment(startDate, this.locale.format);
457
if (typeof startDate === 'object')
458
this.startDate = moment(startDate);
460
if (!this.timePicker)
461
this.startDate = this.startDate.startOf('day');
463
if (this.timePicker && this.timePickerIncrement)
464
this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
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);
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);
479
this.updateElement();
481
this.updateMonthsInView();
484
setEndDate: function(endDate) {
485
if (typeof endDate === 'string')
486
this.endDate = moment(endDate, this.locale.format);
488
if (typeof endDate === 'object')
489
this.endDate = moment(endDate);
491
if (!this.timePicker)
492
this.endDate = this.endDate.endOf('day');
494
if (this.timePicker && this.timePickerIncrement)
495
this.endDate.minute(Math.round(this.endDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
497
if (this.endDate.isBefore(this.startDate))
498
this.endDate = this.startDate.clone();
500
if (this.maxDate && this.endDate.isAfter(this.maxDate))
501
this.endDate = this.maxDate.clone();
503
if (this.maxSpan && this.startDate.clone().add(this.maxSpan).isBefore(this.endDate))
504
this.endDate = this.startDate.clone().add(this.maxSpan);
506
this.previousRightTime = this.endDate.clone();
508
this.container.find('.drp-selected').html(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
511
this.updateElement();
513
this.updateMonthsInView();
516
isInvalidDate: function() {
520
isCustomDate: function() {
524
updateView: function() {
525
if (this.timePicker) {
526
this.renderTimePicker('left');
527
this.renderTimePicker('right');
529
this.container.find('.right .calendar-time select').prop('disabled', true).addClass('disabled');
531
this.container.find('.right .calendar-time select').prop('disabled', false).removeClass('disabled');
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();
541
updateMonthsInView: function() {
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'))
548
(this.endDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.endDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
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);
557
this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
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');
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');
572
updateCalendars: function() {
574
if (this.timePicker) {
575
var hour, minute, second;
577
hour = parseInt(this.container.find('.left .hourselect').val(), 10);
578
minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
580
minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10);
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)
587
if (ampm === 'AM' && hour === 12)
591
hour = parseInt(this.container.find('.right .hourselect').val(), 10);
592
minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
594
minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10);
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)
601
if (ampm === 'AM' && hour === 12)
605
this.leftCalendar.month.hour(hour).minute(minute).second(second);
606
this.rightCalendar.month.hour(hour).minute(minute).second(second);
609
this.renderCalendar('left');
610
this.renderCalendar('right');
613
this.container.find('.ranges li').removeClass('active');
614
if (this.endDate == null) return;
616
this.calculateChosenLabel();
619
renderCalendar: function(side) {
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();
641
calendar.firstDay = firstDay;
642
calendar.lastDay = lastDay;
644
for (var i = 0; i < 6; i++) {
649
var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
650
if (startDay > daysInLastMonth)
653
if (dayOfWeek == this.locale.firstDay)
654
startDay = daysInLastMonth - 6;
656
var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]);
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) {
664
calendar[row][col] = curDate.clone().hour(hour).minute(minute).second(second);
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();
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();
678
if (side == 'left') {
679
this.leftCalendar.calendar = calendar;
681
this.rightCalendar.calendar = calendar;
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'};
693
var html = '<table class="table-condensed">';
698
if (this.showWeekNumbers || this.showISOWeekNumbers)
701
if ((!minDate || minDate.isBefore(calendar.firstDay)) && (!this.linkedCalendars || side == 'left')) {
702
html += '<th class="prev available"><span></span></th>';
707
var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
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;
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>";
724
monthHtml += "<option value='" + m + "'" +
725
(m === currentMonth ? " selected='selected'" : "") +
726
" disabled='disabled'>" + this.locale.monthNames[m] + "</option>";
729
monthHtml += "</select>";
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>';
737
yearHtml += '</select>';
739
dateHtml = monthHtml + yearHtml;
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>';
753
if (this.showWeekNumbers || this.showISOWeekNumbers)
754
html += '<th class="week">' + this.locale.weekLabel + '</th>';
756
$.each(this.locale.daysOfWeek, function(index, dayOfWeek) {
757
html += '<th>' + dayOfWeek + '</th>';
766
if (this.endDate == null && this.maxSpan) {
767
var maxLimit = this.startDate.clone().add(this.maxSpan).endOf('day');
768
if (!maxDate || maxLimit.isBefore(maxDate)) {
773
for (var row = 0; row < 6; row++) {
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>';
782
for (var col = 0; col < 7; col++) {
787
if (calendar[row][col].isSame(new Date(), "day"))
788
classes.push('today');
791
if (calendar[row][col].isoWeekday() > 5)
792
classes.push('weekend');
795
if (calendar[row][col].month() != calendar[1][1].month())
796
classes.push('off', 'ends');
799
if (this.minDate && calendar[row][col].isBefore(this.minDate, 'day'))
800
classes.push('off', 'disabled');
803
if (maxDate && calendar[row][col].isAfter(maxDate, 'day'))
804
classes.push('off', 'disabled');
807
if (this.isInvalidDate(calendar[row][col]))
808
classes.push('off', 'disabled');
811
if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD'))
812
classes.push('active', 'start-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');
819
if (this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate)
820
classes.push('in-range');
823
var isCustom = this.isCustomDate(calendar[row][col]);
824
if (isCustom !== false) {
825
if (typeof isCustom === 'string')
826
classes.push(isCustom);
828
Array.prototype.push.apply(classes, isCustom);
831
var cname = '', disabled = false;
832
for (var i = 0; i < classes.length; i++) {
833
cname += classes[i] + ' ';
834
if (classes[i] == 'disabled')
838
cname += 'available';
840
html += '<td class="' + cname.replace(/^\s+|\s+$/g, '') + '" data-title="' + 'r' + row + 'c' + col + '">' + calendar[row][col].date() + '</td>';
849
this.container.find('.drp-calendar.' + side + ' .calendar-table').html(html);
853
renderTimePicker: function(side) {
857
if (side == 'right' && !this.endDate) return;
859
var html, selected, minDate, maxDate = this.maxDate;
861
if (this.maxSpan && (!this.maxDate || this.startDate.clone().add(this.maxSpan).isBefore(this.maxDate)))
862
maxDate = this.startDate.clone().add(this.maxSpan);
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;
872
var timeSelector = this.container.find('.drp-calendar.right .calendar-time');
873
if (timeSelector.html() != '') {
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());
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)
889
if (selected.isBefore(this.startDate))
890
selected = this.startDate.clone();
892
if (maxDate && selected.isAfter(maxDate))
893
selected = maxDate.clone();
901
html = '<select class="hourselect">';
903
var start = this.timePicker24Hour ? 0 : 1;
904
var end = this.timePicker24Hour ? 23 : 12;
906
for (var i = start; i <= end; i++) {
908
if (!this.timePicker24Hour)
909
i_in_24 = selected.hour() >= 12 ? (i == 12 ? 12 : i + 12) : (i == 12 ? 0 : i);
911
var time = selected.clone().hour(i_in_24);
912
var disabled = false;
913
if (minDate && time.minute(59).isBefore(minDate))
915
if (maxDate && time.minute(0).isAfter(maxDate))
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>';
923
html += '<option value="' + i + '">' + i + '</option>';
927
html += '</select> ';
933
html += ': <select class="minuteselect">';
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);
939
var disabled = false;
940
if (minDate && time.second(59).isBefore(minDate))
942
if (maxDate && time.second(0).isAfter(maxDate))
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>';
950
html += '<option value="' + i + '">' + padded + '</option>';
954
html += '</select> ';
960
if (this.timePickerSeconds) {
961
html += ': <select class="secondselect">';
963
for (var i = 0; i < 60; i++) {
964
var padded = i < 10 ? '0' + i : i;
965
var time = selected.clone().second(i);
967
var disabled = false;
968
if (minDate && time.isBefore(minDate))
970
if (maxDate && time.isAfter(maxDate))
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>';
978
html += '<option value="' + i + '">' + padded + '</option>';
982
html += '</select> ';
989
if (!this.timePicker24Hour) {
990
html += '<select class="ampmselect">';
995
if (minDate && selected.clone().hour(12).minute(0).second(0).isBefore(minDate))
996
am_html = ' disabled="disabled" class="disabled"';
998
if (maxDate && selected.clone().hour(0).minute(0).second(0).isAfter(maxDate))
999
pm_html = ' disabled="disabled" class="disabled"';
1001
if (selected.hour() >= 12) {
1002
html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>';
1004
html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>';
1007
html += '</select>';
1010
this.container.find('.drp-calendar.' + side + ' .calendar-time').html(html);
1014
updateFormInputs: function() {
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);
1019
this.container.find('button.applyBtn').prop('disabled', true);
1025
var parentOffset = { top: 0, left: 0 },
1029
var parentRightEdge = $(window).width();
1030
if (!this.parentEl.is('body')) {
1032
top: this.parentEl.offset().top - this.parentEl.scrollTop(),
1033
left: this.parentEl.offset().left - this.parentEl.scrollLeft()
1035
parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
1040
containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
1041
if (containerTop + this.container.outerHeight() >= this.parentEl[0].scrollHeight) {
1042
containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
1047
containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
1050
containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
1055
this.container.css({
1060
var containerWidth = this.container.outerWidth();
1062
this.container.toggleClass('drop-up', drops == 'up');
1064
if (this.opens == 'left') {
1065
var containerRight = parentRightEdge - this.element.offset().left - this.element.outerWidth();
1066
if (containerWidth + containerRight > $(window).width()) {
1067
this.container.css({
1073
this.container.css({
1075
right: containerRight,
1079
} else if (this.opens == 'center') {
1080
var containerLeft = this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
1081
- containerWidth / 2;
1082
if (containerLeft < 0) {
1083
this.container.css({
1088
} else if (containerLeft + containerWidth > $(window).width()) {
1089
this.container.css({
1095
this.container.css({
1097
left: containerLeft,
1102
var containerLeft = this.element.offset().left - parentOffset.left;
1103
if (containerLeft + containerWidth > $(window).width()) {
1104
this.container.css({
1110
this.container.css({
1112
left: containerLeft,
1120
if (this.isShowing) return;
1123
this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this);
1127
.on('mousedown.daterangepicker', this._outsideClickProxy)
1129
.on('touchend.daterangepicker', this._outsideClickProxy)
1131
.on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
1133
.on('focusin.daterangepicker', this._outsideClickProxy);
1136
$(window).on('resize.daterangepicker', $.proxy(function(e) { this.move(e); }, this));
1138
this.oldStartDate = this.startDate.clone();
1139
this.oldEndDate = this.endDate.clone();
1140
this.previousRightTime = this.endDate.clone();
1143
this.container.show();
1145
this.element.trigger('show.daterangepicker', this);
1146
this.isShowing = true;
1150
if (!this.isShowing) return;
1153
if (!this.endDate) {
1154
this.startDate = this.oldStartDate.clone();
1155
this.endDate = this.oldEndDate.clone();
1159
if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
1160
this.callback(this.startDate.clone(), this.endDate.clone(), this.chosenLabel);
1163
this.updateElement();
1165
$(document).off('.daterangepicker');
1166
$(window).off('.daterangepicker');
1167
this.container.hide();
1168
this.element.trigger('hide.daterangepicker', this);
1169
this.isShowing = false;
1172
toggle: function(e) {
1173
if (this.isShowing) {
1180
outsideClick: function(e) {
1181
var target = $(e.target);
1186
e.type == "focusin" ||
1187
target.closest(this.element).length ||
1188
target.closest(this.container).length ||
1189
target.closest('.calendar-table').length
1192
this.element.trigger('outsideClick.daterangepicker', this);
1195
showCalendars: function() {
1196
this.container.addClass('show-calendar');
1198
this.element.trigger('showCalendar.daterangepicker', this);
1201
hideCalendars: function() {
1202
this.container.removeClass('show-calendar');
1203
this.element.trigger('hideCalendar.daterangepicker', this);
1206
clickRange: function(e) {
1207
var label = e.target.getAttribute('data-range-key');
1208
this.chosenLabel = label;
1209
if (label == this.locale.customRangeLabel) {
1210
this.showCalendars();
1212
var dates = this.ranges[label];
1213
this.startDate = dates[0];
1214
this.endDate = dates[1];
1216
if (!this.timePicker) {
1217
this.startDate.startOf('day');
1218
this.endDate.endOf('day');
1221
if (!this.alwaysShowCalendars)
1222
this.hideCalendars();
1227
clickPrev: function(e) {
1228
var cal = $(e.target).parents('.drp-calendar');
1229
if (cal.hasClass('left')) {
1230
this.leftCalendar.month.subtract(1, 'month');
1231
if (this.linkedCalendars)
1232
this.rightCalendar.month.subtract(1, 'month');
1234
this.rightCalendar.month.subtract(1, 'month');
1236
this.updateCalendars();
1239
clickNext: function(e) {
1240
var cal = $(e.target).parents('.drp-calendar');
1241
if (cal.hasClass('left')) {
1242
this.leftCalendar.month.add(1, 'month');
1244
this.rightCalendar.month.add(1, 'month');
1245
if (this.linkedCalendars)
1246
this.leftCalendar.month.add(1, 'month');
1248
this.updateCalendars();
1251
hoverDate: function(e) {
1254
if (!$(e.target).hasClass('available')) return;
1256
var title = $(e.target).attr('data-title');
1257
var row = title.substr(1, 1);
1258
var col = title.substr(3, 1);
1259
var cal = $(e.target).parents('.drp-calendar');
1260
var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1263
var leftCalendar = this.leftCalendar;
1264
var rightCalendar = this.rightCalendar;
1265
var startDate = this.startDate;
1266
if (!this.endDate) {
1267
this.container.find('.drp-calendar tbody td').each(function(index, el) {
1270
if ($(el).hasClass('week')) return;
1272
var title = $(el).attr('data-title');
1273
var row = title.substr(1, 1);
1274
var col = title.substr(3, 1);
1275
var cal = $(el).parents('.drp-calendar');
1276
var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col];
1278
if ((dt.isAfter(startDate) && dt.isBefore(date)) || dt.isSame(date, 'day')) {
1279
$(el).addClass('in-range');
1281
$(el).removeClass('in-range');
1289
clickDate: function(e) {
1291
if (!$(e.target).hasClass('available')) return;
1293
var title = $(e.target).attr('data-title');
1294
var row = title.substr(1, 1);
1295
var col = title.substr(3, 1);
1296
var cal = $(e.target).parents('.drp-calendar');
1297
var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1308
if (this.endDate || date.isBefore(this.startDate, 'day')) {
1309
if (this.timePicker) {
1310
var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
1311
if (!this.timePicker24Hour) {
1312
var ampm = this.container.find('.left .ampmselect').val();
1313
if (ampm === 'PM' && hour < 12)
1315
if (ampm === 'AM' && hour === 12)
1318
var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
1319
if (isNaN(minute)) {
1320
minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10);
1322
var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
1323
date = date.clone().hour(hour).minute(minute).second(second);
1325
this.endDate = null;
1326
this.setStartDate(date.clone());
1327
} else if (!this.endDate && date.isBefore(this.startDate)) {
1330
this.setEndDate(this.startDate.clone());
1332
if (this.timePicker) {
1333
var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
1334
if (!this.timePicker24Hour) {
1335
var ampm = this.container.find('.right .ampmselect').val();
1336
if (ampm === 'PM' && hour < 12)
1338
if (ampm === 'AM' && hour === 12)
1341
var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
1342
if (isNaN(minute)) {
1343
minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10);
1345
var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
1346
date = date.clone().hour(hour).minute(minute).second(second);
1348
this.setEndDate(date.clone());
1349
if (this.autoApply) {
1350
this.calculateChosenLabel();
1355
if (this.singleDatePicker) {
1356
this.setEndDate(this.startDate);
1357
if (!this.timePicker && this.autoApply)
1364
e.stopPropagation();
1368
calculateChosenLabel: function () {
1369
var customRange = true;
1371
for (var range in this.ranges) {
1372
if (this.timePicker) {
1373
var format = this.timePickerSeconds ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD HH:mm";
1375
if (this.startDate.format(format) == this.ranges[range][0].format(format) && this.endDate.format(format) == this.ranges[range][1].format(format)) {
1376
customRange = false;
1377
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
1382
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')) {
1383
customRange = false;
1384
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
1391
if (this.showCustomRangeLabel) {
1392
this.chosenLabel = this.container.find('.ranges li:last').addClass('active').attr('data-range-key');
1394
this.chosenLabel = null;
1396
this.showCalendars();
1400
clickApply: function(e) {
1402
this.element.trigger('apply.daterangepicker', this);
1405
clickCancel: function(e) {
1406
this.startDate = this.oldStartDate;
1407
this.endDate = this.oldEndDate;
1409
this.element.trigger('cancel.daterangepicker', this);
1412
monthOrYearChanged: function(e) {
1413
var isLeft = $(e.target).closest('.drp-calendar').hasClass('left'),
1414
leftOrRight = isLeft ? 'left' : 'right',
1415
cal = this.container.find('.drp-calendar.'+leftOrRight);
1418
var month = parseInt(cal.find('.monthselect').val(), 10);
1419
var year = cal.find('.yearselect').val();
1422
if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
1423
month = this.startDate.month();
1424
year = this.startDate.year();
1429
if (year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) {
1430
month = this.minDate.month();
1431
year = this.minDate.year();
1436
if (year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) {
1437
month = this.maxDate.month();
1438
year = this.maxDate.year();
1443
this.leftCalendar.month.month(month).year(year);
1444
if (this.linkedCalendars)
1445
this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month');
1447
this.rightCalendar.month.month(month).year(year);
1448
if (this.linkedCalendars)
1449
this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month');
1451
this.updateCalendars();
1454
timeChanged: function(e) {
1456
var cal = $(e.target).closest('.drp-calendar'),
1457
isLeft = cal.hasClass('left');
1459
var hour = parseInt(cal.find('.hourselect').val(), 10);
1460
var minute = parseInt(cal.find('.minuteselect').val(), 10);
1461
if (isNaN(minute)) {
1462
minute = parseInt(cal.find('.minuteselect option:last').val(), 10);
1464
var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0;
1466
if (!this.timePicker24Hour) {
1467
var ampm = cal.find('.ampmselect').val();
1468
if (ampm === 'PM' && hour < 12)
1470
if (ampm === 'AM' && hour === 12)
1475
var start = this.startDate.clone();
1477
start.minute(minute);
1478
start.second(second);
1479
this.setStartDate(start);
1480
if (this.singleDatePicker) {
1481
this.endDate = this.startDate.clone();
1482
} else if (this.endDate && this.endDate.format('YYYY-MM-DD') == start.format('YYYY-MM-DD') && this.endDate.isBefore(start)) {
1483
this.setEndDate(start.clone());
1485
} else if (this.endDate) {
1486
var end = this.endDate.clone();
1490
this.setEndDate(end);
1494
this.updateCalendars();
1497
this.updateFormInputs();
1500
this.renderTimePicker('left');
1501
this.renderTimePicker('right');
1505
elementChanged: function() {
1506
if (!this.element.is('input')) return;
1507
if (!this.element.val().length) return;
1509
var dateString = this.element.val().split(this.locale.separator),
1513
if (dateString.length === 2) {
1514
start = moment(dateString[0], this.locale.format);
1515
end = moment(dateString[1], this.locale.format);
1518
if (this.singleDatePicker || start === null || end === null) {
1519
start = moment(this.element.val(), this.locale.format);
1523
if (!start.isValid() || !end.isValid()) return;
1525
this.setStartDate(start);
1526
this.setEndDate(end);
1530
keydown: function(e) {
1532
if ((e.keyCode === 9) || (e.keyCode === 13)) {
1537
if (e.keyCode === 27) {
1539
e.stopPropagation();
1545
updateElement: function() {
1546
if (this.element.is('input') && this.autoUpdateInput) {
1547
var newValue = this.startDate.format(this.locale.format);
1548
if (!this.singleDatePicker) {
1549
newValue += this.locale.separator + this.endDate.format(this.locale.format);
1551
if (newValue !== this.element.val()) {
1552
this.element.val(newValue).trigger('change');
1557
remove: function() {
1558
this.container.remove();
1559
this.element.off('.daterangepicker');
1560
this.element.removeData();
1565
$.fn.daterangepicker = function(options, callback) {
1566
var implementOptions = $.extend(true, {}, $.fn.daterangepicker.defaultOptions, options);
1567
this.each(function() {
1569
if (el.data('daterangepicker'))
1570
el.data('daterangepicker').remove();
1571
el.data('daterangepicker', new DateRangePicker(el, implementOptions, callback));
1576
return DateRangePicker;