This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.js
376 lines (338 loc) · 9.67 KB
/
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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
var cal = {
// (A) PROPERTIES
// (A1) COMMON CALENDAR
sMon: true, // Week start on Monday?
mName: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
], // Month Names
// (A2) CALENDAR DATA
mthEvents: {}, // Events for the selected period
sDay: 0,
sMth: 0,
sYear: 0, // Current selected day, month, year
// (A3) COMMON HTML ELEMENTS
nxt: null,
prev: null,
hMth: null,
hYear: null, // month/year selector
hForm: null,
hfDate: null,
hfTxt: null, //event form
hfSave: null,
events: null,
eventsView: null,
evCards: null,
alert: null,
date: null,
dateSel: null,
okDate: null,
cancelDate: null,
color: null,
// (B) INIT CALENDAR
init: () => {
// (B1) GET + SET COMMON HTML ELEMENTS
cal.nxt = document.getElementById("nxtMonth");
cal.nxt.onclick = cal.next;
cal.prev = document.getElementById("prevMonth");
cal.prev.onclick = cal.previous;
cal.hMth = document.getElementById("cal-mth");
cal.hYear = document.getElementById("cal-yr");
cal.hfDate = document.getElementById("evt-date");
cal.hfTxt = document.getElementById("evt-details");
cal.hfSave = document.getElementById("evt-save");
document.getElementById("evt-close").onclick = cal.close;
cal.hfSave.onclick = cal.save;
cal.events = [];
cal.eventsView = document.getElementById("eventsDay");
cal.eventsView.classList.add("ninja");
cal.evCards = document.getElementById("evt-cards");
cal.alert = document.getElementById("alert");
cal.alert.classList.add("ninja");
cal.date = document.getElementById("date");
cal.date.onclick = cal.showDateSel;
cal.dateSel = document.getElementById("dateSel");
cal.dateSel.classList.add("ninja");
cal.okDate = document.getElementById("okDate");
cal.okDate.onclick = cal.okDateSel;
cal.cancelDate = document.getElementById("cancelDate");
cal.cancelDate.onclick = cal.closeDateSel;
cal.color = document.getElementById("evtColor");
// handle past and future state updates
window.webxdc.setUpdateListener(function (update) {
if (update.payload.addition) {
cal.events.push(update.payload);
} else {
var index = cal.events.findIndex((obj) => {
console.log(obj.id + " != " + update.payload.id);
return Number.parseInt(obj.id) === Number.parseInt(update.payload.id);
});
console.log(index);
cal.events.splice(index, 1);
}
cal.list();
});
// (B2) DATE NOW
let now = new Date(),
nowMth = now.getMonth(),
nowYear = parseInt(now.getFullYear());
// (B3) APPEND MONTHS SELECTOR
for (let i = 0; i < 12; i++) {
let opt = document.createElement("option");
opt.value = i;
opt.innerHTML = cal.mName[i];
if (i == nowMth) {
opt.selected = true;
}
cal.hMth.appendChild(opt);
}
// cal.hMth.onchange = cal.list;
// (B4) APPEND YEARS SELECTOR
// Set to 10 years range. Change this as you like.
for (let i = nowYear - 10; i <= nowYear + 10; i++) {
let opt = document.createElement("option");
opt.value = i;
opt.innerHTML = i;
if (i == nowYear) {
opt.selected = true;
}
cal.hYear.appendChild(opt);
}
// cal.hYear.onchange = cal.list;
// (B5) START - DRAW CALENDAR
cal.list();
},
//PREVIOUS MONTH
previous: () => {
if (cal.hMth.value > 0) {
cal.hMth.value = Number.parseInt(cal.hMth.value) - 1;
} else {
cal.hYear.value = Number.parseInt(cal.hYear.value) - 1;
cal.hMth.value = "11";
}
cal.list();
},
//NEXT MONTH
next: () => {
if (cal.hMth.value < 11) {
cal.hMth.value = Number.parseInt(cal.hMth.value) + 1;
} else {
cal.hYear.value = Number.parseInt(cal.hYear.value) + 1;
cal.hMth.value = "0";
}
cal.list();
},
// (C) DRAW CALENDAR FOR SELECTED MONTH
list: () => {
// (C1) BASIC CALCULATIONS - DAYS IN MONTH, START + END DAY
// Note - Jan is 0 & Dec is 11
// Note - Sun is 0 & Sat is 6
cal.sMth = parseInt(cal.hMth.value); // selected month
cal.sYear = parseInt(cal.hYear.value); // selected year
let daysInMth = new Date(cal.sYear, cal.sMth + 1, 0).getDate(), // number of days in selected month
startDay = new Date(cal.sYear, cal.sMth, 1).getDay(), // first day of the month
endDay = new Date(cal.sYear, cal.sMth, daysInMth).getDay(), // last day of the month
now = new Date(), // current date
nowMth = now.getMonth(), // current month
nowYear = parseInt(now.getFullYear()), // current year
nowDay =
cal.sMth == nowMth && cal.sYear == nowYear ? now.getDate() : null;
// (C2) LOAD DATA FROM LOCALSTORAGE
cal.mthEvents = [];
//remove deleted events
//get this month events from the updates
cal.mthEvents = cal.events.filter((event) => {
return event.month == cal.sMth && event.year == cal.sYear;
});
// (C3) DRAWING CALCULATIONS
// Blank squares before start of month
let squares = [];
if (cal.sMon && startDay != 1) {
let blanks = startDay == 0 ? 7 : startDay;
for (let i = 1; i < blanks; i++) {
squares.push("b");
}
}
if (!cal.sMon && startDay != 0) {
for (let i = 0; i < startDay; i++) {
squares.push("b");
}
}
// Days of the month
for (let i = 1; i <= daysInMth; i++) {
squares.push(i);
}
// Blank squares after end of month
if (cal.sMon && endDay != 0) {
let blanks = endDay == 6 ? 1 : 7 - endDay;
for (let i = 0; i < blanks; i++) {
squares.push("b");
}
}
if (!cal.sMon && endDay != 6) {
let blanks = endDay == 0 ? 6 : 6 - endDay;
for (let i = 0; i < blanks; i++) {
squares.push("b");
}
}
// (C4) DRAW HTML CALENDAR
// Get container
let container = document.getElementById("cal-container");
container.innerHTML = "";
// First row - Day names
days = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"];
if (cal.sMon) {
days.push(days.shift());
}
let week = document.createElement("div");
for (let d of days) {
let cCell = document.createElement("div");
cCell.innerHTML = d;
week.appendChild(cCell);
}
container.appendChild(week);
week.classList.add("head");
// Today's date
cal.date.innerHTML = cal.mName[cal.sMth] + " " + cal.sYear;
// Days in Month
let total = squares.length;
for (let i = 0; i < total; i++) {
var day = squares[i];
let cCell = document.createElement("div");
if (day == "b") {
cCell.classList.add("blank");
} else {
if (nowDay == day) {
cCell.classList.add("today");
} else {
cCell.classList.add("day");
}
cCell.innerHTML = `<div class="dd">${day}</div>`;
//retrieve events for this day
var eventsDay = cal.getEvents(day);
if (eventsDay.length !== 0) {
for (let j = 0; j < eventsDay.length; j++) {
var evt = document.createElement("div");
evt.classList.add("evt");
evt.textContent = eventsDay[j].data;
evt.style.backgroundColor = eventsDay[j].color;
cCell.appendChild(evt);
// cCell.innerHTML +=
// "<div class='evt'>" + eventsDay[j].data + "</div>";
}
}
cCell.onclick = () => {
cal.show(cCell);
};
}
container.appendChild(cCell);
}
// (C5) REMOVE ANY PREVIOUS ADD/EDIT EVENT DOCKET
cal.close();
},
// (D) SHOW EDIT EVENT DOCKET FOR SELECTED DAY
show: (el) => {
// (D1) FETCH EXISTING DATA
cal.sDay = Number.parseInt(el.getElementsByClassName("dd")[0].innerHTML);
let dayEvents = cal.getEvents(cal.sDay);
//ADD EVENT BOXES
cal.hfTxt.value = "";
cal.evCards.innerHTML = "";
for (const i in dayEvents) {
var eventBox = document.createElement("div");
var remove = document.createElement("span");
remove.innerHTML = "✕";
remove.setAttribute("data-id", dayEvents[i].id);
remove.onclick = (ev) => {
cal.del(ev.target.getAttribute("data-id"));
};
eventBox.textContent = dayEvents[i].data;
eventBox.style.backgroundColor = dayEvents[i].color;
eventBox.appendChild(remove);
eventBox.classList.add("evt-view");
eventBox.classList.add("block");
cal.evCards.appendChild(eventBox);
}
cal.eventsView.classList.remove("ninja");
// // (D2) UPDATE EVENT FORM
cal.hfDate.innerHTML = `${cal.sDay} ${cal.mName[cal.sMth]} ${cal.sYear}`;
},
// (E) CLOSE EVENT DOCKET
close: () => {
cal.eventsView.classList.add("ninja");
},
// GET ALL EVENTS FROM A DAY
getEvents: (day) => {
var events = cal.mthEvents.filter((event) => {
return event.day === day;
});
return events;
},
// (F) SAVE EVENT
save: () => {
if (cal.hfTxt.value !== "") {
// send new updates
var info = window.webxdc.selfName + " created an event";
window.webxdc.sendUpdate(
{
payload: {
id: Date.now(),
day: cal.sDay,
month: cal.sMth,
year: cal.sYear,
data: cal.hfTxt.value,
color: cal.color.value,
addition: true,
creator: window.webxdc.selfName,
},
info,
},
info
);
return false;
} else {
cal.alert.classList.remove("ninja");
}
},
// (G) DELETE EVENT FOR SELECTED DATE
del: (id) => {
// send new updates
var info = window.webxdc.selfName + " deleted an event";
window.webxdc.sendUpdate(
{
payload: {
id: id,
day: cal.sDay,
month: cal.sMth,
year: cal.sYear,
data: cal.hfTxt.value,
addition: false,
deleter: window.webxdc.selfName,
},
info,
},
info
);
},
showDateSel: () => {
cal.dateSel.classList.remove("ninja");
},
okDateSel: () => {
cal.list();
cal.closeDateSel();
},
closeDateSel: () => {
cal.dateSel.classList.add("ninja");
},
};
window.addEventListener("load", cal.init);