';
+ html += '';
+ html += '';
+
+ //adjust maxDate to reflect the dateLimit setting in order to
+ //grey out end dates beyond the dateLimit
+ if (this.endDate == null && this.dateLimit) {
+ var maxLimit = this.startDate.clone().add(this.dateLimit).endOf('day');
+ if (!maxDate || maxLimit.isBefore(maxDate)) {
+ maxDate = maxLimit;
+ }
+ }
+
+ for (var row = 0; row < 6; row++) {
+ html += '
';
+
+ // add week number
+ if (this.showWeekNumbers)
+ html += '
' + calendar[row][0].week() + '
';
+ else if (this.showISOWeekNumbers)
+ html += '
' + calendar[row][0].isoWeek() + '
';
+
+ for (var col = 0; col < 7; col++) {
+
+ var classes = [];
+
+ //highlight today's date
+ if (calendar[row][col].isSame(new Date(), "day"))
+ classes.push('today');
+
+ //highlight weekends
+ if (calendar[row][col].isoWeekday() > 5)
+ classes.push('weekend');
+
+ //grey out the dates in other months displayed at beginning and end of this calendar
+ if (calendar[row][col].month() != calendar[1][1].month())
+ classes.push('off');
+
+ //don't allow selection of dates before the minimum date
+ if (this.minDate && calendar[row][col].isBefore(this.minDate, 'day'))
+ classes.push('off', 'disabled');
+
+ //don't allow selection of dates after the maximum date
+ if (maxDate && calendar[row][col].isAfter(maxDate, 'day'))
+ classes.push('off', 'disabled');
+
+ //don't allow selection of date if a custom function decides it's invalid
+ if (this.isInvalidDate(calendar[row][col]))
+ classes.push('off', 'disabled');
+
+ //highlight the currently selected start date
+ if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD'))
+ classes.push('active', 'start-date');
+
+ //highlight the currently selected end date
+ if (this.endDate != null && calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD'))
+ classes.push('active', 'end-date');
+
+ //highlight dates in-between the selected dates
+ if (this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate)
+ classes.push('in-range');
+
+ var cname = '', disabled = false;
+ for (var i = 0; i < classes.length; i++) {
+ cname += classes[i] + ' ';
+ if (classes[i] == 'disabled')
+ disabled = true;
+ }
+ if (!disabled)
+ cname += 'available';
+
+ html += '
' + calendar[row][col].date() + '
';
+
+ }
+ html += '
';
+ }
+
+ html += '';
+ html += '
';
+
+ this.container.find('.calendar.' + side + ' .calendar-table').html(html);
+
+ },
+
+ renderTimePicker: function(side) {
+
+ var html, selected, minDate, maxDate = this.maxDate;
+
+ if (this.dateLimit && (!this.maxDate || this.startDate.clone().add(this.dateLimit).isAfter(this.maxDate)))
+ maxDate = this.startDate.clone().add(this.dateLimit);
+
+ if (side == 'left') {
+ selected = this.startDate.clone();
+ minDate = this.minDate;
+ } else if (side == 'right') {
+ selected = this.endDate ? this.endDate.clone() : this.previousRightTime.clone();
+ minDate = this.startDate;
+
+ //Preserve the time already selected
+ var timeSelector = this.container.find('.calendar.right .calendar-time div');
+ if (timeSelector.html() != '') {
+
+ selected.hour(timeSelector.find('.hourselect option:selected').val() || selected.hour());
+ selected.minute(timeSelector.find('.minuteselect option:selected').val() || selected.minute());
+ selected.second(timeSelector.find('.secondselect option:selected').val() || selected.second());
+
+ if (!this.timePicker24Hour) {
+ var ampm = timeSelector.find('.ampmselect option:selected').val();
+ if (ampm === 'PM' && selected.hour() < 12)
+ selected.hour(selected.hour() + 12);
+ if (ampm === 'AM' && selected.hour() === 12)
+ selected.hour(0);
+ }
+
+ if (selected.isBefore(this.startDate))
+ selected = this.startDate.clone();
+
+ if (selected.isAfter(maxDate))
+ selected = maxDate.clone();
+
+ }
+ }
+
+ //
+ // hours
+ //
+
+ html = ' ';
+
+ //
+ // minutes
+ //
+
+ html += ': ';
+
+ //
+ // seconds
+ //
+
+ if (this.timePickerSeconds) {
+ html += ': ';
+ }
+
+ //
+ // AM/PM
+ //
+
+ if (!this.timePicker24Hour) {
+ html += '';
+ }
+
+ this.container.find('.calendar.' + side + ' .calendar-time div').html(html);
+
+ },
+
+ updateFormInputs: function() {
+
+ //ignore mouse movements while an above-calendar text input has focus
+ if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
+ return;
+
+ this.container.find('input[name=daterangepicker_start]').val(this.startDate.format(this.locale.format));
+ if (this.endDate)
+ this.container.find('input[name=daterangepicker_end]').val(this.endDate.format(this.locale.format));
+
+ if (this.singleDatePicker || (this.endDate && (this.startDate.isBefore(this.endDate) || this.startDate.isSame(this.endDate)))) {
this.container.find('button.applyBtn').removeAttr('disabled');
} else {
this.container.find('button.applyBtn').attr('disabled', 'disabled');
}
+
},
- updateFromControl: function () {
- if (!this.element.is('input')) return;
- if (!this.element.val().length) return;
-
- var dateString = this.element.val().split(this.separator),
- start = null,
- end = null;
-
- if(dateString.length === 2) {
- start = moment(dateString[0], this.format).utcOffset(this.timeZone);
- end = moment(dateString[1], this.format).utcOffset(this.timeZone);
- }
-
- if (this.singleDatePicker || start === null || end === null) {
- start = moment(this.element.val(), this.format).utcOffset(this.timeZone);
- end = start;
- }
-
- if (end.isBefore(start)) return;
-
- this.oldStartDate = this.startDate.clone();
- this.oldEndDate = this.endDate.clone();
-
- this.startDate = start;
- this.endDate = end;
-
- if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
- this.notify();
-
- this.updateCalendars();
- },
-
- keydown: function (e) {
- //hide on tab or enter
- if ((e.keyCode === 9) || (e.keyCode === 13)) {
- this.hide();
- }
- },
-
- notify: function () {
- this.updateView();
- this.cb(this.startDate, this.endDate, this.chosenLabel);
- },
-
- move: function () {
+ move: function() {
var parentOffset = { top: 0, left: 0 },
- containerTop;
+ containerTop;
var parentRightEdge = $(window).width();
if (!this.parentEl.is('body')) {
parentOffset = {
@@ -560,11 +1007,11 @@
};
parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
}
-
+
if (this.drops == 'up')
- containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
+ containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
else
- containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
+ containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
this.container[this.drops == 'up' ? 'addClass' : 'removeClass']('dropup');
if (this.opens == 'left') {
@@ -607,23 +1054,12 @@
}
},
- toggle: function (e) {
- if (this.element.hasClass('active')) {
- this.hide();
- } else {
- this.show();
- }
- },
-
- show: function (e) {
+ show: function(e) {
if (this.isShowing) return;
- this.element.addClass('active');
- this.container.show();
- this.move();
-
// Create a click proxy that is private to this instance of datepicker, for unbinding
- this._outsideClickProxy = $.proxy(function (e) { this.outsideClick(e); }, this);
+ this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this);
+
// Bind global datepicker mousedown for hiding and
$(document)
.on('mousedown.daterangepicker', this._outsideClickProxy)
@@ -634,11 +1070,52 @@
// and also close when focus changes to outside the picker (eg. tabbing between controls)
.on('focusin.daterangepicker', this._outsideClickProxy);
- this.isShowing = true;
+ // Reposition the picker if the window is resized while it's open
+ $(window).on('resize.daterangepicker', $.proxy(function(e) { this.move(e); }, this));
+
+ this.oldStartDate = this.startDate.clone();
+ this.oldEndDate = this.endDate.clone();
+ this.previousRightTime = this.endDate.clone();
+
+ this.updateView();
+ this.container.show();
+ this.move();
this.element.trigger('show.daterangepicker', this);
+ this.isShowing = true;
},
- outsideClick: function (e) {
+ hide: function(e) {
+ if (!this.isShowing) return;
+
+ //incomplete date selection, revert to last values
+ if (!this.endDate) {
+ this.startDate = this.oldStartDate.clone();
+ this.endDate = this.oldEndDate.clone();
+ }
+
+ //if a new date range was selected, invoke the user callback function
+ if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
+ this.callback(this.startDate, this.endDate, this.chosenLabel);
+
+ //if picker is attached to a text input, update it
+ this.updateElement();
+
+ $(document).off('.daterangepicker');
+ $(window).off('.daterangepicker');
+ this.container.hide();
+ this.element.trigger('hide.daterangepicker', this);
+ this.isShowing = false;
+ },
+
+ toggle: function(e) {
+ if (this.isShowing) {
+ this.hide();
+ } else {
+ this.show();
+ }
+ },
+
+ outsideClick: function(e) {
var target = $(e.target);
// if the page is clicked anywhere except within the daterangerpicker/button
// itself then call this.hide()
@@ -647,42 +1124,11 @@
e.type == "focusin" ||
target.closest(this.element).length ||
target.closest(this.container).length ||
- target.closest('.calendar-date').length
+ target.closest('.calendar-table').length
) return;
this.hide();
},
- hide: function (e) {
- if (!this.isShowing) return;
-
- $(document)
- .off('.daterangepicker');
-
- this.element.removeClass('active');
- this.container.hide();
-
- if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
- this.notify();
-
- this.oldStartDate = this.startDate.clone();
- this.oldEndDate = this.endDate.clone();
-
- this.isShowing = false;
- this.element.trigger('hide.daterangepicker', this);
- },
-
- enterRange: function (e) {
- // mouse pointer has entered a range label
- var label = e.target.innerHTML;
- if (label == this.locale.customRangeLabel) {
- this.updateView();
- } else {
- var dates = this.ranges[label];
- this.container.find('input[name=daterangepicker_start]').val(dates[0].format(this.format));
- this.container.find('input[name=daterangepicker_end]').val(dates[1].format(this.format));
- }
- },
-
showCalendars: function() {
this.container.addClass('show-calendar');
this.move();
@@ -694,48 +1140,30 @@
this.element.trigger('hideCalendar.daterangepicker', this);
},
- // when a date is typed into the start to end date textboxes
- inputsChanged: function (e) {
- var el = $(e.target);
- var date = moment(el.val(), this.format);
- if (!date.isValid()) return;
+ hoverRange: function(e) {
- var startDate, endDate;
- if (el.attr('name') === 'daterangepicker_start') {
- startDate = (false !== this.minDate && date.isBefore(this.minDate)) ? this.minDate : date;
- endDate = this.endDate;
+ //ignore mouse movements while an above-calendar text input has focus
+ if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
+ return;
+
+ var label = e.target.innerHTML;
+ if (label == this.locale.customRangeLabel) {
+ this.updateView();
} else {
- startDate = this.startDate;
- endDate = (false !== this.maxDate && date.isAfter(this.maxDate)) ? this.maxDate : date;
+ var dates = this.ranges[label];
+ this.container.find('input[name=daterangepicker_start]').val(dates[0].format(this.locale.format));
+ this.container.find('input[name=daterangepicker_end]').val(dates[1].format(this.locale.format));
}
- this.setCustomDates(startDate, endDate);
+
},
- inputsKeydown: function(e) {
- if (e.keyCode === 13) {
- this.inputsChanged(e);
- this.notify();
- }
- },
-
- updateInputText: function() {
- if (this.element.is('input') && !this.singleDatePicker) {
- this.element.val(this.startDate.format(this.format) + this.separator + this.endDate.format(this.format));
- this.element.trigger('change');
- } else if (this.element.is('input')) {
- this.element.val(this.endDate.format(this.format));
- this.element.trigger('change');
- }
- },
-
- clickRange: function (e) {
+ clickRange: function(e) {
var label = e.target.innerHTML;
this.chosenLabel = label;
if (label == this.locale.customRangeLabel) {
this.showCalendars();
} else {
var dates = this.ranges[label];
-
this.startDate = dates[0];
this.endDate = dates[1];
@@ -744,130 +1172,193 @@
this.endDate.endOf('day');
}
- this.leftCalendar.month.month(this.startDate.month()).year(this.startDate.year()).hour(this.startDate.hour()).minute(this.startDate.minute());
- this.rightCalendar.month.month(this.endDate.month()).year(this.endDate.year()).hour(this.endDate.hour()).minute(this.endDate.minute());
- this.updateCalendars();
-
- this.updateInputText();
-
- this.hideCalendars();
- this.hide();
- this.element.trigger('apply.daterangepicker', this);
+ if (!this.alwaysShowCalendars)
+ this.hideCalendars();
+ this.clickApply();
}
},
- clickPrev: function (e) {
+ clickPrev: function(e) {
var cal = $(e.target).parents('.calendar');
if (cal.hasClass('left')) {
this.leftCalendar.month.subtract(1, 'month');
+ if (this.linkedCalendars)
+ this.rightCalendar.month.subtract(1, 'month');
} else {
this.rightCalendar.month.subtract(1, 'month');
}
this.updateCalendars();
},
- clickNext: function (e) {
+ clickNext: function(e) {
var cal = $(e.target).parents('.calendar');
if (cal.hasClass('left')) {
this.leftCalendar.month.add(1, 'month');
} else {
this.rightCalendar.month.add(1, 'month');
+ if (this.linkedCalendars)
+ this.leftCalendar.month.add(1, 'month');
}
this.updateCalendars();
},
- hoverDate: function (e) {
+ hoverDate: function(e) {
+
+ //ignore mouse movements while an above-calendar text input has focus
+ if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
+ return;
+
+ //ignore dates that can't be selected
+ if (!$(e.target).hasClass('available')) return;
+
+ //have the text inputs above calendars reflect the date being hovered over
var title = $(e.target).attr('data-title');
var row = title.substr(1, 1);
var col = title.substr(3, 1);
var cal = $(e.target).parents('.calendar');
+ var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
- if (cal.hasClass('left')) {
- this.container.find('input[name=daterangepicker_start]').val(this.leftCalendar.calendar[row][col].format(this.format));
+ if (this.endDate) {
+ this.container.find('input[name=daterangepicker_start]').val(date.format(this.locale.format));
} else {
- this.container.find('input[name=daterangepicker_end]').val(this.rightCalendar.calendar[row][col].format(this.format));
+ this.container.find('input[name=daterangepicker_end]').val(date.format(this.locale.format));
}
+
+ //highlight the dates between the start date and the date being hovered as a potential end date
+ var leftCalendar = this.leftCalendar;
+ var rightCalendar = this.rightCalendar;
+ var startDate = this.startDate;
+ if (!this.endDate) {
+ this.container.find('.calendar td').each(function(index, el) {
+
+ //skip week numbers, only look at dates
+ if ($(el).hasClass('week')) return;
+
+ var title = $(el).attr('data-title');
+ var row = title.substr(1, 1);
+ var col = title.substr(3, 1);
+ var cal = $(el).parents('.calendar');
+ var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col];
+
+ if (dt.isAfter(startDate) && dt.isBefore(date)) {
+ $(el).addClass('in-range');
+ } else {
+ $(el).removeClass('in-range');
+ }
+
+ });
+ }
+
},
- setCustomDates: function(startDate, endDate) {
- this.chosenLabel = this.locale.customRangeLabel;
- if (startDate.isAfter(endDate)) {
- var difference = this.endDate.diff(this.startDate);
- endDate = moment(startDate).add(difference, 'ms');
- if (this.maxDate && endDate.isAfter(this.maxDate)) {
- endDate = this.maxDate.clone();
+ clickDate: function(e) {
+
+ if (!$(e.target).hasClass('available')) return;
+
+ var title = $(e.target).attr('data-title');
+ var row = title.substr(1, 1);
+ var col = title.substr(3, 1);
+ var cal = $(e.target).parents('.calendar');
+ var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
+
+ //
+ // this function needs to do a few things:
+ // * alternate between selecting a start and end date for the range,
+ // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date
+ // * if autoapply is enabled, and an end date was chosen, apply the selection
+ // * if single date picker mode, and time picker isn't enabled, apply the selection immediately
+ //
+
+ if (this.endDate || date.isBefore(this.startDate, 'day')) {
+ if (this.timePicker) {
+ var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
+ if (!this.timePicker24Hour) {
+ var ampm = this.container.find('.left .ampmselect').val();
+ if (ampm === 'PM' && hour < 12)
+ hour += 12;
+ if (ampm === 'AM' && hour === 12)
+ hour = 0;
+ }
+ var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
+ var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
+ date = date.clone().hour(hour).minute(minute).second(second);
+ }
+ this.endDate = null;
+ this.setStartDate(date.clone());
+ } else if (!this.endDate && date.isBefore(this.startDate)) {
+ //special case: clicking the same date for start/end,
+ //but the time of the end date is before the start date
+ this.setEndDate(this.startDate.clone());
+ } else {
+ if (this.timePicker) {
+ var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
+ if (!this.timePicker24Hour) {
+ var ampm = this.container.find('.right .ampmselect').val();
+ if (ampm === 'PM' && hour < 12)
+ hour += 12;
+ if (ampm === 'AM' && hour === 12)
+ hour = 0;
+ }
+ var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
+ var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
+ date = date.clone().hour(hour).minute(minute).second(second);
+ }
+ this.setEndDate(date.clone());
+ if (this.autoApply) {
+ this.calculateChosenLabel();
+ this.clickApply();
}
}
- this.startDate = startDate;
- this.endDate = endDate;
+
+ if (this.singleDatePicker) {
+ this.setEndDate(this.startDate);
+ if (!this.timePicker)
+ this.clickApply();
+ }
this.updateView();
- this.updateCalendars();
+
},
- clickDate: function (e) {
- var title = $(e.target).attr('data-title');
- var row = title.substr(1, 1);
- var col = title.substr(3, 1);
- var cal = $(e.target).parents('.calendar');
-
- var startDate, endDate;
- if (cal.hasClass('left')) {
- startDate = this.leftCalendar.calendar[row][col];
- endDate = this.endDate;
- if (typeof this.dateLimit === 'object') {
- var maxDate = moment(startDate).add(this.dateLimit).startOf('day');
- if (endDate.isAfter(maxDate)) {
- endDate = maxDate;
- }
- }
- } else {
- startDate = this.startDate;
- endDate = this.rightCalendar.calendar[row][col];
- if (typeof this.dateLimit === 'object') {
- var minDate = moment(endDate).subtract(this.dateLimit).startOf('day');
- if (startDate.isBefore(minDate)) {
- startDate = minDate;
- }
- }
- }
-
- if (this.singleDatePicker && cal.hasClass('left')) {
- endDate = startDate.clone();
- } else if (this.singleDatePicker && cal.hasClass('right')) {
- startDate = endDate.clone();
- }
-
- cal.find('td').removeClass('active');
-
- $(e.target).addClass('active');
-
- this.setCustomDates(startDate, endDate);
-
- if (!this.timePicker)
- endDate.endOf('day');
-
- if (this.singleDatePicker && !this.timePicker)
- this.clickApply();
+ calculateChosenLabel: function() {
+ var customRange = true;
+ var i = 0;
+ for (var range in this.ranges) {
+ if (this.timePicker) {
+ if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
+ customRange = false;
+ this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
+ break;
+ }
+ } else {
+ //ignore times when comparing dates if time picker is not enabled
+ 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')) {
+ customRange = false;
+ this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
+ break;
+ }
+ }
+ i++;
+ }
+ if (customRange) {
+ this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
+ this.showCalendars();
+ }
},
- clickApply: function (e) {
- this.updateInputText();
+ clickApply: function(e) {
this.hide();
this.element.trigger('apply.daterangepicker', this);
},
- clickCancel: function (e) {
+ clickCancel: function(e) {
this.startDate = this.oldStartDate;
this.endDate = this.oldEndDate;
- this.chosenLabel = this.oldChosenLabel;
- this.updateView();
- this.updateCalendars();
this.hide();
this.element.trigger('cancel.daterangepicker', this);
},
- updateMonthYear: function (e) {
+ monthOrYearChanged: function(e) {
var isLeft = $(e.target).closest('.calendar').hasClass('left'),
leftOrRight = isLeft ? 'left' : 'right',
cal = this.container.find('.calendar.'+leftOrRight);
@@ -876,7 +1367,7 @@
var month = parseInt(cal.find('.monthselect').val(), 10);
var year = cal.find('.yearselect').val();
- if (!isLeft && !this.singleDatePicker) {
+ if (!isLeft) {
if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
month = this.startDate.month();
year = this.startDate.year();
@@ -897,25 +1388,28 @@
}
}
-
- this[leftOrRight+'Calendar'].month.month(month).year(year);
+ if (isLeft) {
+ this.leftCalendar.month.month(month).year(year);
+ if (this.linkedCalendars)
+ this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month');
+ } else {
+ this.rightCalendar.month.month(month).year(year);
+ if (this.linkedCalendars)
+ this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month');
+ }
this.updateCalendars();
},
- updateTime: function(e) {
+ timeChanged: function(e) {
var cal = $(e.target).closest('.calendar'),
isLeft = cal.hasClass('left');
var hour = parseInt(cal.find('.hourselect').val(), 10);
var minute = parseInt(cal.find('.minuteselect').val(), 10);
- var second = 0;
+ var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0;
- if (this.timePickerSeconds) {
- second = parseInt(cal.find('.secondselect').val(), 10);
- }
-
- if (this.timePicker12Hour) {
+ if (!this.timePicker24Hour) {
var ampm = cal.find('.ampmselect').val();
if (ampm === 'PM' && hour < 12)
hour += 12;
@@ -928,377 +1422,121 @@
start.hour(hour);
start.minute(minute);
start.second(second);
- this.startDate = start;
- this.leftCalendar.month.hour(hour).minute(minute).second(second);
- if (this.singleDatePicker)
- this.endDate = start.clone();
- } else {
+ this.setStartDate(start);
+ if (this.singleDatePicker) {
+ this.endDate = this.startDate.clone();
+ } else if (this.endDate && this.endDate.format('YYYY-MM-DD') == start.format('YYYY-MM-DD') && this.endDate.isBefore(start)) {
+ this.setEndDate(start.clone());
+ }
+ } else if (this.endDate) {
var end = this.endDate.clone();
end.hour(hour);
end.minute(minute);
end.second(second);
- this.endDate = end;
- if (this.singleDatePicker)
- this.startDate = end.clone();
- this.rightCalendar.month.hour(hour).minute(minute).second(second);
+ this.setEndDate(end);
}
- this.updateView();
+ //update the calendars so all clickable dates reflect the new time component
this.updateCalendars();
+
+ //update the form inputs above the calendars with the new time
+ this.updateFormInputs();
+
+ //re-render the time pickers because changing one selection can affect what's enabled in another
+ this.renderTimePicker('left');
+ this.renderTimePicker('right');
+
},
- updateCalendars: function () {
- this.leftCalendar.calendar = this.buildCalendar(this.leftCalendar.month.month(), this.leftCalendar.month.year(), this.leftCalendar.month.hour(), this.leftCalendar.month.minute(), this.leftCalendar.month.second(), 'left');
- this.rightCalendar.calendar = this.buildCalendar(this.rightCalendar.month.month(), this.rightCalendar.month.year(), this.rightCalendar.month.hour(), this.rightCalendar.month.minute(), this.rightCalendar.month.second(), 'right');
- this.container.find('.calendar.left').empty().html(this.renderCalendar(this.leftCalendar.calendar, this.startDate, this.minDate, this.maxDate, 'left'));
- this.container.find('.calendar.right').empty().html(this.renderCalendar(this.rightCalendar.calendar, this.endDate, this.singleDatePicker ? this.minDate : this.startDate, this.maxDate, 'right'));
+ formInputsChanged: function(e) {
+ var isRight = $(e.target).closest('.calendar').hasClass('right');
+ var start = moment(this.container.find('input[name="daterangepicker_start"]').val(), this.locale.format);
+ var end = moment(this.container.find('input[name="daterangepicker_end"]').val(), this.locale.format);
- this.container.find('.ranges li').removeClass('active');
- var customRange = true;
- var i = 0;
- for (var range in this.ranges) {
- if (this.timePicker) {
- if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
- customRange = false;
- this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')')
- .addClass('active').html();
- }
+ if (start.isValid() && end.isValid()) {
+
+ if (isRight && end.isBefore(start))
+ start = end.clone();
+
+ this.setStartDate(start);
+ this.setEndDate(end);
+
+ if (isRight) {
+ this.container.find('input[name="daterangepicker_start"]').val(this.startDate.format(this.locale.format));
} else {
- //ignore times when comparing dates if time picker is not enabled
- 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')) {
- customRange = false;
- this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')')
- .addClass('active').html();
- }
- }
- i++;
- }
- if (customRange) {
- this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
- this.showCalendars();
- }
- },
-
- buildCalendar: function (month, year, hour, minute, second, side) {
- var daysInMonth = moment([year, month]).daysInMonth();
- var firstDay = moment([year, month, 1]);
- var lastDay = moment([year, month, daysInMonth]);
- var lastMonth = moment(firstDay).subtract(1, 'month').month();
- var lastYear = moment(firstDay).subtract(1, 'month').year();
-
- var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
-
- var dayOfWeek = firstDay.day();
-
- var i;
-
- //initialize a 6 rows x 7 columns array for the calendar
- var calendar = [];
- calendar.firstDay = firstDay;
- calendar.lastDay = lastDay;
-
- for (i = 0; i < 6; i++) {
- calendar[i] = [];
- }
-
- //populate the calendar with date objects
- var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
- if (startDay > daysInLastMonth)
- startDay -= 7;
-
- if (dayOfWeek == this.locale.firstDay)
- startDay = daysInLastMonth - 6;
-
- var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]).utcOffset(this.timeZone);
-
- var col, row;
- for (i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) {
- if (i > 0 && col % 7 === 0) {
- col = 0;
- row++;
- }
- calendar[row][col] = curDate.clone().hour(hour);
- curDate.hour(12);
-
- if (this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') {
- calendar[row][col] = this.minDate.clone();
- }
-
- if (this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') {
- calendar[row][col] = this.maxDate.clone();
+ this.container.find('input[name="daterangepicker_end"]').val(this.endDate.format(this.locale.format));
}
}
- return calendar;
- },
-
- renderDropdowns: function (selected, minDate, maxDate) {
- var currentMonth = selected.month();
- var currentYear = selected.year();
- var maxYear = (maxDate && maxDate.year()) || (currentYear + 5);
- var minYear = (minDate && minDate.year()) || (currentYear - 50);
-
- var monthHtml = '";
-
- var yearHtml = '';
-
- return monthHtml + yearHtml;
- },
-
- renderCalendar: function (calendar, selected, minDate, maxDate, side) {
-
- var html = '
';
- html += '
';
- html += '';
- html += '
';
-
- // add empty cell for week number
- if (this.showWeekNumbers)
- html += '
';
-
- if (!minDate || minDate.isBefore(calendar.firstDay)) {
- html += '
';
- } else {
- html += '
';
- }
-
- var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
-
- if (this.showDropdowns) {
- dateHtml = this.renderDropdowns(calendar[1][1], minDate, maxDate);
- }
-
- html += '
' + dateHtml + '
';
- if (!maxDate || maxDate.isAfter(calendar.lastDay)) {
- html += '
';
- } else {
- html += '
';
- }
-
- html += '
';
- html += '
';
-
- // add week number label
- if (this.showWeekNumbers)
- html += '
' + this.locale.weekLabel + '
';
-
- $.each(this.locale.daysOfWeek, function (index, dayOfWeek) {
- html += '
' + dayOfWeek + '
';
- });
-
- html += '
';
- html += '';
- html += '';
-
- for (var row = 0; row < 6; row++) {
- html += '
';
-
- // add week number
- if (this.showWeekNumbers)
- html += '
' + calendar[row][0].week() + '
';
-
- for (var col = 0; col < 7; col++) {
- var cname = 'available ';
- cname += (calendar[row][col].month() == calendar[1][1].month()) ? '' : 'off';
-
- if ((minDate && calendar[row][col].isBefore(minDate, 'day')) || (maxDate && calendar[row][col].isAfter(maxDate, 'day'))) {
- cname = ' off disabled ';
- } else if (calendar[row][col].format('YYYY-MM-DD') == selected.format('YYYY-MM-DD')) {
- cname += ' active ';
- if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD')) {
- cname += ' start-date ';
- }
- if (calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD')) {
- cname += ' end-date ';
- }
- } else if (calendar[row][col] >= this.startDate && calendar[row][col] <= this.endDate) {
- cname += ' in-range ';
- if (calendar[row][col].isSame(this.startDate)) { cname += ' start-date '; }
- if (calendar[row][col].isSame(this.endDate)) { cname += ' end-date '; }
- }
-
- var title = 'r' + row + 'c' + col;
- html += '
' + calendar[row][col].date() + '
';
- }
- html += '
';
- }
-
- html += '';
- html += '
';
- html += '
';
-
- var i;
+ this.updateCalendars();
if (this.timePicker) {
+ this.renderTimePicker('left');
+ this.renderTimePicker('right');
+ }
+ },
- html += '
';
- html += ' : ';
-
- html += ' ';
-
- if (this.timePickerSeconds) {
- html += ': ';
- }
-
- if (this.timePicker12Hour) {
- html += '';
- }
-
- html += '
';
+ var dateString = this.element.val().split(this.locale.separator),
+ start = null,
+ end = null;
+ if (dateString.length === 2) {
+ start = moment(dateString[0], this.locale.format);
+ end = moment(dateString[1], this.locale.format);
}
- return html;
+ if (this.singleDatePicker || start === null || end === null) {
+ start = moment(this.element.val(), this.locale.format);
+ end = start;
+ }
+ if (!start.isValid() || !end.isValid()) return;
+
+ this.setStartDate(start);
+ this.setEndDate(end);
+ this.updateView();
+ },
+
+ keydown: function(e) {
+ //hide on tab or enter
+ if ((e.keyCode === 9) || (e.keyCode === 13)) {
+ this.hide();
+ }
+ },
+
+ updateElement: function() {
+ if (this.element.is('input') && !this.singleDatePicker && this.autoUpdateInput) {
+ this.element.val(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
+ this.element.trigger('change');
+ } else if (this.element.is('input') && this.autoUpdateInput) {
+ this.element.val(this.startDate.format(this.locale.format));
+ this.element.trigger('change');
+ }
},
remove: function() {
-
this.container.remove();
this.element.off('.daterangepicker');
- this.element.removeData('daterangepicker');
-
+ this.element.removeData();
}
};
- $.fn.daterangepicker = function (options, cb) {
- this.each(function () {
+ $.fn.daterangepicker = function(options, callback) {
+ this.each(function() {
var el = $(this);
if (el.data('daterangepicker'))
el.data('daterangepicker').remove();
- el.data('daterangepicker', new DateRangePicker(el, options, cb));
+ el.data('daterangepicker', new DateRangePicker(el, options, callback));
});
return this;
};
+
+ return DateRangePicker;
}));