Summary Files Commits Branches @commit @diff @stats
path: root / checklist.html
diff options
context:
space:
mode:
Commitc32d4f578d635ad2227f08df9f26fcd7834283d5 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-01-01 10:55:49 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-01-01 10:55:49 +0200
Files6dcca2faec61da571e0d59753fe2aa9c090db668 /checklist.html
small-web-utilities-c32d4f578d635ad2227f08df9f26fcd7834283d5.tar
small-web-utilities-c32d4f578d635ad2227f08df9f26fcd7834283d5.tar.gz
small-web-utilities-c32d4f578d635ad2227f08df9f26fcd7834283d5.zip
Parent9cc0f70b4eb27c438b87c11287f4deafc6e0360b (diff)
[checklist] Put control buttons on the left and added background color on hovering
1 files changed, 59 insertions, 22 deletions
-rw-r--r--checklist.html81
diff --git a/checklist.html b/checklist.html
index 89173a1..1ef6d73 100644
--- a/checklist.html
+++ b/checklist.html
@@ -14,8 +14,28 @@
}
</style>
<style>
+#lists {
+ position: relative;
+ padding-left: 6em;
+ margin-top: 0.5em;
+}
+
+.list-controls:hover + .list-item, .list-item > input:hover {
+ background-color: lightgray;
+}
+
+.list-item > input:focus {
+ background-color: initial;
+}
+
.list-item {
margin-left: 1em;
+ padding: 0.1em 0;
+}
+
+.list-controls {
+ position: absolute;
+ left: 0;
}
input {
@@ -76,15 +96,28 @@ input {
/* Item functionality */
+ function get_checkBox(container) {
+ return container.children[1].children[0];
+ }
+
+ function get_inputBox(container) {
+ return container.children[1].children[1];
+ }
+
+ function get_children(container) {
+ if (!container) return container;
+ // [].slice.call() is supposedly fastest
+ // https://stackoverflow.com/a/22676083/12036073 http://jsben.ch/h2IFA
+ return [].slice.call(container.children[1].children).slice(2);
+ }
+
function changeCheck(current, check) {
- current.children[0].checked = check;
- if (current.children[1].classList.contains('checked') !== check) {
- current.children[1].classList.toggle('checked');
+ get_checkBox(current).checked = check;
+ if (get_inputBox(current).classList.contains('checked') !== check) {
+ get_inputBox(current).classList.toggle('checked');
}
- for (var i = 6; i < current.children.length; i++) {
- changeCheck(current.children[i], check);
- }
+ get_children(current).forEach((child) => changeCheck(child, check));
}
function changeCheckLists(check) {
@@ -95,7 +128,14 @@ input {
function newBlankItem(initialText, initialWidth, checked) {
var holder = document.createElement('div');
- holder.classList.add('list-item');
+ holder.classList.add('list-container');
+
+ var controls = document.createElement('div');
+ controls.classList.add('list-controls');
+ holder.appendChild(controls);
+ var input = document.createElement('div');
+ input.classList.add('list-item');
+ holder.appendChild(input);
var text = document.createElement('input');
text.classList.add('textlike');
@@ -111,26 +151,26 @@ input {
text.classList.toggle('checked');
};
- holder.appendChild(checkbox);
- holder.appendChild(text);
+ input.appendChild(checkbox);
+ input.appendChild(text);
var add = document.createElement('input');
add.type = 'button';
add.value = '+';
add.classList.add('item-add');
add.onclick = function () {
- holder.appendChild(newBlankItem());
+ input.appendChild(newBlankItem());
}
- holder.appendChild(add);
+ controls.appendChild(add);
var sub = document.createElement('input');
sub.type = 'button';
sub.value = '-';
sub.classList.add('item-remove');
sub.onclick = function () {
- holder.remove();
+ input.remove();
}
- holder.appendChild(sub);
+ controls.appendChild(sub);
var uncheck = document.createElement('input');
uncheck.type = 'button';
@@ -138,7 +178,7 @@ input {
uncheck.onclick = function () {
changeCheck(holder, false);
}
- holder.appendChild(uncheck);
+ controls.appendChild(uncheck);
var check = document.createElement('input');
check.type = 'button';
@@ -146,7 +186,7 @@ input {
check.onclick = function () {
changeCheck(holder, true);
}
- holder.appendChild(check);
+ controls.appendChild(check);
if (initialText) text.value = initialText;
if (initialWidth) text.style.width = initialWidth;
@@ -179,18 +219,15 @@ input {
function stringify(current) {
var str = "";
- var start = 6;
+ var children = get_children(current);
if (!current) {
- current = lists;
- start = 0;
+ children = [].slice.call(lists.children);
}
else {
- str = "\"" + encodeURIComponent(current.children[1].value) + "\"" + current.children[1].style.width + current.children[0].checked;
+ str = "\"" + encodeURIComponent(get_inputBox(current).value) + "\"" + get_inputBox(current).style.width + get_checkBox(current).checked;
}
- for (var i = start; i < current.children.length; i++) {
- str += "-" + stringify(current.children[i]) + "_";
- }
+ children.forEach((child) => str += "-" + stringify(child) + "_");
return str;
}