date_popup.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Attaches the calendar behavior to all required fields
  3. */
  4. (function($) {
  5. function makeFocusHandler(e) {
  6. if (!$(this).hasClass('date-popup-init')) {
  7. var datePopup = e.data;
  8. // Explicitely filter the methods we accept.
  9. switch (datePopup.func) {
  10. case 'datepicker':
  11. $(this)
  12. .datepicker(datePopup.settings)
  13. .addClass('date-popup-init');
  14. $(this).click(function(){
  15. $(this).focus();
  16. });
  17. if (datePopup.settings.syncEndDate) {
  18. $('.start-date-wrapper').each(function(){
  19. var start_date_wrapper = this;
  20. $(this).find('input:eq(0)').change(function(){
  21. $(start_date_wrapper).next('.end-date-wrapper').find('input:eq(0)').val($(this).val());
  22. });
  23. });
  24. }
  25. break;
  26. case 'timeEntry':
  27. $(this)
  28. .timeEntry(datePopup.settings)
  29. .addClass('date-popup-init');
  30. $(this).click(function(){
  31. $(this).focus();
  32. });
  33. break;
  34. case 'timepicker':
  35. // Translate the PHP date format into the style the timepicker uses.
  36. datePopup.settings.timeFormat = datePopup.settings.timeFormat
  37. // 12-hour, leading zero,
  38. .replace('h', 'hh')
  39. // 12-hour, no leading zero.
  40. .replace('g', 'h')
  41. // 24-hour, leading zero.
  42. .replace('H', 'HH')
  43. // 24-hour, no leading zero.
  44. .replace('G', 'H')
  45. // AM/PM.
  46. .replace('A', 'p')
  47. // Minutes with leading zero.
  48. .replace('i', 'mm')
  49. // Seconds with leading zero.
  50. .replace('s', 'ss');
  51. datePopup.settings.startTime = new Date(datePopup.settings.startTime);
  52. $(this)
  53. .timepicker(datePopup.settings)
  54. .addClass('date-popup-init');
  55. $(this).click(function(){
  56. $(this).focus();
  57. });
  58. break;
  59. }
  60. }
  61. }
  62. Drupal.behaviors.date_popup = {
  63. attach: function (context) {
  64. for (var id in Drupal.settings.datePopup) {
  65. $('#'+ id).bind('focus', Drupal.settings.datePopup[id], makeFocusHandler);
  66. }
  67. }
  68. };
  69. })(jQuery);