Skip to content

Commit 1dbdd82

Browse files
committed
28 Sep 2023 Calendar update
1 parent c8ce756 commit 1dbdd82

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

calendar/assets/PAGE-calendar.js

+34-23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var cal = {
2929
// (B2) ATTACH CONTROLS
3030
cal.hMth.onchange = cal.load;
3131
cal.hYear.onchange = cal.load;
32+
document.getElementById("calToday").onclick = () => cal.today();
3233
document.getElementById("calBack").onclick = () => cal.pshift();
3334
document.getElementById("calNext").onclick = () => cal.pshift(1);
3435
document.getElementById("calAdd").onclick = () => cal.show();
@@ -64,8 +65,18 @@ var cal = {
6465
cal.hYear.value = cal.sYear;
6566
cal.load();
6667
},
68+
69+
// (D) JUMP TO TODAY
70+
today : () => {
71+
let now = new Date(), ny = now.getFullYear(), nm = now.getMonth()+1;
72+
if (ny!=cal.sYear || (ny==cal.sYear && nm!=cal.sMth)) {
73+
cal.hMth.value = nm;
74+
cal.hYear.value = ny;
75+
cal.load();
76+
}
77+
},
6778

68-
// (D) LOAD EVENTS
79+
// (E) LOAD EVENTS
6980
load : () => {
7081
cal.sMth = parseInt(cal.hMth.value);
7182
cal.sYear = parseInt(cal.hYear.value);
@@ -79,10 +90,10 @@ var cal = {
7990
}
8091
});
8192
},
82-
83-
// (E) DRAW CALENDAR
93+
94+
// (F) DRAW CALENDAR
8495
draw : () => {
85-
// (E1) CALCULATE DAY MONTH YEAR
96+
// (F1) CALCULATE DAY MONTH YEAR
8697
// note - jan is 0 & dec is 11 in js
8798
// note - sun is 0 & sat is 6 in js
8899
let daysInMth = new Date(cal.sYear, cal.sMth, 0).getDate(), // number of days in selected month
@@ -93,11 +104,11 @@ var cal = {
93104
nowYear = parseInt(now.getFullYear()), // current year
94105
nowDay = cal.sMth==nowMth && cal.sYear==nowYear ? now.getDate() : null ;
95106

96-
// (E2) DRAW CALENDAR ROWS & CELLS
97-
// (E2-1) INIT
107+
// (F2) DRAW CALENDAR ROWS & CELLS
108+
// (F2-1) INIT
98109
let rowA, rowB, rowC, rowMap = {}, rowNum = 1, cell, cellNum = 1,
99110

100-
// (E2-2) HELPER - DRAW A NEW ROW
111+
// (F2-2) HELPER - DRAW A NEW ROW
101112
rower = () => {
102113
rowA = document.createElement("div");
103114
rowB = document.createElement("div");
@@ -111,7 +122,7 @@ var cal = {
111122
rowA.appendChild(rowC);
112123
},
113124

114-
// (E2-3) HELPER - DRAW A NEW CELL
125+
// (F2-3) HELPER - DRAW A NEW CELL
115126
celler = day => {
116127
cell = document.createElement("div");
117128
cell.className = "calCell";
@@ -134,10 +145,10 @@ var cal = {
134145
rowC.appendChild(cell);
135146
};
136147

137-
// (E2-4) RESET CALENDAR
148+
// (F2-4) RESET CALENDAR
138149
cal.hCB.innerHTML = ""; rower();
139150

140-
// (E2-5) BLANK CELLS BEFORE START OF MONTH
151+
// (F2-5) BLANK CELLS BEFORE START OF MONTH
141152
if (cal.mon && startDay != 1) {
142153
let blanks = startDay==0 ? 7 : startDay ;
143154
for (let i=1; i<blanks; i++) { celler(); cellNum++; }
@@ -146,15 +157,15 @@ var cal = {
146157
for (let i=0; i<startDay; i++) { celler(); cellNum++; }
147158
}
148159

149-
// (E2-6) DAYS OF THE MONTH
160+
// (F2-6) DAYS OF THE MONTH
150161
for (let i=1; i<=daysInMth; i++) {
151162
rowMap[i] = { r : rowNum, c : cellNum };
152163
celler(i);
153164
if (cellNum%7==0 && i!=daysInMth) { rowNum++; rower(); }
154165
cellNum++;
155166
}
156167

157-
// (E2-7) BLANK CELLS AFTER END OF MONTH
168+
// (F2-7) BLANK CELLS AFTER END OF MONTH
158169
if (cal.mon && endDay != 0) {
159170
let blanks = endDay==6 ? 1 : 7-endDay;
160171
for (let i=0; i<blanks; i++) { celler(); cellNum++; }
@@ -164,16 +175,16 @@ var cal = {
164175
for (let i=0; i<blanks; i++) { celler(); cellNum++; }
165176
}
166177

167-
// (E3) FETCH & DRAW EVENTS
178+
// (F3) FETCH & DRAW EVENTS
168179
if (cal.events !== null) { for (let [id,evt] of Object.entries(cal.events)) {
169-
// (E3-1) EVENT START & END DAY
180+
// (F3-1) EVENT START & END DAY
170181
let sd = new Date(evt.s), ed = new Date(evt.e);
171182
if (sd.getFullYear() != cal.sYear) { sd = 1; }
172183
else { sd = sd.getMonth()+1 < cal.sMth ? 1 : sd.getDate(); }
173184
if (ed.getFullYear() != cal.sYear) { ed = daysInMth; }
174185
else { ed = ed.getMonth()+1 > cal.sMth ? daysInMth : ed.getDate(); }
175186

176-
// (E3-2) "MAP" ONTO HTML CALENDAR
187+
// (F3-2) "MAP" ONTO HTML CALENDAR
177188
cell = {}; rowNum = 0;
178189
for (let i=sd; i<=ed; i++) {
179190
if (rowNum!=rowMap[i]["r"]) {
@@ -183,7 +194,7 @@ var cal = {
183194
if (cell[rowNum]) { cell[rowNum]["e"] = rowMap[i]["c"]; }
184195
}
185196

186-
// (E3-3) DRAW HTML EVENT ROW
197+
// (F3-3) DRAW HTML EVENT ROW
187198
for (let [r,c] of Object.entries(cell)) {
188199
let o = c.s - 1 - ((r-1) * 7), // event cell offset
189200
w = c.e - c.s + 1; // event cell width
@@ -201,7 +212,7 @@ var cal = {
201212
}}
202213
},
203214

204-
// (F) SHOW EVENT FORM
215+
// (G) SHOW EVENT FORM
205216
show : id => {
206217
if (id) {
207218
cal.hfID.value = id;
@@ -220,9 +231,9 @@ var cal = {
220231
else { cal.hFormWrap.show(); }
221232
},
222233

223-
// (G) SAVE EVENT
234+
// (H) SAVE EVENT
224235
save : () => {
225-
// (G1) COLLECT DATA
236+
// (H1) COLLECT DATA
226237
var data = {
227238
start : cal.hfStart.value,
228239
end : cal.hfEnd.value,
@@ -232,13 +243,13 @@ var cal = {
232243
};
233244
if (cal.hfID.value != "") { data.id = cal.hfID.value; }
234245

235-
// (G2) DATE CHECK
246+
// (H2) DATE CHECK
236247
if (new Date(data.start) > new Date(data.end)) {
237248
alert("Start date cannot be later than end date!");
238249
return false;
239250
}
240251

241-
// (G3) AJAX SAVE
252+
// (H3) AJAX SAVE
242253
cb.api({
243254
mod : "calendar", act : "save",
244255
data : data,
@@ -251,7 +262,7 @@ var cal = {
251262
return false;
252263
},
253264

254-
// (H) DELETE EVENT
265+
// (I) DELETE EVENT
255266
del : () => cb.modal("Please confirm", "Delete this event?", () => cb.api({
256267
mod : "calendar", act : "del",
257268
data : { id : cal.hfID.value },
@@ -262,4 +273,4 @@ var cal = {
262273
}
263274
}))
264275
};
265-
window.addEventListener("load", cal.init);
276+
window.addEventListener("load", cal.init);

calendar/pages/PAGE-calendar.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<!-- (B1) PERIOD SELECTOR -->
2121
<div id="calHead" class="d-flex align-items-stretch p-2">
2222
<div id="calPeriod" class="d-flex align-items-stretch flex-grow-1">
23+
<button type="button" id="calToday" class="p-2 ico icon-pushpin"></button>
2324
<button type="button" id="calBack" class="p-2 me-2 ico icon-circle-left"></button>
2425
<select id="calMonth" class="p-2 me-2"><?php foreach ($months as $m=>$mth) {
2526
printf("<option value='%u'%s>%s</option>",

0 commit comments

Comments
 (0)