2022-06-09 15:29:53 +02:00
|
|
|
var counter = 0;
|
2022-06-10 12:40:04 +02:00
|
|
|
|
2022-06-09 15:29:53 +02:00
|
|
|
var globalStyle = document.createElement("style");
|
|
|
|
document.head.appendChild(globalStyle);
|
|
|
|
|
|
|
|
String.prototype.map = Array.prototype.map;
|
|
|
|
|
|
|
|
function toCssAttr(txt){
|
|
|
|
return txt.map(d => (d.toUpperCase() === d && d !== d.toLowerCase()) ? "-" + d.toLowerCase(): d ).join("");
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:33:28 +02:00
|
|
|
const pools = {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-06-09 15:29:53 +02:00
|
|
|
export class Stear{
|
|
|
|
|
|
|
|
elem;
|
|
|
|
#childs = {};
|
|
|
|
|
|
|
|
constructor(elem){
|
|
|
|
this.elem = elem;
|
|
|
|
elem.style.position="relative";
|
|
|
|
}
|
|
|
|
|
|
|
|
style(css){
|
|
|
|
Object.entries(css).forEach(([k, d]) => {
|
|
|
|
this.elem.style[k] = d;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addElement (id,elem){
|
|
|
|
if(!(elem instanceof SFrame))throw new TypeError("Cannot add Element not extending from SFrame");
|
|
|
|
this.#childs[id] = elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
getElement (id) {
|
|
|
|
return this.g(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
g(id){
|
|
|
|
return this.#childs[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
async call (elem, args, layer = 1){
|
|
|
|
return await elem.call(this, args, layer);
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:33:28 +02:00
|
|
|
async include (elem, args, layer = 1, renderParent){
|
|
|
|
return await elem.call(this, args, layer, renderParent);
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:29:53 +02:00
|
|
|
static addGlobalStyleText(text){
|
|
|
|
globalStyle.innerHTML+="\n"+text;
|
|
|
|
}
|
|
|
|
|
2022-06-09 17:30:40 +02:00
|
|
|
static addAnimation(steps, name = "stearAnimation_" + counter++){
|
2022-06-09 15:29:53 +02:00
|
|
|
Stear.addGlobalStyleText(`@keyframes ${name} {
|
|
|
|
${
|
|
|
|
Object.entries(steps).map(([k,d])=>
|
|
|
|
` ${k} {
|
|
|
|
${Object.entries(d).map(d => " " + toCssAttr(d[0]) + ": " + d[1] + ";").join("\n")}
|
|
|
|
}`).join("\n")
|
|
|
|
}
|
|
|
|
}`);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2022-06-09 17:30:40 +02:00
|
|
|
static addGlobalStyleJSON(json, name = ".stearClass_" + counter++){
|
2022-06-09 15:29:53 +02:00
|
|
|
Stear.addGlobalStyleText(`
|
|
|
|
${name} {
|
|
|
|
${Object.entries(json).map(d => " " + toCssAttr(d[0]) + ": " + d[1] + ";").join("\n")}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
return name;
|
|
|
|
}
|
2022-08-11 16:33:28 +02:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2022-06-09 15:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class SFrame{
|
|
|
|
#preRender;
|
|
|
|
#call;
|
|
|
|
|
2022-08-10 21:56:12 +02:00
|
|
|
constructor({ call, preRender = true }){
|
2022-06-09 15:29:53 +02:00
|
|
|
this.#preRender = preRender;
|
|
|
|
this.#call = call;
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:33:28 +02:00
|
|
|
async call(stear, args, layer = 1, renderParent = stear.elem){
|
2022-08-10 21:56:12 +02:00
|
|
|
let lastRender;
|
|
|
|
let renderElem;
|
|
|
|
|
|
|
|
let event = { onloaded: () => { }, onclose: ()=>{}, onresolve: () => { }, onBeforRerender: () => { }, onAfterRerender: () => { } };
|
|
|
|
let find = {};
|
|
|
|
|
|
|
|
let resolved = false;
|
|
|
|
|
|
|
|
async function render(args){
|
|
|
|
if(!renderElem) return;
|
2022-08-11 16:33:28 +02:00
|
|
|
if (lastRender) if ([...(renderParent.children)].includes(lastRender._)) renderParent.removeChild(lastRender._);
|
2022-08-10 21:56:12 +02:00
|
|
|
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);
|
|
|
|
|
2022-08-11 16:33:28 +02:00
|
|
|
renderParent.appendChild(now.render);
|
2022-08-10 21:56:12 +02:00
|
|
|
lastRender = now;
|
|
|
|
|
|
|
|
now._.style.zIndex = layer;
|
|
|
|
|
|
|
|
for (var member in find) delete find[member];
|
|
|
|
Object.assign(find, now.find);
|
|
|
|
return lastRender;
|
2022-06-09 17:30:40 +02:00
|
|
|
}
|
2022-08-10 21:56:12 +02:00
|
|
|
return new Promise(async (res,rej)=>{
|
|
|
|
let firstRender = false;
|
|
|
|
renderElem = await this.#call(stear,{
|
|
|
|
find,
|
|
|
|
render: async (...args) => {
|
|
|
|
await event.onBeforRerender(...args);
|
|
|
|
if(renderElem)await render(args);
|
|
|
|
firstRender = true;
|
|
|
|
await event.onAfterRerender(...args);
|
|
|
|
return find;
|
2022-06-09 15:29:53 +02:00
|
|
|
},
|
2022-08-10 21:56:12 +02:00
|
|
|
resolve: async (r,close=true)=>{
|
|
|
|
if (resolved) return;
|
|
|
|
resolved = true;
|
|
|
|
await event.onresolve();
|
2022-08-11 16:33:28 +02:00
|
|
|
if (close && lastRender && [...(renderParent.children)].includes(lastRender._)) renderParent.removeChild(lastRender._);
|
2022-06-09 15:29:53 +02:00
|
|
|
res(r);
|
|
|
|
},
|
2022-08-10 21:56:12 +02:00
|
|
|
call: (elem, args= {})=>{
|
2022-06-09 15:29:53 +02:00
|
|
|
return stear.call(elem,args,layer+1);
|
2022-06-10 23:00:47 +02:00
|
|
|
},
|
2022-08-10 21:56:12 +02:00
|
|
|
close: async ()=>{
|
|
|
|
await event.onclose();
|
2022-08-11 16:33:28 +02:00
|
|
|
if (close && lastRender && [...(renderParent.children)].includes(lastRender._)) renderParent.removeChild(lastRender._);
|
2022-08-10 21:56:12 +02:00
|
|
|
},
|
|
|
|
event
|
2022-06-09 15:29:53 +02:00
|
|
|
}, args);
|
2022-08-10 21:56:12 +02:00
|
|
|
if(!renderElem) {
|
|
|
|
|
|
|
|
}else{
|
|
|
|
if (this.#preRender | firstRender) await render([]);
|
|
|
|
event.onloaded();
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:29:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SWindow extends SFrame{
|
|
|
|
|
2022-06-10 01:07:55 +02:00
|
|
|
#Frame;
|
2022-06-09 15:29:53 +02:00
|
|
|
|
2022-08-10 21:56:12 +02:00
|
|
|
constructor({ call, preRender = true, backgroundColor = "transparent"}){
|
2022-06-09 15:29:53 +02:00
|
|
|
var Frame = _({
|
2022-06-09 17:30:40 +02:00
|
|
|
style: {
|
|
|
|
top: "0px",
|
|
|
|
left: "0px",
|
|
|
|
position: "absolute",
|
|
|
|
height: "100%",
|
|
|
|
width: "100%",
|
2022-06-10 01:07:55 +02:00
|
|
|
display: "block",
|
|
|
|
backgroundColor,
|
2022-06-10 15:01:23 +02:00
|
|
|
//overflow:"scroll"
|
2022-06-10 01:07:55 +02:00
|
|
|
},
|
|
|
|
find:"main"
|
2022-06-09 17:30:40 +02:00
|
|
|
}, []);
|
|
|
|
super({
|
2022-08-10 21:56:12 +02:00
|
|
|
call: async(...args) => _({
|
|
|
|
style: {
|
|
|
|
top: "0px",
|
|
|
|
left: "0px",
|
|
|
|
position: "absolute",
|
|
|
|
height: "100%",
|
|
|
|
width: "100%",
|
|
|
|
display: "block",
|
|
|
|
backgroundColor,
|
|
|
|
//overflow:"scroll"
|
|
|
|
},
|
|
|
|
find: "main"
|
|
|
|
}, await call(...args)),
|
|
|
|
preRender
|
2022-06-09 17:30:40 +02:00
|
|
|
});
|
2022-06-09 15:29:53 +02:00
|
|
|
this.#Frame = Frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class class_ {
|
|
|
|
#elem;
|
|
|
|
#childs;
|
2022-08-10 21:56:12 +02:00
|
|
|
#build;
|
2022-06-09 15:29:53 +02:00
|
|
|
#find;
|
|
|
|
|
|
|
|
constructor(settings,childs){
|
|
|
|
this.#elem = document.createElement(settings.type ?? "div");
|
|
|
|
this.#childs = childs;
|
|
|
|
var keys = Object.keys(settings);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if(key == "type"){} else if (key == "style"){
|
|
|
|
Object.entries(settings[key]).forEach(([k,d])=>{
|
|
|
|
this.#elem.style[k] = d;
|
|
|
|
});
|
|
|
|
} else if (key == "find"){
|
|
|
|
this.#find = settings[key];
|
2022-06-09 17:30:40 +02:00
|
|
|
} else if (key == "event"){
|
|
|
|
Object.entries(settings[key]).forEach(([k, d]) => {
|
|
|
|
this.#elem.addEventListener(k,d);
|
|
|
|
});
|
|
|
|
} else {
|
2022-06-09 15:29:53 +02:00
|
|
|
this.#elem.setAttribute(key,settings[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set childs(childs){
|
2022-06-09 21:37:23 +02:00
|
|
|
this.#childs = Array.isArray(childs) ? childs : [childs];
|
2022-06-09 15:29:53 +02:00
|
|
|
}
|
2022-08-10 21:56:12 +02:00
|
|
|
|
2022-06-09 15:29:53 +02:00
|
|
|
set settings(settings){
|
|
|
|
var keys = Object.keys(settings);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (key == "type") { } else if (key == "style") {
|
|
|
|
Object.entries(settings[key]).forEach(([k, d]) => {
|
|
|
|
this.#elem.style[k] = d;
|
|
|
|
});
|
|
|
|
} else if (key == "find") {
|
|
|
|
this.#find = settings[key];
|
|
|
|
} else {
|
|
|
|
this.#elem.setAttribute(key, settings[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 21:56:12 +02:00
|
|
|
async build (args) {
|
|
|
|
this.#build = [];
|
|
|
|
for (let i = 0; i < this.#childs.length; i++) {
|
|
|
|
let elem = this.#childs[i];
|
|
|
|
|
|
|
|
if (typeof elem == "function") elem = await elem(...args);
|
|
|
|
|
|
|
|
if(Array.isArray(elem)){
|
|
|
|
for (let j = 0; j < elem.length; j++) {
|
|
|
|
if (elem[j] instanceof class_) await elem[j].build(args);
|
|
|
|
}
|
|
|
|
this.#build.push(...elem);
|
|
|
|
}else{
|
|
|
|
if(elem instanceof class_)await elem.build(args);
|
|
|
|
this.#build.push(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:29:53 +02:00
|
|
|
get render (){
|
|
|
|
this.#elem.innerHTML = "";
|
2022-08-10 21:56:12 +02:00
|
|
|
for (let i = 0; i < this.#build.length; i++) {
|
|
|
|
const elem = this.#build[i];
|
2022-08-11 16:33:28 +02:00
|
|
|
if (typeof elem == "string" || elem instanceof LanguagePoolString) {
|
|
|
|
this.#elem.appendChild(document.createTextNode(String(elem)));
|
2022-08-10 21:56:12 +02:00
|
|
|
} else {
|
2022-06-09 15:29:53 +02:00
|
|
|
this.#elem.appendChild(elem.render);
|
2022-08-10 21:56:12 +02:00
|
|
|
}
|
2022-06-09 15:29:53 +02:00
|
|
|
}
|
|
|
|
return this.#elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
get _ (){
|
|
|
|
return this.#elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
get find (){
|
|
|
|
var out = {};
|
2022-08-10 21:56:12 +02:00
|
|
|
this.#build.forEach(d=>{
|
2022-06-09 15:29:53 +02:00
|
|
|
Object.assign(out,d.find);
|
|
|
|
});
|
|
|
|
if (this.#find) out[this.#find]=this;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
2022-06-09 16:49:03 +02:00
|
|
|
export const _ = (settings = {}, childs = []) => new class_(settings, Array.isArray(childs) ? childs : [childs]);
|
2022-06-09 15:29:53 +02:00
|
|
|
|
|
|
|
|