Skip to content

Commit 405cc69

Browse files
committed
Update jquery.fn.gantt.js
Fixes bugs: Weekly scale showing week zero in some situations (taitems#112) Weekly scale not showing correct number of weeks (52 or 53) (taitems#99) Daily scale showing day 1 inside the previous month group PS: This is my first GitHub contribuition, hope that I'm doing everything right.
1 parent fdbb279 commit 405cc69

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

js/jquery.fn.gantt.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,45 @@
139139

140140
// `getWeekOfYear` returns the week number for the year
141141
Date.prototype.getWeekOfYear = function () {
142-
var ys = new Date(this.getFullYear(), 0, 1);
143-
var sd = new Date(this.getFullYear(), this.getMonth(), this.getDate());
144-
if (ys.getDay() > 3) {
145-
ys = new Date(sd.getFullYear(), 0, (7 - ys.getDay()));
146-
}
147-
var daysCount = sd.getDayOfYear() - ys.getDayOfYear();
148-
return Math.ceil(daysCount / 7);
149-
142+
var d = new Date(this.valueOf());
143+
d.setHours(0, 0, 0);
144+
// Set to nearest Thursday: current date + 4 - current day number
145+
// Make Sunday's day number 7
146+
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
147+
// Get first day of year
148+
var yearStart = new Date(d.getFullYear(), 0, 1);
149+
// Calculate full weeks to nearest Thursday
150+
var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
151+
// Return week number
152+
return weekNo;
150153
};
151154

152155
// `getDaysInMonth` returns the number of days in a month
153156
Date.prototype.getDaysInMonth = function () {
154157
return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate();
155158
};
159+
160+
// `getWeekYear` return the year from the actual week number, not from the date (e.g. December 31th, 2014 it's actually in week 1 from 2015)
161+
Date.prototype.getWeekYear = function () {
162+
var d = new Date(this.valueOf());
163+
d.setHours(0, 0, 0);
164+
// Set to nearest Thursday: current date + 4 - current day number
165+
// Make Sunday's day number 7
166+
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
167+
168+
return d.getFullYear();
169+
};
170+
171+
// `getWeekMonth` return the month from the actual week number, not from the date (e.g. December 31th, 2014 it's actually in January, 2015)
172+
Date.prototype.getWeekMonth = function () {
173+
var d = new Date(this.valueOf());
174+
d.setHours(0, 0, 0);
175+
// Set to nearest Thursday: current date + 4 - current day number
176+
// Make Sunday's day number 7
177+
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
178+
179+
return d.getMonth();
180+
};
156181

157182
// `hasWeek` returns `true` if the date resides on a week boundary
158183
// **????????????????? Don't know if this is true**
@@ -567,27 +592,27 @@
567592
var rday = range[i];
568593

569594
// Fill years
570-
if (rday.getFullYear() !== year) {
595+
if (rday.getWeekYear() !== year) {
571596
yearArr.push(
572597
('<div class="row header year" style="width: '
573598
+ tools.getCellSize() * daysInYear
574599
+ 'px;"><div class="fn-label">'
575600
+ year
576601
+ '</div></div>'));
577-
year = rday.getFullYear();
602+
year = rday.getWeekYear();
578603
daysInYear = 0;
579604
}
580605
daysInYear++;
581606

582607
// Fill months
583-
if (rday.getMonth() !== month) {
608+
if (rday.getWeekMonth() !== month) {
584609
monthArr.push(
585610
('<div class="row header month" style="width:'
586611
+ tools.getCellSize() * daysInMonth
587612
+ 'px;"><div class="fn-label">'
588613
+ settings.months[month]
589614
+ '</div></div>'));
590-
month = rday.getMonth();
615+
month = rday.getWeekMonth();
591616
daysInMonth = 0;
592617
}
593618
daysInMonth++;
@@ -737,14 +762,8 @@
737762
+ '</div></div>');
738763

739764
var dataPanel = core.dataPanel(element, range.length * tools.getCellSize());
740-
741-
742-
// Append panel elements
743-
744-
dataPanel.append(yearArr.join(""));
745-
dataPanel.append(monthArr.join(""));
746-
dataPanel.append($('<div class="row" style="margin-left: 0;" />').html(dayArr.join("")));
747-
dataPanel.append($('<div class="row" style="margin-left: 0;" />').html(dowArr.join("")));
765+
766+
dataPanel.append(yearArr.join("") + monthArr.join("") + dayArr.join("") + (dowArr.join("")));
748767

749768
break;
750769
}

0 commit comments

Comments
 (0)