-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcalendar_controller.js
More file actions
308 lines (272 loc) · 7.89 KB
/
Copy pathcalendar_controller.js
File metadata and controls
308 lines (272 loc) · 7.89 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { Controller } from "@hotwired/stimulus";
import Mustache from "mustache";
export default class extends Controller {
static targets = [
"calendar",
"title",
"weekdaysTemplate",
"disabledDateTemplate",
"selectedDateTemplate",
"todayDateTemplate",
"currentMonthDateTemplate",
"otherMonthDateTemplate",
];
static values = {
selectedDate: {
type: String,
default: null,
},
minDate: {
type: String,
default: null,
},
viewDate: {
type: String,
default: new Date().toISOString().slice(0, 10),
},
format: {
type: String,
default: "yyyy-MM-dd", // Default format
},
};
static outlets = ["ruby-ui--calendar-input"];
initialize() {
this.updateCalendar(); // Initial calendar render
}
nextMonth(e) {
e.preventDefault();
this.viewDateValue = this.adjustMonth(1);
}
prevMonth(e) {
e.preventDefault();
this.viewDateValue = this.adjustMonth(-1);
}
selectDay(e) {
e.preventDefault();
if (this.isDateDisabled(e.currentTarget.dataset.day)) return;
// Set the selected date value
this.selectedDateValue = e.currentTarget.dataset.day;
}
selectedDateValueChanged(value, prevValue) {
const selectedDate = this.selectedDate();
if (!selectedDate) {
this.updateCalendar();
return;
}
// update the viewDateValue to the first day of month of the selected date (This will trigger updateCalendar() function)
const newViewDate = new Date(selectedDate);
newViewDate.setDate(2); // set the day to the 2nd (to avoid issues with months with different number of days and timezones)
this.viewDateValue = newViewDate.toISOString().slice(0, 10);
// Re-render the calendar
this.updateCalendar();
// update the input value
this.rubyUiCalendarInputOutlets.forEach((outlet) => {
const formattedDate = this.formatDate(selectedDate);
outlet.setValue(formattedDate);
});
}
viewDateValueChanged(value, prevValue) {
this.updateCalendar();
}
adjustMonth(adjustment) {
const date = this.viewDate();
date.setDate(2); // set the day to the 2nd (to avoid issues with months with different number of days and timezones)
date.setMonth(date.getMonth() + adjustment);
return date.toISOString().slice(0, 10);
}
updateCalendar() {
// Update the title with month and year
this.titleTarget.textContent = this.monthAndYear();
this.calendarTarget.innerHTML = this.calendarHTML();
}
calendarHTML() {
return this.weekdaysTemplateTarget.innerHTML + this.calendarDays();
}
calendarDays() {
return this.getFullWeeksStartAndEndInMonth()
.map((week) => this.renderWeek(week))
.join("");
}
renderWeek(week) {
const days = week
.map((day) => {
return this.renderDay(day);
})
.join("");
return `<tr class="flex w-full mt-2">${days}</tr>`;
}
renderDay(day) {
const today = new Date();
const selectedDate = this.selectedDate();
let dateHTML = "";
const data = { day: day, dayDate: day.getDate() };
if (this.isDateDisabled(day)) {
// disabledDate
dateHTML = Mustache.render(
this.disabledDateTemplateTarget.innerHTML,
data,
);
} else if (
selectedDate &&
day.toDateString() === selectedDate.toDateString()
) {
// selectedDate
// Render the selected date template target innerHTML with Mustache
dateHTML = Mustache.render(
this.selectedDateTemplateTarget.innerHTML,
data,
);
} else if (day.toDateString() === today.toDateString()) {
// todayDate
dateHTML = Mustache.render(this.todayDateTemplateTarget.innerHTML, data);
} else if (day.getMonth() === this.viewDate().getMonth()) {
// currentMonthDate
dateHTML = Mustache.render(
this.currentMonthDateTemplateTarget.innerHTML,
data,
);
} else {
// otherMonthDate
dateHTML = Mustache.render(
this.otherMonthDateTemplateTarget.innerHTML,
data,
);
}
return dateHTML;
}
monthAndYear() {
const month = this.viewDate().toLocaleString("en-US", { month: "long" });
const year = this.viewDate().getFullYear();
return `${month} ${year}`;
}
selectedDate() {
return this.parseDate(this.selectedDateValue);
}
viewDate() {
return (
this.parseDate(this.viewDateValue) || this.selectedDate() || new Date()
);
}
getFullWeeksStartAndEndInMonth() {
const month = this.viewDate().getMonth();
const year = this.viewDate().getFullYear();
let weeks = [],
firstDate = new Date(year, month, 1),
lastDate = new Date(year, month + 1, 0),
numDays = lastDate.getDate();
let start = 1;
let end;
if (firstDate.getDay() === 1) {
end = 7;
} else if (firstDate.getDay() === 0) {
let preMonthEndDay = new Date(year, month, 0);
start = preMonthEndDay.getDate() - 6 + 1;
end = 1;
} else {
let preMonthEndDay = new Date(year, month, 0);
start = preMonthEndDay.getDate() + 1 - firstDate.getDay() + 1;
end = 7 - firstDate.getDay() + 1;
weeks.push({
start: start,
end: end,
});
start = end + 1;
end = end + 7;
}
while (start <= numDays) {
weeks.push({
start: start,
end: end,
});
start = end + 1;
end = end + 7;
end = start === 1 && end === 8 ? 1 : end;
if (end > numDays && start <= numDays) {
end = end - numDays;
weeks.push({
start: start,
end: end,
});
break;
}
}
// *** the magic starts here
return weeks.map(({ start, end }, index) => {
const sub = +(start > end && index === 0);
return Array.from({ length: 7 }, (_, index) => {
const date = new Date(year, month - sub, start + index);
return date;
});
});
}
formatDate(date) {
const format = this.formatValue;
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const dayOfWeek = date.toLocaleString("en-US", { weekday: "long" });
const monthName = date.toLocaleString("en-US", { month: "long" });
const daySuffix = this.getDaySuffix(day);
const map = {
yyyy: year,
MM: ("0" + month).slice(-2),
dd: ("0" + day).slice(-2),
HH: ("0" + hours).slice(-2),
mm: ("0" + minutes).slice(-2),
ss: ("0" + seconds).slice(-2),
EEEE: dayOfWeek,
MMMM: monthName,
do: day + daySuffix,
PPPP: `${dayOfWeek}, ${monthName} ${day}${daySuffix}, ${year}`,
};
const formattedDate = format.replace(
/yyyy|MM|dd|HH|mm|ss|EEEE|MMMM|do|PPPP/g,
(matched) => map[matched],
);
return formattedDate;
}
getDaySuffix(day) {
if (day > 3 && day < 21) return "th";
switch (day % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
minDate() {
return this.parseDate(this.minDateValue);
}
isDateDisabled(date) {
const minDate = this.minDate();
const candidate = this.parseDate(date);
if (!minDate || !candidate) return false;
return this.startOfDay(candidate) < this.startOfDay(minDate);
}
parseDate(value) {
if (!value) return null;
if (value instanceof Date) return new Date(value);
const isoDate = value.toString().match(/^(\d{4})-(\d{2})-(\d{2})/);
if (isoDate) {
return new Date(
Number(isoDate[1]),
Number(isoDate[2]) - 1,
Number(isoDate[3]),
);
}
const date = new Date(value);
return Number.isNaN(date.getTime()) ? null : date;
}
startOfDay(date) {
const normalizedDate = new Date(date);
normalizedDate.setHours(0, 0, 0, 0);
return normalizedDate;
}
}