-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.js
168 lines (134 loc) · 3.69 KB
/
dialog.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const gel = (id) => { return document.getElementById(id) }
const cel = (root = document.body, tag = 'div', cname) => {
const res = document.createElement(tag)
if (!!res) {
if (!!root) {
root.appendChild(res)
}
if (!!cname) {
res.className = cname
}
}
return res
}
const id_gen = (initial = 0) => {
let id = initial
return () => {
return id++
}
}
const uuid = id_gen(42)
const add_comment = (
id = 0,
repl = null,
from = '',
text = ''
) => ({id, repl, from, text})
const post = {
id: null,
from: 'OP',
title: 'My wonderful post!',
text: 'Реализация, конечно, говно, но ключевая идея такова: нахуя строить "лестницы", в диалогах? То есть, когда люди последовательно отвечают друг другу.',
}
const comments = [
/* add_comment(1, null, 'A', 'Kitties are cute'),
add_comment(2, 1, 'B', 'No they are not'),
add_comment(3, 2, 'A', 'Why?'),
add_comment(4, 3, 'B', 'Because'),
add_comment(5, null, 'C', 'So true'),
add_comment(6, null, 'D', 'I have an opinnion of my own'),
add_comment(7, 2, 'E', 'Just like you'),
add_comment(8, 7, 'F', 'Ya\'ll wrong'),
add_comment(9, 2, 'G', 'Yes they are'),
add_comment(10, 9, 'B', 'NO'),
add_comment(11, null, 'OP', 'Well that escalated fast'),
add_comment(12, 2, 'OP', 'Why so grumpy?'),
add_comment(13, 7, 'B', 'NO U'), */
]
const toggle_reply = (show = false) => {
if (show) {
gel('repl').classList.remove('hidden')
} else {
gel('repl').classList.add('hidden')
}
gel('user-name').value = ''
gel('answer-text').value = ''
}
const reply_handler = (target) => {
return () => {
toggle_reply(true)
gel('send-reply').onclick = () => { add_reply(target) }
setTimeout(() => {
gel('user-name').focus()
}, 0)
}
}
const add_reply = (target) => {
const comm = add_comment(
uuid(),
target,
gel('user-name').value,
gel('answer-text').value,
)
comments.push(comm)
build_tree()
toggle_reply(false)
}
const text_block = (body, data) => {
const el = cel(body)
el.className = 'comm'
if (data.title !== undefined) {
const title = cel(el, 'h3')
title.textContent = data.title
}
const auth = cel(el, 'div', 'auth')
auth.textContent = `${data.from}`
if (!!data.repl) {
const answ = cel(auth, 'span', 'repl')
answ.textContent = `is replying to ${data.parent.from}`
}
const cont = cel(el, 'div', 'text')
cont.textContent = data.text
const crud = cel(el, 'div', 'crud')
const repl = cel(crud, 'button')
repl.textContent = 'reply'
repl.onclick = reply_handler(data?.id)
return el
}
const create_comm = (comm, root) => {
const for_me = comm?.parent?.parent?.from === comm.from
const dest = comm?.parent?.answers || root
const block = cel(dest)
block.className = [!for_me && 'branch', for_me && 'resume'].filter(el => !!el).join(' ')
if (!for_me) {
const coll = cel(block)
//coll.textContent = '+'
coll.className = 'column'
}
const body = cel(block)
body.className = ['body'].filter(el => !!el).join(' ')
/* const text = cel(body)
text.className = ['comm'].filter(el => !!el).join(' ')
text.textContent = `${comm.from} > ${comm?.parent?.from} : ${comm.text}` */
const text = text_block(body, comm)
const reps = cel(body)
reps.className = 'rep-box'
comm['answers'] = reps
}
const build_tree = () => {
gel('comm').innerHTML = ''
const root = cel(gel('comm'))
// build tree
// const tree = {root: create_comm(post, root)}
text_block(root, post)
const tree = {root: {answers: root, from: 'OP'}}
comments.forEach(comm => {
tree[comm.id] = comm
comm.parent = !!comm.repl
? tree[comm.repl]
: tree.root
})
comments.forEach(comm => create_comm(comm, root))
console.log(tree)
}
build_tree()