From 8c375ec5177ea8df714cd606ec1fe481676f6843 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Fri, 23 Jun 2023 23:27:10 -0500 Subject: [PATCH] Init --- index.css | 13 ++++++ index.html | 57 +++++++++++++++++++++++++ index.js | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 index.css create mode 100644 index.html create mode 100644 index.js diff --git a/index.css b/index.css new file mode 100644 index 0000000..58a8d0b --- /dev/null +++ b/index.css @@ -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); +} diff --git a/index.html b/index.html new file mode 100644 index 0000000..25d408c --- /dev/null +++ b/index.html @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + +
+

To-Do List

+ +
+ +
+ +
+
+
+
+ +
+
+ + + +
+ +
+
+
+
+ + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..fb662ed --- /dev/null +++ b/index.js @@ -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 = ` + + + + `; + 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); \ No newline at end of file