moved to API
This commit is contained in:
171
app/ui-now.js
171
app/ui-now.js
@@ -1,6 +1,8 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
const dataService = window.TarotDataService || {};
|
||||
|
||||
const {
|
||||
DAY_IN_MS,
|
||||
getDateKey,
|
||||
@@ -25,62 +27,65 @@
|
||||
let moonCountdownCache = null;
|
||||
let decanCountdownCache = null;
|
||||
|
||||
function updateNowPanel(referenceData, geo, elements, timeFormat = "minutes") {
|
||||
if (!referenceData || !geo || !elements) {
|
||||
return { dayKey: getDateKey(new Date()), skyRefreshKey: "" };
|
||||
function renderNowStatsFromSnapshot(elements, stats) {
|
||||
if (elements.nowStatsPlanetsEl) {
|
||||
elements.nowStatsPlanetsEl.replaceChildren();
|
||||
|
||||
const planetPositions = Array.isArray(stats?.planetPositions) ? stats.planetPositions : [];
|
||||
if (!planetPositions.length) {
|
||||
elements.nowStatsPlanetsEl.textContent = "--";
|
||||
} else {
|
||||
planetPositions.forEach((position) => {
|
||||
const item = document.createElement("div");
|
||||
item.className = "now-stats-planet";
|
||||
item.textContent = String(position?.label || "").trim() || "--";
|
||||
elements.nowStatsPlanetsEl.appendChild(item);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const dayKey = getDateKey(now);
|
||||
if (elements.nowStatsSabianEl) {
|
||||
const sunSabianSymbol = stats?.sunSabianSymbol || null;
|
||||
const moonSabianSymbol = stats?.moonSabianSymbol || null;
|
||||
const sunLine = sunSabianSymbol?.phrase
|
||||
? `Sun Sabian ${sunSabianSymbol.absoluteDegree}: ${sunSabianSymbol.phrase}`
|
||||
: "Sun Sabian: --";
|
||||
const moonLine = moonSabianSymbol?.phrase
|
||||
? `Moon Sabian ${moonSabianSymbol.absoluteDegree}: ${moonSabianSymbol.phrase}`
|
||||
: "Moon Sabian: --";
|
||||
elements.nowStatsSabianEl.textContent = `${sunLine}\n${moonLine}`;
|
||||
}
|
||||
}
|
||||
|
||||
const todayHours = calcPlanetaryHoursForDayAndLocation(now, geo);
|
||||
const yesterday = new Date(now.getTime() - DAY_IN_MS);
|
||||
const yesterdayHours = calcPlanetaryHoursForDayAndLocation(yesterday, geo);
|
||||
const tomorrow = new Date(now.getTime() + DAY_IN_MS);
|
||||
const tomorrowHours = calcPlanetaryHoursForDayAndLocation(tomorrow, geo);
|
||||
const allHours = [...yesterdayHours, ...todayHours, ...tomorrowHours].sort(
|
||||
(a, b) => a.start.getTime() - b.start.getTime()
|
||||
);
|
||||
const currentHour = allHours.find((entry) => now >= entry.start && now < entry.end);
|
||||
const currentHourSkyKey = currentHour
|
||||
? `${currentHour.planetId}-${currentHour.start.toISOString()}`
|
||||
: "no-hour";
|
||||
function applyNowSnapshot(elements, snapshot, timeFormat) {
|
||||
const timestamp = snapshot?.timestamp ? new Date(snapshot.timestamp) : new Date();
|
||||
const dayKey = String(snapshot?.dayKey || getDateKey(timestamp));
|
||||
const currentHour = snapshot?.currentHour || null;
|
||||
|
||||
if (currentHour) {
|
||||
const planet = referenceData.planets[currentHour.planetId];
|
||||
elements.nowHourEl.textContent = planet
|
||||
? `${planet.symbol} ${planet.name}`
|
||||
: currentHour.planetId;
|
||||
if (currentHour?.planet) {
|
||||
elements.nowHourEl.textContent = `${currentHour.planet.symbol} ${currentHour.planet.name}`;
|
||||
if (elements.nowHourTarotEl) {
|
||||
const hourCardName = planet?.tarot?.majorArcana || "";
|
||||
const hourTrumpNumber = planet?.tarot?.number;
|
||||
const hourCardName = currentHour.planet?.tarot?.majorArcana || "";
|
||||
const hourTrumpNumber = currentHour.planet?.tarot?.number;
|
||||
elements.nowHourTarotEl.textContent = hourCardName
|
||||
? nowUiHelpers.getDisplayTarotName(hourCardName, hourTrumpNumber)
|
||||
: "--";
|
||||
}
|
||||
|
||||
const msLeft = Math.max(0, currentHour.end.getTime() - now.getTime());
|
||||
elements.nowCountdownEl.textContent = nowUiHelpers.formatCountdown(msLeft, timeFormat);
|
||||
elements.nowCountdownEl.textContent = nowUiHelpers.formatCountdown(currentHour.msRemaining, timeFormat);
|
||||
|
||||
if (elements.nowHourNextEl) {
|
||||
const nextHour = allHours.find(
|
||||
(entry) => entry.start.getTime() >= currentHour.end.getTime() - 1000
|
||||
);
|
||||
if (nextHour) {
|
||||
const nextPlanet = referenceData.planets[nextHour.planetId];
|
||||
elements.nowHourNextEl.textContent = nextPlanet
|
||||
? `> ${nextPlanet.name}`
|
||||
: `> ${nextHour.planetId}`;
|
||||
} else {
|
||||
elements.nowHourNextEl.textContent = "> --";
|
||||
}
|
||||
const nextPlanet = currentHour.nextHourPlanet;
|
||||
elements.nowHourNextEl.textContent = nextPlanet
|
||||
? `> ${nextPlanet.name}`
|
||||
: "> --";
|
||||
}
|
||||
|
||||
nowUiHelpers.setNowCardImage(
|
||||
elements.nowHourCardEl,
|
||||
planet?.tarot?.majorArcana,
|
||||
currentHour.planet?.tarot?.majorArcana,
|
||||
"Current planetary hour card",
|
||||
planet?.tarot?.number
|
||||
currentHour.planet?.tarot?.number
|
||||
);
|
||||
} else {
|
||||
elements.nowHourEl.textContent = "--";
|
||||
@@ -94,28 +99,27 @@
|
||||
nowUiHelpers.setNowCardImage(elements.nowHourCardEl, null, "Current planetary hour card");
|
||||
}
|
||||
|
||||
const moonIllum = window.SunCalc.getMoonIllumination(now);
|
||||
const moonPhase = getMoonPhaseName(moonIllum.phase);
|
||||
const moonTarot = referenceData.planets.luna?.tarot?.majorArcana || "The High Priestess";
|
||||
elements.nowMoonEl.textContent = `${moonPhase} (${Math.round(moonIllum.fraction * 100)}%)`;
|
||||
elements.nowMoonTarotEl.textContent = nowUiHelpers.getDisplayTarotName(moonTarot, referenceData.planets.luna?.tarot?.number);
|
||||
const moon = snapshot?.moon || null;
|
||||
const illuminationFraction = Number(moon?.illuminationFraction || 0);
|
||||
const moonTarot = moon?.tarot?.majorArcana || "The High Priestess";
|
||||
elements.nowMoonEl.textContent = moon
|
||||
? `${moon.phase} (${Math.round(illuminationFraction * 100)}%)`
|
||||
: "--";
|
||||
elements.nowMoonTarotEl.textContent = moon
|
||||
? nowUiHelpers.getDisplayTarotName(moonTarot, moon?.tarot?.number)
|
||||
: "--";
|
||||
nowUiHelpers.setNowCardImage(
|
||||
elements.nowMoonCardEl,
|
||||
moonTarot,
|
||||
moon?.tarot?.majorArcana,
|
||||
"Current moon phase card",
|
||||
referenceData.planets.luna?.tarot?.number
|
||||
moon?.tarot?.number
|
||||
);
|
||||
|
||||
if (!moonCountdownCache || moonCountdownCache.fromPhase !== moonPhase || now >= moonCountdownCache.changeAt) {
|
||||
moonCountdownCache = nowUiHelpers.findNextMoonPhaseTransition(now);
|
||||
}
|
||||
|
||||
if (elements.nowMoonCountdownEl) {
|
||||
if (moonCountdownCache?.changeAt) {
|
||||
const remaining = moonCountdownCache.changeAt.getTime() - now.getTime();
|
||||
elements.nowMoonCountdownEl.textContent = nowUiHelpers.formatCountdown(remaining, timeFormat);
|
||||
if (moon?.countdown) {
|
||||
elements.nowMoonCountdownEl.textContent = nowUiHelpers.formatCountdown(moon.countdown.msRemaining, timeFormat);
|
||||
if (elements.nowMoonNextEl) {
|
||||
elements.nowMoonNextEl.textContent = `> ${moonCountdownCache.nextPhase}`;
|
||||
elements.nowMoonNextEl.textContent = `> ${moon.countdown.nextPhase}`;
|
||||
}
|
||||
} else {
|
||||
elements.nowMoonCountdownEl.textContent = "--";
|
||||
@@ -125,45 +129,39 @@
|
||||
}
|
||||
}
|
||||
|
||||
const sunInfo = getDecanForDate(now, referenceData.signs, referenceData.decansBySign);
|
||||
const decanSkyKey = sunInfo?.sign
|
||||
? `${sunInfo.sign.id}-${sunInfo.decan?.index || 1}`
|
||||
: "no-decan";
|
||||
if (sunInfo?.sign) {
|
||||
const signStartDate = nowUiHelpers.getSignStartDate(now, sunInfo.sign);
|
||||
const daysSinceSignStart = (now.getTime() - signStartDate.getTime()) / DAY_IN_MS;
|
||||
const signDegree = Math.min(29.9, Math.max(0, daysSinceSignStart));
|
||||
const signMajorName = nowUiHelpers.getDisplayTarotName(sunInfo.sign.tarot.majorArcana, sunInfo.sign.tarot.trumpNumber);
|
||||
elements.nowDecanEl.textContent = `${sunInfo.sign.symbol} ${sunInfo.sign.name} · ${signMajorName} (${signDegree.toFixed(1)}°)`;
|
||||
const decanInfo = snapshot?.decan || null;
|
||||
if (decanInfo?.sign) {
|
||||
const signMajorName = nowUiHelpers.getDisplayTarotName(
|
||||
decanInfo.sign?.tarot?.majorArcana,
|
||||
decanInfo.sign?.tarot?.number
|
||||
);
|
||||
const signDegree = Number.isFinite(Number(decanInfo.signDegree))
|
||||
? Number(decanInfo.signDegree).toFixed(1)
|
||||
: "0.0";
|
||||
|
||||
const currentDecanKey = `${sunInfo.sign.id}-${sunInfo.decan?.index || 1}`;
|
||||
if (!decanCountdownCache || decanCountdownCache.key !== currentDecanKey || now >= decanCountdownCache.changeAt) {
|
||||
decanCountdownCache = nowUiHelpers.findNextDecanTransition(now, referenceData.signs, referenceData.decansBySign);
|
||||
}
|
||||
elements.nowDecanEl.textContent = `${decanInfo.sign.symbol} ${decanInfo.sign.name} · ${signMajorName} (${signDegree}°)`;
|
||||
|
||||
if (sunInfo.decan) {
|
||||
const decanCardName = sunInfo.decan.tarotMinorArcana;
|
||||
elements.nowDecanTarotEl.textContent = nowUiHelpers.getDisplayTarotName(decanCardName);
|
||||
nowUiHelpers.setNowCardImage(elements.nowDecanCardEl, sunInfo.decan.tarotMinorArcana, "Current decan card");
|
||||
if (decanInfo.decan?.tarotMinorArcana) {
|
||||
elements.nowDecanTarotEl.textContent = nowUiHelpers.getDisplayTarotName(decanInfo.decan.tarotMinorArcana);
|
||||
nowUiHelpers.setNowCardImage(elements.nowDecanCardEl, decanInfo.decan.tarotMinorArcana, "Current decan card");
|
||||
} else {
|
||||
const signTarotName = sunInfo.sign.tarot?.majorArcana || "--";
|
||||
const signTarotName = decanInfo.sign?.tarot?.majorArcana || "--";
|
||||
elements.nowDecanTarotEl.textContent = signTarotName === "--"
|
||||
? "--"
|
||||
: nowUiHelpers.getDisplayTarotName(signTarotName, sunInfo.sign.tarot?.trumpNumber);
|
||||
: nowUiHelpers.getDisplayTarotName(signTarotName, decanInfo.sign?.tarot?.number);
|
||||
nowUiHelpers.setNowCardImage(
|
||||
elements.nowDecanCardEl,
|
||||
sunInfo.sign.tarot?.majorArcana,
|
||||
decanInfo.sign?.tarot?.majorArcana,
|
||||
"Current decan card",
|
||||
sunInfo.sign.tarot?.trumpNumber
|
||||
decanInfo.sign?.tarot?.number
|
||||
);
|
||||
}
|
||||
|
||||
if (elements.nowDecanCountdownEl) {
|
||||
if (decanCountdownCache?.changeAt) {
|
||||
const remaining = decanCountdownCache.changeAt.getTime() - now.getTime();
|
||||
elements.nowDecanCountdownEl.textContent = nowUiHelpers.formatCountdown(remaining, timeFormat);
|
||||
if (decanInfo.countdown) {
|
||||
elements.nowDecanCountdownEl.textContent = nowUiHelpers.formatCountdown(decanInfo.countdown.msRemaining, timeFormat);
|
||||
if (elements.nowDecanNextEl) {
|
||||
elements.nowDecanNextEl.textContent = `> ${nowUiHelpers.getDisplayTarotName(decanCountdownCache.nextLabel)}`;
|
||||
elements.nowDecanNextEl.textContent = `> ${nowUiHelpers.getDisplayTarotName(decanInfo.countdown.nextLabel)}`;
|
||||
}
|
||||
} else {
|
||||
elements.nowDecanCountdownEl.textContent = "--";
|
||||
@@ -184,14 +182,23 @@
|
||||
}
|
||||
}
|
||||
|
||||
nowUiHelpers.updateNowStats(referenceData, elements, now);
|
||||
renderNowStatsFromSnapshot(elements, snapshot?.stats || {});
|
||||
|
||||
return {
|
||||
dayKey,
|
||||
skyRefreshKey: `${currentHourSkyKey}|${decanSkyKey}|${moonPhase}`
|
||||
skyRefreshKey: String(snapshot?.skyRefreshKey || "")
|
||||
};
|
||||
}
|
||||
|
||||
async function updateNowPanel(referenceData, geo, elements, timeFormat = "minutes") {
|
||||
if (!referenceData || !geo || !elements) {
|
||||
return { dayKey: getDateKey(new Date()), skyRefreshKey: "" };
|
||||
}
|
||||
|
||||
const snapshot = await dataService.fetchNowSnapshot?.(geo, new Date());
|
||||
return applyNowSnapshot(elements, snapshot || {}, timeFormat);
|
||||
}
|
||||
|
||||
window.TarotNowUi = {
|
||||
updateNowPanel
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user