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

Assignment-(Todo List) by Amanveer Singh #54

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
170 changes: 126 additions & 44 deletions spa/index.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,132 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<style>
.striked {
<html>
<head>
<style type="text/css">
body {
margin: 10px auto;
min-width: 250px;
max-width: 50%;
background-color: silver;
}
* {
box-sizing: border-box;
}
ul {
margin: 0;
padding: 0;
}
ul li {
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
list-style-type: none;
background: #eee;
font-size: 18px;
transition: 0.2s;
/* make the list items unselectable */
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Set zebra-stripes: all odd list items to a different color */
ul li:nth-child(odd) {
background: #f9f9f9;
}
ul li:hover {
background: #ddd;
}
ul li.checked {
background: teal;
color: white;
text-decoration: line-through;
}
ul > li:hover {
ul li.checked::before {
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
/* Style the close button */
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
}
.close:hover {
background-color: crimson;
color: white;
}
.header {
background-color: crimson;
padding: 30px 40px;
color: white;
text-align: center;
}
.header:after {
content: "";
display: table;
clear: both;
}
input {
margin: 0;
border: none;
border-radius: 0;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
.addBtn {
padding: 10px;
width: 25%;
background: #d9d9d9;
color: #555;
float: left;
text-align: center;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
border-radius: 0;
}
</style>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-8 form-group">
<input class="form-control" id="newTodo" type="text" placeholder="Today i must...">
<small class="form-text text-muted">Add a new todo</small>
</div>
<div class="col-4">
<div class="flex-row justify-content-center align-items-center">
<button class="btn btn-primary mt-1 mt-md-0" onclick="addTodo()">ADD +</button>
<button class="btn btn-secondary mt-1 mt-md-0" >Reset</button>
<button class="btn btn-danger mt-1" onclick="removeTodos()">Bin</button>
</div>
</div>
</div>
<div class="row mt-2">
<div class="col-10 offset-1">
<ul class="list-group">
</ul>
</div>

.addBtn:hover {
background-color: #bbb;
}
#clear-list{
width: 30%;
font-weight: bold;
font-style: italic;
font-size: xx-large;
font-family: "Times New Roman";
margin: 40px 35%;
background-color: darkblue;
color: white;
}
#clear-list:hover{
background-color: blue;
color: white;
}
</style>
</head>
<body>
<div class="header">
<h2 style="margin:5px">AMANVEER's To Do List</h2>
<input type="text" id="myInput" placeholder="Add the Todo...">
<span onclick="newElement()" class="addBtn">Add</span>
</div>


</div>


<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="index.js" defer></script>
</body>
</html>
<ul id="myUL">
<li class="checked">Wake Up at 6 am</li>
<li>Have Breakfast </li>
<li class="checked">Leave for Work</li>
</ul>
<button type="button" id="clear-list" onclick="removeAll()">Clear All Items</button>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
113 changes: 50 additions & 63 deletions spa/index.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,54 @@
let todos = [
{
id: 1,
name: "Teach Class at Nagarro",
done: true
},
{
id: 2,
name: "Get Coffee",
done: false
}
];

function render(state) {
return state
.map(todo => {
// const li = document.createElement('li')
// li.classList.add("striked")
// document.body.append(li)
const classString = todo.done ? `class = "list-group-item striked"` : `class = "list-group-item"`
return `<li data-todo="${todo.id}" ${classString}> ${todo.name} </li>`;
})
.join("");
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
myNodelist[i].appendChild(span);
}

function paint() {
$("ul").html(render(todos));
// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}

function addTodo() {
// document.getElementById('newTodo') != $('#newTodo')
const inputBox = $('#newTodo')
todos.push({
id: todos.length + 1,
name: inputBox.val(),
done: false
})

inputBox.val('')

paint()
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
}
}, false);
// Create a new list item when clicking on the "Add" button
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}



function removeTodos() {
todos = todos.filter(todo => !todo.done)

paint()
//Clearing the list
function removeAll(){
var lst = document.getElementsByTagName("ul");
lst[0].innerHTML = "";
}


$('ul').on("click", function (e) {
const idToFind = e.target.dataset.todo
const todo = todos.find(todo => todo.id == idToFind)
todo.done = !todo.done

paint()
})

$('#newTodo').on("keypress", function (e) {
if (e.which == 13) {
addTodo()
}
})

paint();