Skip to content
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
130 changes: 130 additions & 0 deletions todo-list-muhamad-firmansyah/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"use strict";
class Todo {
constructor(title) {
this.id = Math.random().toString().split('.')[1];
this.title = title;
this.status = false;
}
set setTitle(title) {
this.title = title;
}
set setStatus(status) {
this.status = status;
}
getTodo() {
const { id, title, status } = this;
return {
id,
title,
status
};
}
}
class Todos {
constructor() {
this.todos = [];
}
addTodos(data) {
this.todos.push(data);
return this.todos;
}
getTodos() {
return this.todos;
}
toggleStatus(id) {
const todos = this.todos.map(i => {
if (i.id === id) {
i.status = !i.status;
}
return i;
});
this.todos = todos;
return true;
}
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id);
return true;
}
}
class Controller extends Todos {
constructor() {
super();
this.todoInput = document.querySelector('#input-title');
this.submitButton = document.querySelector('#button-submit');
this.listTodos = document.querySelector('#list-todos');
this.itemTodo = document.getElementsByClassName('main-text');
this.itemButton = document.getElementsByClassName('action');
this.submit();
this.setListTodos();
this.clickItems();
}
init() {
this.setListTodos();
this.clickItems();
}
clickItems() {
for (let index = 0; index < this.itemTodo.length; index++) {
const element = this.itemTodo[index];
const button = this.itemButton[index];
button.addEventListener('click', this.handlerDelete.bind(this));
element.addEventListener('click', this.handlerItem.bind(this));
}
}
handlerDelete(e) {
e.preventDefault();
const el = e.currentTarget.querySelector('button');
const id = el.getAttribute('data-id');
this.deleteTodo(id);
this.init();
}
handlerItem(e) {
e.preventDefault();
const el = e.currentTarget.querySelector('input');
const id = el.id;
this.toggleStatus(id);
this.init();
}
clearForm() {
this.todoInput.value = '';
}
setListTodos() {
const todos = this.getTodos();
this.listTodos.innerHTML = '';
let htmlElements = '';
todos.forEach((todo) => {
htmlElements += `<li class="item-todo">
<div class="main-text">
<input type="checkbox" name="" id="${todo.id}" ${todo.status && 'checked'}>
<label for="${todo.id}">${todo.title}</label>
</div>
<div class="action">
<button data-id="${todo.id}">delete</button>
</div>
</li>`;
});
this.listTodos.innerHTML = htmlElements;
}
validation(value) {
if (value === '') {
return false;
}
return true;
}
submit() {
this.submitButton.addEventListener('click', this.submitHandler.bind(this));
}
submitHandler(e) {
e.preventDefault();
const title = this.todoInput.value;
if (this.validation(title)) {
const todo = new Todo(title);
this.addTodos(todo.getTodo());
this.clearForm();
this.init();
}
else {
alert("You need to input something there!");
}
}
}
const controller = new Controller;
157 changes: 157 additions & 0 deletions todo-list-muhamad-firmansyah/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List &mdash; Muhamad Firmansyah</title>

<style>
* {
margin: 0;
padding: 0;
font-family: 'Courier New', Courier, monospace;
box-sizing: border-box;
color: white;
}

body {
background-color: rgb(12, 12, 12);
}

.container {
width: 35%;
margin: 40px auto;
border-radius: 8px;
padding: 16px 32px;
}

header {
border-bottom: 1px solid rgb(109, 109, 109);
padding: 12px 0px;
margin-bottom: 32px;
display: flex;
align-items: center;
justify-content: space-between;
}

header button {
border-radius: 50%;
border: none;
color: green;
padding: 12px 16px;
}

.section-form {
margin-bottom: 32px;
}

section form {
display: flex;
gap: 12px;
}

section form input {
width: 100%;
background-color: transparent;
font-size: 18px;
color: green;
border: none;
outline: none;
border: 2px solid rgb(0, 128, 0);
padding: 12px 16px;
}

section form button {
padding: 12px 16px;
font-size: 18px;
color: green;
background-color: transparent;
border: 2px solid green;
outline: none;
cursor: pointer;
}

section ul {
list-style: none;
}

section ul li {
margin: 18px 0px;
}

section ul li .main-text {
display: flex;
align-items: flex-start;
gap: 18px;
font-size: 22px;
}

section ul li .action {
margin-left: 32px;
margin-top: 4px;
}

section ul li .action button {
background-color: transparent;
border: none;
cursor: pointer;
color: green;
opacity: 0.4;
}

section ul li .action button:hover {
opacity: 1;
}

section ul li input[type="checkbox"] {
margin-top: 4px;
transform: scale(1.5);
filter: hue-rotate(240deg)
}

section ul li input[type="checkbox"]:checked~label {
color: green;
text-decoration: line-through;
}

@media screen and (max-width: 1120px) {
.container {
width: 60%;
}
}

@media screen and (max-width: 720px) {
.container {
width: 80%;
}
}

@media screen and (max-width: 420px) {
.container {
width: 90%;
}
}
</style>
</head>

<body>
<main class="container">
<header>
<h1>Todo List</h1>
</header>
<section class="section-form">
<form action="">
<input type="text" id="input-title" required>
<button type="submit" id="button-submit">&#65291;</button>
</form>
</section>
<section>
<ul id="list-todos"></ul>
</section>
</main>
<script src="./dist/index.js"></script>
</body>

</html>
Loading