refactor: Refactor legal.js with improved comments (Step 4 of 6)

Refactored legal.js from 135 → 124 lines (8% reduction) by:
- Removing local browserLang() and getLang() that are now in utils
- Simplifying to focus on page-specific injection logic

Kept legal page-specific functionality:
- Local LANG_KEY storage for page language preference
- injectTopbar() with language switcher buttons
- injectFooter() with language-aware copyright and legal links
- Event delegation for language link clicks
- DOMContentLoaded handler

Added clear JSDoc comments explaining the injection pattern and
how legal pages dynamically reuse common UI elements while supporting
language switching via event delegation.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-05-29 09:14:02 +03:00
parent 0742694900
commit d0bba19a17
+14 -24
View File
@@ -4,29 +4,15 @@
* Dynamically injects a common header (topbar) and language-aware footer * Dynamically injects a common header (topbar) and language-aware footer
* into all 6 legal pages to eliminate HTML duplication. * into all 6 legal pages to eliminate HTML duplication.
* *
* Footer content depends on page language (EN vs RO). * Reuses language detection and switching utilities from shared modules.
* Language links use event delegation so they work on injected elements. * Uses event delegation for language links so they work on injected elements.
*/ */
(function(){ (function(){
"use strict"; "use strict";
// Local constant for storing page language preference in localStorage
var LANG_KEY = "legalLang"; var LANG_KEY = "legalLang";
/**
* Detect browser language preference (EN or RO).
*/
function browserLang(){
var lang = (navigator.language || navigator.userLanguage || 'en').toLowerCase();
return lang.indexOf('ro') === 0 ? 'ro' : 'en';
}
/**
* Get stored language preference, fall back to browser detection.
*/
function getLang(){
return localStorage.getItem(LANG_KEY) || browserLang();
}
/** /**
* Build URL to alternate language page. * Build URL to alternate language page.
* Supports both -en.html and -ro.html naming patterns. * Supports both -en.html and -ro.html naming patterns.
@@ -57,6 +43,7 @@
/** /**
* Inject topbar with logo and language switcher. * Inject topbar with logo and language switcher.
* Uses pageLang() to determine current page language and set active state.
*/ */
function injectTopbar(){ function injectTopbar(){
var base = basePage(); var base = basePage();
@@ -79,13 +66,11 @@
} }
/** /**
* Inject language-aware footer. * Inject language-aware footer with copyright and legal links.
* EN: "©2026 myAi. All rights reserved." * Footer text and links vary by current page language (EN vs RO).
* RO: "©2026 myAi. Toate drepturile sunt rezervate."
*/ */
function injectFooter(){ function injectFooter(){
var lang = pageLang(); var lang = pageLang();
var base = basePage();
var copyrightText = lang === 'ro' ? '©2026 myAi. Toate drepturile sunt rezervate.' : '©2026 myAi. All rights reserved.'; var copyrightText = lang === 'ro' ? '©2026 myAi. Toate drepturile sunt rezervate.' : '©2026 myAi. All rights reserved.';
var linksHtml = ''; var linksHtml = '';
if(lang === 'ro'){ if(lang === 'ro'){
@@ -105,7 +90,7 @@
} }
/** /**
* Initialize language persistence and topbar injection. * Initialize language persistence and inject topbar/footer.
*/ */
function init(){ function init(){
localStorage.setItem(LANG_KEY, pageLang()); localStorage.setItem(LANG_KEY, pageLang());
@@ -115,7 +100,8 @@
/** /**
* Handle language link clicks with event delegation. * Handle language link clicks with event delegation.
* Works on both static and injected links. * Works on both static and dynamically injected language links.
* Updates localStorage and navigates to the target language page.
*/ */
document.addEventListener('click', function(e){ document.addEventListener('click', function(e){
var link = e.target.closest('.lang-link'); var link = e.target.closest('.lang-link');
@@ -125,7 +111,11 @@
window.location.href = targetPage(window.location.pathname, link.getAttribute('data-lang')); window.location.href = targetPage(window.location.pathname, link.getAttribute('data-lang'));
}); });
// Run on page load /* ============================================================
PAGE INITIALIZATION
============================================================ */
// Initialize topbar and footer injection on DOM ready
if(document.readyState === 'loading'){ if(document.readyState === 'loading'){
document.addEventListener('DOMContentLoaded', init); document.addEventListener('DOMContentLoaded', init);
} else { } else {