-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d06f08
commit 21dd9ca
Showing
2 changed files
with
49 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,74 @@ | ||
// Constants | ||
DEFAULT_LANGUAGE = "EN"; | ||
_LANGUAGE_PARENT_ID = "sdev-header__language"; | ||
_LANGUAGE_TEXT_HIGHLIGHT_CLASS = "sdev__text-highlight"; | ||
loadLangStartup(); | ||
const OPERATION = { | ||
ADD: "add", | ||
REMOVE: "remove" | ||
} | ||
|
||
// Page Startup | ||
function loadLangStartup() { | ||
// Public Functions | ||
function sdev_header__startLanguage() { | ||
|
||
// Add the highlight to the current language | ||
// Fetch the stored language | ||
const DEFAULT_LANGUAGE = "EN"; | ||
const langID = fetchLocalStorage("lang", DEFAULT_LANGUAGE); | ||
const langHTML = fetchObjectHTML(_LANGUAGE_PARENT_ID, langID); | ||
langHTML.classList.add(_LANGUAGE_TEXT_HIGHLIGHT_CLASS); | ||
|
||
// Add the highlight to the stored language | ||
_sdev_header__languageHighlight(langID, OPERATION.ADD); | ||
|
||
// Load the content for the language | ||
try { | ||
loadLangContent(langID); | ||
} catch (error) { | ||
console.error("Implementation of loadLangContent(langID) is missing.\nError: " + error); | ||
debugPrint(langID); | ||
} | ||
_sdev_header__loadLangContent(langID); | ||
} | ||
function sdev_header__switchLanguage(langID) { | ||
|
||
// Switch Language | ||
function switchLanguage(langID) { | ||
|
||
// If the language is already highlighted, ignore the click | ||
if (fetchLocalStorage("lang") === langID) { return; } | ||
// Fetch the current language | ||
const currentLangID = fetchLocalStorage("lang"); | ||
|
||
// Remove the highlight from the previous language | ||
const previousLangID = fetchLocalStorage("lang"); | ||
const previousLangHTML = fetchObjectHTML(_LANGUAGE_PARENT_ID, previousLangID); | ||
previousLangHTML.classList.remove(_LANGUAGE_TEXT_HIGHLIGHT_CLASS); | ||
// If the language is the same, then return | ||
if (currentLangID === langID) { return; } | ||
|
||
// Highlight the clicked language | ||
const langHTML = fetchObjectHTML(_LANGUAGE_PARENT_ID, langID); | ||
langHTML.classList.add(_LANGUAGE_TEXT_HIGHLIGHT_CLASS); | ||
// Swap the highlight from the previous language | ||
_sdev_header__languageHighlight(currentLangID, OPERATION.REMOVE); | ||
_sdev_header__languageHighlight(langID, OPERATION.ADD); | ||
|
||
// Save the selected language to the local storage | ||
localStorage.setItem("lang", langID); | ||
|
||
// Load the content for the language | ||
_sdev_header__loadLangContent(langID); | ||
} | ||
|
||
// Private Functions | ||
function _sdev_header__loadLangContent(langID) { | ||
try { | ||
loadLangContent(langID); | ||
} catch (error) { | ||
console.error("Implementation of loadLangContent(langID) is missing.\nError: " + error); | ||
debugPrint(langID); | ||
_sdev_header__languageDebugPrint(langID); | ||
} | ||
} | ||
function _sdev_header__languageHighlight(langID, operation) { | ||
|
||
// Parameters | ||
const LANGUAGE_PARENT_ID = "sdev-header__language"; | ||
const LANGUAGE_TEXT_HIGHLIGHT_CLASS = "sdev__text-highlight"; | ||
|
||
// Fetch the language HTML object | ||
const langHTML = fetchObjectHTML(LANGUAGE_PARENT_ID, langID); | ||
|
||
// Debug Language Content | ||
function debugPrint(langID) { | ||
// Add or remove the highlight | ||
if (operation === OPERATION.ADD) { | ||
langHTML.classList.add(LANGUAGE_TEXT_HIGHLIGHT_CLASS); | ||
} else if (operation === OPERATION.REMOVE) { | ||
langHTML.classList.remove(LANGUAGE_TEXT_HIGHLIGHT_CLASS); | ||
} | ||
} | ||
function _sdev_header__languageDebugPrint(langID) { | ||
const langIDtoContent = { | ||
"EN": "English", | ||
"ES": "Spanish", | ||
"FR": "French" | ||
}; | ||
console.debug("[switchLanguages.js] Selected Language: " + langIDtoContent[langID]); | ||
} | ||
} | ||
|
||
// Startup | ||
sdev_header__startLanguage(); |