Skip to content

Commit f321489

Browse files
Added methods to enforce date range limits.
1 parent 1891b53 commit f321489

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,4 +1535,109 @@ var timeZoneString = function(date)
15351535
$.timepicker = new Timepicker(); // singleton instance
15361536
$.timepicker.version = "1.0.2";
15371537

1538+
/**
1539+
* Calls `timepicker()` on the `startTime` and `endTime` elements, and configures them to
1540+
* enforce date range limits.
1541+
* n.b. The input value must be correctly formatted (reformatting is not supported)
1542+
* @param Element startTime
1543+
* @param Element endTime
1544+
* @param obj options Options for the timepicker() call
1545+
* @return jQuery
1546+
*/
1547+
$.timepicker.timeRange = function( startTime, endTime, options ) {
1548+
return $.timepicker.handleRange('timepicker', startTime, endTime, options);
1549+
}
1550+
1551+
/**
1552+
* Calls `datetimepicker` on the `startTime` and `endTime` elements, and configures them to
1553+
* enforce date range limits.
1554+
* @param Element startTime
1555+
* @param Element endTime
1556+
* @param obj options Options for the `timepicker()` call. Also supports `reformat`,
1557+
* a boolean value that can be used to reformat the input values to the `dateFormat`.
1558+
* @param string method Can be used to specify the type of picker to be added
1559+
* @return jQuery
1560+
*/
1561+
$.timepicker.dateTimeRange = function( startTime, endTime, options ) {
1562+
$.timepicker.dateRange(startTime, endTime, options, 'datetimepicker');
1563+
}
1564+
1565+
/**
1566+
* Calls `method` on the `startTime` and `endTime` elements, and configures them to
1567+
* enforce date range limits.
1568+
* @param Element startTime
1569+
* @param Element endTime
1570+
* @param obj options Options for the `timepicker()` call. Also supports `reformat`,
1571+
* a boolean value that can be used to reformat the input values to the `dateFormat`.
1572+
* @param string method Can be used to specify the type of picker to be added
1573+
* @return jQuery
1574+
*/
1575+
$.timepicker.dateRange = function( startTime, endTime, options, method ) {
1576+
method = method || 'datepicker';
1577+
$.timepicker.handleRange(method, startTime, endTime, options);
1578+
}
1579+
1580+
/**
1581+
* Calls `method` on the `startTime` and `endTime` elements, and configures them to
1582+
* enforce date range limits.
1583+
* @param string method Can be used to specify the type of picker to be added
1584+
* @param Element startTime
1585+
* @param Element endTime
1586+
* @param obj options Options for the `timepicker()` call. Also supports `reformat`,
1587+
* a boolean value that can be used to reformat the input values to the `dateFormat`.
1588+
* @return jQuery
1589+
*/
1590+
$.timepicker.handleRange = function( method, startTime, endTime, options ) {
1591+
$.fn[method].call(startTime, $.extend({}, {
1592+
onClose: function(dateText, inst) {
1593+
checkDates(this, endTime, dateText);
1594+
},
1595+
onSelect: function (selectedDateTime) {
1596+
selected(this, endTime, 'minDate');
1597+
}
1598+
}, options)
1599+
);
1600+
$.fn[method].call(endTime, $.extend({}, {
1601+
onClose: function(dateText, inst) {
1602+
checkDates(this, startTime, dateText);
1603+
},
1604+
onSelect: function (selectedDateTime) {
1605+
selected(this, startTime, 'maxDate');
1606+
}
1607+
}, options)
1608+
);
1609+
// timepicker doesn't provide access to its 'timeFormat' option,
1610+
// nor could I get datepicker.formatTime() to behave with times, so I
1611+
// have disabled reformatting for timepicker
1612+
if( method != 'timepicker' && options.reformat ) {
1613+
$([startTime, endTime]).each(function() {
1614+
var format = $(this)[method].call($(this), 'option', 'dateFormat'),
1615+
date = new Date($(this).val());
1616+
if( $(this).val() && date ) {
1617+
$(this).val($.datepicker.formatDate(format, date));
1618+
}
1619+
});
1620+
}
1621+
checkDates(startTime, endTime, startTime.val());
1622+
function checkDates(changed, other, dateText) {
1623+
if( other.val() && (new Date(startTime.val()) > new Date(endTime.val())) ) {
1624+
other.val(dateText);
1625+
}
1626+
}
1627+
selected(startTime, endTime, 'minDate');
1628+
selected(endTime, startTime, 'maxDate');
1629+
function selected(changed, other, option) {
1630+
if( !$(changed).val() ) {
1631+
return;
1632+
}
1633+
var date = $(changed)[method].call($(changed), 'getDate');
1634+
// timepicker doesn't implement 'getDate' and returns a jQuery
1635+
if( date.getTime ) {
1636+
$(other)[method].call($(other), 'option', option, date);
1637+
}
1638+
}
1639+
return $([startTime.get(0), endTime.get(0)]);
1640+
};
1641+
1642+
15381643
})(jQuery);

0 commit comments

Comments
 (0)