all repos — h3 @ d3af11eb2d542073ec7c2dd3fc7b3d60ec84e500

A tiny, extremely minimalist JavaScript microframework.

TodoList app now works.
h3rald h3rald@h3rald.com
Thu, 09 Apr 2020 09:37:55 +0200
commit

d3af11eb2d542073ec7c2dd3fc7b3d60ec84e500

parent

37ef0bb1feb21702ade34ccbbd29ca51bdd8f9e3

4 files changed, 20 insertions(+), 14 deletions(-)

jump to
M example/assets/js/app.jsexample/assets/js/app.js

@@ -29,11 +29,11 @@ self.todo = Todo(self)

// Paginator Component self.paginator = Paginator(self) - // Actual DOM creation/redrawing - self.redraw = () => { + // Actual DOM creation/updateing + self.update = () => { self.save(); const newTree = self.render(); - self.view.redraw(newTree); + self.view.update(self.container.childNodes[0], newTree); self.view = newTree; };

@@ -43,7 +43,7 @@ self.addTodo = () => {

const newTodo = document.getElementById("new-todo"); if (!newTodo.value) { self.emptyTodoError = true; - self.redraw(); + self.update(); document.getElementById("new-todo").focus(); return; }

@@ -53,7 +53,7 @@ key: `todo_${Date.now()}__${newTodo.value}`, // Make todos "unique-enough" to ensure they are processed correctly

text: newTodo.value, }); newTodo.value = ""; - self.redraw(); + self.update(); document.getElementById("new-todo").focus(); };

@@ -69,7 +69,7 @@ // Set the todo filter.

self.setFilter = (event) => { let filter = document.getElementById("filter-text"); self.filter = filter.value; - self.redraw(); + self.update(); filter = document.getElementById("filter-text"); filter.focus(); };

@@ -84,7 +84,7 @@

// Clear error message self.clearError = () => { self.emptyTodoError = false; - self.redraw(); + self.update(); document.getElementById("new-todo").focus(); };
M example/assets/js/paginator.jsexample/assets/js/paginator.js

@@ -11,12 +11,12 @@ const nextClass = page < pages ? ".link" : ".disabled";

function setPreviousPage() { page = page - 1; window.location.hash = `/?page=${page}`; - app.redraw(); + app.update(); } function setNextPage() { page = page + 1; window.location.hash = `/?page=${page}`; - app.redraw(); + app.update(); } return h3("div.paginator", [ h3(`span.previous-page.fas.fa-arrow-left${previousClass}`, {
M example/assets/js/todo.jsexample/assets/js/todo.js

@@ -5,11 +5,11 @@ return function(data) {

const todoStateClass = data.done ? ".done" : ".todo"; function toggle() { data.done = !data.done; - app.redraw(); + app.update(); } function remove() { app.todos = app.todos.filter(({ key }) => key !== data.key); - app.redraw(); + app.update(); } return h3("div.todo-item", { id: data.key }, [ h3(`div.todo-content${todoStateClass}`, [
M h3.jsh3.js

@@ -109,9 +109,8 @@ return node;

} // Updates the current Virtual Node with a new Virtual Node (and syncs the existing DOM Node) - update(newvnode) { + update(node, newvnode) { const oldvnode = this; - const node = this.element; if ( oldvnode.constructor !== newvnode.constructor || oldvnode.element !== newvnode.element

@@ -183,7 +182,14 @@ if (equal(newmap, oldmap) && (notFoundInNew >= 0 || notFoundInOld >= 0)) {

// Something changed for (let i = 0; i < newmap.length; i++) { if (newmap[i] === -1) { - oldvnode.children[o].update(newnode.children[i]); + // TODO: refactor and handle text nodes properly. + if (typeof oldvnode.children[i] === 'string') { + console.log(oldvnode.children[i], newvnode.children[i]); + oldvnode.children[i] = newvnode.children[i]; + node.childNodes[i].nodeValue = newvnode.children[i]; + } else { + oldvnode.children[i].update(node.childNodes[i], newvnode.children[i]); + } } } } else {