init.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. $(document).ready(function(){
  2. $('input[data-grav-field-datetime]').each(function() {
  3. var $input = $(this),
  4. min = $input.attr('min'),
  5. max = $input.attr('max'),
  6. regex, match,
  7. userOptions = $input.data('dateFormats') || {},
  8. kendoOptions = { format: "dd-MM-yyyy HH:mm", timeFormat: "HH:mm" };
  9. kendoOptions = $.extend({}, kendoOptions, userOptions);
  10. if (min || max) {
  11. regex = /(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})/;
  12. }
  13. if (min && (match = regex.exec(min))) {
  14. kendoOptions.min = new Date(
  15. (+match[3]),
  16. (+match[2])-1,
  17. (+match[1]),
  18. (+match[4]),
  19. (+match[5])
  20. );
  21. }
  22. if (max && (match = regex.exec(max))) {
  23. kendoOptions.max = new Date(
  24. (+match[3]),
  25. (+match[2])-1,
  26. (+match[1]),
  27. (+match[4]),
  28. (+match[5])
  29. );
  30. }
  31. $input.kendoDateTimePicker(kendoOptions);
  32. // Reset when user manually types in invalid date
  33. $input.on('change', function () {
  34. $input.css('opacity', 1);
  35. $input.parents('.form-data').css('opacity', 1);
  36. var kWidget = $input.data('kendoDateTimePicker');
  37. if (kWidget && kWidget.value() === null && $input.val()) {
  38. kWidget.value($input.data('kendo-previous') || "");
  39. } else {
  40. $input.data('kendo-previous', kWidget.value() || "");
  41. }
  42. });
  43. });
  44. });