Skip to content

Commit e6156e9

Browse files
committed
Fix issue #5
1 parent eb1e689 commit e6156e9

File tree

2 files changed

+81
-70
lines changed

2 files changed

+81
-70
lines changed

dist/vanillaCalendar.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vanillaCalendar.js

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,109 +7,120 @@ var vanillaCalendar = {
77
date: new Date(),
88
todaysDate: new Date(),
99

10-
init: function () {
11-
this.date.setDate(1)
12-
this.createMonth()
13-
this.createListeners()
10+
init: function() {
11+
this.date.setDate(1);
12+
this.createMonth();
13+
this.createListeners();
1414
},
1515

16-
createListeners: function () {
17-
var _this = this
18-
this.next.addEventListener('click', function () {
19-
_this.clearCalendar()
20-
var nextMonth = _this.date.getMonth() + 1
21-
_this.date.setMonth(nextMonth)
22-
_this.createMonth()
23-
})
16+
createListeners: function() {
17+
var _this = this;
18+
this.next.addEventListener("click", function() {
19+
_this.clearCalendar();
20+
var nextMonth = _this.date.getMonth() + 1;
21+
_this.date.setMonth(nextMonth);
22+
_this.createMonth();
23+
});
2424
// Clears the calendar and shows the previous month
25-
this.previous.addEventListener('click', function () {
26-
_this.clearCalendar()
27-
var prevMonth = _this.date.getMonth() - 1
28-
_this.date.setMonth(prevMonth)
29-
_this.createMonth()
30-
})
25+
this.previous.addEventListener("click", function() {
26+
_this.clearCalendar();
27+
var prevMonth = _this.date.getMonth() - 1;
28+
_this.date.setMonth(prevMonth);
29+
_this.createMonth();
30+
});
3131
},
3232

33-
createDay: function (num, day, year) {
34-
var newDay = document.createElement('div')
35-
var dateEl = document.createElement('span')
36-
dateEl.innerHTML = num
37-
newDay.className = 'vcal-date'
38-
newDay.setAttribute('data-calendar-date', this.date)
33+
createDay: function(num, day, year) {
34+
var newDay = document.createElement("div");
35+
var dateEl = document.createElement("span");
36+
dateEl.innerHTML = num;
37+
newDay.className = "vcal-date";
38+
newDay.setAttribute("data-calendar-date", this.date);
3939

40+
// if it's the first day of the month
4041
if (num === 1) {
41-
var offset = ((day - 1) * 14.28)
42-
if (offset > 0) {
43-
newDay.style.marginLeft = offset + '%'
42+
if (day === 0) {
43+
newDay.style.marginLeft = (6 * 14.28) + "%";
44+
} else {
45+
newDay.style.marginLeft = ((day - 1) * 14.28) + "%";
4446
}
4547
}
4648

4749
if (this.date.getTime() <= this.todaysDate.getTime() - 1) {
48-
newDay.classList.add('vcal-date--disabled')
50+
newDay.classList.add("vcal-date--disabled");
4951
} else {
50-
newDay.classList.add('vcal-date--active')
51-
newDay.setAttribute('data-calendar-status', 'active')
52+
newDay.classList.add("vcal-date--active");
53+
newDay.setAttribute("data-calendar-status", "active");
5254
}
5355

5456
if (this.date.toString() === this.todaysDate.toString()) {
55-
newDay.classList.add('vcal-date--today')
57+
newDay.classList.add("vcal-date--today");
5658
}
5759

58-
newDay.appendChild(dateEl)
59-
this.month.appendChild(newDay)
60+
newDay.appendChild(dateEl);
61+
this.month.appendChild(newDay);
6062
},
6163

62-
dateClicked: function () {
63-
var _this = this
64-
this.activeDates = document.querySelectorAll('[data-calendar-status="active"]')
64+
dateClicked: function() {
65+
var _this = this;
66+
this.activeDates = document.querySelectorAll(
67+
'[data-calendar-status="active"]'
68+
);
6569
for (var i = 0; i < this.activeDates.length; i++) {
66-
this.activeDates[i].addEventListener('click', function (event) {
67-
var picked = document.querySelectorAll('[data-calendar-label="picked"]')[0]
68-
picked.innerHTML = this.dataset.calendarDate
69-
_this.removeActiveClass()
70-
this.classList.add('vcal-date--selected')
71-
})
70+
this.activeDates[i].addEventListener("click", function(event) {
71+
var picked = document.querySelectorAll(
72+
'[data-calendar-label="picked"]'
73+
)[0];
74+
picked.innerHTML = this.dataset.calendarDate;
75+
_this.removeActiveClass();
76+
this.classList.add("vcal-date--selected");
77+
});
7278
}
7379
},
7480

75-
createMonth: function () {
76-
var currentMonth = this.date.getMonth()
81+
createMonth: function() {
82+
var currentMonth = this.date.getMonth();
7783
while (this.date.getMonth() === currentMonth) {
78-
this.createDay(this.date.getDate(), this.date.getDay(), this.date.getFullYear())
79-
this.date.setDate(this.date.getDate() + 1)
84+
this.createDay(
85+
this.date.getDate(),
86+
this.date.getDay(),
87+
this.date.getFullYear()
88+
);
89+
this.date.setDate(this.date.getDate() + 1);
8090
}
8191
// while loop trips over and day is at 30/31, bring it back
82-
this.date.setDate(1)
83-
this.date.setMonth(this.date.getMonth() - 1)
92+
this.date.setDate(1);
93+
this.date.setMonth(this.date.getMonth() - 1);
8494

85-
this.label.innerHTML = this.monthsAsString(this.date.getMonth()) + ' ' + this.date.getFullYear()
86-
this.dateClicked()
95+
this.label.innerHTML =
96+
this.monthsAsString(this.date.getMonth()) + " " + this.date.getFullYear();
97+
this.dateClicked();
8798
},
8899

89-
monthsAsString: function (monthIndex) {
100+
monthsAsString: function(monthIndex) {
90101
return [
91-
'January',
92-
'Febuary',
93-
'March',
94-
'April',
95-
'May',
96-
'June',
97-
'July',
98-
'August',
99-
'September',
100-
'October',
101-
'November',
102-
'December'
103-
][monthIndex]
102+
"January",
103+
"Febuary",
104+
"March",
105+
"April",
106+
"May",
107+
"June",
108+
"July",
109+
"August",
110+
"September",
111+
"October",
112+
"November",
113+
"December"
114+
][monthIndex];
104115
},
105116

106-
clearCalendar: function () {
107-
vanillaCalendar.month.innerHTML = ''
117+
clearCalendar: function() {
118+
vanillaCalendar.month.innerHTML = "";
108119
},
109120

110-
removeActiveClass: function () {
121+
removeActiveClass: function() {
111122
for (var i = 0; i < this.activeDates.length; i++) {
112-
this.activeDates[i].classList.remove('vcal-date--selected')
123+
this.activeDates[i].classList.remove("vcal-date--selected");
113124
}
114125
}
115-
}
126+
};

0 commit comments

Comments
 (0)