Skip to content

Commit

Permalink
Merge pull request #1 from nguyenphuminh/v0.10.0
Browse files Browse the repository at this point in the history
v0.10.0
  • Loading branch information
nguyenphuminh authored May 15, 2022
2 parents 4551500 + f6266a6 commit 41b28c0
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 165 deletions.
3 changes: 3 additions & 0 deletions examples/counter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<body>
<script>
const { el, setState, mount, unmount } = Reval;

class Counter {
constructor() {
this.states = { counter: 1 };
Expand All @@ -29,6 +31,7 @@
]);
}
}

const counter = new Counter();

mount(document.body, counter);
Expand Down
105 changes: 54 additions & 51 deletions examples/counter/reval.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,66 @@
"use strict";

function el(tag, attr, body) {
const newEl = document.createElement(tag);
const Reval = {
el(tag, attr, body) {
const newEl = document.createElement(tag);

Array.isArray(body) ? body.forEach(item => newEl.append(item)) : newEl.append(body);

Array.isArray(body) ? body.forEach(item => newEl.append(item)) : newEl.append(body);

if (typeof attr === "object" && attr !== null) {
Object.keys(attr).forEach(attrName => {
if (typeof attr[attrName] === "function") {
newEl[attrName] = attr[attrName];
} else {
newEl.setAttribute(attrName, attr[attrName])
}
});
}
if (typeof attr === "object" && attr !== null) {
Object.keys(attr).forEach(attrName => {
if (typeof attr[attrName] === "function") {
newEl[attrName] = attr[attrName];
} else {
newEl.setAttribute(attrName, attr[attrName])
}
});
}

return newEl;
}
return newEl;
},

function mount(target, child, before, replace) {
if (typeof child.render !== "function") {
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child, before) : target.insertBefore(child, before)) :
target.append(child);
} else {
child.el = child.render();
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child.el, before) : target.insertBefore(child.el, before)) :
target.append(child.el);
child.mountedTo = target;
typeof child.onmount === "function" && child.onmount();
}
}
mount(target, child, before, replace) {
if (typeof child.render !== "function") {
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child, before) : target.insertBefore(child, before)) :
target.append(child);
} else {
child.el = child.render();
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child.el, before) : target.insertBefore(child.el, before)) :
target.append(child.el);

function unmount(target, child) {
if (typeof child.render !== "function") {
target.removeChild(child);
} else {
target.removeChild(child.el);
child.mountedTo = null;
typeof child.onunmount === "function" && child.onunmount();
}
}
child.mountedTo = target;

function setState(component, states) {
Object.keys(states).forEach(key => {
if (typeof states[key] === "object") {
Object.keys(states[key]).forEach(stateKey => {
component.states[key][stateKey] = states[key][stateKey];
});
typeof child.onmount === "function" && child.onmount();
}
},

unmount(target, child) {
if (typeof child.render !== "function") {
target.removeChild(child);
} else {
component.states[key] = states[key];
target.removeChild(child.el);

child.mountedTo = null;

typeof child.onunmount === "function" && child.onunmount();
}
})
component.mountedTo.removeChild(component.el);
component.el = component.render();
component.mountedTo.appendChild(component.el);
}
},

setState(component, states) {
Object.keys(states).forEach(key => {
component.states[key] = states[key];
});

component.mountedTo.removeChild(component.el);
component.el = component.render();
component.mountedTo.appendChild(component.el);

typeof component.onupdate === "function" && component.onupdate();
}
};

if (typeof module === "object" && typeof module.exports === "object") {
module.exports = { el, mount, unmount, setState };
module.exports = Reval;
}
2 changes: 2 additions & 0 deletions examples/shopping cart/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
</head>
<body>
<script>
const { el, setState, mount, unmount } = Reval;

