-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.js
143 lines (124 loc) · 4.31 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import NewTask from './new-task.js'
import dataKeeper from './data-keeper.js'
class Render {
constructor(data) {
this.data = data || {}
this.panelMountPoint = document.getElementById('js-panels')
}
dragstartHandler(event) {
event.dataTransfer.setData('plain/text', event.target.dataset.taskId)
event.dataTransfer.dropEffect = 'move'
}
onDragOverHandler(event) {
event.preventDefault()
event.dataTransfer.dropEffect = 'move'
}
onDropHandler(event) {
event.preventDefault()
const panelName = event.target.dataset.panelName
if(!panelName) return false
const taskId = event.dataTransfer.getData('plain/text')
this.updateDataInPanel(panelName, taskId)
this.updateInterface()
}
findTaskById(data, id) {
for(let [key, value] of Object.entries(data)) {
const {tasks} = value
const task = tasks.find(task => task.id === parseInt(id, 10))
if(task) {
return task
}
}
return null
}
removeTaskById(data, id) {
for(let [key, value] of Object.entries(data)) {
const {tasks} = value
const taskIndex = tasks.findIndex(task => task.id === parseInt(id, 10))
if(taskIndex > -1) {
let newTasks = {...tasks}
newTasks = [...tasks.slice(0, taskIndex), ...tasks.slice(taskIndex+1)]
value.tasks = newTasks
}
}
return data
}
addTaskToPanel(data, panelName, task) {
const panelData = data[panelName]
panelData.tasks.push(task)
return data
}
updateDataInPanel(panelName, taskId) {
const task = this.findTaskById(this.data, taskId)
this.data = this.removeTaskById(this.data, taskId)
this.data = this.addTaskToPanel(this.data, panelName, task)
dataKeeper.set(this.data)
}
generateTaskCards(tasks) {
if(!tasks || tasks.length === 0) return ''
return tasks.map((task) => {
const {id, title, description} = task
return `
<section data-task-id="${id}" class="tasks-card" draggable="true" ondragstart="dragstartHandler(event)">
<header>
<h3 class="tasks-card__header_text">${title}</h3>
</header>
<article class="tasks-card__description">
<p class="tasks-card__text">${description}</p>
</article>
</section>
`
}).join(' ')
}
generatePanelValues({panelName, panelData}) {
const {name, tasks} = panelData
return `
<section class="list" id='js-${panelName}' data-panel-name="${panelName}" ondragover="onDragOverHandler(event)" ondrop="onDropHandler(event)">
<header class="list__header">
<h2 class="list__header_text">${name}</h2>
</header>
<article>
${this.generateTaskCards(tasks)}
</article>
</section>
`
}
/**
* create panels of `to-do`, `in-progress` & `done`
*/
createPanels() {
if(this.panelMountPoint) {
let panels = []
for(let [key, value] of Object.entries(this.data)) {
const generatedPanel = this.generatePanelValues({panelName: key, panelData: value})
panels.push(generatedPanel)
}
this.panelMountPoint.insertAdjacentHTML('beforeend', panels.join(' '))
}
}
destroyPanels() {
if(this.panelMountPoint) {
while(this.panelMountPoint.firstChild) {
this.panelMountPoint.removeChild(this.panelMountPoint.firstChild)
}
}
}
updateInterface() {
this.destroyPanels()
this.createPanels()
}
updateComponent(data) {
this.data = data
this.destroyPanels()
this.createPanels()
}
init() {
new NewTask()
dataKeeper.subscribe(this.updateComponent.bind(this))
window.dragstartHandler = this.dragstartHandler
window.onDragOverHandler = this.onDragOverHandler
window.onDropHandler = this.onDropHandler.bind(this)
this.createPanels()
}
}
export default Render