Skip to content

lightweight, client-only calendar web application. All data persists in the URL hash for instant sharing, No backend required. Optional AES-GCM password protection keeps shared links locked without a server

JavaScript

feat: Implement modular calendar and agenda rendering with enhanced event duration display across views.

Supun Lakmal86f788c4f890
diff --git a/modules/agendaRender.js b/modules/agendaRender.js
index 82ab45f..3e0ee7e 100644
--- a/modules/agendaRender.js
+++ b/modules/agendaRender.js
@@ -53,9 +53,19 @@ function buildAgendaData({ events, colors, rangeMonths }) {
const occurrencesByDay = new Map();
occurrences.forEach((occ) => {
- const key = formatDateKey(new Date(occ.start));
- if (!occurrencesByDay.has(key)) occurrencesByDay.set(key, []);
- occurrencesByDay.get(key).push(occ);
+ const start = new Date(occ.start);
+ const end = new Date(occ.end);
+ const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
+ const dayEnd = new Date(end.getFullYear(), end.getMonth(), end.getDate());
+
+ while (current <= dayEnd) {
+ if (current >= rangeStart && current <= rangeEnd) {
+ const key = formatDateKey(current);
+ if (!occurrencesByDay.has(key)) occurrencesByDay.set(key, []);
+ occurrencesByDay.get(key).push(occ);
+ }
+ current.setDate(current.getDate() + 1);
+ }
});
return { occurrences, occurrencesByDay, range: { start: rangeStart, end: rangeEnd } };
@@ -67,7 +77,7 @@ export function renderAgendaView({ events, colors, container, rangeMonths = 6, o
container.innerHTML = "";
- if (!data.occurrences.length) {
+ if (!data.occurrencesByDay.size) {
const empty = document.createElement("div");
empty.className = "agenda-empty";
empty.textContent = t("calendar.noUpcoming");
@@ -78,43 +88,46 @@ export function renderAgendaView({ events, colors, container, rangeMonths = 6, o
const list = document.createElement("div");
list.className = "agenda-list";
- let lastKey = "";
- data.occurrences.forEach((event) => {
- const date = new Date(event.start);
- const key = formatDateKey(date);
-
- if (key !== lastKey) {
- const header = document.createElement("h3");
- header.className = "agenda-date-header";
- header.textContent = formatAgendaHeader(date);
- list.appendChild(header);
- lastKey = key;
- }
+ const sortedDays = Array.from(data.occurrencesByDay.keys()).sort();
- const item = document.createElement("div");
- item.className = "agenda-event-item";
- item.style.borderLeftColor = event.color;
+ sortedDays.forEach((key) => {
+ const dayEvents = data.occurrencesByDay.get(key);
+ const date = new Date(key + "T00:00:00"); // Use key directly for date header
- const time = document.createElement("div");
- time.className = "agenda-time";
- time.textContent = event.timeLabel;
+ const header = document.createElement("h3");
+ header.className = "agenda-date-header";
+ header.textContent = formatAgendaHeader(date);
+ list.appendChild(header);
- const details = document.createElement("div");
- details.className = "agenda-details";
+ dayEvents.forEach((event) => {
+ const isStart = formatDateKey(new Date(event.start)) === key;
+ const item = document.createElement("div");
+ item.className = "agenda-event-item";
+ if (!isStart) item.classList.add("is-continuation");
+ item.style.setProperty("--bg-color", event.color);
+ item.style.borderLeftColor = event.color;
- const title = document.createElement("div");
- title.className = "agenda-title";
- title.textContent = event.title;
+ const time = document.createElement("div");
+ time.className = "agenda-time";
+ time.textContent = isStart ? event.timeLabel : "→";
- details.appendChild(title);
- item.appendChild(time);
- item.appendChild(details);
+ const details = document.createElement("div");
+ details.className = "agenda-details";
- if (typeof onEventClick === "function") {
- item.addEventListener("click", () => onEventClick(event));
- }
+ const title = document.createElement("div");
+ title.className = "agenda-title";
+ title.textContent = event.title;
+
+ details.appendChild(title);
+ item.appendChild(time);
+ item.appendChild(details);
+
+ if (typeof onEventClick === "function") {
+ item.addEventListener("click", () => onEventClick(event));
+ }
- list.appendChild(item);
+ list.appendChild(item);
+ });
});
container.appendChild(list);
diff --git a/modules/calendarRender.js b/modules/calendarRender.js
index c7345c4..143e971 100644
--- a/modules/calendarRender.js
+++ b/modules/calendarRender.js
@@ -106,12 +106,19 @@ export function renderCalendar({
const chip = document.createElement("button");
chip.type = "button";
chip.className = "event-chip";
- chip.style.background = event.color;
+
+ const isStart = formatDateKey(new Date(event.start)) === key;
+ if (!isStart) {
+ chip.classList.add("is-continuation");
+ }
+
+ chip.style.setProperty("--bg-color", event.color);
chip.style.borderColor = event.color;
const time = document.createElement("span");
time.className = "time";
- time.textContent = event.timeLabel;
+ time.textContent = isStart ? event.timeLabel : "→";
+
const title = document.createElement("span");
title.textContent = event.title;
@@ -146,7 +153,7 @@ export function renderTimeGrid({ container, dates, occurrences, onSelectDay, onE
const dayKeys = dates.map((date) => formatDateKey(date));
const buckets = new Map();
dayKeys.forEach((key) => {
- buckets.set(key, { allDay: [], hours: new Map() });
+ buckets.set(key, { allDay: [], timed: [] });
});
occurrences.forEach((occ) => {
@@ -155,39 +162,45 @@ export function renderTimeGrid({ container, dates, occurrences, onSelectDay, onE
if (!bucket) return;
if (occ.isAllDay) {
bucket.allDay.push(occ);
- return;
+ } else {
+ bucket.timed.push(occ);
}
- const hour = new Date(occ.start).getHours();
- if (!bucket.hours.has(hour)) bucket.hours.set(hour, []);
- bucket.hours.get(hour).push(occ);
});
buckets.forEach((bucket) => {
bucket.allDay.sort((a, b) => a.start - b.start);
- bucket.hours.forEach((list) => list.sort((a, b) => a.start - b.start));
+ bucket.timed.sort((a, b) => a.start - b.start);
});
container.innerHTML = "";
+ // Header row (32px) + 1440 rows (one for each minute)
+ // We use 0.7333px per minute to get 44px per hour (44 / 60)
container.style.gridTemplateColumns = `72px repeat(${dates.length}, minmax(0, 1fr))`;
- container.style.gridTemplateRows = `32px repeat(24, minmax(44px, 1fr))`;
+ container.style.gridTemplateRows = `32px repeat(1440, minmax(0.7333px, 1fr))`;
+ // 1. All-Day Section
const allDayLabel = document.createElement("div");
allDayLabel.className = "time-label all-day-label";
allDayLabel.textContent = t("calendar.allDay");
+ allDayLabel.style.gridRow = "1 / 2";
+ allDayLabel.style.gridColumn = "1";
container.appendChild(allDayLabel);
- dates.forEach((date) => {
+ dates.forEach((date, i) => {
const key = formatDateKey(date);
const cell = document.createElement("div");
cell.className = "time-cell all-day";
cell.dataset.date = key;
+ cell.style.gridRow = "1 / 2";
+ cell.style.gridColumn = `${i + 2}`;
+
const bucket = buckets.get(key);
const events = bucket ? bucket.allDay : [];
events.slice(0, 2).forEach((event) => {
const chip = document.createElement("button");
chip.type = "button";
chip.className = "time-event";
- chip.style.background = event.color;
+ chip.style.setProperty("--bg-color", event.color); // Changed from chip.style.background
chip.textContent = event.title;
chip.addEventListener("click", (e) => {
e.stopPropagation();
@@ -205,42 +218,135 @@ export function renderTimeGrid({ container, dates, occurrences, onSelectDay, onE
container.appendChild(cell);
});
+ // 2. Hour Labels and Background Cells
for (let hour = 0; hour < 24; hour += 1) {
const label = document.createElement("div");
label.className = "time-label";
label.textContent = formatHourLabel(hour);
+ // Each hour spans 60 minutes
+ const startRow = 2 + hour * 60;
+ label.style.gridRow = `${startRow} / span 60`;
+ label.style.gridColumn = "1";
container.appendChild(label);
- dates.forEach((date) => {
+ dates.forEach((date, i) => {
const key = formatDateKey(date);
const cell = document.createElement("div");
cell.className = "time-cell";
cell.dataset.date = key;
cell.dataset.hour = String(hour);
- const bucket = buckets.get(key);
- const events = bucket && bucket.hours.get(hour) ? bucket.hours.get(hour) : [];
- events.slice(0, 3).forEach((event) => {
- const chip = document.createElement("button");
- chip.type = "button";
- chip.className = "time-event";
- chip.style.background = event.color;
- chip.textContent = event.title;
- chip.addEventListener("click", (e) => {
- e.stopPropagation();
- onEventClick(event);
- });
- cell.appendChild(chip);
- });
- if (events.length > 3) {
- const more = document.createElement("div");
- more.className = "event-more";
- more.textContent = t("calendar.moreEvents", { count: events.length - 3 });
- cell.appendChild(more);
- }
+ cell.style.gridRow = `${startRow} / span 60`;
+ cell.style.gridColumn = `${i + 2}`;
cell.addEventListener("click", () => onSelectDay(date));
container.appendChild(cell);
});
}
+
+ // 3. Timed Events
+ const timedEvents = occurrences.filter(event => !event.isAllDay);
+
+ dates.forEach((dayStart, dayIndex) => {
+ const dayStartMs = dayStart.getTime();
+ const dayEndMs = dayStartMs + 24 * 60 * 60 * 1000;
+ const dayKey = dayKeys[dayIndex];
+
+ // Filter events for this specific day
+ const dayEvents = timedEvents.filter(event => event.start < dayEndMs && event.end > dayStartMs);
+
+ // Sort by start time, then duration
+ dayEvents.sort((a, b) => a.start - b.start || (b.end - b.start) - (a.end - a.start));
+
+ // Simple slotting algorithm
+ const clusters = [];
+ dayEvents.forEach(event => {
+ let cluster = clusters.find(c => c.end > event.start);
+ if (!cluster) {
+ cluster = { end: event.end, events: [] };
+ clusters.push(cluster);
+ } else {
+ cluster.end = Math.max(cluster.end, event.end);
+ }
+ cluster.events.push(event);
+ });
+
+ clusters.forEach(cluster => {
+ const columns = [];
+ cluster.events.forEach(event => {
+ let colIndex = columns.findIndex(col => col <= event.start);
+ if (colIndex === -1) {
+ columns.push(event.end);
+ colIndex = columns.length - 1;
+ } else {
+ columns[colIndex] = event.end;
+ }
+ event.colIndex = colIndex;
+ });
+ cluster.maxCols = columns.length;
+ });
+
+ dayEvents.forEach((event) => {
+ const overlapStartMs = Math.max(event.start, dayStartMs);
+ const overlapEndMs = Math.min(event.end, dayEndMs);
+
+ const startLocal = new Date(overlapStartMs);
+ const startMin = startLocal.getHours() * 60 + startLocal.getMinutes();
+ const duration = Math.max(15, (overlapEndMs - overlapStartMs) / 60000);
+
+ // Create or get daily wrapper
+ let dayWrapper = container.querySelector(`.day-events-wrapper[data-day="${dayIndex}"]`);
+ if (!dayWrapper) {
+ dayWrapper = document.createElement("div");
+ dayWrapper.className = "day-events-wrapper";
+ dayWrapper.dataset.day = String(dayIndex);
+ dayWrapper.style.gridRow = "2 / 1442";
+ dayWrapper.style.gridColumn = `${dayIndex + 2}`;
+ dayWrapper.style.position = "relative";
+ dayWrapper.style.pointerEvents = "none";
+ container.appendChild(dayWrapper);
+ }
+
+ const chip = document.createElement("button");
+ chip.type = "button";
+ chip.className = "time-event";
+
+ const cluster = clusters.find(c => c.events.includes(event));
+ const width = 100 / (cluster ? cluster.maxCols : 1);
+ const left = (event.colIndex || 0) * width;
+
+ chip.style.setProperty("--bg-color", event.color);
+ chip.style.zIndex = "5";
+ chip.style.pointerEvents = "auto";
+ chip.style.width = `calc(${width}% - 4px)`;
+ chip.style.left = `calc(${left}% + 2px)`;
+ chip.style.position = "absolute";
+
+ // Relative positioning within the dayWrapper
+ chip.style.top = `${startMin * 0.7333}px`;
+ chip.style.height = `${duration * 0.7333}px`;
+
+ const timeSpan = document.createElement("span");
+ timeSpan.className = "time";
+
+ if (overlapStartMs === event.start) {
+ timeSpan.textContent = event.timeLabel;
+ } else {
+ timeSpan.textContent = "→";
+ }
+
+ const titleSpan = document.createElement("span");
+ titleSpan.className = "title";
+ titleSpan.textContent = event.title;
+
+ chip.appendChild(timeSpan);
+ chip.appendChild(titleSpan);
+
+ chip.addEventListener("click", (e) => {
+ e.stopPropagation();
+ onEventClick(event);
+ });
+ dayWrapper.appendChild(chip);
+ });
+ });
}
export function renderYearView({ container, year, eventsByDay, selectedDate, weekStartsOnMonday, onSelectDay }) {
diff --git a/script.js b/script.js
index 501ee76..0fd76c0 100644
--- a/script.js
+++ b/script.js
@@ -970,9 +970,19 @@ function updateLanguageUI() {
function groupOccurrences(occurrences) {
const map = new Map();
occurrences.forEach((occ) => {
- const key = formatDateKey(new Date(occ.start));
- if (!map.has(key)) map.set(key, []);
- map.get(key).push(occ);
+ const start = new Date(occ.start);
+ const end = new Date(occ.end);
+
+ // Normalize start/end to day boundaries for iteration
+ const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
+ const dayEnd = new Date(end.getFullYear(), end.getMonth(), end.getDate());
+
+ while (current <= dayEnd) {
+ const key = formatDateKey(current);
+ if (!map.has(key)) map.set(key, []);
+ map.get(key).push(occ);
+ current.setDate(current.getDate() + 1);
+ }
});
map.forEach((list) => {
diff --git a/styles.css b/styles.css
index 4c88c97..759a7fd 100644
--- a/styles.css
+++ b/styles.css
@@ -655,19 +655,28 @@ button:disabled {
gap: 12px;
align-items: center;
padding: 12px 14px;
- border-radius: 10px;
+ border-radius: 12px;
border: 1px solid var(--border-soft);
- border-left: 4px solid var(--accent);
- background: var(--panel);
+ border-left: 4px solid var(--bg-color, var(--accent));
+ background: color-mix(in srgb, var(--bg-color, var(--accent)), transparent 95%);
+ backdrop-filter: blur(4px);
+ -webkit-backdrop-filter: blur(4px);
box-shadow: var(--shadow-sm);
cursor: pointer;
- transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.2s ease, border-color 0.2s ease;
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.agenda-event-item:hover {
transform: translateY(-2px);
box-shadow: var(--shadow-md);
- border-color: var(--border);
+ border-color: rgba(255, 255, 255, 0.2);
+ background: color-mix(in srgb, var(--bg-color, var(--accent)), transparent 90%);
+}
+
+.agenda-event-item.is-continuation {
+ opacity: 0.7;
+ background: color-mix(in srgb, var(--bg-color, var(--accent)), transparent 97%);
+ border-left-style: dashed;
}
.agenda-time {
@@ -751,31 +760,50 @@ button:disabled {
}
.event-chip {
- border-radius: 12px;
- padding: 3px 8px;
+ border-radius: 10px;
+ padding: 4px 10px;
font-size: 0.72rem;
display: inline-flex;
align-items: center;
- gap: 6px;
+ gap: 8px;
color: #fff;
- border: 1px solid transparent;
- box-shadow: 0 1px 2px rgba(0,0,0,0.1);
+ background: var(--bg-color, var(--accent));
+ border: 1px solid rgba(255, 255, 255, 0.2);
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
+ backdrop-filter: blur(4px);
+ -webkit-backdrop-filter: blur(4px);
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
- transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.2s ease;
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
cursor: pointer;
}
.event-chip:hover {
- transform: translateY(-2px);
- box-shadow: var(--shadow-sm);
+ transform: translateY(-2px) scale(1.02);
+ box-shadow: var(--shadow-md);
filter: brightness(1.1);
+ color: #000;
+ border-color: rgba(255, 255, 255, 0.4);
+}
+
+body[data-theme="dark"] .event-chip:hover {
+ color: #fff;
+ filter: brightness(1.2);
+}
+
+.event-chip.is-continuation {
+ opacity: 0.7;
+ border-style: solid;
+ border-width: 1px;
+ background: color-mix(in srgb, var(--bg-color, var(--accent)), transparent 40%);
}
.event-chip .time {
- font-weight: 600;
+ font-weight: 700;
font-size: 0.65rem;
+ opacity: 0.9;
+ font-family: var(--font-mono, monospace);
}
.event-more {
@@ -826,13 +854,50 @@ button:disabled {
}
.time-event {
- border-radius: 4px;
- padding: 2px 6px;
- font-size: 0.68rem;
+ border-radius: 8px;
+ padding: 6px 10px;
+ font-size: 0.72rem;
color: #fff;
- border: none;
+ background: color-mix(in srgb, var(--bg-color, var(--accent)), transparent 15%);
+ border: 1px solid rgba(255, 255, 255, 0.2);
+ backdrop-filter: blur(8px);
+ -webkit-backdrop-filter: blur(8px);
text-align: left;
cursor: pointer;
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
+ overflow: hidden;
+}
+
+.time-event:hover {
+ transform: scale(1.02);
+ z-index: 10 !important;
+ filter: brightness(1.1);
+ color: #000;
+ box-shadow: var(--shadow-lg);
+ border-color: rgba(255, 255, 255, 0.5);
+}
+
+body[data-theme="dark"] .time-event:hover {
+ color: #fff;
+ filter: brightness(1.2);
+}
+
+.time-event .time {
+ font-weight: 700;
+ font-size: 0.65rem;
+ opacity: 0.9;
+ font-family: var(--font-mono, monospace);
+}
+
+.time-event .title {
+ font-weight: 600;
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
}
.calendar-grid.year-view {