-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple-calendar.js
162 lines (133 loc) · 4.59 KB
/
simple-calendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* SIMPLE CALENDAR BOILERPLATE
* this is not meant to be a stantalone widget but to be a starter template to jumpstart your calendar widget (or any widget with a calendar).
* dates can be difficult to work with, so this template is here to make it easier
*/
/*
* BASE WIDGET STYLING
* change the below variables to customize your calendar style
*/
const spaceBetween = 2
const dateTextSize = 6
const dateSize = 13
const padding = 2
const cornerRadius = 2
const color = Color.dynamic(Color.lightGray(), Color.darkGray())
const weekColor = Color.blue()
const todayColor = Color.red()
const textColor = Color.white()
const titleColor = Color.white()
/*
* CALENDAR PREFERENCES
* change the below variables to change how the calendar behaves
*/
// Only show the days in the month or show the week leading to the first day in the month and the week leaving the last
const showOnlyMonth = true
// Week will start Monday or Sunday
const weekStartsMonday = false
/*
* CREATE THE WIDGET
* only change the below if you know whats happening
*/
// get the calendar data
const calendarData = calendar(weekStartsMonday)
// Start making the widget
const widget = new ListWidget()
// Add the top day titles
let columnTitle = widget.addStack()
columnTitle.layoutHorizontally()
columnTitle.spacing = spaceBetween
columnTitle.addSpacer()
const dayTitles = weekStartsMonday
? ["M", "T", "W", "T", "F", "S", "S"]
: ["S", "M", "T", "W", "T", "F", "S"]
for (let title of dayTitles) {
const titleStack = columnTitle.addStack()
titleStack.setPadding(padding, padding, padding, padding)
titleStack.backgroundColor = weekColor
titleStack.size = new Size(dateSize, dateSize)
titleStack.centerAlignContent()
titleStack.cornerRadius = cornerRadius
const titleText = titleStack.addText(title)
titleText.font = Font.boldSystemFont(dateTextSize)
titleText.textColor = titleColor
}
columnTitle.addSpacer()
widget.addSpacer(spaceBetween * 3)
// Add the days for each week
for (let week of calendarData) {
const weekStack = widget.addStack()
widget.addSpacer(spaceBetween)
weekStack.layoutHorizontally()
weekStack.addSpacer()
weekStack.spacing = spaceBetween
for (let day of week) {
const dayStack = weekStack.addStack()
dayStack.setPadding(padding, padding, padding, padding)
dayStack.backgroundColor = day.isToday ? todayColor : color
dayStack.size = new Size(dateSize, dateSize)
dayStack.centerAlignContent()
dayStack.cornerRadius = cornerRadius
if (showOnlyMonth && !day.isInMonth) {
dayStack.backgroundColor = new Color("#000", 0)
continue
}
const dayText = dayStack.addText(day.day.toString())
dayText.font = Font.regularSystemFont(dateTextSize)
dayText.textColor = textColor
}
weekStack.addSpacer()
}
// Present the widget
widget.presentSmall()
/*
* GETING THE CALENDAR DATA
* this is the function that gets the data that is used to make the widget
*/
function calendar(weekStartsMonday = false) {
const today = new Date()
const currentMonth = today.getMonth()
const currentYear = today.getFullYear()
// Get the number corresponding to the start of the week day
const weekStart = weekStartsMonday ? 1 : 0
// Start day count earlier for if the month does not begin on the week start day
// Set to -6 because it will be incremented to -5 right away and it will get 6 days before the first day because 0 is not the first day in a month
let dayCount = -6
// Store days and week
const days = []
// Repeat to get all the dates for the calendar
while (true) {
dayCount++
// Get the next date
let date = new Date(currentYear, currentMonth, dayCount)
// Do not add the date if it is the first one and not the week start day
if (days.length === 0 && date.getDay() !== weekStart) {
continue
// Exit the loop if it reaches a week start day that is not the first added and not in the current month
} else if (
days.length !== 0 &&
date.getDay() === weekStart &&
date.getMonth() !== currentMonth
) {
break
}
// Push date information to the days array
/*
* EXTRA DAY DATA
* here is where you would include other data for the day (for example, `isGame: bool` for a sport calendar)
*/
days.push({
day: date.getDate(),
isInMonth: date.getMonth() === currentMonth,
isToday:
date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth()
})
}
// Store a 2D array of the month with week arrays and the 7 days of the week inside
const month = []
for (let i = 0; i < days.length; i += 7) {
month.push(days.slice(i, i + 7))
}
return month
}