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
* into all 6 legal pages to eliminate HTML duplication.
*
* Footer content depends on page language (EN vs RO).
* Language links use event delegation so they work on injected elements.
* Reuses language detection and switching utilities from shared modules.
* Uses event delegation for language links so they work on injected elements.
*/
(function(){
"use strict";
// Local constant for storing page language preference in localStorage
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.
* Supports both -en.html and -ro.html naming patterns.
@@ -57,6 +43,7 @@
/**
* Inject topbar with logo and language switcher.
* Uses pageLang() to determine current page language and set active state.
*/
function injectTopbar(){
var base = basePage();
@@ -79,13 +66,11 @@
}
/**
* Inject language-aware footer.
* EN: "©2026 myAi. All rights reserved."
* RO: "©2026 myAi. Toate drepturile sunt rezervate."
* Inject language-aware footer with copyright and legal links.
* Footer text and links vary by current page language (EN vs RO).
*/
function injectFooter(){
var lang = pageLang();
var base = basePage();
var copyrightText = lang === 'ro' ? '©2026 myAi. Toate drepturile sunt rezervate.' : '©2026 myAi. All rights reserved.';
var linksHtml = '';
if(lang === 'ro'){
@@ -105,7 +90,7 @@
}
/**
* Initialize language persistence and topbar injection.
* Initialize language persistence and inject topbar/footer.
*/
function init(){
localStorage.setItem(LANG_KEY, pageLang());
@@ -115,7 +100,8 @@
/**
* 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){
var link = e.target.closest('.lang-link');
@@ -125,7 +111,11 @@
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'){
document.addEventListener('DOMContentLoaded', init);
} else {