time-grid.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { getBoundingRect } from '../lib/dom-geom'
  2. export function dragTimeGridEvent(eventEl, dropDate) {
  3. return new Promise(function(resolve) {
  4. var modifiedEvent = null
  5. currentCalendar.on('eventDragStop', function() {
  6. setTimeout(function() { // wait for eventDrop to be called
  7. resolve(modifiedEvent)
  8. })
  9. })
  10. currentCalendar.on('eventDrop', function(event) {
  11. modifiedEvent = event
  12. })
  13. eventEl.simulate('drag', {
  14. localPoint: { left: '50%', top: 1 }, // 1 for zoom
  15. end: getTimeGridPoint(dropDate)
  16. })
  17. })
  18. }
  19. export function selectTimeGrid(start, inclusiveEnd) {
  20. return new Promise(function(resolve) {
  21. var selectInfo = null
  22. currentCalendar.on('select', function(start, end) {
  23. selectInfo = { start: start, end: end }
  24. })
  25. getTimeGridDayEls(start).simulate('drag', {
  26. point: getTimeGridPoint(start),
  27. end: getTimeGridPoint(inclusiveEnd),
  28. onRelease: function() {
  29. setTimeout(function() { // wait for eventDrop to be called
  30. resolve(selectInfo)
  31. })
  32. }
  33. })
  34. })
  35. }
  36. export function getTimeGridPoint(date) {
  37. date = $.fullCalendar.moment.parseZone(date)
  38. var top = getTimeGridTop(date.time())
  39. var dayEls = getTimeGridDayEls(date)
  40. var dayRect
  41. expect(dayEls.length).toBe(1)
  42. dayRect = getBoundingRect(dayEls.eq(0))
  43. return {
  44. left: (dayRect.left + dayRect.right) / 2,
  45. top: top
  46. }
  47. }
  48. export function getTimeGridLine(date) { // not in Scheduler
  49. date = $.fullCalendar.moment.parseZone(date)
  50. var top = getTimeGridTop(date.time())
  51. var dayEls = getTimeGridDayEls(date)
  52. var dayRect
  53. expect(dayEls.length).toBe(1)
  54. dayRect = getBoundingRect(dayEls.eq(0))
  55. return {
  56. left: dayRect.left,
  57. right: dayRect.right,
  58. top: top,
  59. bottom: top
  60. }
  61. }
  62. /*
  63. targetTime is a time (duration) that can be in between slots
  64. */
  65. export function getTimeGridTop(targetTime) {
  66. let slotEl
  67. targetTime = moment.duration(targetTime)
  68. let slotEls = getTimeGridSlotEls(targetTime)
  69. const topBorderWidth = 1 // TODO: kill
  70. // exact slot match
  71. if (slotEls.length === 1) {
  72. return slotEls.eq(0).offset().top + topBorderWidth
  73. }
  74. slotEls = $('.fc-time-grid .fc-slats tr[data-time]') // all slots
  75. let slotTime = null
  76. let prevSlotTime = null
  77. for (let i = 0; i < slotEls.length; i++) { // traverse earlier to later
  78. slotEl = slotEls[i]
  79. slotEl = $(slotEl)
  80. prevSlotTime = slotTime
  81. slotTime = moment.duration(slotEl.data('time'))
  82. // is target time between start of previous slot but before this one?
  83. if (targetTime < slotTime) {
  84. // before first slot
  85. if (!prevSlotTime) {
  86. return slotEl.offset().top + topBorderWidth
  87. } else {
  88. const prevSlotEl = slotEls.eq(i - 1)
  89. return prevSlotEl.offset().top + // previous slot top
  90. topBorderWidth +
  91. (prevSlotEl.outerHeight() *
  92. ((targetTime - prevSlotTime) / (slotTime - prevSlotTime)))
  93. }
  94. }
  95. }
  96. // target time must be after the start time of the last slot.
  97. // `slotTime` is set to the start time of the last slot.
  98. // guess the duration of the last slot, based on previous duration
  99. const slotMsDuration = slotTime - prevSlotTime
  100. return slotEl.offset().top + // last slot's top
  101. topBorderWidth +
  102. (slotEl.outerHeight() *
  103. Math.min(1, (targetTime - slotTime) / slotMsDuration)) // don't go past end of last slot
  104. }
  105. export function getTimeGridDayEls(date) {
  106. date = $.fullCalendar.moment.parseZone(date)
  107. return $('.fc-time-grid .fc-day[data-date="' + date.format('YYYY-MM-DD') + '"]')
  108. }
  109. export function getTimeGridSlotEls(timeDuration) {
  110. timeDuration = moment.duration(timeDuration)
  111. const date = $.fullCalendar.moment.utc('2016-01-01').time(timeDuration)
  112. if (date.date() === 1) { // ensure no time overflow/underflow
  113. return $(`.fc-time-grid .fc-slats tr[data-time="${date.format('HH:mm:ss')}"]`)
  114. } else {
  115. return $()
  116. }
  117. }
  118. // TODO: discourage use
  119. export function getTimeGridDowEls(dayAbbrev) {
  120. return $(`.fc-time-grid .fc-day.fc-${dayAbbrev}`)
  121. }