1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-08-21 13:21:29 +02:00

clean up comments

This commit is contained in:
Morris Brodersen
2023-11-26 14:31:37 +01:00
parent c91b27980e
commit 2815a1eb4c
9 changed files with 54 additions and 47 deletions

View File

@@ -11,7 +11,7 @@ if (!navigator.webdriver) {
let reload = true; let reload = true;
// hot reload stylesheets // Hot reload stylesheets
document.querySelectorAll('link[rel=stylesheet]').forEach((el) => { document.querySelectorAll('link[rel=stylesheet]').forEach((el) => {
if (el.getAttribute('href') === data.url) { if (el.getAttribute('href') === data.url) {
el.setAttribute('href', data.url); el.setAttribute('href', data.url);
@@ -19,7 +19,7 @@ if (!navigator.webdriver) {
} }
}); });
// hot reload images // Hot reload images
document.querySelectorAll('img').forEach((el) => { document.querySelectorAll('img').forEach((el) => {
if (el.getAttribute('src') === data.url) { if (el.getAttribute('src') === data.url) {
el.setAttribute('src', data.url); el.setAttribute('src', data.url);
@@ -27,7 +27,7 @@ if (!navigator.webdriver) {
} }
}); });
// otherwise, reload page // Otherwise, reload page
if (reload) location.reload(); if (reload) location.reload();
}); });

View File

