@@ -7,109 +7,120 @@ var vanillaCalendar = {
7
7
date : new Date ( ) ,
8
8
todaysDate : new Date ( ) ,
9
9
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 ( ) ;
14
14
} ,
15
15
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
+ } ) ;
24
24
// 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
+ } ) ;
31
31
} ,
32
32
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 ) ;
39
39
40
+ // if it's the first day of the month
40
41
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 ) + "%" ;
44
46
}
45
47
}
46
48
47
49
if ( this . date . getTime ( ) <= this . todaysDate . getTime ( ) - 1 ) {
48
- newDay . classList . add ( ' vcal-date--disabled' )
50
+ newDay . classList . add ( " vcal-date--disabled" ) ;
49
51
} 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" ) ;
52
54
}
53
55
54
56
if ( this . date . toString ( ) === this . todaysDate . toString ( ) ) {
55
- newDay . classList . add ( ' vcal-date--today' )
57
+ newDay . classList . add ( " vcal-date--today" ) ;
56
58
}
57
59
58
- newDay . appendChild ( dateEl )
59
- this . month . appendChild ( newDay )
60
+ newDay . appendChild ( dateEl ) ;
61
+ this . month . appendChild ( newDay ) ;
60
62
} ,
61
63
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
+ ) ;
65
69
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
+ } ) ;
72
78
}
73
79
} ,
74
80
75
- createMonth : function ( ) {
76
- var currentMonth = this . date . getMonth ( )
81
+ createMonth : function ( ) {
82
+ var currentMonth = this . date . getMonth ( ) ;
77
83
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 ) ;
80
90
}
81
91
// 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 ) ;
84
94
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 ( ) ;
87
98
} ,
88
99
89
- monthsAsString : function ( monthIndex ) {
100
+ monthsAsString : function ( monthIndex ) {
90
101
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 ] ;
104
115
} ,
105
116
106
- clearCalendar : function ( ) {
107
- vanillaCalendar . month . innerHTML = ''
117
+ clearCalendar : function ( ) {
118
+ vanillaCalendar . month . innerHTML = "" ;
108
119
} ,
109
120
110
- removeActiveClass : function ( ) {
121
+ removeActiveClass : function ( ) {
111
122
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" ) ;
113
124
}
114
125
}
115
- }
126
+ } ;
0 commit comments