stear/main.js

228 lines
6.1 KiB
JavaScript
Raw Normal View History

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("");
}
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);
}
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;
}
}
export class SFrame{
#preRender;
#render;
#call;
#lastRender;
constructor({ preRender, render, call }){
this.#preRender = preRender;
this.#render = render;
this.#call = call;
if(this.#preRender){
this.render();
}
}
render(args={}){
this.#lastRender = this.#render({}, args);
this.#lastRender.render;
return this.#lastRender;
}
async call(stear, args, layer = 1){
2022-06-09 17:30:40 +02:00
if(this.#preRender){
stear.elem.appendChild(this.#lastRender._);
this.#lastRender._.style.zIndex = layer;
}
2022-06-09 15:29:53 +02:00
var find = (this.#lastRender?.find) ?? {};
return new Promise((res,rej)=>{
this.#call(stear,{
find,
render: (args) => {
this.render(args);
for (var member in find) delete find[member];
Object.assign(find, (this.#lastRender?.find) ?? {});
2022-06-09 17:30:40 +02:00
if ([...(stear.elem.children)].indexOf(this.#lastRender._) >= 0) stear.elem.removeChild(this.#lastRender._);
stear.elem.appendChild(this.#lastRender._);
this.#lastRender._.style.zIndex = layer;
2022-06-09 15:29:53 +02:00
return find;
},
2022-06-09 21:37:23 +02:00
resolve: (r,close=true)=>{
if (close&&[...(stear.elem.children)].indexOf(this.#lastRender._) >= 0) stear.elem.removeChild(this.#lastRender._);
2022-06-09 15:29:53 +02:00
res(r);
},
call: (elem, args)=>{
return stear.call(elem,args,layer+1);
}
}, args);
});
}
}
export class SWindow extends SFrame{
2022-06-10 01:07:55 +02:00
#Frame;
2022-06-09 15:29:53 +02:00
2022-06-10 01:07:55 +02:00
constructor({ preRender, render, call, 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({
preRender, render: (settings, args) => {
Frame.childs = [render(settings, args)];
return Frame;
}, call
});
2022-06-09 15:29:53 +02:00
this.#Frame = Frame;
}
}
export class class_ {
#elem;
#childs;
#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
}
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]);
}
}
}
get render (){
this.#elem.innerHTML = "";
for (let i = 0; i < this.#childs.length; i++) {
const elem = this.#childs[i];
if(typeof elem == "string"){
this.#elem.appendChild(document.createTextNode(elem));
}else{
this.#elem.appendChild(elem.render);
}
}
return this.#elem;
}
get _ (){
return this.#elem;
}
get find (){
var out = {};
this.#childs.forEach(d=>{
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