@@ -22,10 +22,10 @@ export function AppDraggable(el, options) {
let imageX, imageY; let imageX, imageY;
let currentTarget; let currentTarget;
el.addEventListener('touchstart', start); el.addEventListener('touchstart', start, { passive: true });
el.addEventListener('mousedown', start); el.addEventListener('mousedown', start);
// maybe prevent click // Maybe prevent click
el.addEventListener( el.addEventListener(
'click', 'click',
(e) => { (e) => {

View File

@@ -11,13 +11,13 @@ export function AppFlip(el, options) {
let first; let first;
let level = 0; let level = 0;
// enable animations only after an initial delay // Enable animations only after an initial delay.
setTimeout(() => { setTimeout(() => {
enabled = true; enabled = true;
}, options.initialDelay ?? 100); }, options.initialDelay ?? 100);
// take a snapshot before any HTML changes // Take a snapshot before any HTML changes.
// do this only for the first beforeFlip event in the current cycle // Do this only for the first beforeFlip event in the current cycle.
el.addEventListener('beforeFlip', () => { el.addEventListener('beforeFlip', () => {
if (!enabled) return; if (!enabled) return;
if (++level > 1) return; if (++level > 1) return;
@@ -25,8 +25,8 @@ export function AppFlip(el, options) {
first = snapshot(); first = snapshot();
}); });
// take a snapshot after HTML changes, calculate and play animations // Take a snapshot after HTML changes, calculate and play animations.
// do this only for the last flip event in the current cycle // Do this only for the last flip event in the current cycle.
el.addEventListener('flip', () => { el.addEventListener('flip', () => {
if (!enabled) return; if (!enabled) return;
if (--level > 0) return; if (--level > 0) return;
@@ -45,16 +45,16 @@ export function AppFlip(el, options) {
}); });
}); });
// build a snapshot of the current HTML's client rectangles // Build a snapshot of the current HTML's client rectangles,
// includes original transforms and hierarchy // including original transforms and hierarchy.
function snapshot() { function snapshot() {
const map = new Map(); const map = new Map();
el.querySelectorAll(options.selector).forEach((el) => { el.querySelectorAll(options.selector).forEach((el) => {
const key = el.dataset.key ?? el; const key = el.dataset.key ?? el;
// parse original transform // Parse original transform,
// i.e. strip inverse transform using "scale(1)" marker // i.e. strip inverse transform using "scale(1)" marker.
const transform = el.style.transform const transform = el.style.transform
? el.style.transform.replace(/^.*scale\(1\)/, '') ? el.style.transform.replace(/^.*scale\(1\)/, '')
: ''; : '';
@@ -90,7 +90,7 @@ export function AppFlip(el, options) {
}); });
} }
// reinsert removed elements at their original position // Reinsert removed elements at their original position.
function invertForRemoval(first, last) { function invertForRemoval(first, last) {
const toRemove = []; const toRemove = [];
@@ -120,8 +120,8 @@ export function AppFlip(el, options) {
} }
} }
// set position of moved elements to their original position // Set position of moved elements to their original position,
// or set opacity to zero for new elements to appear nicely // or set opacity to zero for new elements to appear nicely.
function invertForAnimation(first, last) { function invertForAnimation(first, last) {
const toAnimate = []; const toAnimate = [];
@@ -135,7 +135,7 @@ export function AppFlip(el, options) {
entry.el.style.opacity = '0'; entry.el.style.opacity = '0';
toAnimate.push(entry); toAnimate.push(entry);
} else if (entry.deltaX !== 0 || entry.deltaY !== 0) { } else if (entry.deltaX !== 0 || entry.deltaY !== 0) {
// set inverted transform with "scale(1)" marker, see above // Set inverted transform with "scale(1)" marker, see above.
entry.el.style.transition = 'none'; entry.el.style.transition = 'none';
entry.el.style.transform = `translate(${entry.deltaX}px, ${entry.deltaY}px) scale(1) ${entry.transform}`; entry.el.style.transform = `translate(${entry.deltaX}px, ${entry.deltaY}px) scale(1) ${entry.transform}`;
toAnimate.push(entry); toAnimate.push(entry);
@@ -144,7 +144,7 @@ export function AppFlip(el, options) {
return toAnimate; return toAnimate;
// calculate inverse transform relative to any animated ancestors // Calculate inverse transform relative to any animated ancestors.
function calculate(entry) { function calculate(entry) {
if (entry.calculated) return; if (entry.calculated) return;
entry.calculated = true; entry.calculated = true;
@@ -169,7 +169,7 @@ export function AppFlip(el, options) {
} }
} }
// play remove animations and remove elements after timeout // Play remove animations and remove elements after timeout.
function remove(entries) { function remove(entries) {
entries.forEach((entry) => { entries.forEach((entry) => {
entry.el.style.transition = ''; entry.el.style.transition = '';
@@ -185,7 +185,7 @@ export function AppFlip(el, options) {
}, options.removeTimeout); }, options.removeTimeout);
} }
// play move/appear animations // Play move/appear animations.
function animate(entries) { function animate(entries) {
entries.forEach((entry) => { entries.forEach((entry) => {
entry.el.style.transition = ''; entry.el.style.transition = '';

View File

@@ -57,14 +57,14 @@ export function TodoApp(el) {
TodoFrameDays(el.querySelector('.todo-frame.-days')); TodoFrameDays(el.querySelector('.todo-frame.-days'));
TodoFrameCustom(el.querySelector('.todo-frame.-custom')); TodoFrameCustom(el.querySelector('.todo-frame.-custom'));
// each of these events make changes to the HTML to be animated using FLIP // Each of these events make changes to the HTML to be animated using FLIP.
// listening to them using "capture" dispatches "beforeFlip" before any changes // Listening to them using "capture" dispatches "beforeFlip" before any changes.
el.addEventListener('todoData', beforeFlip, true); el.addEventListener('todoData', beforeFlip, true);
el.addEventListener('sortableUpdate', beforeFlip, true); el.addEventListener('sortableUpdate', beforeFlip, true);
el.addEventListener('draggableCancel', beforeFlip, true); el.addEventListener('draggableCancel', beforeFlip, true);
el.addEventListener('draggableDrop', beforeFlip, true); el.addEventListener('draggableDrop', beforeFlip, true);
// some necessary work to orchestrate drag & drop with FLIP animations // Some necessary work to orchestrate drag & drop with FLIP animations
el.addEventListener('draggableStart', (e) => { el.addEventListener('draggableStart', (e) => {
e.detail.image.classList.add('_noflip'); e.detail.image.classList.add('_noflip');
el.appendChild(e.detail.image); el.appendChild(e.detail.image);
@@ -83,8 +83,8 @@ export function TodoApp(el) {
e.detail.placeholder.classList.add('_noflip'); e.detail.placeholder.classList.add('_noflip');
}); });
// dispatch "focusOther" on .use-focus-other inputs if they are not active // Dispatch "focusOther" on .use-focus-other inputs if they are not active.
// ensures only one edit input is active // Ensures only one edit input is active.
el.addEventListener('focusin', (e) => { el.addEventListener('focusin', (e) => {
if (!e.target.classList.contains('use-focus-other')) return; if (!e.target.classList.contains('use-focus-other')) return;
@@ -94,16 +94,16 @@ export function TodoApp(el) {
}); });
}); });
// listen to the TodoStore's data // Listen to the TodoStore's data.
// this is the main update // This is the main update.
// everything else is related to drag & drop or FLIP animations // Everything else is related to drag & drop or FLIP animations.
el.addEventListener('todoData', (e) => { el.addEventListener('todoData', (e) => {
todoData = e.detail; todoData = e.detail;
update(); update();
}); });
// dispatch "flip" after HTML changes from these events // Dispatch "flip" after HTML changes from the following events.
// this plays the FLIP animations // This plays the FLIP animations.
el.addEventListener('todoData', flip); el.addEventListener('todoData', flip);
el.addEventListener('sortableUpdate', flip); el.addEventListener('sortableUpdate', flip);
el.addEventListener('draggableCancel', flip); el.addEventListener('draggableCancel', flip);

View File

@@ -38,9 +38,13 @@ export function TodoCustomList(el) {
update(); update();
}); });
deleteEl.addEventListener('touchstart', () => { deleteEl.addEventListener(
'touchstart',
() => {
saveOnBlur = false; saveOnBlur = false;
}); },
{ passive: true },
);
deleteEl.addEventListener('mousedown', () => { deleteEl.addEventListener('mousedown', () => {
saveOnBlur = false; saveOnBlur = false;
@@ -91,10 +95,10 @@ export function TodoCustomList(el) {
e.detail.data.list = list; e.detail.data.list = list;
e.detail.data.key = list.id; e.detail.data.key = list.id;
// update image (default would only be title element) // Update image (default would only be title element).
e.detail.setImage(el); e.detail.setImage(el);
// override for horizontal dragging only // Override for horizontal dragging only.
e.detail.image.addEventListener('draggableDrag', (e) => { e.detail.image.addEventListener('draggableDrag', (e) => {
const x = e.detail.clientX - e.detail.imageX; const x = e.detail.clientX - e.detail.imageX;
const y = e.detail.originY - e.detail.imageY; const y = e.detail.originY - e.detail.imageY;

View File

@@ -133,7 +133,7 @@ export function TodoFrameCustom(el) {
container.children[i].style.height = `${height + 30}px`; container.children[i].style.height = `${height + 30}px`;
} }
// update collapsible on changing heights // Update collapsible on changing heights
el.dispatchEvent(new CustomEvent('collapse', { bubbles: true })); el.dispatchEvent(new CustomEvent('collapse', { bubbles: true }));
} }

View File

@@ -8,6 +8,7 @@ import { formatDateId } from './util.js';
*/ */
export function TodoFrameDays(el) { export function TodoFrameDays(el) {
const RANGE = 14; const RANGE = 14;
let todoData = { let todoData = {
items: [], items: [],
at: formatDateId(new Date()), at: formatDateId(new Date()),

View File

@@ -32,9 +32,13 @@ export function TodoItem(el) {
el.querySelectorAll('.app-icon').forEach(AppIcon); el.querySelectorAll('.app-icon').forEach(AppIcon);
checkboxEl.addEventListener('touchstart', () => { checkboxEl.addEventListener(
'touchstart',
() => {
saveOnBlur = false; saveOnBlur = false;
}); },
{ passive: true },
);
checkboxEl.addEventListener('mousedown', () => { checkboxEl.addEventListener('mousedown', () => {
saveOnBlur = false; saveOnBlur = false;
@@ -62,10 +66,10 @@ export function TodoItem(el) {
inputEl.addEventListener('keyup', (e) => { inputEl.addEventListener('keyup', (e) => {
switch (e.keyCode) { switch (e.keyCode) {
case 13: // enter case 13: // Enter
save(); save();
break; break;
case 27: // escape case 27: // Escape
cancelEdit(); cancelEdit();
break; break;
} }
@@ -100,7 +104,7 @@ export function TodoItem(el) {
const label = inputEl.value.trim(); const label = inputEl.value.trim();
if (label === '') { if (label === '') {
// deferred deletion prevents a bug at reconciliation in TodoList: // Deferred deletion prevents a bug at reconciliation in TodoList:
// Failed to execute 'removeChild' on 'Node': The node to be removed is // Failed to execute 'removeChild' on 'Node': The node to be removed is
// no longer a child of this node. Perhaps it was moved in a 'blur' // no longer a child of this node. Perhaps it was moved in a 'blur'
// event handler? // event handler?
@@ -137,8 +141,6 @@ export function TodoItem(el) {
} }
function update() { function update() {
// TODO optimize
el.classList.toggle('-done', item.done); el.classList.toggle('-done', item.done);
checkboxEl.querySelector('input').checked = item.done; checkboxEl.querySelector('input').checked = item.done;
labelEl.innerText = item.label; labelEl.innerText = item.label;

View File

@@ -18,10 +18,10 @@ export function TodoItemInput(el) {
inputEl.addEventListener('keyup', (e) => { inputEl.addEventListener('keyup', (e) => {
switch (e.keyCode) { switch (e.keyCode) {
case 13: // enter case 13: // Enter
save(); save();
break; break;
case 27: // escape case 27: // Escape
clear(); clear();
break; break;
} }