Language switching suboptimally implemented.

This commit is contained in:
wearrrrr 2024-01-24 13:04:30 -06:00
parent 64a31c0daf
commit e98458148a
2 changed files with 88 additions and 47 deletions

View file

@ -1,4 +0,0 @@
export default {
defaultLocale: "en",
locales: ["en", "jp"],
};

View file

@ -42,35 +42,53 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
</div> </div>
</div> </div>
<script is:inline> <script>
import { navigate } from 'astro:transitions/client';
// Extend window object to store currently selected tab.
declare global {
interface Window {
currentlySelectedTab: string,
loadedContentStorage: any // Technically this is an object storing strings of html, but im lazy.
}
}
document.addEventListener("astro:before-swap", () => { document.addEventListener("astro:before-swap", () => {
window.currentlySelectedTab = "" window.currentlySelectedTab = ""
document.removeEventListener('setting-tabChange', determineListener) document.removeEventListener('setting-tabChange', determineListener as EventListener)
}) })
window.currentlySelectedTab; window.currentlySelectedTab;
window.loadedContentStorage = {} window.loadedContentStorage = {}
Array.from(document.getElementsByClassName('setting-tab')).forEach((tab)=>{ function beginLoad() {
let contentToLoad = document.getElementById('content-' + tab.id) Array.from(document.getElementsByClassName('setting-tab')).forEach((tab)=>{
if (contentToLoad) { let contentToLoad = document.getElementById('content-' + tab.id)
window.loadedContentStorage[tab.id] = contentToLoad.innerHTML if (contentToLoad) {
contentToLoad.remove() window.loadedContentStorage[tab.id] = contentToLoad.innerHTML
} contentToLoad.remove()
}
tab.addEventListener('click', (event) => { tab.addEventListener('click', (event) => {
loadContent(event.target.id) loadContent((event.target as HTMLElement).id)
})
}) })
}) }
beginLoad();
function loadContent(tabID) {
function loadContent(tabID: string) {
if (window.currentlySelectedTab == tabID) return if (window.currentlySelectedTab == tabID) return
else window.currentlySelectedTab = tabID else window.currentlySelectedTab = tabID
let currentContent = document.getElementById('current-content') let currentContent = document.getElementById('current-content')
if (currentContent) { if (currentContent) {
currentContent.style.opacity = '0' currentContent.style.opacity = '0'
setTimeout(() => { setTimeout(() => {
currentContent.innerHTML = window.loadedContentStorage[tabID] if (currentContent) {
currentContent.style.opacity = '1' currentContent.innerHTML = window.loadedContentStorage[tabID]
currentContent.style.opacity = '1'
} else {
throw new Error("Current content not found!")
}
document.dispatchEvent(new CustomEvent('setting-tabChange', {detail: tabID })) document.dispatchEvent(new CustomEvent('setting-tabChange', {detail: tabID }))
document.dispatchEvent(new CustomEvent('setting-tabLoad', {detail: tabID })) document.dispatchEvent(new CustomEvent('setting-tabLoad', {detail: tabID }))
}, 250); }, 250);
@ -85,11 +103,11 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
if (dropdown) { if (dropdown) {
if (dropdown.style.maxHeight == '0px' || dropdown.style.maxHeight == '') { if (dropdown.style.maxHeight == '0px' || dropdown.style.maxHeight == '') {
dropdown.style.maxHeight = dropdown.scrollHeight + 'px'; dropdown.style.maxHeight = dropdown.scrollHeight + 'px';
toggle.style.borderRadius = '10px 10px 0 0'; document.getElementById(toggle.id)!.style.borderRadius = '10px 10px 0 0';
} else { } else {
dropdown.style.maxHeight = '0px'; dropdown.style.maxHeight = '0px';
setTimeout(() => { setTimeout(() => {
toggle.style.borderRadius = '10px'; document.getElementById(toggle.id)!.style.borderRadius = '10px';
}, 300); }, 300);
} }
} }
@ -97,7 +115,7 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
}) })
} }
function determineListener(event) { function determineListener(event: CustomEvent) {
if (event.detail == "setting-tab-proxy") { if (event.detail == "setting-tab-proxy") {
addDropdownListener() addDropdownListener()
} else if (event.detail == "setting-tab-customization") { } else if (event.detail == "setting-tab-customization") {
@ -105,32 +123,39 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
} }
} }
function closeDropdown(dropdownID) { function closeDropdown(dropdownID: string) {
let dropdown = document.getElementById(dropdownID) let dropdown = document.getElementById(dropdownID)
if (dropdown) { if (dropdown) {
dropdown.style.maxHeight = '0px'; dropdown.style.maxHeight = '0px';
setTimeout(() => { setTimeout(() => {
let dropdown_toggle = document.getElementById(dropdownID.replace('-menu', '')); let dropdown_toggle = document.getElementById(dropdownID.replace('-menu', ''));
dropdown_toggle.style.borderRadius = '10px'; if (dropdown_toggle) {
dropdown_toggle.style.borderRadius = '10px';
} else {
throw new Error("Dropdown toggle not found!")
}
}, 300); }, 300);
} }
} }
function applySavedLocalStorage(localStorageItem, dropdownID) { function applySavedLocalStorage(localStorageItem: string, dropdownID: string) {
if (localStorage.getItem(localStorageItem)) { if (localStorage.getItem(localStorageItem)) {
let dropdown_toggle = document.getElementById(dropdownID); let dropdown_toggle = document.getElementById(dropdownID);
if (dropdown_toggle) { if (dropdown_toggle) {
dropdown_toggle.innerText = localStorage.getItem(localStorageItem).charAt(0).toUpperCase() + localStorage.getItem(localStorageItem).slice(1) dropdown_toggle.innerText = (localStorage.getItem(localStorageItem) || "").charAt(0).toUpperCase() + (localStorage.getItem(localStorageItem) || "").slice(1)
} }
} }
} }
function applyDropdownEventListeners(item, dropdownID, localStorageItem, optionalCallback) { function applyDropdownEventListeners(item: HTMLElement, dropdownID: string, localStorageItem: string, optionalCallback: Function | undefined = undefined) {
Array.from(item.children).forEach((item) => { Array.from(item.children).forEach((item) => {
item.addEventListener('click', () => { item.addEventListener('click', () => {
localStorage.setItem(localStorageItem, item.dataset.setting) // TODO: Allow an optional field for a custom localStorage value, instead of the input value.
localStorage.setItem(localStorageItem, ((item as HTMLElement).dataset.setting) || "")
applySavedLocalStorage(localStorageItem, dropdownID) applySavedLocalStorage(localStorageItem, dropdownID)
closeDropdown(item.parentElement.id); if (item.parentElement) {
closeDropdown(item.parentElement.id);
}
if (typeof optionalCallback == "function") { if (typeof optionalCallback == "function") {
optionalCallback(); optionalCallback();
@ -139,27 +164,45 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
}) })
} }
function applyInputListeners(item, localStorageItem) { function applyInputListeners(item: HTMLInputElement, localStorageItem: string) {
item.addEventListener('input', () => { item.addEventListener('input', () => {
// TODO: Allow an optional field for a custom localStorage value, instead of the input value.
localStorage.setItem(localStorageItem, item.value) localStorage.setItem(localStorageItem, item.value)
}) })
} }
document.addEventListener('setting-tabChange', determineListener) document.addEventListener('setting-tabChange', determineListener as EventListener)
loadContent('setting-tab-proxy') loadContent('setting-tab-proxy')
function setupCustomizationSettings() { function setupCustomizationSettings() {
applySavedLocalStorage('alu__selectedTheme', 'dropdown__selected-theme'); applySavedLocalStorage('alu__selectedTheme', 'dropdown__selected-theme');
applySavedLocalStorage('alu__selectedLanguage', 'dropdown__selected-language'); applySavedLocalStorage('alu__selectedLanguage', 'dropdown__selected-language');
let dropdownTheme = document.getElementById('dropdown__selected-theme-menu') // TODO: Null checking lol.
let dropdownLanguage = document.getElementById('dropdown__selected-language-menu') let dropdownTheme = document.getElementById('dropdown__selected-theme-menu') as HTMLElement
let dropdownLanguage = document.getElementById('dropdown__selected-language-menu') as HTMLElement
applyDropdownEventListeners(dropdownTheme, 'dropdown__selected-theme', 'alu__selectedTheme', changeTheme); applyDropdownEventListeners(dropdownTheme, 'dropdown__selected-theme', 'alu__selectedTheme', changeTheme);
applyDropdownEventListeners(dropdownLanguage, 'dropdown__selected-language', 'alu__selectedLanguage'); applyDropdownEventListeners(dropdownLanguage, 'dropdown__selected-language', 'alu__selectedLanguage', navigateToNewLangaugePage);
}
function navigateToNewLangaugePage() {
console.log("Here")
console.log(localStorage.getItem("alu__selectedLanguage"))
switch (localStorage.getItem("alu__selectedLanguage")) {
case "English":
window.location.href = "/en/settings/"
break;
case "日本語":
window.location.href = "/jp/settings/"
break;
}
beginLoad();
} }
function changeTheme() { function changeTheme() {
let theme = localStorage.getItem('alu__selectedTheme') // || "" is safe to use here, because it will default to the default theme if the localStorage item is not found.
let theme = localStorage.getItem('alu__selectedTheme') || ""
if (theme) { if (theme) {
document.documentElement.setAttribute('data-theme', theme.toLowerCase()) document.documentElement.setAttribute('data-theme', theme.toLowerCase())
} }
@ -169,20 +212,21 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
} }
} }
function setupSettings(event) { function setupSettings(event: CustomEvent) {
if (event.detail == "setting-tab-proxy") { if (event.detail == "setting-tab-proxy") {
applySavedLocalStorage('alu__selectedProxy', 'dropdown__selected-proxy'); applySavedLocalStorage('alu__selectedProxy', 'dropdown__selected-proxy');
applySavedLocalStorage('alu__search_engine', 'dropdown__search-engine'); applySavedLocalStorage('alu__search_engine', 'dropdown__search-engine');
applySavedLocalStorage("alu__selectedOpenWith", 'dropdown__open-with'); applySavedLocalStorage("alu__selectedOpenWith", 'dropdown__open-with');
let selectedProxyDropdown = document.getElementById('dropdown__selected-proxy-menu') let selectedProxyDropdown = document.getElementById('dropdown__selected-proxy-menu') as HTMLElement
let searchEngineDropdown = document.getElementById('dropdown__search-engine-menu') let searchEngineDropdown = document.getElementById('dropdown__search-engine-menu') as HTMLElement
let openWithDropdown = document.getElementById('dropdown__open-with-menu') let openWithDropdown = document.getElementById('dropdown__open-with-menu') as HTMLElement
let bareUrlInput = document.getElementById('bare-url-input') let bareUrlInput = document.getElementById('bare-url-input') as HTMLInputElement
let searxngUrlInput = document.getElementById('searxng-url-input') let searxngUrlInput = document.getElementById('searxng-url-input') as HTMLInputElement
let savedSearxngUrl = localStorage.getItem("alu__searxngUrl") let savedSearxngUrl = localStorage.getItem("alu__searxngUrl")
if (savedSearxngUrl != undefined) { if (savedSearxngUrl != undefined) {
if (savedSearxngUrl == "") localStorage.setItem("alu__searxngUrl", "https://searxng.site/") if (savedSearxngUrl == "") localStorage.setItem("alu__searxngUrl", "https://searxng.site/")
searxngUrlInput.value = localStorage.getItem("alu__searxngUrl") // Typescript.. we literally make sure that it won't be undefined RIGHT ABOVE THIS COMMENT.
if (searxngUrlInput) searxngUrlInput.value = localStorage.getItem("alu__searxngUrl") || "https://searxng.site/"
} }
// Proxy settings // Proxy settings
applyInputListeners(bareUrlInput, 'alu__bareUrl') applyInputListeners(bareUrlInput, 'alu__bareUrl')
@ -190,8 +234,9 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
applyDropdownEventListeners(searchEngineDropdown, 'dropdown__search-engine', 'alu__search_engine', checkSearxng); applyDropdownEventListeners(searchEngineDropdown, 'dropdown__search-engine', 'alu__search_engine', checkSearxng);
applyDropdownEventListeners(selectedProxyDropdown, 'dropdown__selected-proxy', 'alu__selectedProxy'); applyDropdownEventListeners(selectedProxyDropdown, 'dropdown__selected-proxy', 'alu__selectedProxy');
applyDropdownEventListeners(openWithDropdown, 'dropdown__open-with', 'alu__selectedOpenWith'); applyDropdownEventListeners(openWithDropdown, 'dropdown__open-with', 'alu__selectedOpenWith');
if (localStorage.getItem('bareUrl')) { // Currently does nothing functionally, but it's here for future use.
bareUrlInput.value = localStorage.getItem('bareUrl') if (localStorage.getItem('alu__bareUrl')) {
bareUrlInput.value = localStorage.getItem('bareUrl') || ""
} else { } else {
bareUrlInput.value = '/bare/' bareUrlInput.value = '/bare/'
} }
@ -204,15 +249,15 @@ import CustomizationTab from "./SettingsContent/CustomizationTab.astro";
function checkSearxng() { function checkSearxng() {
// This function checks if the "searxng" option was clicked, display an additional option if so. // This function checks if the "searxng" option was clicked, display an additional option if so.
if (localStorage.getItem("alu__search_engine")) { if (localStorage.getItem("alu__search_engine")) {
if (localStorage.getItem("alu__search_engine").toLowerCase() == "searx") { if ((localStorage.getItem("alu__search_engine") || "").toLowerCase() == "searx") {
document.getElementsByClassName('setting__searxng-url')[0].style.opacity = '1' (document.getElementsByClassName('setting__searxng-url')[0] as HTMLElement).style.opacity = '1'
} else { } else {
document.getElementsByClassName('setting__searxng-url')[0].style.opacity = '0' (document.getElementsByClassName('setting__searxng-url')[0] as HTMLElement).style.opacity = '0'
} }
} }
} }
document.addEventListener('setting-tabLoad', setupSettings) document.addEventListener('setting-tabLoad', setupSettings as EventListener);
</script> </script>
<style is:global> <style is:global>
.content-hidden { .content-hidden {