All files features.js

91.85% Statements 124/135
75.86% Branches 44/58
83.33% Functions 20/24
92.42% Lines 122/132

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266              11x 11x   11x 3x     8x 9x 9x   9x 1x 1x     8x   8x   3x 7x 7x 7x         2x 2x   2x 2x 2x         3x     3x     3x           5x   5x     1x                   11x 11x 11x 11x   11x 3x     8x 8x 8x   1x       8x 2x 2x           8x 3x 3x       8x 4x 1x     3x 4x 2x     4x 4x 4x 4x 1x 1x             4x     8x 4x   4x 4x   4x 1x 1x 1x 1x       4x 1x 1x 1x           8x   8x 3x 5x 1x   4x                 9x 9x   6x 7x 7x 7x 7x   7x   6x 6x 6x 6x 6x 6x     6x       8x 1x 1x     7x 7x 7x   7x 7x   7x       6x                   6x 6x 3x     3x     3x 3x   3x 3x 3x     3x 3x     3x 3x 3x 3x       6x           6x 1x 1x 1x 1x       6x                
/* FEATURES.JS - Slideshows, Analytics, Cursor Follower =================================
   Copyright 2025, Mark Forscher */
/* ======================================================================================= */
 
export class Features {
  // Initialize slideshows (moved from inline script for CSP compliance)
  initSlideshows() {
    try {
      const slideshows = document.querySelectorAll(".content-slides");
 
      if (!slideshows || slideshows.length === 0) {
        return; // No slideshows on this page
      }
 
      slideshows.forEach((slideshow) => {
        try {
          const slides = slideshow.querySelectorAll("img, a > img");
 
          if (!slides || slides.length === 0) {
            console.warn('Slideshow container found but no images:', slideshow);
            return;
          }
 
          let currentIndex = 0;
 
          if (slides.length > 1) {
            // Initialize slide styles
            slides.forEach((slide, index) => {
              slide.style.opacity = index === 0 ? "1" : "0";
              slide.style.position = "absolute";
              slide.style.transition = "opacity 1s ease-in-out";
            });
 
            // Slide transition function
            function showNextSlide() {
              Eif (slides[currentIndex]) {
                slides[currentIndex].style.opacity = "0";
              }
              currentIndex = (currentIndex + 1) % slides.length;
              Eif (slides[currentIndex]) {
                slides[currentIndex].style.opacity = "1";
              }
            }
 
            // Start slideshow with interval tracking for cleanup
            const intervalId = setInterval(showNextSlide, 4000);
 
            // Store interval ID on element for potential cleanup
            slideshow.dataset.slideshowInterval = intervalId;
 
            // Optional: Add cleanup on page unload to prevent memory leaks
            window.addEventListener('beforeunload', () => {
              if (slideshow.dataset.slideshowInterval) {
                clearInterval(parseInt(slideshow.dataset.slideshowInterval));
              }
            }, { once: true });
 
          } else Eif (slides.length === 1) {
            // Single image - just ensure it's visible
            slides[0].style.opacity = "1";
          }
        } catch (error) {
          console.error('Slideshow initialization failed for element:', slideshow, error);
        }
      });
    } catch (error) {
      console.error('Slideshow initialization failed:', error);
    }
  }
 
