Skip to content

Commit

Permalink
shift TimeGrid tests around. test util for TimeGrid event resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Dec 26, 2018
1 parent 37fdb1e commit e65878a
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 169 deletions.
2 changes: 1 addition & 1 deletion tests/automated/event-drag/TimeGridEventDragUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as EventDragUtils from './EventDragUtils'
import { computeSpanRects } from '../event-render/TimeGridEventRenderUtils'
import { computeSpanRects } from '../view-render/TimeGridRenderUtils'


export function drag(startDate, endDate, debug) {
Expand Down
2 changes: 1 addition & 1 deletion tests/automated/event-drag/all-day-change.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { drag } from './EventDragUtils'
import { computeSpanRects } from '../event-render/TimeGridEventRenderUtils'
import { computeSpanRects } from '../view-render/TimeGridRenderUtils'
import { getDayEl } from '../view-render/DayGridRenderUtils'

describe('allDay change', function() {
Expand Down
165 changes: 1 addition & 164 deletions tests/automated/event-render/TimeGridEventRenderUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getDayOfWeekHeaderEls } from '../view-render/DayGridRenderUtils'
import { getSlotEls } from '../lib/time-grid'
import { getEventEls, getEventElTimeText } from './EventRenderUtils'


export function getTimeTexts() {
return getEventEls().map(function(i, eventEl) {
return getEventElTimeText(eventEl)
Expand Down Expand Up @@ -63,165 +62,3 @@ function checkEventRenderingMatch(expectedRects, eventEls) {

return true
}


export function computeSpanRects(start, end) {
var dayStructs = computeDays()
var slotStructs = computeSlots()
var dayI, dayStruct
var slotI, slotStruct
var slotDayStart
var slotStart
var slotEnd
var coverage
var startTop = null
var endTop = null
var rects = []

for (dayI = 0; dayI < dayStructs.length; dayI++) {
dayStruct = dayStructs[dayI]

for (slotI = 0; slotI < slotStructs.length; slotI++) {
slotStruct = slotStructs[slotI]

slotDayStart = FullCalendar.addDays(
dayStruct.date,
slotStruct.dayOffset
)

slotStart = FullCalendar.addMs(
slotDayStart,
slotStruct.startTimeMs
)

slotEnd = FullCalendar.addMs(
slotDayStart,
slotStruct.endTimeMs
)

if (startTop === null) { // looking for the start
coverage = (start - slotStart) / (slotEnd - slotStart)
startTop = (coverage > 0 && coverage <= 1)
? (slotStruct.top + slotStruct.height * coverage)
: null
} else { // looking for the end
coverage = (end - slotStart) / (slotEnd - slotStart)
endTop = (coverage >= 0 && coverage < 1) // exclusive
? (slotStruct.top + slotStruct.height * coverage)
: null

if (endTop !== null) { // found end
rects.push({
left: dayStruct.left,
right: dayStruct.right,
top: startTop,
bottom: endTop,
width: dayStruct.right - dayStruct.left,
height: endTop - startTop
})
startTop = null
}
}
}

if (startTop !== null) { // could not find the start in this day
rects.push({
left: dayStruct.left,
right: dayStruct.right,
top: startTop,
bottom: slotStruct.bottom,
width: dayStruct.right - dayStruct.left,
height: slotStruct.bottom - startTop
})
startTop = slotStructs[0].top // top of next column
}
}

return rects
}


function computeDays() {
var dayOfWeekHeaderEls = getDayOfWeekHeaderEls()

var days = dayOfWeekHeaderEls.map(function(i, node) {
var rect = node.getBoundingClientRect()
return $.extend({}, rect, {
date: FullCalendar.parseMarker(
$(node).data('date')
).marker
})
}).get()

return days
}


function computeSlots() {
var slotEls = getSlotEls()

var slots = slotEls.map(function(i, node) {
var rect = node.getBoundingClientRect()
return $.extend({}, rect, {
startTimeMs: FullCalendar.createDuration(
$(node).data('time')
).milliseconds
})
}).get()

var len = slots.length
if (len < 3) {
console.log('need at least 3 slots')
return []
}

var mid = Math.floor(len / 2)
var i = mid - 1
var standardMs = slots[mid + 1].startTimeMs - slots[mid].startTimeMs
var ms
var dayOffset = 0

// iterate from one-before middle to beginning
for (i = mid - 1; i >= 0; i--) {
ms = slots[i + 1].startTimeMs - slots[i].startTimeMs

// big deviation? assume moved to previous day (b/c of special minTime)
if (Math.abs(ms - standardMs) > standardMs * 2) {
dayOffset--
slots[i].endTimeMs = slots[i].startTimeMs + standardMs
} else { // otherwise, current slot's end is next slot's beginning
slots[i].endTimeMs = slots[i + 1].startTimeMs
}

slots[i].dayOffset = dayOffset
}

dayOffset = 0

// iterate from middle to one-before last
for (i = mid; i < len - 1; i++) {
ms = slots[i + 1].startTimeMs - slots[i].startTimeMs

slots[i].dayOffset = dayOffset

// big deviation? assume moved to next day (b/c of special maxTime)
if (Math.abs(ms - standardMs) > standardMs * 2) {
dayOffset++ // will apply to the next slotStruct
slots[i].endTimeMs = slots[i].startTimeMs + standardMs
} else { // otherwise, current slot's end is next slot's beginning
slots[i].endTimeMs = slots[i + 1].startTimeMs
}
}

// assume last slot has the standard duration
slots[i].endTimeMs = slots[i].startTimeMs + standardMs
slots[i].dayOffset = dayOffset

// if last slot went over the day threshold
if (slots[i].endTimeMs > 1000 * 60 * 60 * 24) {
slots[i].endTimeMs -= 1000 * 60 * 60 * 24
slots[i].dayOffset++
}

return slots
}
6 changes: 3 additions & 3 deletions tests/automated/event-resize/EventResizeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getRectCenter, getRectTopLeft, subtractPoints, addPoints } from '../lib
import * as EventRenderUtils from '../event-render/EventRenderUtils'


export function resize(rect0, rect1, fromStart, debug) {
export function resize(point0, point1, fromStart, debug) {
var eventEl = EventRenderUtils.getSingleEl()

eventEl.simulate('mouseover') // so that resize handle is revealed
Expand All @@ -13,10 +13,10 @@ export function resize(rect0, rect1, fromStart, debug) {

var vector = subtractPoints(
resizerCenter,
getRectTopLeft(rect0)
point0
)
var point1 = addPoints(
getRectTopLeft(rect1),
point1,
vector
)
var deferred = $.Deferred()
Expand Down
1 change: 1 addition & 0 deletions tests/automated/lib/geom.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function addPoints(point0, point1) {
)
}

// in most situations you can just use the rect directly, since the interface is a superset
export function getRectTopLeft(rect) {
return buildPoint(rect.left, rect.top)
}
Expand Down
169 changes: 169 additions & 0 deletions tests/automated/view-render/TimeGridRenderUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { getDayOfWeekHeaderEls } from './DayGridRenderUtils' // bad to rely on day grid
import { getSlotEls } from '../lib/time-grid'
import { ensureDate } from '../datelib/utils'


export function getTimeAxisInfo() {
return $('.fc-slats tr[data-time]').map(function(i, tr) {
Expand All @@ -13,3 +17,168 @@ export function getTimeAxisInfo() {
export function isStructureValid() {
return $('.fc-time-grid .fc-content-skeleton').length === 1
}


export function computeSpanRects(start, end) {
start = ensureDate(start)
end = ensureDate(end)

var dayStructs = computeDays()
var slotStructs = computeSlots()
var dayI, dayStruct
var slotI, slotStruct
var slotDayStart
var slotStart
var slotEnd
var coverage
var startTop = null
var endTop = null
var rects = []

for (dayI = 0; dayI < dayStructs.length; dayI++) {
dayStruct = dayStructs[dayI]

for (slotI = 0; slotI < slotStructs.length; slotI++) {
slotStruct = slotStructs[slotI]

slotDayStart = FullCalendar.addDays(
dayStruct.date,
slotStruct.dayOffset
)

slotStart = FullCalendar.addMs(
slotDayStart,
slotStruct.startTimeMs
)

slotEnd = FullCalendar.addMs(
slotDayStart,
slotStruct.endTimeMs
)

if (startTop === null) { // looking for the start
coverage = (start - slotStart) / (slotEnd - slotStart)
startTop = (coverage > 0 && coverage <= 1)
? (slotStruct.top + slotStruct.height * coverage)
: null
} else { // looking for the end
coverage = (end - slotStart) / (slotEnd - slotStart)
endTop = (coverage >= 0 && coverage < 1) // exclusive
? (slotStruct.top + slotStruct.height * coverage)
: null

if (endTop !== null) { // found end
rects.push({
left: dayStruct.left,
right: dayStruct.right,
top: startTop,
bottom: endTop,
width: dayStruct.right - dayStruct.left,
height: endTop - startTop
})
startTop = null
}
}
}

if (startTop !== null) { // could not find the start in this day
rects.push({
left: dayStruct.left,
right: dayStruct.right,
top: startTop,
bottom: slotStruct.bottom,
width: dayStruct.right - dayStruct.left,
height: slotStruct.bottom - startTop
})
startTop = slotStructs[0].top // top of next column
}
}

return rects
}


function computeDays() {
var dayOfWeekHeaderEls = getDayOfWeekHeaderEls()

var days = dayOfWeekHeaderEls.map(function(i, node) {
var rect = node.getBoundingClientRect()
return $.extend({}, rect, {
date: FullCalendar.parseMarker(
$(node).data('date')
).marker
})
}).get()

return days
}


function computeSlots() {
var slotEls = getSlotEls()

var slots = slotEls.map(function(i, node) {
var rect = node.getBoundingClientRect()
return $.extend({}, rect, {
startTimeMs: FullCalendar.createDuration(
$(node).data('time')
).milliseconds
})
}).get()

var len = slots.length
if (len < 3) {
console.log('need at least 3 slots')
return []
}

var mid = Math.floor(len / 2)
var i = mid - 1
var standardMs = slots[mid + 1].startTimeMs - slots[mid].startTimeMs
var ms
var dayOffset = 0

// iterate from one-before middle to beginning
for (i = mid - 1; i >= 0; i--) {
ms = slots[i + 1].startTimeMs - slots[i].startTimeMs

// big deviation? assume moved to previous day (b/c of special minTime)
if (Math.abs(ms - standardMs) > standardMs * 2) {
dayOffset--
slots[i].endTimeMs = slots[i].startTimeMs + standardMs
} else { // otherwise, current slot's end is next slot's beginning
slots[i].endTimeMs = slots[i + 1].startTimeMs
}

slots[i].dayOffset = dayOffset
}

dayOffset = 0

// iterate from middle to one-before last
for (i = mid; i < len - 1; i++) {
ms = slots[i + 1].startTimeMs - slots[i].startTimeMs

slots[i].dayOffset = dayOffset

// big deviation? assume moved to next day (b/c of special maxTime)
if (Math.abs(ms - standardMs) > standardMs * 2) {
dayOffset++ // will apply to the next slotStruct
slots[i].endTimeMs = slots[i].startTimeMs + standardMs
} else { // otherwise, current slot's end is next slot's beginning
slots[i].endTimeMs = slots[i + 1].startTimeMs
}
}

// assume last slot has the standard duration
slots[i].endTimeMs = slots[i].startTimeMs + standardMs
slots[i].dayOffset = dayOffset

// if last slot went over the day threshold
if (slots[i].endTimeMs > 1000 * 60 * 60 * 24) {
slots[i].endTimeMs -= 1000 * 60 * 60 * 24
slots[i].dayOffset++
}

return slots
}

0 comments on commit e65878a

Please sign in to comment.