/**
* TaskMate Calendar Card
* One-day view of the chores assigned to each child. Each child gets a
* section with the chores scheduled for the selected day, colour-coded
* by completion state. Prev/Next/Today buttons step through days.
*/
const LitElement = customElements.get("hui-masonry-view")
? Object.getPrototypeOf(customElements.get("hui-masonry-view"))
: Object.getPrototypeOf(customElements.get("hui-view"));
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;
const _safeColor = (c, d) => (typeof c === "string" && /^#[0-9a-fA-F]{3,8}$/.test(c) ? c : d);
const DAY_NAMES = [
"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday",
];
const WINDOW_DAYS = {
every_2_days: 2,
weekly: 7,
every_2_weeks: 14,
monthly: 30,
every_3_months: 90,
every_6_months: 180,
};
function ymd(date, tz) {
return date.toLocaleDateString("en-CA", { timeZone: tz });
}
function addDays(date, days) {
const d = new Date(date);
d.setDate(d.getDate() + days);
return d;
}
function diffDays(a, b) {
const ms = 24 * 60 * 60 * 1000;
return Math.round((a.getTime() - b.getTime()) / ms);
}
class TaskMateCalendarCard extends LitElement {
static get properties() {
return {
hass: { type: Object },
config: { type: Object },
_dayOffset: { type: Number, state: true },
};
}
shouldUpdate(changedProps) {
if (changedProps.has("hass")) {
return window.__taskmate_hasChanged
? window.__taskmate_hasChanged(changedProps.get("hass"), this.hass, this.config?.entity)
: true;
}
return true;
}
constructor() {
super();
this._dayOffset = 0;
}
_t(key, params) {
const fn = window.__taskmate_localize;
return fn ? fn(this.hass, key, params) : key;
}
static get styles() {
const base = css`
:host {
display: block;
--cal-green: #2ecc71;
--cal-amber: #f39c12;
--cal-grey: #bdc3c7;
--cal-blue: #3498db;
}
ha-card { overflow: hidden; }
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
row-gap: 8px;
padding: 14px 18px;
background: var(--taskmate-header-bg, #3498db);
color: white;
}
.header-content { display: flex; align-items: center; gap: 10px; min-width: 0; }
.header-icon { --mdc-icon-size: 28px; opacity: 0.9; flex: none; }
.header-title { font-size: 1.2rem; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.day-nav {
display: flex;
align-items: center;
gap: 6px;
}
.day-nav button {
background: rgba(255,255,255,0.2);
border: none;
color: white;
border-radius: 8px;
padding: 4px 8px;
cursor: pointer;
font-size: 0.8rem;
font-weight: 500;
display: inline-flex;
align-items: center;
gap: 4px;
}
.day-nav button:hover { background: rgba(255,255,255,0.3); }
.day-nav ha-icon { --mdc-icon-size: 18px; }
.day-label {
background: rgba(255,255,255,0.2);
padding: 4px 12px;
border-radius: 12px;
font-size: 0.85rem;
font-weight: 600;
white-space: nowrap;
}
.card-content {
padding: 14px;
display: flex;
flex-direction: column;
gap: 12px;
}
.child-block {
background: var(--secondary-background-color, #f5f5f5);
border-radius: 12px;
padding: 10px 12px;
}
.child-head {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
}
.child-avatar {
width: 34px;
height: 34px;
border-radius: 50%;
background: linear-gradient(135deg, #9b59b6, #a569bd);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.child-avatar ha-icon { --mdc-icon-size: 20px; color: white; }
.child-name {
font-weight: 700;
font-size: 0.95rem;
color: var(--primary-text-color);
flex: 1;
}
.child-summary {
font-size: 0.75rem;
color: var(--secondary-text-color);
font-weight: 500;
}
.chore-list {
display: flex;
flex-direction: column;
gap: 4px;
}
.chore-row {
display: flex;
align-items: center;
gap: 8px;
padding: 6px 10px;
background: var(--card-background-color, white);
border-radius: 8px;
border-left: 4px solid var(--cal-grey);
}
.chore-row.approved { border-left-color: var(--cal-green); }
.chore-row.pending { border-left-color: var(--cal-amber); }
.chore-row.rotating { opacity: 0.6; font-style: italic; }
.chore-icon { --mdc-icon-size: 18px; color: var(--secondary-text-color); }
.chore-icon.approved { color: var(--cal-green); }
.chore-icon.pending { color: var(--cal-amber); }
.chore-name {
flex: 1;
font-size: 0.88rem;
color: var(--primary-text-color);
}
.chore-points {
display: inline-flex;
align-items: center;
gap: 3px;
font-size: 0.78rem;
color: var(--secondary-text-color);
font-weight: 600;
}
.chore-points ha-icon { --mdc-icon-size: 14px; color: #f1c40f; }
.no-chores {
padding: 8px 10px;
font-size: 0.82rem;
color: var(--secondary-text-color);
font-style: italic;
}
.legend {
display: flex;
gap: 12px;
flex-wrap: wrap;
padding: 4px 4px 0;
font-size: 0.72rem;
color: var(--secondary-text-color);
}
.legend-item { display: flex; align-items: center; gap: 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot.approved { background: var(--cal-green); }
.dot.pending { background: var(--cal-amber); }
.dot.due { background: var(--cal-grey); }
.error-state, .empty-state {
display: flex; flex-direction: column; align-items: center;
justify-content: center; padding: 40px 20px;
color: var(--secondary-text-color); text-align: center;
}
.error-state { color: var(--error-color, #f44336); }
.error-state ha-icon, .empty-state ha-icon {
--mdc-icon-size: 48px; margin-bottom: 12px; opacity: 0.5;
}
/* ── Designed styles (playroom / console / cleanpro) ── */
.cal-nav { justify-content: center; gap: 10px; margin-bottom: 13px; }
.cal-nav-btn { width: 32px; height: 32px; font-size: 16px; }
.cal-nav-cur { cursor: pointer; font-size: 13px; padding: 6px 16px; }
.cal-nav-cn { justify-content: space-between; margin-bottom: 12px; }
.cal-nav-cn .cal-nav-cur { font-size: 13px; color: var(--tmd-accent); padding: 0; }
.cal-grid { gap: 11px; }
.cal-head { margin-bottom: 8px; }
.cal-name { font-weight: 800; }
.cal-chips { gap: 7px; flex-wrap: wrap; }
.cal-chip { white-space: normal; overflow-wrap: anywhere; }
.cal-empty-line { font-size: 12px; font-style: italic; }
.cal-card-pl { background: var(--tmd-surface-2); border-radius: 18px; padding: 11px 12px; }
/* Console */
.cal-grid-cn { gap: 9px; }
.cal-card-cn { background: var(--tmd-surface-2); border: 1px solid var(--tmd-border); border-radius: 8px; padding: 10px 11px; }
.cal-head-cn { margin-bottom: 7px; }
.cal-name-cn { font-weight: 700; font-size: 12.5px; }
.cal-count { margin-left: auto; font-size: 10px; }
/* Clean Pro */
.cal-grid-cp { gap: 0; }
.cal-row-cp { align-items: flex-start; padding: 11px 0; border-bottom: 1px solid var(--tmd-border); }
.cal-row-cp:last-child { border-bottom: none; }
.cal-row-mid { flex: 1; min-width: 0; }
.cal-name-cp { font-weight: 600; font-size: 13px; margin-bottom: 5px; }
`;
const tokens = window.__taskmate_design && window.__taskmate_design.styles
? window.__taskmate_design.styles() : null;
return tokens ? [tokens, base] : base;
}
setConfig(config) {
if (!config.entity) throw new Error(this._t("calendar.error.entity_required"));
this.config = {
title: null,
child_id: null,
header_color: "#3498db",
...config,
};
}
getCardSize() { return 4; }
static getConfigElement() { return document.createElement("taskmate-calendar-card-editor"); }
static getStubConfig() {
return { entity: "sensor.taskmate_overview", title: "Task Calendar" };
}
_shiftDay(delta) { this._dayOffset = (this._dayOffset || 0) + delta; }
_resetDay() { this._dayOffset = 0; }
_getSelectedDay(tz) {
const today = new Date();
const selected = addDays(today, this._dayOffset || 0);
const dow = selected.getDay(); // 0 = Sunday
const dayNameIdx = dow === 0 ? 6 : dow - 1;
return {
key: ymd(selected, tz),
dow: DAY_NAMES[dayNameIdx],
date: selected,
todayKey: ymd(today, tz),
};
}
_isChoreScheduledOn(chore, dayDow, dayDate, todayKey, tz) {
if (chore.enabled === false) return false;
const scheduleMode = chore.schedule_mode || "specific_days";
const createdDate = chore.created_date || "";
if (scheduleMode === "one_shot") {
if (!createdDate) return false;
return createdDate === ymd(dayDate, tz);
}
if (createdDate) {
try {
const created = new Date(createdDate + "T00:00:00");
if (dayDate < new Date(created.toDateString())) return false;
} catch (e) { /* ignore */ }
}
if (scheduleMode === "specific_days") {
const dueDays = Array.isArray(chore.due_days) ? chore.due_days : [];
if (dueDays.length === 0) return true;
return dueDays.includes(dayDow);
}
if (scheduleMode === "recurring") {
const recurrence = chore.recurrence || "weekly";
const recurrenceDay = (chore.recurrence_day || "").toLowerCase();
const recurrenceStart = chore.recurrence_start || "";
// Fall back to created_date as the cadence anchor for interval
// recurrences with no explicit start, so they still project (ERR-2).
const anchorStr = recurrenceStart || createdDate;
if (recurrenceDay && (recurrence === "weekly" || recurrence === "every_2_weeks")) {
if (recurrenceDay !== dayDow) return false;
if (recurrence === "every_2_weeks" && recurrenceStart) {
try {
const anchor = new Date(recurrenceStart + "T00:00:00");
const diff = diffDays(dayDate, anchor);
if (diff < 0) return false;
if (Math.floor(diff / 7) % 2 !== 0) return false;
} catch (e) { /* ignore */ }
}
return true;
}
if (recurrence === "every_2_days" && anchorStr) {
try {
const anchor = new Date(anchorStr + "T00:00:00");
const diff = diffDays(dayDate, anchor);
if (diff < 0) return false;
return diff % 2 === 0;
} catch (e) { return false; }
}
const monthSteps = { monthly: 1, every_3_months: 3, every_6_months: 6 }[recurrence];
if (monthSteps && anchorStr) {
try {
const anchor = new Date(anchorStr + "T00:00:00");
if (dayDate < anchor) return false;
const monthsDiff =
(dayDate.getFullYear() - anchor.getFullYear()) * 12 +
(dayDate.getMonth() - anchor.getMonth());
if (monthsDiff % monthSteps !== 0) return false;
// Clamp the anchor day for shorter months (e.g. 31st -> Feb 28th)
const lastDay = new Date(dayDate.getFullYear(), dayDate.getMonth() + 1, 0).getDate();
return dayDate.getDate() === Math.min(anchor.getDate(), lastDay);
} catch (e) { return false; }
}
const windowDays = WINDOW_DAYS[recurrence] || 7;
if (windowDays === 7 || windowDays === 14) {
const todayDow = new Date(todayKey + "T00:00:00").getDay();
const todayIdx = todayDow === 0 ? 6 : todayDow - 1;
return dayDow === DAY_NAMES[todayIdx];
}
return false;
}
return false;
}
_isAssignedTo(chore, childId) {
const assignedTo = Array.isArray(chore.assigned_to) ? chore.assigned_to : [];
if (assignedTo.length === 0) return true;
return assignedTo.includes(childId);
}
_rotationRenderMode(chore, childId, dayKey, todayKey) {
const mode = chore.assignment_mode || "everyone";
if (mode === "everyone") return "active";
const current = chore.assignment_current_child_id || "";
if (dayKey === todayKey) {
return current === childId ? "active" : "hidden";
}
return "rotating";
}
render() {
if (!this.hass || !this.config) return html``;
const design = window.__taskmate_design
? window.__taskmate_design.apply(this, this.hass, this.config, this.config.entity)
: "classic";
if (design !== "classic") return this._renderDesigned(design);
const entity = this.hass.states[this.config.entity];
if (!entity) {
return html`