/**
* walkthrough-engine.js - Motor de Asistencia Interactiva y Spotlight para UdeCi (SDD-011)
* "Enseñar a pescar, no dar el pescado". Ilumina elementos en vivo, guía al usuario
* y aguarda a que sea él quien ejecute la acción.
*/
(function() {
'use strict';
const PENDING_STEP_KEY = 'edu_space_walkthrough_pending_step';
let currentStepData = null;
let activeHighlightEl = null;
let backdropEl = null;
let tooltipEl = null;
let mobileBarEl = null;
let redirectBannerEl = null;
function isMobile() {
return window.innerWidth < 768;
}
function removeElement(el) {
if (el && el.parentNode) {
el.parentNode.removeChild(el);
}
}
function clearSpotlight() {
if (activeHighlightEl) {
activeHighlightEl.classList.remove('walkthrough-highlight');
activeHighlightEl = null;
}
removeElement(backdropEl);
backdropEl = null;
removeElement(tooltipEl);
tooltipEl = null;
removeElement(mobileBarEl);
mobileBarEl = null;
removeElement(redirectBannerEl);
redirectBannerEl = null;
currentStepData = null;
}
function createBackdrop() {
if (backdropEl) return backdropEl;
backdropEl = document.createElement('div');
backdropEl.className = 'walkthrough-backdrop';
document.body.appendChild(backdropEl);
return backdropEl;
}
function showRedirectBanner(targetUrl, stepMessage) {
clearSpotlight();
redirectBannerEl = document.createElement('div');
redirectBannerEl.className = 'walkthrough-redirect-banner';
redirectBannerEl.innerHTML = `
Guía UdeCiDebemos ir a la pantalla correspondiente para continuar.
`;
document.body.appendChild(redirectBannerEl);
document.getElementById('walkthroughGoBtn').addEventListener('click', () => {
sessionStorage.setItem(PENDING_STEP_KEY, JSON.stringify(currentStepData));
window.location.href = targetUrl;
});
document.getElementById('walkthroughCancelRedirectBtn').addEventListener('click', () => {
clearSpotlight();
sessionStorage.removeItem(PENDING_STEP_KEY);
});
}
function positionTooltip(targetEl, tooltip) {
const rect = targetEl.getBoundingClientRect();
const tooltipWidth = 320;
const margin = 12;
let top = rect.bottom + window.scrollY + margin;
let left = rect.left + window.scrollX;
// Ajuste si se sale a la derecha
if (left + tooltipWidth > window.innerWidth - 20) {
left = window.innerWidth - tooltipWidth - 20;
}
if (left < 10) left = 10;
// Si se sale por abajo de la pantalla, posicionar arriba del elemento
if (rect.bottom + 180 > window.innerHeight) {
top = rect.top + window.scrollY - 160;
}
tooltip.style.top = `${Math.max(10, top)}px`;
tooltip.style.left = `${left}px`;
}
function renderTooltipDesktop(targetEl, step) {
removeElement(tooltipEl);
tooltipEl = document.createElement('div');
tooltipEl.className = 'walkthrough-tooltip';
tooltipEl.innerHTML = `
UdeCi · Paso ${step.step_number} de ${step.total_steps}