2
0
mirror of https://github.com/esiur/iui.git synced 2026-04-04 06:58:22 +00:00

initial commit

This commit is contained in:
2021-02-22 11:39:50 +03:00
commit e82f4bc4cf
87 changed files with 14463 additions and 0 deletions

159
src/Router/Route.js Normal file
View File

@@ -0,0 +1,159 @@
import IUIElement from "../Core/IUIElement.js";
import { IUI } from "../Core/IUI.js";
import Router from "./Router.js";
export default IUI.module(class Route extends IUIElement {
constructor() {
super();
this.routes = [];
this.refs = {};
this._register("show");
this._register("hide");
}
async setData(value) {
if (this.hasAttribute("debug"))
debugger;
return await super.setData(value);
}
_updateLinks() {
for (var i = 0; i < this.children.length; i++) {
if (this.children[i] instanceof Route) {
this.routes.push(this.children[i]);
window.router.add(this.children[i], this);
i--;
}
}
}
get link() {
var link = this.name;
var parent = this.parent;
while (parent != null) {
link = parent.name + "/" + link;
parent = parent.parent;
}
return link;
}
get name() {
return this.getAttribute("name");
}
get src() {
return this.getAttribute("src");
}
get dst() {
return this._dst || this.getAttribute("dst");
}
set dst(value){
this._dst = value;
}
get caption() {
return this.getAttribute("caption");
}
get private() {
return this.hasAttribute("private");
}
get icon() {
return this.getAttribute("icon");
}
_getParent() {
let e = null;//this.parentElement;
while (e = this.parentElement) {
if (e instanceof Route || e instanceof Router)
return e;
}
return null;
}
async create() {
//window.router.add(this);
this._updateLinks();
if (this.hasAttribute("src")) {
let src = this.getAttribute("src").replace(/^\/+|\/+$/g, '');
let x = await fetch(src);
if (x.status != 200)
return;
let t = await x.text();
this.innerHTML = t;
//let xeval = (code) => eval(code);
}
// call create for the new elements
var newElements = this.querySelectorAll("*");
for (var i = 0; i < newElements.length; i++) {
// set route for all elements
var el = newElements[i];
// newElements[i].route = this;
el.view = this;
el.route = this;
if (el.hasAttribute("ref")) {
this.refs[el.getAttribute("ref")] = el;
}
if (el instanceof HTMLScriptElement) {
// this because HTML parsers don't evaluate script tag
// xeval.call(el.parentElement, "//# sourceURL=iui://" + src + "\r\n" + el.text);
//let func = new Function("//# sourceURL=iui://" +
// src + "-" + Math.round(Math.random() * 10000) + "\r\n return " + el.text.trim());
let func = new Function("//# sourceURL=iui://" + this.link
+ "\r\n return " + el.text.trim());
let rt = func.call(el.parentElement);
if (typeof (rt) === "object") {
for (var k in rt)
el.parentElement[k] = rt[k];
}
}
}
}
created() {
//this.updateBindings();
}
set(value) {
if (value == this.visible)
return;
if (value) {
this.setAttribute("selected", "");
this._emit("show");
}
else
{
this.removeAttribute("selected");
this._emit("hide");
}
}
get visible() { return this.hasAttribute("selected"); }
set visible(value) { this.set(value); }
});