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

Belajar Typescript 2 : Membuat Aplikasi Todo list (Agung Hidayat) #2

Open
wants to merge 2 commits into
base: main
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
28 changes: 28 additions & 0 deletions todo-list-agung-hidayat/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>

<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; Agung Hidayat</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<div class="container">
<div class="wrapper">
<div id="myDIV" class="header">
<h2 style="margin:15px">To Do List</h2>
<input type="text" id="myInput" placeholder="Title...">
<span class="addBtn">Add</span>
</div>
<ul id="myUL">
</ul>
</div>
</div>

<script src="index.js"></script>
</body>

</html>
115 changes: 115 additions & 0 deletions todo-list-agung-hidayat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"use strict";
// Models
class Todo {
constructor() {
this.title = "";
this.status = false;
this.id = String(Math.floor(Math.random() * (9000 + 1)));
}
set setTodo(title) {
this.title = title;
this.id = String(Math.floor(Math.random() * (9000 + 1)));
}
get getTodo() {
return { id: this.id, title: this.title, status: this.status };
}
}
class Todos extends Todo {
constructor() {
super();
this.todo = [];
}
get myTodos() {
return this.todo;
}
set addTodo(todo) {
this.todo.push(todo);
}
set toggleStatus(id) {
console.log(id);
this.todo = this.todo.map((item) => {
if (item.id === id) {
item.status = !item.status;
}
return item;
});
}
set deleteTodo(id) {
this.todo = this.todo.filter((item) => item.id !== id);
}
}
// Controller
class Controller extends Todos {
constructor() {
super();
this.elementParent = document.querySelector("ul");
this.close = document.getElementsByClassName("close");
this.addButton = document.querySelector(".addBtn");
this.list = document.querySelector("ul");
this.init();
this.initEventListener();
}
init() {
this.initElement();
this.initCloseButton();
}
newElement() {
var li = document.createElement("li");
var input = document.getElementById("myInput");
var inputValue = input.value;
if (inputValue === "") {
alert("You must write something!");
}
else {
this.setTodo = inputValue;
this.addTodo = this.getTodo;
input.value = "";
this.init();
}
}
initElement() {
while (this.elementParent.firstChild) {
this.elementParent.removeChild(this.elementParent.firstChild);
}
for (let i = 0; i < this.myTodos.length; i++) {
const todo = this.myTodos[i].title;
const status = this.myTodos[i].status ? "checked" : "";
var li = document.createElement("li");
this.title = document.createTextNode(todo);
li.className = status;
li.setAttribute('data-id', String(this.myTodos[i].id));
li.appendChild(this.title);
var span = document.createElement("SPAN");
this.txt = document.createTextNode("\u00D7");
span.className = "close";
span.setAttribute('data-id', String(this.myTodos[i].id));
span.appendChild(this.txt);
li.appendChild(span);
this.elementParent.appendChild(li);
}
}
initCloseButton() {
for (let i = 0; i < this.close.length; i++) {
this.close[i].addEventListener("click", (ev) => {
this.deleteTodo = ev.target.getAttribute('data-id');
this.init();
});
}
}
initEventListener() {
this.list.addEventListener("click", (event) => {
this.toggleStatus = event.target.getAttribute('data-id');
this.init();
});
this.addButton.addEventListener("click", () => {
this.newElement();
});
var input = document.getElementById("myInput");
input.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
this.newElement();
}
});
}
}
const controller = new Controller();
163 changes: 163 additions & 0 deletions todo-list-agung-hidayat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Types
interface ITodo {
id: string,
title: string,
status: boolean,
}

interface ITodos {
todo: ITodo[]
}


// Models
class Todo implements ITodo {
id: string
title: string
status: boolean

constructor(){
this.title = ""
this.status = false
this.id = String(Math.floor(Math.random() * (9000 + 1)))
}

set setTodo(title: string){
this.title = title
this.id = String(Math.floor(Math.random() * (9000 + 1)))
}

get getTodo(){
return {id: this.id, title: this.title, status: this.status}
}
}

class Todos extends Todo implements ITodos {
todo: ITodo[]

constructor(){
super()
this.todo = []
}

get myTodos(){
return this.todo
}

set addTodo(todo: ITodo){
this.todo.push(todo)
}

set toggleStatus(id: any){
console.log(id)
this.todo = this.todo.map((item) => {
if(item.id === id){
item.status = !item.status
}

return item
})
}

set deleteTodo(id: string){
this.todo = this.todo.filter((item) => item.id !== id)
}
}

// Controller
class Controller extends Todos {
elementParent: HTMLUListElement
title: any
txt: any

close: any
addButton: HTMLButtonElement
list: HTMLUListElement

constructor() {
super()
this.elementParent = document.querySelector("ul") as HTMLUListElement;
this.close = document.getElementsByClassName("close");
this.addButton = document.querySelector(".addBtn") as HTMLButtonElement;
this.list = document.querySelector("ul") as HTMLUListElement;

this.init()
this.initEventListener()
}

init(){
this.initElement()
this.initCloseButton()
}

newElement() {
var li = document.createElement("li");
var input: HTMLInputElement = document.getElementById("myInput") as HTMLInputElement;
var inputValue: string = input.value;
if (inputValue === "") {
alert("You must write something!");
}
else {
this.setTodo = inputValue
this.addTodo = this.getTodo
input.value = ""
this.init()
}
}

initElement(){
while (this.elementParent.firstChild) {
this.elementParent.removeChild(this.elementParent.firstChild);
}

for (let i = 0; i < this.myTodos.length; i++) {
const todo: string = this.myTodos[i].title;
const status: string = this.myTodos[i].status ? "checked" : "";

var li: HTMLLIElement = document.createElement("li");
this.title = document.createTextNode(todo);
li.className = status;
li.setAttribute('data-id', String(this.myTodos[i].id));
li.appendChild(this.title);

var span = document.createElement("SPAN");
this.txt = document.createTextNode("\u00D7");
span.className = "close";
span.setAttribute('data-id', String(this.myTodos[i].id));
span.appendChild(this.txt);
li.appendChild(span);

this.elementParent.appendChild(li);
}
}

initCloseButton(){
for (let i = 0; i < this.close.length; i++) {
this.close[i].addEventListener("click", (ev: any ): void => {
this.deleteTodo = ev.target.getAttribute('data-id')
this.init()
})
}
}

initEventListener(){
this.list.addEventListener("click", (event: any) : void => {
this.toggleStatus = event.target.getAttribute('data-id')
this.init()
});

this.addButton.addEventListener("click", (): void => {
this.newElement()
})

var input: HTMLInputElement = document.getElementById("myInput") as HTMLInputElement;
input.addEventListener("keyup", (event: { keyCode: number}): void => {

if(event.keyCode === 13){
this.newElement()
}
})
}
}

const controller = new Controller()
Loading