1
0
Fork 0

Add rtl direction support and playground.js

This commit is contained in:
Kitteh 2021-05-07 20:41:40 +01:00
parent 44cefde7b6
commit ea45339a11
11 changed files with 45 additions and 6 deletions

View file

@ -15,6 +15,7 @@ parserOptions:
globals: globals:
pageContent: writable pageContent: writable
module: writable module: writable
process: writable
rules: rules:

View file

@ -35,6 +35,13 @@ export class PageState extends Page {
localStorage.setItem('token', value); localStorage.setItem('token', value);
} }
get pageDirection() {
return localStorage.getItem('pageDirection') || "ltr";
}
set pageDirection(value) {
localStorage.setItem('pageDirection', value);
}
get language() { get language() {
return localStorage.getItem('language') || ""; return localStorage.getItem('language') || "";
} }

View file

@ -33,7 +33,7 @@ function ListItem(children) {
}); });
} }
function onLoad() { async function onLoad() {
document.body.innerHTML = ""; document.body.innerHTML = "";
document.body.appendChild(makeElement({ document.body.appendChild(makeElement({
tag: "nav", tag: "nav",
@ -103,6 +103,11 @@ function onLoad() {
})); }));
window.pageContent = document.querySelector("#pageContent"); window.pageContent = document.querySelector("#pageContent");
if (process.env.NODE_ENV == "development") {
await (await import("./playground.js")).playground();
}
renderPage(); renderPage();
setInterval(async () => { setInterval(async () => {
@ -117,7 +122,7 @@ function onLoad() {
}, 5000); }, 5000);
} }
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', async function () {
i18next.init({ i18next.init({
lng: pageState.language, lng: pageState.language,
fallbackLng: 'en', fallbackLng: 'en',
@ -129,7 +134,7 @@ document.addEventListener('DOMContentLoaded', function () {
return value; return value;
} }
} }
}).then(function (_) { }).then(async function (_) {
onLoad(); await onLoad();
}); });
}, false); }, false);

View file

@ -82,6 +82,7 @@ export function changePage(page, shouldSwitch = true) {
} }
export function renderPage() { export function renderPage() {
document.documentElement.dir = pageState.pageDirection;
console.log("Rendering Page: ", pageState.currentPage.name); console.log("Rendering Page: ", pageState.currentPage.name);
document.querySelector("#pageContent").innerHTML = ""; document.querySelector("#pageContent").innerHTML = "";
setPageTitle(pageState.currentPage.name); setPageTitle(pageState.currentPage.name);

View file

@ -54,8 +54,9 @@ export class SetLanguagePage extends Page {
let language = formData.get("language"); let language = formData.get("language");
pageState.language = language; pageState.language = language;
console.log(pageState.language); console.log(pageState.language);
i18next.changeLanguage(language).then((_) => { i18next.changeLanguage(language).then((t) => {
changePage("HOME", false); changePage("HOME", false);
pageState.pageDirection = t("language_direction");
location.reload(); location.reload();
}); });
}); });

14
src/playground.js Normal file
View file

@ -0,0 +1,14 @@
import { pageState } from "./globalPageState.js";
import i18next from 'i18next';
// Playground is a way to debug and test things.
// Anything you put in here is gonna be run on page initial load
// before rendering.
// Also it only runs when process.env.NODE_ENV == "development"
// Please empty this function before committing.
export async function playground() {
console.log("Welcome to Playground!");
window.pageState = pageState;
window.i18next = i18next;
}

View file

@ -1,6 +1,8 @@
module.exports = { module.exports = {
// The localised name for the language // The localised name for the language
"language_name": "Deutsche", "language_name": "Deutsche",
// Internal: The direction of text (ltr or rtl)
"language_direction": "ltr",
// These are the buttons on the top bar. // These are the buttons on the top bar.
"home_btn": "Startseite", "home_btn": "Startseite",

View file

@ -1,6 +1,8 @@
module.exports = { module.exports = {
// The localised name for the language // The localised name for the language
"language_name": "English", "language_name": "English",
// Internal: The direction of text (ltr or rtl)
"language_direction": "ltr",
// These are the buttons on the top bar. // These are the buttons on the top bar.
"home_btn": "Home", "home_btn": "Home",

View file

@ -1,6 +1,8 @@
module.exports = { module.exports = {
// The localised name for the language // The localised name for the language
"language_name": "Français", "language_name": "Français",
// Internal: The direction of text (ltr or rtl)
"language_direction": "ltr",
// These are the buttons on the top bar. // These are the buttons on the top bar.
"home_btn": "Accueil", "home_btn": "Accueil",

View file

@ -1,6 +1,8 @@
module.exports = { module.exports = {
// The localised name for the language // The localised name for the language
"language_name": "Nederlands", "language_name": "Nederlands",
// Internal: The direction of text (ltr or rtl)
"language_direction": "ltr",
// These are the buttons on the top bar. // These are the buttons on the top bar.
"home_btn": "Homepagina", "home_btn": "Homepagina",

View file

@ -1,7 +1,9 @@
module.exports = { module.exports = {
// The localised name for the language // The localised name for the language
"language_name": "русский", "language_name": "русский",
// Internal: The direction of text (ltr or rtl)
"language_direction": "ltr",
// These are the buttons on the top bar. // These are the buttons on the top bar.
"home_btn": "Главная страница", "home_btn": "Главная страница",
"back_btn": "Назад", "back_btn": "Назад",