  // Initialize Google Analytics consent management (moved from inline script for CSP compliance)
  initAnalyticsConsent() {
    try {
      const CONSENT_KEY = 'ua-ga-consent';
      const trackingId = 'G-5HHFZ48NKC';
      const banner = document.getElementById('ua-consent-banner');
 
      if (!banner) {
        return; // Analytics not enabled on this page
      }
 
      const getStoredConsent = () => {
        try {
          return window.localStorage.getItem(CONSENT_KEY);
        } catch (error) {
          return null;
        }
      };
 
      const storeConsent = (value) => {
        try {
          window.localStorage.setItem(CONSENT_KEY, value);
        } catch (error) {
          // Ignore storage errors (private browsing, etc.)
        }
      };
 
      const hideBanner = () => {
        Eif (banner) {
          banner.setAttribute('hidden', 'hidden');
        }
      };
 
      const loadAnalytics = () => {
        if (window.gtag) {
          return; // Already loaded
        }
 
        window.dataLayer = window.dataLayer || [];
        window.gtag = function gtag() {
          window.dataLayer.push(arguments);
        };
 
        const script = document.createElement('script');
        script.async = true;
        script.src = `https://www.googletagmanager.com/gtag/js?id=${trackingId}`;
        script.onload = () => {
          window.gtag('js', new Date());
          window.gtag('config', trackingId, {
            anonymize_ip: true,
            allow_google_signals: false,
            allow_ad_personalization_signals: false
          });
        };
 
        document.head.appendChild(script);
      };
 
      const showBanner = () => {
        banner.removeAttribute('hidden');
 
        const acceptButton = banner.querySelector('[data-consent="accept"]');
        const rejectButton = banner.querySelector('[data-consent="reject"]');
 
        if (acceptButton) {
          acceptButton.addEventListener('click', () => {
            storeConsent('accepted');
            hideBanner();
            loadAnalytics();
          }, { once: true });
        }
 
        if (rejectButton) {
          rejectButton.addEventListener('click', () => {
            storeConsent('rejected');
            hideBanner();
          }, { once: true });
        }
      };
 
      // Check consent status and act accordingly
      const consent = getStoredConsent();
 
      if (consent === 'accepted') {
        loadAnalytics();
      } else if (consent === 'rejected') {
        hideBanner();
      } else {
        showBanner();
      }
    } catch (error) {
      console.error('Analytics consent initialization failed:', error);
    }
  }
 
  // Initialize projects list cursor follower
  initProjectsCursorFollower() {
    const wrappers = document.querySelectorAll('[data-follower-wrap]');
    if (!wrappers || wrappers.length === 0) return;
 
    wrappers.forEach(wrap => {
      const collection = wrap.querySelector('[data-follower-collection]');
      const items = wrap.querySelectorAll('[data-follower-item]');
      const follower = wrap.querySelector('[data-follower-cursor]');
      const followerInner = wrap.querySelector('[data-follower-cursor-inner]');
 
      if (!collection || !items || !follower || !followerInner) return;
 
      let currentX = 0;
      let currentY = 0;
      let targetX = 0;
      let targetY = 0;
      let animationFrameId = null;
      let isHovering = false;
 
      // Initialize follower position
      follower.style.transform = 'translate(-50%, -50%)';
 
      // Smooth animation loop for cursor following
      function animateFollower() {
        if (!isHovering) {
          animationFrameId = null;
          return;
        }
 
        const easingFactor = 0.15;
        currentX += (targetX - currentX) * easingFactor;
        currentY += (targetY - currentY) * easingFactor;
 
        follower.style.left = `${currentX}px`;
        follower.style.top = `${currentY}px`;
 
        animationFrameId = requestAnimationFrame(animateFollower);
      }
 
      // Move follower on mousemove
      window.addEventListener('mousemove', e => {
        targetX = e.clientX;
        targetY = e.clientY;
 
        if (isHovering && !animationFrameId) {
          animateFollower();
        }
      });
 
      // Handle each project item
      items.forEach((item) => {
        item.addEventListener('mouseenter', () => {
          isHovering = true;
 
          // Clear any existing visuals (using replaceChildren for security)
          followerInner.replaceChildren();
 
          // Clone and insert current project's visual
          const visual = item.querySelector('[data-follower-visual]');
          Iif (!visual) return;
 
          const clone = visual.cloneNode(true);
          clone.style.display = 'block';
          followerInner.appendChild(clone);
 
          // Show follower
          follower.style.opacity = '1';
          follower.style.transform = 'translate(-50%, -50%) scale(1)';
 
          // Start cursor animation
          Eif (!animationFrameId) {
            currentX = targetX;
            currentY = targetY;
            animateFollower();
          }
        });
 
        item.addEventListener('mouseleave', () => {
          // Don't immediately hide - let collection mouseleave handle it
        });
      });
 
      // Clear visuals when leaving collection
      collection.addEventListener('mouseleave', () => {
        followerInner.replaceChildren();
        follower.style.opacity = '0';
        follower.style.transform = 'translate(-50%, -50%) scale(0.8)';
        isHovering = false;
      });
 
      // Cleanup on page unload
      window.addEventListener('beforeunload', () => {
        if (animationFrameId) {
          cancelAnimationFrame(animationFrameId);
        }
      }, { once: true });
    });
  }
}