/** * AI Creative Studio - Main JavaScript * Vanilla JavaScript (No jQuery) * * @package AI_Creative_Studio */ (function() { 'use strict'; /** * External Links Handler */ function aicsInitExternalLinks() { const links = document.querySelectorAll('a[href^="http"]'); const currentDomain = window.location.hostname; links.forEach(function(link) { try { const linkDomain = new URL(link.href).hostname; if (linkDomain !== currentDomain) { link.setAttribute('target', '_blank'); link.setAttribute('rel', 'noopener noreferrer'); } } catch (e) { // Do nothing on URL parsing error } }); } /** * Smooth Scroll Initialization */ function aicsInitSmoothScroll() { const links = document.querySelectorAll('a[href^="#"]'); links.forEach(function(link) { link.addEventListener('click', function(e) { const href = this.getAttribute('href'); if (href === '#' || href === '#top') { e.preventDefault(); window.scrollTo({ top: 0, behavior: 'smooth' }); return; } const target = document.querySelector(href); if (target) { e.preventDefault(); target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); } /** * Mobile Menu Initialization */ function aicsInitMobileMenu() { const menuToggle = document.querySelector('.menu-toggle'); const navigation = document.querySelector('.main-navigation'); if (!menuToggle || !navigation) { return; } // Menu toggle click event menuToggle.addEventListener('click', function() { const isExpanded = this.getAttribute('aria-expanded') === 'true'; // Toggle aria-expanded attribute this.setAttribute('aria-expanded', !isExpanded); // Toggle navigation display navigation.classList.toggle('is-open'); // Control body scroll document.body.style.overflow = !isExpanded ? 'hidden' : ''; }); // Close on outside click document.addEventListener('click', function(e) { if (!navigation.contains(e.target) && !menuToggle.contains(e.target)) { if (navigation.classList.contains('is-open')) { menuToggle.setAttribute('aria-expanded', 'false'); navigation.classList.remove('is-open'); document.body.style.overflow = ''; } } }); // Close menu on ESC key document.addEventListener('keydown', function(e) { if (e.key === 'Escape' && navigation.classList.contains('is-open')) { menuToggle.setAttribute('aria-expanded', 'false'); navigation.classList.remove('is-open'); document.body.style.overflow = ''; menuToggle.focus(); } }); } /** * Execute after DOM loaded */ document.addEventListener('DOMContentLoaded', function() { // 外部リンクに target="_blank" を追加 aicsInitExternalLinks(); // Smooth scroll aicsInitSmoothScroll(); // Mobile menu aicsInitMobileMenu(); // Debug information output if (typeof aicsData !== 'undefined') { console.log('AI Creative Studio Theme Loaded - Version:', aicsData.theme.version); } }); })();