event.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. $(document).ready(function() {
  2. // get current time from server
  3. var today = new Date($('now').val());
  4. $("#event-startdate").datepicker({
  5. // Don't let the user set a start date < before today
  6. minDate: today,
  7. onClose: onStartDateSelected
  8. });
  9. $("#event-enddate").datepicker({
  10. minDate: today,
  11. onClose: onEndDateSelected
  12. });
  13. $("#event-starttime").change(function(e) {
  14. var tz = $("#tz").val();
  15. var startDate = $("#event-startdate").val();
  16. var startTime = $("#event-starttime option:selected").val().replace(/(pm|am)/, ' $1');
  17. var startStr = startDate + ' ' + startTime + ' ' + tz;
  18. var endDate = $("#event-enddate").val();
  19. var endTime = $("#event-endtime option:selected").val();
  20. var endStr = endDate + ' ' + endTime.replace(/(pm|am)/, ' $1') + ' ' + tz;
  21. // just need to compare hours
  22. var start = new Date(startStr);
  23. var end = new Date(endStr);
  24. updateTimes(startStr, (startDate === endDate), function (data) {
  25. var times = [];
  26. $.each(data, function(key, val) {
  27. times.push('<option value="' + key + '">' + val + '</option>');
  28. });
  29. $("#event-endtime").html(times.join(''));
  30. if (start > end) {
  31. $("#event-endtime").val(startTime).attr("selected", "selected");
  32. } else {
  33. $("#event-endtime").val(endTime).attr("selected", "selected");
  34. }
  35. });
  36. });
  37. $("#event-endtime").change(function(e) {
  38. var HOUR = 60 * 60 * 1000;
  39. var tz = $("#tz").val();
  40. var startDate = $("#event-startdate").val();
  41. var endDate = $("#event-enddate").val();
  42. var starttime = $("#event-starttime option:selected").val();
  43. var endtime = $("#event-endtime option:selected").val();
  44. var endtimeText = $("#event-endtime option:selected").text();
  45. // If the end time is in the next day then update the start date
  46. if (startDate === endDate) {
  47. var startstr = startDate + ' ' + starttime.replace(/(pm|am)/, ' $1') + ' ' + tz;
  48. var start = new Date(startstr);
  49. var matches = endtimeText.match(/\(.*\)/);
  50. var hours;
  51. if (matches) {
  52. hours = matches[0].substr(1).split(' ')[0]; // get x from (x hours)
  53. if (hours) {
  54. if (hours == 30) {
  55. hours = .5; // special case: x == 30 from (30 mins)
  56. }
  57. var end = new Date(start.getTime() + (hours * HOUR));
  58. if (end.getDate() > start.getDate()) {
  59. $("#event-enddate").datepicker('setDate', end);
  60. var endstr = endDate + ' 12:00 am ' + tz;
  61. updateTimes(endstr, false, function(data) {
  62. var times = [];
  63. $.each(data, function(key, val) {
  64. times.push('<option value="' + key + '">' + val + '</option>');
  65. });
  66. $("#event-endtime").html(times.join(''));
  67. if (start > end) {
  68. $("#event-endtime").val(starttime).attr("selected", "selected");
  69. } else {
  70. $("#event-endtime").val(endtime).attr("selected", "selected");
  71. }
  72. });
  73. }
  74. }
  75. }
  76. }
  77. });
  78. function onStartDateSelected(dateText, inst) {
  79. var tz = $("#tz").val();
  80. var startTime = $("#event-starttime option:selected").val();
  81. var startDateTime = new Date(dateText + ' ' + startTime.replace(/(pm|am)/, ' $1') + ' ' + tz);
  82. // When we update the start date and time, we need to update the end date and time
  83. // to make sure they are equal or in the future
  84. $("#event-enddate").datepicker('option', 'minDate', startDateTime);
  85. recalculateTimes();
  86. }
  87. function onEndDateSelected(dateText, inst) {
  88. recalculateTimes();
  89. }
  90. function recalculateTimes(showDuration) {
  91. var tz = $("#tz").val();
  92. var startDate = $("#event-startdate").val();
  93. var startTime = $("#event-starttime option:selected").val();
  94. var startStr = startDate + ' ' + startTime.replace(/(pm|am)/, ' $1') + ' ' + tz;
  95. var startDateTime = new Date(startStr);
  96. var endDate = $("#event-enddate").val();
  97. var endTime = $("#event-endtime option:selected").val();
  98. var endDateTime = new Date(endDate + ' ' + endTime.replace(/(pm|am)/, ' $1') + ' ' + tz);
  99. var showDuration = true;
  100. if (endDateTime.getDate() !== startDateTime.getDate()) {
  101. starStr = endDate + ' 12:00 am ' + tz;
  102. showDuration = false;
  103. }
  104. updateTimes(startStr, showDuration, function(data) {
  105. var times = [];
  106. $.each(data, function(key, val) {
  107. times.push('<option value="' + key + '">' + val + '</option>');
  108. });
  109. $("#event-endtime").html(times.join(''));
  110. if (startDateTime > endDateTime) {
  111. $("#event-endtime").val(startTime).attr("selected", "selected");
  112. } else {
  113. $("#event-endtime").val(endTime).attr("selected", "selected");
  114. }
  115. });
  116. }
  117. function updateTimes(start, duration, onSuccess) {
  118. $.getJSON($('#timelist_action_url').val(), {start: start, ajax: true, duration: duration}, onSuccess);
  119. }
  120. });