This commit is contained in:
oitsjustjose 2023-06-23 23:27:10 -05:00
commit 8c375ec517
3 changed files with 190 additions and 0 deletions

13
index.css Normal file
View file

@ -0,0 +1,13 @@
body {
font-family: "m5x7", Helvetica, Arial, sans-serif;
}
.svg-icon {
width: 1.25em;
height: 1.25em;
}
.svg-icon path,
.svg-icon polygon,
.svg-icon rect {
fill: var(--light);
}

57
index.html Normal file
View file

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./index.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="container">
<h1 class="text-center">To-Do List</h1>
<div id="checklist"></div>
<hr>
<form action="#" id="form">
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" aria-label="Checkbox for following text input" id="new-item-done">
</div>
</div>
<input type="text" class="form-control" aria-label="Text input with checkbox"
placeholder="Insert your new checklist item and (optionally) mark it as done!" id="new-item-name">
<div class="input-group-append">
<button class="btn btn-success" type="submit">
<svg class="svg-icon" viewBox="0 0 20 20">
<path
d="M17.064,4.656l-2.05-2.035C14.936,2.544,14.831,2.5,14.721,2.5H3.854c-0.229,0-0.417,0.188-0.417,0.417v14.167c0,0.229,0.188,0.417,0.417,0.417h12.917c0.229,0,0.416-0.188,0.416-0.417V4.952C17.188,4.84,17.144,4.733,17.064,4.656M6.354,3.333h7.917V10H6.354V3.333z M16.354,16.667H4.271V3.333h1.25v7.083c0,0.229,0.188,0.417,0.417,0.417h8.75c0.229,0,0.416-0.188,0.416-0.417V3.886l1.25,1.239V16.667z M13.402,4.688v3.958c0,0.229-0.186,0.417-0.417,0.417c-0.229,0-0.417-0.188-0.417-0.417V4.688c0-0.229,0.188-0.417,0.417-0.417C13.217,4.271,13.402,4.458,13.402,4.688">
</path>
</svg>
</button>
</div>
</div>
</form>
</div>
</body>
</html>

120
index.js Normal file
View file

@ -0,0 +1,120 @@
const key = "checklist-items";
const objs = JSON.parse(window.localStorage.getItem(key));
let currentId = 0;
const save = () => {
window.localStorage.setItem(key, JSON.stringify(objs));
};
const updateChecklistItem = (id, name, done) => {
objs[id] = { name, done };
};
const deleteChecklistItem = (id) => {
delete objs[id];
save();
};
const onChange = () => {
save();
render();
};
const render = () => {
const checklistEl = document.getElementById("checklist");
checklistEl.innerHTML = "";
Object.keys(objs).forEach(id => {
const { name, done } = objs[id];
const inputGroup = document.createElement('div');
inputGroup.className = 'input-group mb-3';
/* Prepend (for the checkbox) */
const inputPrepend = document.createElement('div');
inputPrepend.className = 'input-group-prepend';
const inputPrependGroupText = document.createElement('div');
inputPrependGroupText.className = 'input-group-text';
/* Checkbox and input together so they can read each other's vals on change */
const checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.ariaLabel = "Checkbox for following text input";
checkbox.checked = done;
inputPrependGroupText.appendChild(checkbox);
inputPrepend.appendChild(inputPrependGroupText);
const input = document.createElement('input');
input.type = "text";
input.className = "form-control";
input.ariaLabel = "Text input with checkbox";
input.value = name;
input.style.textDecoration = done ? 'line-through' : 'none';
input.addEventListener('input', _ => {
updateChecklistItem(id, input.value, checkbox.checked);
save();
});
checkbox.addEventListener('change', _ => {
updateChecklistItem(id, input.value, checkbox.checked);
if (checkbox.checked) {
input.style.textDecoration = 'line-through';
} else {
input.style.textDecoration = 'none';
}
save();
});
/* The delete button */
const inputGroupAppend = document.createElement('div');
inputGroupAppend.className = 'input-group-append';
const btn = document.createElement('button');
btn.className = "btn btn-danger";
btn.type = 'button';
btn.addEventListener('click', _ => {
deleteChecklistItem(id);
onChange();
});
btn.innerHTML = `
<svg class="svg-icon" viewBox="0 0 20 20">
<path d="M17.114,3.923h-4.589V2.427c0-0.252-0.207-0.459-0.46-0.459H7.935c-0.252,0-0.459,0.207-0.459,0.459v1.496h-4.59c-0.252,0-0.459,0.205-0.459,0.459c0,0.252,0.207,0.459,0.459,0.459h1.51v12.732c0,0.252,0.207,0.459,0.459,0.459h10.29c0.254,0,0.459-0.207,0.459-0.459V4.841h1.511c0.252,0,0.459-0.207,0.459-0.459C17.573,4.127,17.366,3.923,17.114,3.923M8.394,2.886h3.214v0.918H8.394V2.886z M14.686,17.114H5.314V4.841h9.372V17.114z M12.525,7.306v7.344c0,0.252-0.207,0.459-0.46,0.459s-0.458-0.207-0.458-0.459V7.306c0-0.254,0.205-0.459,0.458-0.459S12.525,7.051,12.525,7.306M8.394,7.306v7.344c0,0.252-0.207,0.459-0.459,0.459s-0.459-0.207-0.459-0.459V7.306c0-0.254,0.207-0.459,0.459-0.459S8.394,7.051,8.394,7.306"></path>
</svg>
`;
inputGroupAppend.appendChild(btn);
inputGroup.appendChild(inputPrepend);
inputGroup.appendChild(input);
inputGroup.appendChild(inputGroupAppend);
checklistEl.appendChild(inputGroup);
});
};
const load = () => {
if (objs) {
try {
currentId = Math.max(Object.keys(objs));
} catch (ex) {
alert(`Failed to load saved checklist: ${ex}`);
}
}
document.getElementById('form').addEventListener('submit', evt => {
evt.preventDefault();
evt.stopPropagation();
currentId += 1;
objs[currentId] = {
name: document.getElementById('new-item-name').value,
done: document.getElementById('new-item-done').checked
};
onChange();
document.getElementById('new-item-name').value = '';
document.getElementById('new-item-done').checked = false;
});
render();
};
window.addEventListener('load', load);