d0bba19a17
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>
125 lines
4.2 KiB
JavaScript
125 lines
4.2 KiB
JavaScript
/**
|
|
* Legal Pages Helper
|
|
*
|
|
* Dynamically injects a common header (topbar) and language-aware footer
|
|
* into all 6 legal pages to eliminate HTML duplication.
|
|
*
|
|
* 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";
|
|
|
|
/**
|
|
* Build URL to alternate language page.
|
|
* Supports both -en.html and -ro.html naming patterns.
|
|
*/
|
|
function targetPage(current, lang){
|
|
if(current.indexOf('-ro.html') !== -1) return current.replace('-ro.html', '-' + lang + '.html');
|
|
if(current.indexOf('-en.html') !== -1) return current.replace('-en.html', '-' + lang + '.html');
|
|
return current;
|
|
}
|
|
|
|
/**
|
|
* Get current page base name (terms, privacy, cookies).
|
|
*/
|
|
function basePage(){
|
|
var path = window.location.pathname;
|
|
if(path.indexOf('terms') !== -1) return 'terms';
|
|
if(path.indexOf('privacy') !== -1) return 'privacy';
|
|
if(path.indexOf('cookies') !== -1) return 'cookies';
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Detect page language (EN or RO).
|
|
*/
|
|
function pageLang(){
|
|
return window.location.pathname.indexOf('-ro.html') !== -1 ? 'ro' : 'en';
|
|
}
|
|
|
|
/**
|
|
* Inject topbar with logo and language switcher.
|
|
* Uses pageLang() to determine current page language and set active state.
|
|
*/
|
|
function injectTopbar(){
|
|
var base = basePage();
|
|
var lang = pageLang();
|
|
var html = '<div class="topbar">' +
|
|
'<a class="brand" href="/" aria-label="' + (lang === 'ro' ? 'Înapoi' : 'Back') + '">' +
|
|
'<span class="brand-mark"><img src="/img/myai-logo.svg" alt="MyAi.ro"></span>' +
|
|
'<span><span class="brand-text">' + (lang === 'ro' ? 'Înapoi' : 'Back') + '</span></span>' +
|
|
'</a>' +
|
|
'<div class="switcher">' +
|
|
'<a href="' + base + '-ro.html" class="lang-link ' + (lang === 'ro' ? 'active' : '') + '" data-lang="ro" aria-label="Română">' +
|
|
'<img src="/img/flags/ro.svg" alt="Română">' +
|
|
'</a>' +
|
|
'<a href="' + base + '-en.html" class="lang-link ' + (lang === 'en' ? 'active' : '') + '" data-lang="en" aria-label="English">' +
|
|
'<img src="/img/flags/en.svg" alt="English">' +
|
|
'</a>' +
|
|
'</div>' +
|
|
'</div>';
|
|
$('.wrap').prepend(html);
|
|
}
|
|
|
|
/**
|
|
* 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 copyrightText = lang === 'ro' ? '©2026 myAi. Toate drepturile sunt rezervate.' : '©2026 myAi. All rights reserved.';
|
|
var linksHtml = '';
|
|
if(lang === 'ro'){
|
|
linksHtml = '<a href="terms-ro.html">Termeni și condiții</a>' +
|
|
'<a href="privacy-ro.html">Politica de confidențialitate</a>' +
|
|
'<a href="cookies-ro.html">Politica de COOKIES</a>';
|
|
} else {
|
|
linksHtml = '<a href="terms-en.html">Terms and Conditions</a>' +
|
|
'<a href="privacy-en.html">Privacy Policy</a>' +
|
|
'<a href="cookies-en.html">Cookies Policy</a>';
|
|
}
|
|
var html = '<div class="footer">' +
|
|
'<small>' + copyrightText + '</small>' +
|
|
'<div class="footer-links">' + linksHtml + '</div>' +
|
|
'</div>';
|
|
$('.wrap').append(html);
|
|
}
|
|
|
|
/**
|
|
* Initialize language persistence and inject topbar/footer.
|
|
*/
|
|
function init(){
|
|
localStorage.setItem(LANG_KEY, pageLang());
|
|
injectTopbar();
|
|
injectFooter();
|
|
}
|
|
|
|
/**
|
|
* Handle language link clicks with event delegation.
|
|
* 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');
|
|
if(!link) return;
|
|
e.preventDefault();
|
|
localStorage.setItem(LANG_KEY, link.getAttribute('data-lang'));
|
|
window.location.href = targetPage(window.location.pathname, link.getAttribute('data-lang'));
|
|
});
|
|
|
|
/* ============================================================
|
|
PAGE INITIALIZATION
|
|
============================================================ */
|
|
|
|
// Initialize topbar and footer injection on DOM ready
|
|
if(document.readyState === 'loading'){
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|