refactoring
This commit is contained in:
116
app/ui-home-calendar.js
Normal file
116
app/ui-home-calendar.js
Normal file
@@ -0,0 +1,116 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
let config = {};
|
||||
let lastNowSkyGeoKey = "";
|
||||
let lastNowSkySourceUrl = "";
|
||||
|
||||
function getNowSkyLayerEl() {
|
||||
return config.nowSkyLayerEl || null;
|
||||
}
|
||||
|
||||
function getNowPanelEl() {
|
||||
return config.nowPanelEl || null;
|
||||
}
|
||||
|
||||
function getCurrentGeo() {
|
||||
return config.getCurrentGeo?.() || null;
|
||||
}
|
||||
|
||||
function normalizeGeoForSky(geo) {
|
||||
const latitude = Number(geo?.latitude);
|
||||
const longitude = Number(geo?.longitude);
|
||||
|
||||
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
latitude: Number(latitude.toFixed(4)),
|
||||
longitude: Number(longitude.toFixed(4))
|
||||
};
|
||||
}
|
||||
|
||||
function buildStellariumObserverUrl(geo) {
|
||||
const normalizedGeo = normalizeGeoForSky(geo);
|
||||
if (!normalizedGeo) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const stellariumUrl = new URL("https://stellarium-web.org/");
|
||||
stellariumUrl.searchParams.set("lat", String(normalizedGeo.latitude));
|
||||
stellariumUrl.searchParams.set("lng", String(normalizedGeo.longitude));
|
||||
stellariumUrl.searchParams.set("elev", "0");
|
||||
stellariumUrl.searchParams.set("date", new Date().toISOString());
|
||||
stellariumUrl.searchParams.set("az", "0");
|
||||
stellariumUrl.searchParams.set("alt", "90");
|
||||
stellariumUrl.searchParams.set("fov", "180");
|
||||
|
||||
return stellariumUrl.toString();
|
||||
}
|
||||
|
||||
function syncNowSkyBackground(geo, force = false) {
|
||||
const nowSkyLayerEl = getNowSkyLayerEl();
|
||||
if (!nowSkyLayerEl || !geo) {
|
||||
return;
|
||||
}
|
||||
|
||||
const normalizedGeo = normalizeGeoForSky(geo);
|
||||
if (!normalizedGeo) {
|
||||
return;
|
||||
}
|
||||
|
||||
const geoKey = `${normalizedGeo.latitude.toFixed(4)},${normalizedGeo.longitude.toFixed(4)}`;
|
||||
const stellariumUrl = buildStellariumObserverUrl(normalizedGeo);
|
||||
if (!stellariumUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!force && geoKey === lastNowSkyGeoKey && stellariumUrl === lastNowSkySourceUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (stellariumUrl === lastNowSkySourceUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
nowSkyLayerEl.src = stellariumUrl;
|
||||
lastNowSkyGeoKey = geoKey;
|
||||
lastNowSkySourceUrl = stellariumUrl;
|
||||
}
|
||||
|
||||
function syncNowPanelTheme(referenceDate = new Date()) {
|
||||
const nowPanelEl = getNowPanelEl();
|
||||
if (!nowPanelEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentGeo = getCurrentGeo();
|
||||
if (!currentGeo || !window.SunCalc) {
|
||||
nowPanelEl.classList.remove("is-day");
|
||||
nowPanelEl.classList.add("is-night");
|
||||
return;
|
||||
}
|
||||
|
||||
const sunPosition = window.SunCalc.getPosition(referenceDate, currentGeo.latitude, currentGeo.longitude);
|
||||
const sunAltitudeDeg = (sunPosition.altitude * 180) / Math.PI;
|
||||
const isDaytime = sunAltitudeDeg >= -4;
|
||||
|
||||
nowPanelEl.classList.toggle("is-day", isDaytime);
|
||||
nowPanelEl.classList.toggle("is-night", !isDaytime);
|
||||
}
|
||||
|
||||
function init(nextConfig = {}) {
|
||||
config = {
|
||||
...config,
|
||||
...nextConfig
|
||||
};
|
||||
}
|
||||
|
||||
window.TarotHomeUi = {
|
||||
...(window.TarotHomeUi || {}),
|
||||
init,
|
||||
syncNowSkyBackground,
|
||||
syncNowPanelTheme
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user