Skip to content

Commit

Permalink
don
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Schwartz committed Jun 15, 2024
1 parent d6b3af6 commit b107cf1
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 103 deletions.
225 changes: 125 additions & 100 deletions assets/js/lookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,150 @@
import { auth } from './firebaseInit.js';
import { signUp } from './authentication.js';

document.addEventListener('DOMContentLoaded', function() {
function openModalById(modalId, stepId) {
console.log("Attempting to open modal", modalId);
const modal = document.getElementById(modalId);
if (modal) {
console.log("Modal Found:", modalId);
modal.style.display = 'block';
if (stepId) {
modal.querySelectorAll('.modal-step').forEach(step => {
step.style.display = 'none';
});
const step = document.getElementById(stepId);
if (step) {
step.style.display = 'block';
} else {
console.error('Specified step not found:', stepId);
}
}
} else {
console.error('Modal not found:', modalId);
}
}


function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
function closeModalById(modalId) {
const modal = document.getElementById(modalId);
if (modal) modal.style.display = 'none';
}
document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Lax; Secure";
}

function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
// Check if the button exists before adding the event listener
const openModalButton = document.getElementById('openModalButton');
if (openModalButton) {
openModalButton.addEventListener('click', function() {
openModalById('auth-modal', 'step1');
});
} else {
console.error('openModalButton not found');
}
return null;
}

function eraseCookie(name) {
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
// Check if the elements with class 'close' exist before adding the event listeners
document.querySelectorAll('.close').forEach(function(element) {
element.addEventListener('click', function() {
const modalId = element.closest('.modal').id;
closeModalById(modalId);
});
});

// Utility function to apply language settings asynchronously
async function applyLanguageSettings(language) {
try {
// Fetch the translation file for the selected language
const response = await fetch(`assets/trans/${language}.json`);
const translations = await response.json();
// Additional initialization if necessary
checkForModalOpening();

// Apply translations to all elements with the 'data-translate' attribute
document.querySelectorAll('[data-translate]').forEach(function (elem) {
const key = elem.getAttribute('data-translate');
if (translations[key]) {
elem.textContent = translations[key]; // Using textContent for security
}
});
} catch (error) {
console.error('Error loading or applying translations:', error);
// Cookie Utility Functions
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Lax; Secure";
}
}

function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

function setLanguagePreference(language) {
setCookie('userLanguage', language, 365);
applyLanguageSettings(language);
}
function eraseCookie(name) {
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

function acceptCookies() {
setCookie('userConsent', 'accepted', 365);
closeModalById('cookie-consent-modal');
}
// Utility function to apply language settings asynchronously
async function applyLanguageSettings(language) {
try {
// Fetch the translation file for the selected language
const response = await fetch(`assets/trans/${language}.json`);
const translations = await response.json();

// Apply translations to all elements with the 'data-translate' attribute
document.querySelectorAll('[data-translate]').forEach(function (elem) {
const key = elem.getAttribute('data-translate');
if (translations[key]) {
elem.textContent = translations[key]; // Using textContent for security
}
});
} catch (error) {
console.error('Error loading or applying translations:', error);
}
}

function declineCookies() {
setCookie('userConsent', 'declined', 365);
closeModalById('cookie-consent-modal');
}
function setLanguagePreference(language) {
setCookie('userLanguage', language, 365);
applyLanguageSettings(language);
}

function closeCookieModal() {
modal.style.display = 'none';
bodyContent.classList.remove('blur-background');
}
function acceptCookies() {
setCookie('userConsent', 'accepted', 365);
closeModalById('cookie-consent-modal');
}

function declineCookies() {
setCookie('userConsent', 'declined', 365);
closeModalById('cookie-consent-modal');
}

function showModal() {
modal.style.display = 'block';
bodyContent.classList.add('blur-background');
}
function closeCookieModal() {
modal.style.display = 'none';
bodyContent.classList.remove('blur-background');
}

function transitionModalStep(currentStepId, nextStepId) {
const currentStep = document.getElementById(currentStepId);
const nextStep = document.getElementById(nextStepId);
if (currentStep && nextStep) {
currentStep.style.display = 'none';
nextStep.style.display = 'block';
} else {
console.error('Error transitioning steps: Step elements not found.');
function showModal() {
modal.style.display = 'block';
bodyContent.classList.add('blur-background');
}
}

function switchLanguage(lang) {
document.querySelectorAll('[data-translate]').forEach(function (elem) {
var key = elem.getAttribute('data-translate');
if (translations[key] && translations[key][lang]) {
elem.textContent = translations[key][lang];
function transitionModalStep(currentStepId, nextStepId) {
const currentStep = document.getElementById(currentStepId);
const nextStep = document.getElementById(nextStepId);
if (currentStep && nextStep) {
currentStep.style.display = 'none';
nextStep.style.display = 'block';
} else {
console.error('Error transitioning steps: Step elements not found.');
}
});
}
}

function switchLanguage(lang) {
document.querySelectorAll('[data-translate]').forEach(function (elem) {
var key = elem.getAttribute('data-translate');
if (translations[key] && translations[key][lang]) {
elem.textContent = translations[key][lang];
}
});
}

// Variables for UI elements
var userLanguage = getCookie('userLanguage') || 'en';
var modal = document.getElementById('cookie-consent-modal');
var bodyContent = document.querySelector('.main-content');
var languageDropdown = document.getElementById('language-dropdown');
// Variables for UI elements
var userLanguage = getCookie('userLanguage') || 'en';
var modal = document.getElementById('cookie-consent-modal');
var bodyContent = document.querySelector('.main-content');
var languageDropdown = document.getElementById('language-dropdown');
});

// Utility functions for cookies and language settings

Expand Down Expand Up @@ -284,27 +331,5 @@ function setupEventListeners() {
document.addEventListener('DOMContentLoaded', setupEventListeners);

// Additional helper functions
function openModalById(modalId, stepId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.style.display = 'block';
if (stepId) {
modal.querySelectorAll('.modal-step').forEach(step => {
step.style.display = 'none';
});
const step = document.getElementById(stepId);
if (step) {
step.style.display = 'block';
} else{
console.error('Spec step not found:', stepId);
}
}
} else {
console.error('Modal not found:', modalID);
}}

function closeModalById(modalId) {
const modal = document.getElementById(modalId);
if (modal) modal.style.display = 'none';

}}
}
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ <h1>NEW<a href= "https://labase.languapps.com"> English Grammar for Learners</a>
<section id="intro" class="main style1 dark fullscreen">
<div class="content">
<h2>Languapps</h2>
<button id="loginButton">Login / Register</button>


<button id="openModalButton">Login / Register</button>
<p data-translate="statement">Play Hangman in English <br>Guess letters to solve <br> Hint : fruits and vegetables. </p>
<header> <!-- Hangman Game Elements -->
<div id="hangman-container">
Expand Down

0 comments on commit b107cf1

Please sign in to comment.