time-grid.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // TODO: consolidate with scheduler
  2. function dragTimeGridEvent(eventEl, dropDate) {
  3. return new Promise(function(resolve) {
  4. var calendar = $('#cal').fullCalendar('getCalendar');
  5. var modifiedEvent = null;
  6. calendar.on('eventDragStop', function() {
  7. setTimeout(function() { // wait for eventDrop to be called
  8. resolve(modifiedEvent);
  9. });
  10. });
  11. calendar.on('eventDrop', function(event) {
  12. modifiedEvent = event;
  13. });
  14. eventEl.simulate('drag', {
  15. localPoint: { left: '50%', top: 0 },
  16. end: getTimeGridPoint(dropDate),
  17. });
  18. });
  19. }
  20. function selectTimeGrid(start, inclusiveEnd) {
  21. return new Promise(function(resolve) {
  22. var calendar = $('#cal').fullCalendar('getCalendar');
  23. var selectInfo = null;
  24. calendar.on('select', function(start, end) {
  25. selectInfo = { start: start, end: end };
  26. });
  27. getTimeGridDayEls(start).simulate('drag', {
  28. point: getTimeGridPoint(start),
  29. end: getTimeGridPoint(inclusiveEnd),
  30. onRelease: function() {
  31. setTimeout(function() { // wait for eventDrop to be called
  32. resolve(selectInfo);
  33. });
  34. }
  35. });
  36. });
  37. }
  38. function getTimeGridPoint(date) {
  39. var date = $.fullCalendar.moment.parseZone(date);
  40. var top = getTimeGridTop(date.time());
  41. var dayEls = getTimeGridDayEls(date);
  42. var dayRect;
  43. expect(dayEls.length).toBe(1);
  44. dayRect = getBoundingRect(dayEls.eq(0));
  45. return {
  46. left: (dayRect.left + dayRect.right) / 2,
  47. top: top
  48. };
  49. }
  50. function getTimeGridLine(date) { // not in Scheduler
  51. var date = $.fullCalendar.moment.parseZone(date);
  52. var top = getTimeGridTop(date.time());
  53. var dayEls = getTimeGridDayEls(date);
  54. var dayRect;
  55. expect(dayEls.length).toBe(1);
  56. dayRect = getBoundingRect(dayEls.eq(0));
  57. return {
  58. left: dayRect.left,
  59. right: dayRect.right,
  60. top: top,
  61. bottom: top
  62. };
  63. }
  64. function getTimeGridTop(time) {
  65. var time = moment.duration(time);
  66. var slotEls = getTimeGridSlotEls(time);
  67. expect(slotEls.length).toBe(1);
  68. return slotEls.offset().top + 1; // +1 make sure after border
  69. }
  70. function getTimeGridDayEls(date) {
  71. var date = $.fullCalendar.moment.parseZone(date);
  72. return $('.fc-time-grid .fc-day[data-date="' + date.format('YYYY-MM-DD') + '"]');
  73. }
  74. function getTimeGridSlotEls(timeDuration) {
  75. var timeDuration = moment.duration(timeDuration);
  76. var date = $.fullCalendar.moment.utc('2016-01-01').time(timeDuration);
  77. return $('.fc-time-grid .fc-slats tr[data-time="' + date.format('HH:mm:ss') + '"]');
  78. }
  79. function isElWithinRtl(el) {
  80. return el.closest('.fc').hasClass('fc-rtl');
  81. }
  82. function getBoundingRect(el) {
  83. var el = $(el);
  84. expect(el.length).toBe(1);
  85. var rect = el.offset();
  86. rect.right = rect.left + el.outerWidth();
  87. rect.bottom = rect.top + el.outerHeight();
  88. rect.node = el[0]; // very useful for debugging
  89. return rect;
  90. }