Nicola Peluchetti
Nicola Peluchetti

Reputation: 76890

Creating events on a calendar in js with jQuery, i'm stuck with a strange bug

I'm stuck with a strange bug in my code. Actually I modified legacy code which wasn't really clear and ended up with this problem. You can see an example live here in a fiddle.

Basically I've got a calendar and I need to stretch multi day events so that they span the correct number of days. Everything works but for one case: if the first day of the month is the first day of the week. In that case an event that span multiple days is one day shorter only for the first week. I solved this by using a workaround

// There is a bug that happens only when the first day of the 
// month is the first day of the week. In that case events that start on the first of the month
// or multy day events that span from the previous month end up being a day too short in the 
// first week. So for example an event that lasted the full month was only six days in the first week.
// This workaround works for me, couldn't understand what's wrong.
// if (startDay === 1 && daysFirstWeek === 7) {
   // days += 1;
// }

If you uncomment the lines everything works, but obviously this is not a solution.

I need some fresh eyes on this matter, probably it's wrong the concept that lies beneath the whole thing and I should start from scratch, but I'm annoyed when I can't find a solution.

Upvotes: 1

Views: 137

Answers (1)

Ed Bayiates
Ed Bayiates

Reputation: 11230

UPDATED

OK, got it this time. Your check is wrong in the other condition:

        if (cellNum !== 0) {
            // Extend initial event bar to the end of first (!) week.
            if (curLine === 0) {
                days++;
            }
        } else if (day > startDay && daysLeft !== 0) {    // WRONG CHECK HERE!

day > startDay needs to be day >= startDay. Then all works correctly.

This is the case because of the loop initial check:

if (day >= startDay && day <= endDay) {

With the later check being day > startDay, it misses adding the first cell that comes along to days, and, curLine does not get incremented on time. The first cell from the next row ends up being processed with curLine being 1 less than it should. If you console log the curLine and cellNum, you will see they go like this and don't line up where they should. With the fix, the processing goes into the inner condition on the first cell like it should.

Upvotes: 1

Related Questions