forked from CEOS-Developers/vanilla-todo-20th
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from psst54/feature/class-definitions
Feature/class definitions: define subject / task class
- Loading branch information
Showing
8 changed files
with
149 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const OPEN = 'open'; | ||
const DONE = 'done'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
console.log('작성 시작'); | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const taskViewModel = new TaskViewModel(); | ||
const subjectViewModel = new SubjectViewModel(taskViewModel); | ||
|
||
const openSubject1 = new Subject('Opened Subject 1', OPEN); | ||
taskViewModel.addTask('task 1', openSubject1.getId()); | ||
taskViewModel.addTask('task 2', openSubject1.getId()); | ||
subjectViewModel.addSubject(openSubject1); | ||
|
||
const openSubject2 = new Subject('Opened Subject 2', OPEN); | ||
taskViewModel.addTask('task 1', openSubject2.getId()); | ||
subjectViewModel.addSubject(openSubject2); | ||
|
||
const doneSubject1 = new Subject('Done Subject 1', DONE); | ||
taskViewModel.addTask('task 1', doneSubject1.getId()); | ||
taskViewModel.addTask('task 2', doneSubject1.getId()); | ||
subjectViewModel.addSubject(doneSubject1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Subject { | ||
constructor(title = 'New Subject', state = OPEN) { | ||
this.title = title; | ||
this.taskList = []; | ||
this.state = state; | ||
this.id = Math.random().toString(36).substring(2); // [todo] make random id function | ||
} | ||
|
||
addTask(task) { | ||
this.taskList.push(task); | ||
} | ||
|
||
getTitle() { | ||
return this.title; | ||
} | ||
|
||
getState() { | ||
return this.state; | ||
} | ||
|
||
getTaskList() { | ||
return this.taskList; | ||
} | ||
|
||
getId() { | ||
return this.id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Task { | ||
constructor(title, subjectId) { | ||
this.title = title; | ||
this.subjectId = subjectId; | ||
} | ||
|
||
getTitle() { | ||
return this.title; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class SubjectViewModel { | ||
constructor(taskViewModel) { | ||
this.subjectList = []; | ||
this.taskViewModel = taskViewModel; | ||
} | ||
|
||
addSubject(subject) { | ||
this.subjectList.push(subject); | ||
this.render(); | ||
} | ||
|
||
render() { | ||
const columnList = [OPEN, DONE]; | ||
|
||
columnList.forEach((column) => { | ||
const subjectListElement = document.getElementById( | ||
`${column}-subject-list` | ||
); | ||
subjectListElement.innerHTML = ''; | ||
|
||
this.subjectList.forEach((subject) => { | ||
const subjectId = subject.getId(); | ||
const taskListElementId = `${subjectId}-task-list`; | ||
if (subject.getState() !== column) return; | ||
|
||
const subjectElement = document.createElement('li'); | ||
subjectElement.classList.add('card'); | ||
subjectElement.innerHTML = ` | ||
<header> | ||
<h3>${subject.getTitle()}</h3> | ||
<header> | ||
<main> | ||
<ol id=${taskListElementId}> | ||
</ol> | ||
</main> | ||
`; | ||
subjectListElement.appendChild(subjectElement); | ||
|
||
const taskListElement = document.getElementById(taskListElementId); | ||
this.renderTasks(subjectId, taskListElement); | ||
}); | ||
}); | ||
} | ||
|
||
renderTasks(subjectId, taskListElement) { | ||
this.taskViewModel.getTasksBySubject(subjectId).forEach((task) => { | ||
const taskElement = document.createElement('li'); | ||
taskElement.className = 'task'; | ||
const checkboxElement = document.createElement('input'); | ||
checkboxElement.type = 'checkbox'; | ||
const titleElement = document.createElement('p'); | ||
titleElement.innerText = task.getTitle(); | ||
|
||
taskElement.appendChild(checkboxElement); | ||
taskElement.appendChild(titleElement); | ||
taskListElement.appendChild(taskElement); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class TaskViewModel { | ||
constructor() { | ||
this.taskList = new Map(); | ||
} | ||
|
||
addTask(title, subjectId) { | ||
const task = new Task(title, subjectId); | ||
if (!this.taskList.has(subjectId)) { | ||
this.taskList.set(subjectId, []); | ||
} | ||
this.taskList.get(subjectId).push(task); | ||
} | ||
|
||
getTasksBySubject(subjectId) { | ||
return this.taskList.get(subjectId) || []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters