vendor/assets/javascripts/bootstrap-datetimepicker.js in bootstrap3-datetimepicker-rails-3.0.0.1 vs vendor/assets/javascripts/bootstrap-datetimepicker.js in bootstrap3-datetimepicker-rails-3.0.0.2
- old
+ new
@@ -42,48 +42,28 @@
}
}
(function ($, moment) {
if (typeof moment === 'undefined') {
- alert("momentjs is requried");
+ alert("momentjs is required");
throw new Error('momentjs is required');
};
var dpgId = 0,
pMoment = moment,
// ReSharper disable once InconsistentNaming
DateTimePicker = function (element, options) {
- var defaults = {
- pickDate: true,
- pickTime: true,
- useMinutes: true,
- useSeconds: false,
- useCurrent: true,
- minuteStepping: 1,
- minDate: new pMoment({ y: 1900 }),
- maxDate: new pMoment().add(100, "y"),
- showToday: true,
- collapse: true,
- language: "en",
- defaultDate: "",
- disabledDates: false,
- enabledDates: false,
- icons: {},
- useStrict: false,
- direction: "auto",
- sideBySide: false,
- daysOfWeekDisabled: false
- },
+ var defaults = $.fn.datetimepicker.defaults,
- icons = {
- time: 'glyphicon glyphicon-time',
- date: 'glyphicon glyphicon-calendar',
- up: 'glyphicon glyphicon-chevron-up',
- down: 'glyphicon glyphicon-chevron-down'
- },
+ icons = {
+ time: 'glyphicon glyphicon-time',
+ date: 'glyphicon glyphicon-calendar',
+ up: 'glyphicon glyphicon-chevron-up',
+ down: 'glyphicon glyphicon-chevron-down'
+ },
picker = this,
init = function () {
@@ -199,14 +179,23 @@
picker.date.minutes((Math.round(picker.date.minutes() / rInterval) * rInterval) % 60).seconds(0);
}
},
getPickerInput = function () {
+ var input;
+
if (picker.isInput) {
return picker.element;
} else {
- return dateStr = picker.element.find('input');
+ input = picker.element.find('.datepickerinput');
+ if (input.size() === 0) {
+ input = picker.element.find('input');
+ }
+ else if (!input.is('input')) {
+ throw new Error('CSS class "datepickerinput" cannot be applied to non input element');
+ }
+ return input;
}
},
dataToOptions = function () {
var eData
@@ -316,11 +305,11 @@
update = function (newDate) {
pMoment.lang(picker.options.language);
var dateStr = newDate;
if (!dateStr) {
- dateStr = getPickerInput().val()
+ dateStr = getPickerInput().val();
if (dateStr) picker.date = pMoment(dateStr, picker.format, picker.options.useStrict);
if (!picker.date) picker.date = pMoment();
}
picker.viewDate = pMoment(picker.date).startOf("month");
fillDate();
@@ -354,10 +343,11 @@
}
picker.widget.find('.datepicker-months td').append(html);
},
fillDate = function () {
+ if(!picker.options.pickDate) return;
pMoment.lang(picker.options.language);
var year = picker.viewDate.year(),
month = picker.viewDate.month(),
startYear = picker.options.minDate.year(),
startMonth = picker.options.minDate.month(),
@@ -396,11 +386,11 @@
clsName += ' new';
}
if (prevMonth.isSame(pMoment({ y: picker.date.year(), M: picker.date.month(), d: picker.date.date() }))) {
clsName += ' active';
}
- if (isInDisableDates(prevMonth) || !isInEnableDates(prevMonth)) {
+ if (isInDisableDates(prevMonth, 'day') || !isInEnableDates(prevMonth)) {
clsName += ' disabled';
}
if (picker.options.showToday === true) {
if (prevMonth.isSame(pMoment(), 'day')) {
clsName += ' today';
@@ -710,10 +700,15 @@
stopEvent = function (e) {
e.stopPropagation();
e.preventDefault();
},
+ keydown = function (e) {
+ if (e.keyCode === 27) // allow escape to hide picker
+ picker.hide();
+ },
+
change = function (e) {
pMoment.lang(picker.options.language);
var input = $(e.target), oldDate = pMoment(picker.date), newDate = pMoment(input.val(), picker.format, picker.options.useStrict);
if (newDate.isValid() && !isInDisableDates(newDate) && isInEnableDates(newDate)) {
update();
@@ -740,21 +735,22 @@
attachDatePickerEvents = function () {
var $this, $parent, expanded, closed, collapseData;
picker.widget.on('click', '.datepicker *', $.proxy(click, this)); // this handles date picker clicks
picker.widget.on('click', '[data-action]', $.proxy(doAction, this)); // this handles time picker clicks
picker.widget.on('mousedown', $.proxy(stopEvent, this));
+ picker.element.on('keydown', $.proxy(keydown, this));
if (picker.options.pickDate && picker.options.pickTime) {
picker.widget.on('click.togglePicker', '.accordion-toggle', function (e) {
e.stopPropagation();
$this = $(this);
$parent = $this.closest('ul');
expanded = $parent.find('.in');
closed = $parent.find('.collapse:not(.in)');
if (expanded && expanded.length) {
collapseData = expanded.data('collapse');
- if (collapseData && collapseData.date - transitioning) return;
+ if (collapseData && collapseData.transitioning) return;
expanded.collapse('hide');
closed.collapse('show');
$this.find('span').toggleClass(picker.options.icons.time + ' ' + picker.options.icons.date);
picker.element.find('.input-group-addon span').toggleClass(picker.options.icons.time + ' ' + picker.options.icons.date);
}
@@ -866,13 +862,21 @@
picker.date.subtract(amount, unit);
}
picker.unset = false;
},
- isInDisableDates = function (date) {
+ isInDisableDates = function (date, timeUnit) {
pMoment.lang(picker.options.language);
- if (date.isAfter(picker.options.maxDate) || date.isBefore(picker.options.minDate)) return true;
+ var maxDate = picker.options.maxDate;
+ var minDate = picker.options.minDate;
+
+ if(timeUnit) {
+ maxDate = pMoment(maxDate).endOf(timeUnit);
+ minDate = pMoment(minDate).startOf(timeUnit);
+ }
+
+ if (date.isAfter(maxDate) || date.isBefore(minDate)) return true;
if (picker.options.disabledDates === false) {
return false;
}
return picker.options.disabledDates[pMoment(date).format("YYYY-MM-DD")] === true;
},
@@ -1096,14 +1100,15 @@
return;
// Ignore event if in the middle of a picker transition
var collapse = picker.widget.find('.collapse'), i, collapseData;
for (i = 0; i < collapse.length; i++) {
collapseData = collapse.eq(i).data('collapse');
- if (collapseData && collapseData.date - transitioning)
+ if (collapseData && collapseData.transitioning)
return;
}
picker.widget.hide();
+ picker.widget.removeClass("picker-open");
picker.viewMode = picker.startViewMode;
showMode();
picker.element.trigger({
type: 'dp.hide',
date: pMoment(picker.date)
@@ -1117,11 +1122,11 @@
picker.unset = true;
set();
} else {
picker.unset = false;
}
- if (!pMoment.isMoment(newDate)) newDate = pMoment(newDate, picker.format);
+ if (!pMoment.isMoment(newDate)) newDate = (newDate instanceof Date) ? pMoment(newDate) : pMoment(newDate, picker.format);
if (newDate.isValid()) {
picker.date = newDate;
set();
picker.viewDate = pMoment({ y: picker.date.year(), M: picker.date.month() });
fillDate();
@@ -1175,6 +1180,29 @@
return this.each(function () {
var $this = $(this), data = $this.data('DateTimePicker');
if (!data) $this.data('DateTimePicker', new DateTimePicker(this, options));
});
};
+
+ $.fn.datetimepicker.defaults = {
+ pickDate: true,
+ pickTime: true,
+ useMinutes: true,
+ useSeconds: false,
+ useCurrent: true,
+ minuteStepping: 1,
+ minDate: new pMoment({ y: 1900 }),
+ maxDate: new pMoment().add(100, "y"),
+ showToday: true,
+ collapse: true,
+ language: "en",
+ defaultDate: "",
+ disabledDates: false,
+ enabledDates: false,
+ icons: {},
+ useStrict: false,
+ direction: "auto",
+ sideBySide: false,
+ daysOfWeekDisabled: false
+ };
+
}));