diff --git a/README.md b/README.md index 088e443..c5594aa 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,9 @@ MYAPP.HelloWorld = function (el) { update: update, }; + // initial update + update(); + // define idempotent update function function update(next) { // update state @@ -265,7 +268,7 @@ MYAPP.MyCounter = function (el) { // no ES6 template literals :( el.innerHTML = [ '
', - ' ', + ' ', ' ', ' ', '
', @@ -407,15 +410,20 @@ VT.TodoList = function (el) { // mark current children for removal var obsolete = new Set(container.children); + // map current children by data-key + var childrenByKey = new Map(); + + obsolete.forEach(function (child) { + childrenByKey.set(child.getAttribute('data-key'), child); + }); + // build new list of child elements from data var children = state.items.map(function (item) { - // find existing child by key - var child = container.querySelector( - '.todo-item[data-key="' + item.id + '"]' - ); + // find existing child by data-key + var child = childrenByKey.get(item.id); if (child) { - // if it exists, keep child + // if child exists, keep it obsolete.delete(child); } else { // otherwise, create new child diff --git a/public/scripts/TodoFrameCustom.js b/public/scripts/TodoFrameCustom.js index 3534f4b..a612920 100644 --- a/public/scripts/TodoFrameCustom.js +++ b/public/scripts/TodoFrameCustom.js @@ -62,7 +62,7 @@ VT.TodoFrameCustom = function (el) { el.addEventListener('draggableOver', function (e) { if (!e.detail.data.list) return; - updateTranslation(); + updatePositions(); }); el.todoFrameCustom = { @@ -75,11 +75,14 @@ VT.TodoFrameCustom = function (el) { var lists = getLists(); var container = el.querySelector('.container'); var obsolete = new Set(container.children); + var childrenByKey = new Map(); + + obsolete.forEach(function (child) { + childrenByKey.set(child.getAttribute('data-key'), child); + }); var children = lists.map(function (list) { - var child = container.querySelector( - '.todo-custom-list[data-key="' + list.id + '"]' - ); + var child = childrenByKey.get(list.id); if (child) { obsolete.delete(child); @@ -105,11 +108,11 @@ VT.TodoFrameCustom = function (el) { } }); - updateTranslation(); + updatePositions(); updateHeight(); } - function updateTranslation() { + function updatePositions() { el.querySelectorAll('.container > *').forEach(function (child, index) { child.style.transform = 'translateX(' + (index - state.at) * 100 + '%)'; }); diff --git a/public/scripts/TodoFrameDays.js b/public/scripts/TodoFrameDays.js index ea98774..88a7167 100644 --- a/public/scripts/TodoFrameDays.js +++ b/public/scripts/TodoFrameDays.js @@ -58,11 +58,14 @@ VT.TodoFrameDays = function (el) { var container = el.querySelector('.container'); var obsolete = new Set(container.children); + var childrenByKey = new Map(); + + obsolete.forEach(function (child) { + childrenByKey.set(child.getAttribute('data-key'), child); + }); var children = days.map(function (day) { - var child = container.querySelector( - '.todo-day[data-key="' + day.id + '"]' - ); + var child = childrenByKey.get(day.id); if (child) { obsolete.delete(child); diff --git a/public/scripts/TodoList.js b/public/scripts/TodoList.js index 412b07a..1e08989 100644 --- a/public/scripts/TodoList.js +++ b/public/scripts/TodoList.js @@ -31,10 +31,14 @@ VT.TodoList = function (el) { var container = el.querySelector('.items'); var obsolete = new Set(container.children); + var childrenByKey = new Map(); + + obsolete.forEach(function (child) { + childrenByKey.set(child.getAttribute('data-key'), child); + }); + var children = state.items.map(function (item) { - var child = container.querySelector( - '.todo-item[data-key="' + item.id + '"]' - ); + var child = childrenByKey.get(item.id); if (child) { obsolete.delete(child);