class TodoList {
constructor() {
this.states = { list: [] };
Expand Down
103 changes: 56 additions & 47 deletions examples/shopping cart/reval.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,66 @@
"use strict";

function el(tag, attr, body) {
const newEl = document.createElement(tag);
const Reval = {
el(tag, attr, body) {
const newEl = document.createElement(tag);

Array.isArray(body) ? body.forEach(item => newEl.append(item)) : newEl.append(body);

Array.isArray(body) ? body.forEach(item => newEl.append(item)) : newEl.append(body);

if (typeof attr === "object" && attr !== null) {
Object.keys(attr).forEach(attrName => {
if (typeof attr[attrName] === "function") {
newEl[attrName] = attr[attrName];
} else {
newEl.setAttribute(attrName, attr[attrName])
}
});
}
if (typeof attr === "object" && attr !== null) {
Object.keys(attr).forEach(attrName => {
if (typeof attr[attrName] === "function") {
newEl[attrName] = attr[attrName];
} else {
newEl.setAttribute(attrName, attr[attrName])
}
});
}

return newEl;
}
return newEl;
},

function mount(target, child, before, replace) {
if (typeof child.render !== "function") {
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child, before) : target.insertBefore(child, before)) :
target.append(child);
} else {
child.el = child.render();
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child.el, before) : target.insertBefore(child.el, before)) :
target.append(child.el);
child.mountedTo = target;
typeof child.onmount === "function" && child.onmount();
}
}
mount(target, child, before, replace) {
if (typeof child.render !== "function") {
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child, before) : target.insertBefore(child, before)) :
target.append(child);
} else {
child.el = child.render();
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child.el, before) : target.insertBefore(child.el, before)) :
target.append(child.el);

function unmount(target, child) {
if (typeof child.render !== "function") {
target.removeChild(child);
} else {
target.removeChild(child.el);
child.mountedTo = null;
typeof child.onunmount === "function" && child.onunmount();
}
}
child.mountedTo = target;

function setState(component, states) {
Object.keys(states).forEach(key => {
component.states[key] = Array.isArray(states[key]) ? [...states[key]] : typeof states[key] === "object" ? {...states[key]} : states[key];
})
component.mountedTo.removeChild(component.el);
component.el = component.render();
component.mountedTo.appendChild(component.el);
}
typeof child.onmount === "function" && child.onmount();
}
},

unmount(target, child) {
if (typeof child.render !== "function") {
target.removeChild(child);
} else {
target.removeChild(child.el);

child.mountedTo = null;

typeof child.onunmount === "function" && child.onunmount();
}
},

setState(component, states) {
Object.keys(states).forEach(key => {
component.states[key] = states[key];
});

component.mountedTo.removeChild(component.el);
component.el = component.render();
component.mountedTo.appendChild(component.el);

typeof component.onupdate === "function" && component.onupdate();
}
};

if (typeof module === "object" && typeof module.exports === "object") {
module.exports = { el, mount, unmount, setState };
module.exports = Reval;
}
4 changes: 3 additions & 1 deletion examples/todo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
<html>
<head>
<meta charset="utf-8">
<script src="./reval.min.js"></script>
<script src="./reval.js"></script>
</head>
<body>
<script>
const { el, setState, mount, unmount } = Reval;

class TodoList {
constructor() {
this.states = { todos: [] };
Expand Down
66 changes: 66 additions & 0 deletions examples/todo/reval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";

const Reval = {
el(tag, attr, body) {
const newEl = document.createElement(tag);

Array.isArray(body) ? body.forEach(item => newEl.append(item)) : newEl.append(body);

if (typeof attr === "object" && attr !== null) {
Object.keys(attr).forEach(attrName => {
if (typeof attr[attrName] === "function") {
newEl[attrName] = attr[attrName];
} else {
newEl.setAttribute(attrName, attr[attrName])
}
});
}

return newEl;
},

mount(target, child, before, replace) {
if (typeof child.render !== "function") {
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child, before) : target.insertBefore(child, before)) :
target.append(child);
} else {
child.el = child.render();
(!(typeof before === "undefined" || before === null)) ?
(replace ? target.replaceChild(child.el, before) : target.insertBefore(child.el, before)) :
target.append(child.el);

child.mountedTo = target;

typeof child.onmount === "function" && child.onmount();
}
},

unmount(target, child) {
if (typeof child.render !== "function") {
target.removeChild(child);
} else {
target.removeChild(child.el);

child.mountedTo = null;

typeof child.onunmount === "function" && child.onunmount();
}
},

setState(component, states) {
Object.keys(states).forEach(key => {
component.states[key] = states[key];
});

component.mountedTo.removeChild(component.el);
component.el = component.render();
component.mountedTo.appendChild(component.el);

typeof component.onupdate === "function" && component.onupdate();
}
};

if (typeof module === "object" && typeof module.exports === "object") {
module.exports = Reval;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "revaljs",
"version": "0.9.0",
"description": "An extremely lightweight Javascript library to build web apps",
"version": "0.10.0",
"description": "A front-end library created just for the sake of being light",
"main": "reval.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
Loading

0 comments on commit 41b28c0

Please sign in to comment.