2025-11-15 19:08:26 -03:00
|
|
|
|
const sections = {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
drive: "drive/drive.html",
|
|
|
|
|
|
tasks: "tasks/tasks.html",
|
|
|
|
|
|
mail: "mail/mail.html",
|
|
|
|
|
|
chat: "chat/chat.html",
|
2025-11-15 19:08:26 -03:00
|
|
|
|
};
|
2025-11-17 10:16:01 -03:00
|
|
|
|
const sectionCache = {};
|
2025-11-15 10:16:09 -03:00
|
|
|
|
|
2025-11-17 12:11:13 -03:00
|
|
|
|
function getBasePath() {
|
|
|
|
|
|
// All static assets (HTML, CSS, JS) are served from the site root.
|
|
|
|
|
|
// Returning a leading slash ensures URLs like "/drive/drive.html" resolve correctly
|
2025-11-20 14:35:23 -03:00
|
|
|
|
return "/";
|
2025-11-17 12:11:13 -03:00
|
|
|
|
}
|
2025-11-17 12:16:53 -03:00
|
|
|
|
|
|
|
|
|
|
// Preload chat CSS to avoid flash on first load
|
|
|
|
|
|
function preloadChatCSS() {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const chatCssPath = getBasePath() + "chat/chat.css";
|
2025-11-17 12:16:53 -03:00
|
|
|
|
const existing = document.querySelector(`link[href="${chatCssPath}"]`);
|
|
|
|
|
|
if (!existing) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const link = document.createElement("link");
|
|
|
|
|
|
link.rel = "stylesheet";
|
2025-11-17 12:16:53 -03:00
|
|
|
|
link.href = chatCssPath;
|
|
|
|
|
|
document.head.appendChild(link);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 19:08:26 -03:00
|
|
|
|
async function loadSectionHTML(path) {
|
2025-11-17 12:11:13 -03:00
|
|
|
|
const fullPath = getBasePath() + path;
|
|
|
|
|
|
const response = await fetch(fullPath);
|
2025-11-20 14:35:23 -03:00
|
|
|
|
if (!response.ok) throw new Error("Failed to load section: " + fullPath);
|
2025-11-15 19:08:26 -03:00
|
|
|
|
return await response.text();
|
|
|
|
|
|
}
|
2025-11-15 10:16:09 -03:00
|
|
|
|
|
2025-11-15 19:08:26 -03:00
|
|
|
|
async function switchSection(section) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const mainContent = document.getElementById("main-content");
|
2025-11-17 10:16:01 -03:00
|
|
|
|
|
2025-11-15 19:08:26 -03:00
|
|
|
|
try {
|
2025-11-15 21:52:53 -03:00
|
|
|
|
const htmlPath = sections[section];
|
2025-11-20 14:35:23 -03:00
|
|
|
|
console.log("Loading section:", section, "from", htmlPath);
|
2025-11-17 12:11:13 -03:00
|
|
|
|
// Resolve CSS path relative to the base directory.
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const cssPath = getBasePath() + htmlPath.replace(".html", ".css");
|
2025-11-17 10:16:01 -03:00
|
|
|
|
|
2025-11-17 12:16:53 -03:00
|
|
|
|
// Preload chat CSS if the target is chat
|
2025-11-20 14:35:23 -03:00
|
|
|
|
if (section === "chat") {
|
2025-11-17 12:16:53 -03:00
|
|
|
|
preloadChatCSS();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 21:52:53 -03:00
|
|
|
|
// Remove any existing section CSS
|
2025-11-20 14:35:23 -03:00
|
|
|
|
document
|
|
|
|
|
|
.querySelectorAll("link[data-section-css]")
|
|
|
|
|
|
.forEach((link) => link.remove());
|
2025-11-17 10:16:01 -03:00
|
|
|
|
|
|
|
|
|
|
// Load CSS first (skip if already loaded)
|
|
|
|
|
|
let cssLink = document.querySelector(`link[href="${cssPath}"]`);
|
|
|
|
|
|
if (!cssLink) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
cssLink = document.createElement("link");
|
|
|
|
|
|
cssLink.rel = "stylesheet";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
cssLink.href = cssPath;
|
2025-11-20 14:35:23 -03:00
|
|
|
|
cssLink.setAttribute("data-section-css", "true");
|
2025-11-17 10:16:01 -03:00
|
|
|
|
document.head.appendChild(cssLink);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hide previously loaded sections and show the requested one
|
|
|
|
|
|
// Ensure a container exists for sections
|
2025-11-20 14:35:23 -03:00
|
|
|
|
let container = document.getElementById("section-container");
|
2025-11-17 10:16:01 -03:00
|
|
|
|
if (!container) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
container = document.createElement("div");
|
|
|
|
|
|
container.id = "section-container";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
mainContent.appendChild(container);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const targetDiv = document.getElementById(`section-${section}`);
|
|
|
|
|
|
|
|
|
|
|
|
if (targetDiv) {
|
|
|
|
|
|
// Section already loaded: hide others, show this one
|
2025-11-20 14:35:23 -03:00
|
|
|
|
container.querySelectorAll(".section").forEach((div) => {
|
|
|
|
|
|
div.style.display = "none";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
});
|
2025-11-20 14:35:23 -03:00
|
|
|
|
targetDiv.style.display = "block";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
} else {
|
|
|
|
|
|
// Show loading placeholder inside the container
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const loadingDiv = document.createElement("div");
|
|
|
|
|
|
loadingDiv.className = "loading";
|
|
|
|
|
|
loadingDiv.textContent = "Loading…";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
container.appendChild(loadingDiv);
|
|
|
|
|
|
|
|
|
|
|
|
// Load HTML
|
|
|
|
|
|
const html = await loadSectionHTML(htmlPath);
|
|
|
|
|
|
// Create wrapper for the new section
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const wrapper = document.createElement("div");
|
2025-11-17 10:16:01 -03:00
|
|
|
|
wrapper.id = `section-${section}`;
|
2025-11-20 14:35:23 -03:00
|
|
|
|
wrapper.className = "section";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
wrapper.innerHTML = html;
|
|
|
|
|
|
|
|
|
|
|
|
// Hide any existing sections
|
2025-11-20 14:35:23 -03:00
|
|
|
|
container.querySelectorAll(".section").forEach((div) => {
|
|
|
|
|
|
div.style.display = "none";
|
2025-11-17 10:16:01 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Remove loading placeholder
|
|
|
|
|
|
container.removeChild(loadingDiv);
|
|
|
|
|
|
|
|
|
|
|
|
// Add the new section to the container and cache it
|
|
|
|
|
|
container.appendChild(wrapper);
|
|
|
|
|
|
sectionCache[section] = wrapper;
|
|
|
|
|
|
|
2025-11-17 12:16:53 -03:00
|
|
|
|
// Ensure the new section is visible with a fast GSAP fade-in
|
|
|
|
|
|
gsap.fromTo(
|
|
|
|
|
|
wrapper,
|
|
|
|
|
|
{ opacity: 0 },
|
2025-11-20 14:35:23 -03:00
|
|
|
|
{ opacity: 1, duration: 0.15, ease: "power2.out" },
|
2025-11-17 12:16:53 -03:00
|
|
|
|
);
|
2025-11-17 10:16:01 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Then load JS after HTML is inserted (skip if already loaded)
|
2025-11-17 12:11:13 -03:00
|
|
|
|
// Resolve JS path relative to the base directory.
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const jsPath = getBasePath() + htmlPath.replace(".html", ".js");
|
2025-11-15 21:52:53 -03:00
|
|
|
|
const existingScript = document.querySelector(`script[src="${jsPath}"]`);
|
2025-11-20 14:35:23 -03:00
|
|
|
|
|
2025-11-15 21:52:53 -03:00
|
|
|
|
if (!existingScript) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
// Create script and wait for it to load before initializing Alpine
|
|
|
|
|
|
const script = document.createElement("script");
|
2025-11-15 21:52:53 -03:00
|
|
|
|
script.src = jsPath;
|
|
|
|
|
|
script.defer = true;
|
2025-11-20 14:35:23 -03:00
|
|
|
|
|
|
|
|
|
|
// Wait for script to load before initializing Alpine
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
|
script.onload = resolve;
|
|
|
|
|
|
script.onerror = reject;
|
|
|
|
|
|
document.body.appendChild(script);
|
|
|
|
|
|
});
|
2025-11-15 21:52:53 -03:00
|
|
|
|
}
|
2025-11-20 14:35:23 -03:00
|
|
|
|
|
|
|
|
|
|
window.history.pushState({}, "", `#${section}`);
|
2025-11-20 14:40:15 -03:00
|
|
|
|
|
|
|
|
|
|
// Start Alpine on first load, then just init the tree for new sections
|
|
|
|
|
|
if (typeof window.startAlpine === "function") {
|
|
|
|
|
|
window.startAlpine();
|
|
|
|
|
|
delete window.startAlpine;
|
|
|
|
|
|
} else if (window.Alpine) {
|
|
|
|
|
|
window.Alpine.initTree(mainContent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const inputEl = document.getElementById("messageInput");
|
2025-11-17 12:16:53 -03:00
|
|
|
|
if (inputEl) {
|
|
|
|
|
|
inputEl.focus();
|
|
|
|
|
|
}
|
2025-11-15 19:08:26 -03:00
|
|
|
|
} catch (err) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
console.error("Error loading section:", err);
|
2025-11-15 19:08:26 -03:00
|
|
|
|
mainContent.innerHTML = `<div class="error">Failed to load ${section} section</div>`;
|
2025-11-15 10:16:09 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 19:08:26 -03:00
|
|
|
|
// Handle initial load based on URL hash
|
2025-11-17 12:11:13 -03:00
|
|
|
|
function getInitialSection() {
|
|
|
|
|
|
// 1️⃣ Prefer hash fragment (e.g., #chat)
|
|
|
|
|
|
let section = window.location.hash.substring(1);
|
|
|
|
|
|
// 2️⃣ Fallback to pathname segments (e.g., /chat)
|
|
|
|
|
|
if (!section) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const parts = window.location.pathname.split("/").filter((p) => p);
|
2025-11-17 12:11:13 -03:00
|
|
|
|
const last = parts[parts.length - 1];
|
2025-11-20 14:35:23 -03:00
|
|
|
|
if (["drive", "tasks", "mail", "chat"].includes(last)) {
|
2025-11-17 12:11:13 -03:00
|
|
|
|
section = last;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 3️⃣ As a last resort, inspect the full URL for known sections
|
|
|
|
|
|
if (!section) {
|
2025-11-20 14:35:23 -03:00
|
|
|
|
const match = window.location.href.match(
|
|
|
|
|
|
/\/(drive|tasks|mail|chat)(?:\.html)?(?:[?#]|$)/i,
|
|
|
|
|
|
);
|
2025-11-17 12:11:13 -03:00
|
|
|
|
if (match) {
|
|
|
|
|
|
section = match[1].toLowerCase();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-17 12:16:53 -03:00
|
|
|
|
// Default to chat if nothing matches
|
2025-11-20 14:35:23 -03:00
|
|
|
|
return section || "chat";
|
2025-11-17 12:11:13 -03:00
|
|
|
|
}
|
2025-11-20 14:35:23 -03:00
|
|
|
|
window.addEventListener("DOMContentLoaded", () => {
|
2025-11-17 12:11:13 -03:00
|
|
|
|
switchSection(getInitialSection());
|
2025-11-15 19:08:26 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Handle browser back/forward navigation
|
2025-11-20 14:35:23 -03:00
|
|
|
|
window.addEventListener("popstate", () => {
|
2025-11-17 12:11:13 -03:00
|
|
|
|
switchSection(getInitialSection());
|
2025-11-15 19:08:26 -03:00
|
|
|
|
});
|