Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
app-todo
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Geovanny E. Vera Pazmino
app-todo
Commits
413e57b8
Commit
413e57b8
authored
Apr 07, 2020
by
Milan Iliev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extracted TaskController and DetailView
parent
548e89e4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
89 deletions
+94
-89
main.js
web/main.js
+6
-89
controller.js
web/tasks/controller.js
+42
-0
detail_view.js
web/tasks/detail_view.js
+46
-0
No files found.
web/main.js
View file @
413e57b8
import
Task
from
"./tasks/task.js"
import
Task
from
"./tasks/task.js"
import
Task
View
from
"./tasks/view
.js"
import
Task
Controller
from
"./tasks/controller
.js"
class
TaskController
{
let
task_controller
=
new
TaskController
()
constructor
(){
this
.
tasks
=
[]
}
addTask
(
task
){
let
view
=
new
TaskView
(
task
)
view
.
element
.
addEventListener
(
"click"
,
()
=>
detail_view
.
task
=
task
)
list
.
appendChild
(
view
.
element
)
task_views
.
push
(
view
)
task
.
addEventListener
(
"change"
,
()
=>
this
.
save
(
task
))
}
async
refresh
(){
let
tasks_response
=
await
fetch
(
"/tasks"
,
{
method
:
"GET"
})
let
tasks_data
=
await
tasks_response
.
json
()
for
(
let
task_data
of
tasks_data
){
this
.
tasks
.
push
(
new
Task
(
task_data
))
}
for
(
let
task
of
this
.
tasks
){
this
.
addTask
(
task
)
}
}
async
save
(
task
){
let
tasks_response
=
await
fetch
(
`/tasks/
${
task
.
id
}
`
,
{
method
:
"PUT"
,
body
:
JSON
.
stringify
(
task
.
data
),
headers
:
{
'Content-Type'
:
'application/json'
}
})
}
}
let
task_views
=
[]
let
list
=
document
.
querySelector
(
"task-list"
)
class
DetailTaskView
{
constructor
(
element
){
this
.
element
=
element
this
.
fields
=
{
done
:
this
.
element
.
querySelector
(
"[name=done]"
),
description
:
this
.
element
.
querySelector
(
"[name=description]"
)
}
this
.
fields
.
description
.
addEventListener
(
"input"
,
()
=>
{
this
.
task
.
description
=
this
.
fields
.
description
.
value
})
this
.
fields
.
done
.
addEventListener
(
"change"
,
()
=>
{
this
.
task
.
done
=
this
.
fields
.
done
.
checked
})
this
.
deleteButton
=
this
.
element
.
querySelector
(
"#delete_button"
)
this
.
deleteButton
.
addEventListener
(
"click"
,
()
=>
{
let
summary_view_for_deleted_task
=
task_views
.
find
((
summary_view
)
=>
summary_view
.
task
===
this
.
task
)
task_views
.
remove
(
summary_view_for_deleted_task
)
tasks
.
remove
(
this
.
task
)
list
.
removeChild
(
summary_view_for_deleted_task
.
element
)
})
}
get
task
(){
return
this
.
_task
}
set
task
(
new_task
){
this
.
_task
=
new_task
this
.
_task
.
addEventListener
(
"change"
,
()
=>
this
.
update
())
this
.
update
()
}
update
()
{
this
.
fields
.
done
.
checked
=
this
.
task
.
done
this
.
fields
.
description
.
value
=
this
.
task
.
description
}
}
let
detail_view
=
new
DetailTaskView
(
document
.
querySelector
(
"task-detail"
))
let
addButton
=
document
.
querySelector
(
'#add_button'
)
let
addButton
=
document
.
querySelector
(
'#add_button'
)
addButton
.
addEventListener
(
"click"
,
()
=>
{
addTask
(
new
Task
({
done
:
false
,
description
:
"New Task"
}))
})
let
task_controller
=
new
TaskController
()
let
main
=
async
()
=>
{
let
main
=
async
()
=>
{
addButton
.
addEventListener
(
"click"
,
()
=>
{
task_controller
.
addTask
(
new
Task
({
done
:
false
,
description
:
"New Task"
}))
})
task_controller
.
refresh
()
task_controller
.
refresh
()
}
}
...
...
web/tasks/controller.js
0 → 100644
View file @
413e57b8
import
Task
from
"./task.js"
import
TaskView
from
"./view.js"
import
DetailTaskView
from
"./detail_view.js"
class
TaskController
{
constructor
(){
this
.
tasks
=
[]
this
.
task_views
=
[]
this
.
list_element
=
document
.
querySelector
(
"task-list"
)
this
.
detail_view
=
new
DetailTaskView
(
document
.
querySelector
(
"task-detail"
))
}
addTask
(
task
){
let
view
=
new
TaskView
(
task
)
view
.
element
.
addEventListener
(
"click"
,
()
=>
this
.
detail_view
.
task
=
task
)
this
.
list_element
.
appendChild
(
view
.
element
)
this
.
task_views
.
push
(
view
)
task
.
addEventListener
(
"change"
,
()
=>
this
.
save
(
task
))
}
async
refresh
(){
let
tasks_response
=
await
fetch
(
"/tasks"
,
{
method
:
"GET"
})
let
tasks_data
=
await
tasks_response
.
json
()
for
(
let
task_data
of
tasks_data
){
this
.
tasks
.
push
(
new
Task
(
task_data
))
}
for
(
let
task
of
this
.
tasks
){
this
.
addTask
(
task
)
}
}
async
save
(
task
){
let
tasks_response
=
await
fetch
(
`/tasks/
${
task
.
id
}
`
,
{
method
:
"PUT"
,
body
:
JSON
.
stringify
(
task
.
data
),
headers
:
{
'Content-Type'
:
'application/json'
}
})
}
}
export
default
TaskController
\ No newline at end of file
web/tasks/detail_view.js
0 → 100644
View file @
413e57b8
class
DetailTaskView
{
constructor
(
element
){
this
.
element
=
element
this
.
fields
=
{
done
:
this
.
element
.
querySelector
(
"[name=done]"
),
description
:
this
.
element
.
querySelector
(
"[name=description]"
)
}
this
.
fields
.
description
.
addEventListener
(
"input"
,
()
=>
{
this
.
task
.
description
=
this
.
fields
.
description
.
value
})
this
.
fields
.
done
.
addEventListener
(
"change"
,
()
=>
{
this
.
task
.
done
=
this
.
fields
.
done
.
checked
})
this
.
deleteButton
=
this
.
element
.
querySelector
(
"#delete_button"
)
this
.
deleteButton
.
addEventListener
(
"click"
,
()
=>
{
let
summary_view_for_deleted_task
=
task_views
.
find
((
summary_view
)
=>
summary_view
.
task
===
this
.
task
)
task_views
.
remove
(
summary_view_for_deleted_task
)
tasks
.
remove
(
this
.
task
)
list
.
removeChild
(
summary_view_for_deleted_task
.
element
)
})
}
get
task
(){
return
this
.
_task
}
set
task
(
new_task
){
this
.
_task
=
new_task
this
.
_task
.
addEventListener
(
"change"
,
()
=>
this
.
update
())
this
.
update
()
}
update
()
{
this
.
fields
.
done
.
checked
=
this
.
task
.
done
this
.
fields
.
description
.
value
=
this
.
task
.
description
}
}
export
default
DetailTaskView
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment