Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

todo list #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions assets/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ body {
color: #efede0;
}

button, .add {
background-color: #efede0;
border-radius: 8px;
color: #66635a;
}

input[name="write_item"] {
background-color: #ffffff;
border-radius: 8px;
color: #66635a;
width: 200px;
transition: width 2s;
}

header,
footer {
padding: 20px;
Expand Down
110 changes: 110 additions & 0 deletions assets/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

document
.querySelector("ul")
.addEventListener("click", e =>{
if(e.target.tagName === 'LI'){
if(e.metaKey){
e.target.remove();
}
else{
e.target.style = 'text-decoration : line-through';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bug 😢

}
}
});

for(let butt of document.querySelectorAll("button")){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Language!

butt.addEventListener("click", e =>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since each button is an individual controller for a distinct action, we should add event listeners to them individually, not in a loop.

for(let item of document.querySelectorAll("li")){
if(e.target.name === "mark_all"){
item.style = 'text-decoration : line-through';
}
else if(e.target.name === "delete_all" && item.style.textDecoration === 'line-through'){
item.remove();
}
}
});
}


document.getElementById("write_item")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not listen to form's change event and check if form is valid? This opens up the door for new "validation" bugs.

.addEventListener("input", e =>{
if(e.target.value !== "" && e.target.value !== "Write up your todo here"){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value you see is called "placeholder". It's not literally a value.

document.getElementById("add").disabled = false;
}
else{
document.getElementById("add").disabled = true;
}
});

document.getElementById("add")
.addEventListener("click", e =>{
e.preventDefault();
const item = document.createElement("li");
const node = document.createTextNode(document.getElementById("write_item").value);
item.append(node);
document.querySelector("ul").prepend(item);
document.getElementById("write_item").value = "Write up your todo here";
document.getElementById("write_item").style.width = '';
document.getElementById("add").disabled = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a form's .reset() method

});

document.getElementById("write_item")
.addEventListener("focus", e =>{
e.target.style.width = '500px';
});

document.getElementById("write_item")
.addEventListener("blur", e =>{
if(e.target.value === "" || e.target.value === "Write up your todo here"){
e.target.style.width = '';
}
});

document
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reimplement the focus mechanism? Add tabindex=0 to the elements :)

.addEventListener("keydown", e =>{
let list_li = document.querySelectorAll("li");
if(e.keyCode === 9 && list_li.length > 0){ //tab
e.preventDefault();
let index = -1;
for(let i = 0; i < list_li.length; i++){
if(document.activeElement === list_li[i]){
index = i;
}
}
if(index === -1 || index === list_li.length-1){
list_li[0].focus();
}
else{
list_li[index+1].focus();
}
console.log(index);
}
});

document
.querySelector("ul")
.addEventListener("focus", e =>{
if(e.target.tagName === 'LI'){
if(e.target.textDecoration === 'line-through'){
e.target.textDecoration = 'underline line-through';
}
else{
e.target.textDecoration = 'underline';
}
e.target.style.listStyleType = 'circle';
}
});

document
.querySelector("ul")
.addEventListener("blur", e =>{
if(e.target.tagName === 'LI'){
if(e.target.textDecoration === 'underline line-through'){
e.target.textDecoration = 'line-through';
}
else{
e.target.textDecoration = '';
}
e.target.style.listStyleType = 'disc';
}
});
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ <h1>ExCo.</h1>
</header>
<main>
<h2>My Todos List</h2>
<form>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why not use a section? :(

<input type="text" name="write_item" id="write_item" value="Write up your todo here">
<input type="submit" class="add" id="add" value="Add" disabled>
</form>
<ul>
<li>Have morning coffee</li>
<li>Eat lunch</li>
<li>Make awesome things happen</li>
</ul>
<span>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A span instead of a section? A SPAN?!?! 😭

<button type="button" name="mark_all">Mark all as done</button>
<button type="button" name="delete_all">Delete all done</button>
</span>
</main>
<footer>
<small>&copy; Copyright 2058, Example Corporation</small>
Expand Down