Skip to content

Commit

Permalink
Refactor modules to use classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Re committed Apr 14, 2022
1 parent 116bfba commit 8893677
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 62 deletions.
8 changes: 7 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
<body>
<header>TODOs</header>
<aside>
Projects
<h1>Projects</h1>
<input type="text" name="projectsInputField" id="projectsInputField">
<button class="addProjectButton">+</button>
<ul class="projectList">

</ul>
</aside>
<main>
<input type="text" name="todoInputField" id="todoInputField">
<button class="addTodoButton">+</button>
<div class="content">
<h1>Todos</h1>
<table>
<thead>
<tr>
Expand Down
10 changes: 5 additions & 5 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
<body>
<header>TODOs</header>
<aside>
Projects
<h1>Projects</h1>
<input type="text" name="projectsInputField" id="projectsInputField">
<button class="addProjectButton">+</button>
<ul class="projectList">

</ul>
</aside>
<main>
<input type="text" name="todoInputField" id="todoInputField">
<button class="addTodoButton">+</button>
<div class="content">
<h1>Todos</h1>
<table>
<thead>
<tr>
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import css from "./style.css";
import '@fortawesome/fontawesome-free/js/fontawesome'
import '@fortawesome/fontawesome-free/js/solid'
import {listenForAdd} from "./modules/controller";
import {Controller} from "./modules/controller";


window.addEventListener("load", () => {
listenForAdd();
const controller = new Controller();
controller.listenForProjectAdd();
controller.listenForTodoAdd();
controller.listenForProjects();
});
57 changes: 47 additions & 10 deletions src/modules/controller.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
import {addTodo} from "./model.js";
import {display} from "./view.js";
import { View } from "./view";
import { Model } from "./model.js";

let todoInputField = document.querySelector("#todoInputField");
const addButton = document.querySelector(".addTodoButton");
class Controller {
model;
view;
todoInputField;
addTodoButton;
projectInputField;
addProjectButton;
currentProjectId;
allCurrentProjects;

function listenForAdd() {
console.log(addButton);
addButton.addEventListener("click", () => {
let todoTitle = todoInputField.value;
display(addTodo(todoTitle));
constructor() {
this.model = new Model();
this.view = new View();
this.todoInputField = document.querySelector("#todoInputField");
this.addTodoButton = document.querySelector(".addTodoButton");
this.projectInputField = document.querySelector("#projectsInputField");
this.addProjectButton = document.querySelector(".addProjectButton");
this.currentProjectId = 0;
this.allCurrentProjects = document.querySelector("li");
}

listenForProjects() {
this.allCurrentProjects?.forEach((project) => {
project.addEventListener("click", (e) => {
this.currentProjectId = e.target.getAttribute("id");
});
});
}

listenForProjectAdd() {
this.addProjectButton.addEventListener("click", () => {
let projectTitle = this.projectInputField.value;
this.model.addProject(projectTitle);
this.view.displayProjects(this.model.getProjects());
});
}

listenForTodoAdd() {
this.addTodoButton.addEventListener("click", () => {
let todoTitle = this.todoInputField.value;
this.model.addTodoToProject(this.currentProjectId);
this.view.displayTodos(
this.model.getProject(this.currentProjectId).getTodos()
);
});
}
}

export {listenForAdd};
export { Controller };
60 changes: 40 additions & 20 deletions src/modules/entities.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Todo {
title;
description;
dueDate;
priority;
due;
prio;

constructor(title, description, dueDate, priority) {
constructor(title, due, prio) {
this.title = title;
this.description = description;
this.dueDate = dueDate;
this.priority = priority;
this.due = due;
this.prio = prio;
}

setTitle(title) {
Expand All @@ -19,29 +17,51 @@ class Todo {
return this.title;
}

setDescription(description) {
this.description = description;

setDue(due) {
this.due = due;
}

getDue() {
return this.due;
}

setPriority(prio) {
this.prio = prio;
}

getDescription() {
return this.description;
getPrio() {
return this.prio;
}
}

class Project {
title;
todoList = [];

setDueDate(dueDate) {
this.dueDate = dueDate;
constructor(title){
this.title = title;
}

setTitle(title){
this.title = title;
}

getTitle(){
return this.title;
}

getDueDate() {
return this.dueDate;
addTodo(title,due,prio){
this.todoList.push(new Todo(title,due,prio));
}

setPriority(priority) {
this.priority = priority;
getTodo(id){
return this.todoList[id];
}

getPriority() {
return this.priority;
getTodos(){
return this.todoList;
}
}

export {Todo};
export {Todo, Project};
36 changes: 29 additions & 7 deletions src/modules/model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import {Todo} from "./entities";
import {Project} from "./entities";

const todoList = [];
class Model {
projectList;

constructor(){
const defaultProject = new Project("default");
this.projectList = [defaultProject];
}

addProject(title){
console.log("inside model: add project");
this.projectList.push(new Project(title));
}

getProject(id){
console.log(this.projectList[id]);
return this.projectList[id];
}

getProjects(){
console.log("inside model: get projects");
return this.projectList;
}

addTodoToProject(id, title, due, prio){
this.projectList[id].addTodo(title,due,prio);
}

function addTodo(title, due, prio){
let newTodo = new Todo(title, due, prio);
todoList.push(newTodo);
return todoList;
}

export {addTodo};

export {Model};
Loading

0 comments on commit 8893677

Please sign in to comment.