diff --git a/bower.json b/bower.json index 8350605..5c774ab 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name":"datetimepicker", - "version":"2.4.1", + "version":"2.4.2", "main": [ "jquery.datetimepicker.js", "jquery.datetimepicker.css" diff --git a/datetimepicker.jquery.json b/datetimepicker.jquery.json index 9cdff03..171375f 100644 --- a/datetimepicker.jquery.json +++ b/datetimepicker.jquery.json @@ -1,6 +1,6 @@ { "name": "datetimepicker", - "version": "2.4.1", + "version": "2.4.2", "title": "jQuery Date and Time picker", "description": "jQuery plugin for date, time, or datetime manipulation in form", "keywords": [ diff --git a/index.html b/index.html index 1e925d5..7321f87 100644 --- a/index.html +++ b/index.html @@ -77,7 +77,8 @@ $('.some_class').datetimepicker(); $('#default_datetimepicker').datetimepicker({ formatTime:'H:i', formatDate:'d.m.Y', - defaultDate:'8.12.1986', // it's my birthday + //defaultDate:'8.12.1986', // it's my birthday + defaultDate:'+03.01.1970', // it's my birthday defaultTime:'10:00', timepickerScrollbar:false }); @@ -133,7 +134,7 @@ $('#destroy').click(function(){ } }); var logic = function( currentDateTime ){ - if( currentDateTime.getDay()==6 ){ + if (currentDateTime && currentDateTime.getDay() == 6){ this.setOptions({ minTime:'11:00' }); diff --git a/jquery.datetimepicker.js b/jquery.datetimepicker.js index 1532146..71b8362 100644 --- a/jquery.datetimepicker.js +++ b/jquery.datetimepicker.js @@ -1,9 +1,9 @@ /** - * @preserve jQuery DateTimePicker plugin v2.4.1 + * @preserve jQuery DateTimePicker plugin v2.4.2 * @homepage http://xdsoft.net/jqplugins/datetimepicker/ * (c) 2014, Chupurnov Valeriy. */ -/*global document,window,jQuery,setTimeout,clearTimeout*/ +/*global document,window,jQuery,setTimeout,clearTimeout,HighlightedDate,getCurrentValue*/ (function ($) { 'use strict'; var default_options = { @@ -445,7 +445,7 @@ monthChangeSpinner: true, closeOnDateSelect: false, - closeOnTimeSelect: false, + closeOnTimeSelect: true, closeOnWithoutClick: true, closeOnInputClick: true, @@ -831,6 +831,48 @@ }); datetimepicker.setOptions = function (_options) { + var highlightedDates = {}, + getCaretPos = function (input) { + try { + if (document.selection && document.selection.createRange) { + var range = document.selection.createRange(); + return range.getBookmark().charCodeAt(2) - 2; + } + if (input.setSelectionRange) { + return input.selectionStart; + } + } catch (e) { + return 0; + } + }, + setCaretPos = function (node, pos) { + node = (typeof node === "string" || node instanceof String) ? document.getElementById(node) : node; + if (!node) { + return false; + } + if (node.createTextRange) { + var textRange = node.createTextRange(); + textRange.collapse(true); + textRange.moveEnd('character', pos); + textRange.moveStart('character', pos); + textRange.select(); + return true; + } + if (node.setSelectionRange) { + node.setSelectionRange(pos, pos); + return true; + } + return false; + }, + isValidValue = function (mask, value) { + var reg = mask + .replace(/([\[\]\/\{\}\(\)\-\.\+]{1})/g, '\\$1') + .replace(/_/g, '{digit+}') + .replace(/([0-9]{1})/g, '{digit$1}') + .replace(/\{digit([0-9]{1})\}/g, '[0-$1_]{1}') + .replace(/\{digit[\+]\}/g, '[0-9_]{1}'); + return (new RegExp(reg)).test(value); + }; options = $.extend(true, {}, options, _options); if (_options.allowTimes && $.isArray(_options.allowTimes) && _options.allowTimes.length) { @@ -840,15 +882,15 @@ if (_options.weekends && $.isArray(_options.weekends) && _options.weekends.length) { options.weekends = $.extend(true, [], _options.weekends); } - + if (_options.highlightedDates && $.isArray(_options.highlightedDates) && _options.highlightedDates.length) { - var highlightedDates = new Object(); - $.each(_options.highlightedDates, function(index, value) { + $.each(_options.highlightedDates, function (index, value) { var splitData = $.map(value.split(','), $.trim), + exDesc, hDate = new HighlightedDate(Date.parseDate(splitData[0], options.formatDate), splitData[1], splitData[2]), // date, desc, style keyDate = hDate.date.dateFormat(options.formatDate); - if(keyDate in highlightedDates) { - var exDesc = highlightedDates[keyDate].desc; + if (highlightedDates[keyDate] !== undefined) { + exDesc = highlightedDates[keyDate].desc; if (exDesc && exDesc.length && hDate.desc && hDate.desc.length) { highlightedDates[keyDate].desc = exDesc + "\n" + hDate.desc; } @@ -856,25 +898,28 @@ highlightedDates[keyDate] = hDate; } }); - + options.highlightedDates = $.extend(true, [], highlightedDates); } - + if (_options.highlightedPeriods && $.isArray(_options.highlightedPeriods) && _options.highlightedPeriods.length) { - var highlightedDates = $.extend(true, [], options.highlightedDates); - $.each(_options.highlightedPeriods, function(index, value) { + highlightedDates = $.extend(true, [], options.highlightedDates); + $.each(_options.highlightedPeriods, function (index, value) { var splitData = $.map(value.split(','), $.trim), dateTest = Date.parseDate(splitData[0], options.formatDate), // start date dateEnd = Date.parseDate(splitData[1], options.formatDate), - desc = splitData[2], + desc = splitData[2], + hDate, + keyDate, + exDesc, style = splitData[3]; while (dateTest <= dateEnd) { - var hDate = new HighlightedDate(dateTest, desc, style), - keyDate = dateTest.dateFormat(options.formatDate); + hDate = new HighlightedDate(dateTest, desc, style); + keyDate = dateTest.dateFormat(options.formatDate); dateTest.setDate(dateTest.getDate() + 1); - if(keyDate in highlightedDates) { - var exDesc = highlightedDates[keyDate].desc; + if (highlightedDates[keyDate] !== undefined) { + exDesc = highlightedDates[keyDate].desc; if (exDesc && exDesc.length && hDate.desc && hDate.desc.length) { highlightedDates[keyDate].desc = exDesc + "\n" + hDate.desc; } @@ -883,7 +928,7 @@ } } }); - + options.highlightedDates = $.extend(true, [], highlightedDates); } @@ -921,8 +966,8 @@ if (options.value) { _xdsoft_datetime.setCurrentTime(options.value); if (input && input.val) { - input.val(_xdsoft_datetime.str); - } + input.val(_xdsoft_datetime.str); + } } if (isNaN(options.dayOfWeekStart)) { @@ -958,48 +1003,6 @@ .css('visibility', !options.nextButton ? 'hidden' : 'visible'); if (options.mask) { - var e, - getCaretPos = function (input) { - try { - if (document.selection && document.selection.createRange) { - var range = document.selection.createRange(); - return range.getBookmark().charCodeAt(2) - 2; - } - if (input.setSelectionRange) { - return input.selectionStart; - } - } catch (e) { - return 0; - } - }, - setCaretPos = function (node, pos) { - node = (typeof node === "string" || node instanceof String) ? document.getElementById(node) : node; - if (!node) { - return false; - } - if (node.createTextRange) { - var textRange = node.createTextRange(); - textRange.collapse(true); - textRange.moveEnd('character', pos); - textRange.moveStart('character', pos); - textRange.select(); - return true; - } - if (node.setSelectionRange) { - node.setSelectionRange(pos, pos); - return true; - } - return false; - }, - isValidValue = function (mask, value) { - var reg = mask - .replace(/([\[\]\/\{\}\(\)\-\.\+]{1})/g, '\\$1') - .replace(/_/g, '{digit+}') - .replace(/([0-9]{1})/g, '{digit$1}') - .replace(/\{digit([0-9]{1})\}/g, '[0-$1_]{1}') - .replace(/\{digit[\+]\}/g, '[0-9_]{1}'); - return (new RegExp(reg)).test(value); - }; input.off('keydown.xdsoft'); if (options.mask === true) { @@ -1075,28 +1078,28 @@ input .off('blur.xdsoft') .on('blur.xdsoft', function () { - if (options.allowBlank && !$.trim($(this).val()).length) { - $(this).val(null); - datetimepicker.data('xdsoft_datetime').empty(); - } else if (!Date.parseDate($(this).val(), options.format)) { - var splittedHours = +([$(this).val()[0], $(this).val()[1]].join('')), - splittedMinutes = +([$(this).val()[2], $(this).val()[3]].join('')); + if (options.allowBlank && !$.trim($(this).val()).length) { + $(this).val(null); + datetimepicker.data('xdsoft_datetime').empty(); + } else if (!Date.parseDate($(this).val(), options.format)) { + var splittedHours = +([$(this).val()[0], $(this).val()[1]].join('')), + splittedMinutes = +([$(this).val()[2], $(this).val()[3]].join('')); - // parse the numbers as 0312 => 03:12 - if(!options.datepicker && options.timepicker && splittedHours >= 0 && splittedHours < 24 && splittedMinutes >= 0 && splittedMinutes < 60) { - $(this).val([splittedHours, splittedMinutes].map(function(item) { - return item > 9 ? item : '0' + item - }).join(':')); + // parse the numbers as 0312 => 03:12 + if (!options.datepicker && options.timepicker && splittedHours >= 0 && splittedHours < 24 && splittedMinutes >= 0 && splittedMinutes < 60) { + $(this).val([splittedHours, splittedMinutes].map(function (item) { + return item > 9 ? item : '0' + item; + }).join(':')); + } else { + $(this).val((_xdsoft_datetime.now()).dateFormat(options.format)); + } + + datetimepicker.data('xdsoft_datetime').setCurrentTime($(this).val()); } else { - $(this).val((_xdsoft_datetime.now()).dateFormat(options.format)); + datetimepicker.data('xdsoft_datetime').setCurrentTime($(this).val()); } - datetimepicker.data('xdsoft_datetime').setCurrentTime($(this).val()); - } else { - datetimepicker.data('xdsoft_datetime').setCurrentTime($(this).val()); - } - - datetimepicker.trigger('changedatetime.xdsoft'); + datetimepicker.trigger('changedatetime.xdsoft'); }); } options.dayOfWeekStartPrev = (options.dayOfWeekStart === 0) ? 6 : options.dayOfWeekStart - 1; @@ -1126,7 +1129,7 @@ datetimepicker .append(datepicker) - .append(timepicker); + .append(timepicker); if (options.withoutCopyright !== true) { datetimepicker @@ -1149,7 +1152,7 @@ time; if (!norecursion && options.defaultDate) { - date = _this.strToDate(options.defaultDate); + date = _this.strToDateTime(options.defaultDate); d.setFullYear(date.getFullYear()); d.setMonth(date.getMonth()); d.setDate(date.getDate()); @@ -1164,7 +1167,6 @@ d.setHours(time.getHours()); d.setMinutes(time.getMinutes()); } - return d; }; @@ -1266,7 +1268,7 @@ } if (tmpDate && tmpDate[2]) { timeOffset = tmpDate[2].getTime() - (tmpDate[2].getTimezoneOffset()) * 60000; - currentTime = new Date((_xdsoft_datetime.now()).getTime() + parseInt(tmpDate[1] + '1', 10) * timeOffset); + currentTime = new Date((_this.now(true)).getTime() + parseInt(tmpDate[1] + '1', 10) * timeOffset); } else { currentTime = sDateTime ? Date.parseDate(sDateTime, options.format) : _this.now(); } @@ -1283,7 +1285,7 @@ return sDate; } - var currentTime = sDate ? Date.parseDate(sDate, options.formatDate) : _this.now(true); + var currentTime = sDate ? Date.parse(sDate, options.formatDate) : _this.now(true); if (!_this.isValidDate(currentTime)) { currentTime = _this.now(true); } @@ -1311,7 +1313,7 @@ applyButton.on('click', function (e) {//pathbrite e.preventDefault(); - datetimepicker.data('changed',true); + datetimepicker.data('changed', true); _xdsoft_datetime.setCurrentTime(getCurrentValue()); input.val(_xdsoft_datetime.str()); datetimepicker.trigger('close.xdsoft'); @@ -1401,6 +1403,7 @@ today = _xdsoft_datetime.now(), maxDate = false, minDate = false, + hDate, d, y, m, @@ -1480,12 +1483,12 @@ classes.push('xdsoft_today'); } - if (start.getDay() === 0 || start.getDay() === 6 || ~options.weekends.indexOf(start.dateFormat(options.formatDate))) { + if (start.getDay() === 0 || start.getDay() === 6 || options.weekends.indexOf(start.dateFormat(options.formatDate)) !== -1) { classes.push('xdsoft_weekend'); } - - if (start.dateFormat(options.formatDate) in options.highlightedDates) { - var hDate = options.highlightedDates[start.dateFormat(options.formatDate)]; + + if (options.highlightedDates[start.dateFormat(options.formatDate)] !== undefined) { + hDate = options.highlightedDates[start.dateFormat(options.formatDate)]; classes.push(hDate.style === undefined ? 'xdsoft_highlighted_default' : hDate.style); description = hDate.desc === undefined ? '' : hDate.desc; } @@ -1525,20 +1528,20 @@ h = ''; m = ''; line_time = function line_time(h, m) { - var now = _xdsoft_datetime.now(); + var now = _xdsoft_datetime.now(), optionDateTime, current_time; now.setHours(h); h = parseInt(now.getHours(), 10); now.setMinutes(m); m = parseInt(now.getMinutes(), 10); - var optionDateTime = new Date(_xdsoft_datetime.currentTime); + optionDateTime = new Date(_xdsoft_datetime.currentTime); optionDateTime.setHours(h); optionDateTime.setMinutes(m); classes = []; - if((options.minDateTime !== false && options.minDateTime > optionDateTime) || (options.maxTime !== false && _xdsoft_datetime.strtotime(options.maxTime).getTime() < now.getTime()) || (options.minTime !== false && _xdsoft_datetime.strtotime(options.minTime).getTime() > now.getTime())) { + if ((options.minDateTime !== false && options.minDateTime > optionDateTime) || (options.maxTime !== false && _xdsoft_datetime.strtotime(options.maxTime).getTime() < now.getTime()) || (options.minTime !== false && _xdsoft_datetime.strtotime(options.minTime).getTime() > now.getTime())) { classes.push('xdsoft_disabled'); } - var current_time = new Date(_xdsoft_datetime.currentTime); + current_time = new Date(_xdsoft_datetime.currentTime); current_time.setHours(parseInt(_xdsoft_datetime.currentTime.getHours(), 10)); current_time.setMinutes(Math[options.roundTime](_xdsoft_datetime.currentTime.getMinutes() / options.step) * options.step); @@ -1582,7 +1585,7 @@ yearselect.children().eq(0) .html(opt); - for (i = parseInt(options.monthStart), opt = ''; i <= parseInt(options.monthEnd); i += 1) { + for (i = parseInt(options.monthStart, 10), opt = ''; i <= parseInt(options.monthEnd, 10); i += 1) { opt += '