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("");
|
||||
}
|
||||
|
||||
const pools = {
|
||||
|
||||
};
|
||||
|
||||
export class Stear{
|
||||
|
||||
elem;
|
||||
|
@ -42,6 +46,10 @@ export class Stear{
|
|||
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){
|
||||
globalStyle.innerHTML+="\n"+text;
|
||||
}
|
||||
|
@ -66,6 +74,102 @@ ${Object.entries(json).map(d => " " + toCssAttr(d[0]) + ": " + d[1] + ";").jo
|
|||
`)
|
||||
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{
|
||||
|
@ -77,7 +181,7 @@ export class SFrame{
|
|||
this.#call = call;
|
||||
}
|
||||
|
||||
async call(stear, args, layer = 1){
|
||||
async call(stear, args, layer = 1, renderParent = stear.elem){
|
||||
let lastRender;
|
||||
let renderElem;
|
||||
|
||||
|
@ -88,13 +192,13 @@ export class SFrame{
|
|||
|
||||
async function render(args){
|
||||
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;
|
||||
if(typeof now == "function")now = await now();
|
||||
if(!(now instanceof class_)) throw new Error("The Element to render is not an instance of class_");
|
||||
await now.build(args);
|
||||
|
||||
stear.elem.appendChild(now.render);
|
||||
renderParent.appendChild(now.render);
|
||||
lastRender = now;
|
||||
|
||||
now._.style.zIndex = layer;
|
||||
|
@ -118,7 +222,7 @@ export class SFrame{
|
|||
if (resolved) return;
|
||||
resolved = true;
|
||||
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);
|
||||
},
|
||||
call: (elem, args= {})=>{
|
||||
|
@ -126,7 +230,7 @@ export class SFrame{
|
|||
},
|
||||
close: async ()=>{
|
||||
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
|
||||
}, args);
|
||||
|
@ -252,8 +356,8 @@ export class class_ {
|
|||
this.#elem.innerHTML = "";
|
||||
for (let i = 0; i < this.#build.length; i++) {
|
||||
const elem = this.#build[i];
|
||||
if (typeof elem == "string") {
|
||||
this.#elem.appendChild(document.createTextNode(elem));
|
||||
if (typeof elem == "string" || elem instanceof LanguagePoolString) {
|
||||
this.#elem.appendChild(document.createTextNode(String(elem)));
|
||||
} else {
|
||||
this.#elem.appendChild(elem.render);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue