Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
nikeedev committed Sep 27, 2024
1 parent 41858ae commit 9ae5f4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
33 changes: 30 additions & 3 deletions kefir.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class Kefir {
this.ui.forEach((elem, i) => {
if (typeof elem === "string") {
if (elem == "br") {
const br = document.createElement("br");
root.appendChild(br);
} else if (elem.trimStart().startsWith("css:")) {
root.appendChild(document.createElement("br"));
} else if (elem == "hr") {
root.appendChild(document.createElement("hr"));
}
else if (elem.trimStart().startsWith("css:")) {
const css = document.createElement("style");
css.innerHTML = elem.split("css:")[1].trim();
document.head.appendChild(css);
Expand Down Expand Up @@ -107,6 +109,9 @@ class Kefir {
case "img":
case "image":
const img = document.createElement("img");
if (elem.id !== undefined && elem.id.trim() !== "") {
input.id = elem.id;
}
try {
img.src = elem.src;
img.alt = elem.alt != "" ? elem.alt : "";
Expand All @@ -116,12 +121,34 @@ class Kefir {
if (elem.height !== undefined) {
img.height = elem.height;
}
if (elem.action !== undefined) {
img.onclick = elem.action;
}
} catch (e) {
console.error(e);
}
root.appendChild(img);
break;

case "input":
const input = document.createElement("input");
if (elem.id !== undefined && elem.id.trim() !== "") {
input.id = elem.id;
}
try {
input.type = elem.input_type;
if (elem.placeholder != undefined)
input.placeholder = elem.placeholder;

if (elem.action !== undefined) {
input.onchange = elem.action;
}
} catch (e) {
console.error(e);
}
root.appendChild(input);
break;

default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<title>Kefir tests</title>
</head>
<body>
<div id="root"></div>
<div id="root">
</div>
<script type="module" src="test.js"></script>
</body>
</html>
7 changes: 6 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ let button = localStorage.getItem("counter") || 0;
const ui = [
"Hello",
"br",
"br",
{
type: "button",
text: "Increment: " + button,
Expand All @@ -16,6 +15,7 @@ const ui = [
id: "incr"
},
"br",
"br",
{
type: "button",
text: "Clear",
Expand All @@ -24,6 +24,11 @@ const ui = [
document.getElementById("incr").innerText = "Increment: " + button;
localStorage.setItem("counter", button);
}
},
"hr",
{
type: "input",
input_type: "checkbox",
}
]

Expand Down

0 comments on commit 9ae5f4d

Please sign in to comment.