language
This commit is contained in:
parent
fd8f139bf4
commit
4bc4ca5a16
1 changed files with 111 additions and 7 deletions
118
main.js
118
main.js
|
@ -9,6 +9,10 @@ function toCssAttr(txt){
|
||||||
return txt.map(d => (d.toUpperCase() === d && d !== d.toLowerCase()) ? "-" + d.toLowerCase(): d ).join("");
|
return txt.map(d => (d.toUpperCase() === d && d !== d.toLowerCase()) ? "-" + d.toLowerCase(): d ).join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pools = {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
export class Stear{
|
export class Stear{
|
||||||
|
|
||||||
elem;
|
elem;
|
||||||
|
@ -42,6 +46,10 @@ export class Stear{
|
||||||
return await elem.call(this, args, layer);
|
return await elem.call(this, args, layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async include (elem, args, layer = 1, renderParent){
|
||||||
|
return await elem.call(this, args, layer, renderParent);
|
||||||
|
}
|
||||||
|
|
||||||
static addGlobalStyleText(text){
|
static addGlobalStyleText(text){
|
||||||
globalStyle.innerHTML+="\n"+text;
|
globalStyle.innerHTML+="\n"+text;
|
||||||
}
|
}
|
||||||
|
@ -66,6 +74,102 @@ ${Object.entries(json).map(d => " " + toCssAttr(d[0]) + ": " + d[1] + ";").jo
|
||||||
`)
|
`)
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static addLanguagePool(name){
|
||||||
|
if(typeof pools[name] == "undefined"){
|
||||||
|
pools[name] = new LanguagePool();
|
||||||
|
}
|
||||||
|
return pools[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
static addLanguageFile(data,lang){
|
||||||
|
Object.entries(data).forEach(([k,d]) => {
|
||||||
|
if (typeof pools[k] == "undefined") pools[k] = new LanguagePool();
|
||||||
|
pools[k].addFile(d,lang);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static set lang (lang){
|
||||||
|
Object.entries(pools).forEach(([k, d]) => {
|
||||||
|
d.lang = lang;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static get getLanguageFile(){
|
||||||
|
var out = {};
|
||||||
|
Object.entries(pools).forEach(([k, d]) => {
|
||||||
|
out[k] = d.getFile;
|
||||||
|
});
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LanguagePool{
|
||||||
|
#pool = {};
|
||||||
|
constructor(){
|
||||||
|
|
||||||
|
}
|
||||||
|
addFile(elems,lang){
|
||||||
|
Object.entries(elems).forEach(([k,d])=>{
|
||||||
|
if(typeof this.#pool[k] == "undefined"){
|
||||||
|
this.#pool[k] = new LanguagePoolString();
|
||||||
|
}
|
||||||
|
this.#pool[k].set(d,lang);
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
set lang (lang){
|
||||||
|
Object.entries(this.#pool).forEach(([k, d]) => {
|
||||||
|
d.lang = lang;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
get getFile(){
|
||||||
|
var out = {};
|
||||||
|
Object.entries(this.#pool).forEach(([k, d])=>{
|
||||||
|
out[k] = d.get("default");
|
||||||
|
});
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
add(tag, defaultText=""){
|
||||||
|
let newS = typeof this.#pool[tag] == "undefined" ? new LanguagePoolString() : this.#pool[tag];
|
||||||
|
this.#pool[tag] = newS;
|
||||||
|
newS.set(defaultText,"default");
|
||||||
|
return newS;
|
||||||
|
}
|
||||||
|
get(tag){
|
||||||
|
return this.#pool[tag];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LanguagePoolString {
|
||||||
|
#value = {
|
||||||
|
|
||||||
|
};
|
||||||
|
#lang = "en";
|
||||||
|
constructor(){}
|
||||||
|
|
||||||
|
set(v,lang){
|
||||||
|
this.#value[lang] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(lang){
|
||||||
|
return this.#value[lang];
|
||||||
|
}
|
||||||
|
|
||||||
|
set lang(lang){
|
||||||
|
this.#lang = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
r (...str){
|
||||||
|
var out = this.#value[this.#lang] == null ? this.#value.default : this.#value[this.#lang];
|
||||||
|
for (let i = 0; i < str.length; i++) {
|
||||||
|
out = out.replace("{}",str[i]);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
toString(){
|
||||||
|
return this.#value[this.#lang] == null ? this.#value.default : this.#value[this.#lang];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SFrame{
|
export class SFrame{
|
||||||
|
@ -77,7 +181,7 @@ export class SFrame{
|
||||||
this.#call = call;
|
this.#call = call;
|
||||||
}
|
}
|
||||||
|
|
||||||
async call(stear, args, layer = 1){
|
async call(stear, args, layer = 1, renderParent = stear.elem){
|
||||||
let lastRender;
|
let lastRender;
|
||||||
let renderElem;
|
let renderElem;
|
||||||
|
|
||||||
|
@ -88,13 +192,13 @@ export class SFrame{
|
||||||
|
|
||||||
async function render(args){
|
async function render(args){
|
||||||
if(!renderElem) return;
|
if(!renderElem) return;
|
||||||
if(lastRender)if ([...(stear.elem.children)].includes(lastRender._)) stear.elem.removeChild(lastRender._);
|
if (lastRender) if ([...(renderParent.children)].includes(lastRender._)) renderParent.removeChild(lastRender._);
|
||||||
let now = renderElem;
|
let now = renderElem;
|
||||||
if(typeof now == "function")now = await now();
|
if(typeof now == "function")now = await now();
|
||||||
if(!(now instanceof class_)) throw new Error("The Element to render is not an instance of class_");
|
if(!(now instanceof class_)) throw new Error("The Element to render is not an instance of class_");
|
||||||
await now.build(args);
|
await now.build(args);
|
||||||
|
|
||||||
stear.elem.appendChild(now.render);
|
renderParent.appendChild(now.render);
|
||||||
lastRender = now;
|
lastRender = now;
|
||||||
|
|
||||||
now._.style.zIndex = layer;
|
now._.style.zIndex = layer;
|
||||||
|
@ -118,7 +222,7 @@ export class SFrame{
|
||||||
if (resolved) return;
|
if (resolved) return;
|
||||||
resolved = true;
|
resolved = true;
|
||||||
await event.onresolve();
|
await event.onresolve();
|
||||||
if (close&&lastRender&&[...(stear.elem.children)].includes(lastRender._))stear.elem.removeChild(lastRender._);
|
if (close && lastRender && [...(renderParent.children)].includes(lastRender._)) renderParent.removeChild(lastRender._);
|
||||||
res(r);
|
res(r);
|
||||||
},
|
},
|
||||||
call: (elem, args= {})=>{
|
call: (elem, args= {})=>{
|
||||||
|
@ -126,7 +230,7 @@ export class SFrame{
|
||||||
},
|
},
|
||||||
close: async ()=>{
|
close: async ()=>{
|
||||||
await event.onclose();
|
await event.onclose();
|
||||||
if (close && lastRender && [...(stear.elem.children)].includes(lastRender._)) stear.elem.removeChild(lastRender._);
|
if (close && lastRender && [...(renderParent.children)].includes(lastRender._)) renderParent.removeChild(lastRender._);
|
||||||
},
|
},
|
||||||
event
|
event
|
||||||
}, args);
|
}, args);
|
||||||
|
@ -252,8 +356,8 @@ export class class_ {
|
||||||
this.#elem.innerHTML = "";
|
this.#elem.innerHTML = "";
|
||||||
for (let i = 0; i < this.#build.length; i++) {
|
for (let i = 0; i < this.#build.length; i++) {
|
||||||
const elem = this.#build[i];
|
const elem = this.#build[i];
|
||||||
if (typeof elem == "string") {
|
if (typeof elem == "string" || elem instanceof LanguagePoolString) {
|
||||||
this.#elem.appendChild(document.createTextNode(elem));
|
this.#elem.appendChild(document.createTextNode(String(elem)));
|
||||||
} else {
|
} else {
|
||||||
this.#elem.appendChild(elem.render);
|
this.#elem.appendChild(elem.render);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue