forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageArea.js
More file actions
378 lines (336 loc) · 11 KB
/
messageArea.js
File metadata and controls
378 lines (336 loc) · 11 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Common code for a discussion are a of messages about something
//
import { icons } from './iconBase'
import * as login from './login/login'
import { solidLogicSingleton } from 'solid-logic'
import ns from './ns'
import * as rdf from 'rdflib' // pull in first avoid cross-refs
import { style } from './style'
import * as utils from './utils'
import * as widgets from './widgets'
const UI = { icons, ns, rdf, style, widgets }
export function messageArea (dom, kb, subject, messageStore, options) {
kb = kb || solidLogicSingleton.store
messageStore = messageStore.doc() // No hash
const ns = UI.ns
const WF = rdf.Namespace('http://www.w3.org/2005/01/wf/flow#')
const DCT = rdf.Namespace('http://purl.org/dc/terms/')
options = options || {}
const newestFirst = !!options.newestFirst
const messageBodyStyle =
'white-space: pre-wrap; width: 90%; font-size:100%; border: 0.07em solid #eee; padding: .2em 0.5em; margin: 0.1em 1em 0.1em 1em;'
// 'font-size: 100%; margin: 0.1em 1em 0.1em 1em; background-color: white; white-space: pre-wrap; padding: 0.1em;'
const div = dom.createElement('div')
// eslint-disable-next-line prefer-const
let messageTable // Shared by initial build and addMessageFromBindings
let me
const updater = solidLogicSingleton.store.updater
const anchor = function (text, term) {
// If there is no link return an element anyway
const a = dom.createElement('a')
if (term && term.uri) {
a.setAttribute('href', term.uri)
a.addEventListener('click', UI.widgets.openHrefInOutlineMode, true)
a.setAttribute('style', 'color: #3B5998; text-decoration: none; ') // font-weight: bold
}
a.textContent = text
return a
}
const mention = function mention (message, style) {
const pre = dom.createElement('pre')
pre.setAttribute('style', style || 'color: grey')
div.appendChild(pre)
pre.appendChild(dom.createTextNode(message))
return pre
}
const announce = {
log: function (message) {
mention(message, 'color: #111;')
},
warn: function (message) {
mention(message, 'color: #880;')
},
error: function (message) {
mention(message, 'color: #800;')
}
}
// Form for a new message
//
const newMessageForm = function () {
const form = dom.createElement('tr')
const lhs = dom.createElement('td')
const middle = dom.createElement('td')
const rhs = dom.createElement('td')
form.appendChild(lhs)
form.appendChild(middle)
form.appendChild(rhs)
form.AJAR_date = '9999-01-01T00:00:00Z' // ISO format for field sort
const sendMessage = function () {
// titlefield.setAttribute('class','pendingedit')
// titlefield.disabled = true
field.setAttribute('class', 'pendingedit')
field.disabled = true
const sts = []
const now = new Date()
const timestamp = '' + now.getTime()
const dateStamp = rdf.term(now)
// http://www.w3schools.com/jsref/jsref_obj_date.asp
const message = kb.sym(messageStore.uri + '#' + 'Msg' + timestamp)
sts.push(
new rdf.Statement(subject, ns.wf('message'), message, messageStore)
)
sts.push(
new rdf.Statement(
message,
ns.sioc('content'),
kb.literal(field.value),
messageStore
)
)
sts.push(
new rdf.Statement(message, DCT('created'), dateStamp, messageStore)
)
if (me) {
sts.push(
new rdf.Statement(message, ns.foaf('maker'), me, messageStore)
)
}
const sendComplete = function (uri, success, body) {
if (!success) {
form.appendChild(
UI.widgets.errorMessageBlock(dom, 'Error writing message: ' + body)
)
} else {
const bindings = {
'?msg': message,
'?content': kb.literal(field.value),
'?date': dateStamp,
'?creator': me
}
renderMessage(bindings, false) // not green
field.value = '' // clear from out for reuse
field.setAttribute('class', '')
field.disabled = false
}
}
updater.update([], sts, sendComplete)
}
form.appendChild(dom.createElement('br'))
let field, sendButton
const turnOnInput = function () {
creatorAndDate(lhs, me, '', null)
field = dom.createElement('textarea')
middle.innerHTML = ''
middle.appendChild(field)
field.rows = 3
// field.cols = 40
field.setAttribute('style', messageBodyStyle + 'background-color: #eef;')
field.addEventListener(
'keyup',
function (e) {
// User preference?
if (e.keyCode === 13) {
if (!e.altKey) {
// Alt-Enter just adds a new line
sendMessage()
}
}
},
false
)
rhs.innerHTML = ''
sendButton = UI.widgets.button(
dom,
UI.icons.iconBase + 'noun_383448.svg',
'Send'
)
sendButton.setAttribute('style', UI.style.buttonStyle + 'float: right;')
sendButton.addEventListener('click', sendMessage, false)
rhs.appendChild(sendButton)
}
const context = { div: middle, dom }
login.ensureLoggedIn(context).then(context => {
me = context.me
turnOnInput()
})
return form
}
function nick (person) {
const s = solidLogicSingleton.store.any(person, UI.ns.foaf('nick'))
if (s) return '' + s.value
return '' + utils.label(person)
}
function creatorAndDate (td1, creator, date, message) {
const nickAnchor = td1.appendChild(anchor(nick(creator), creator))
if (creator.uri) {
solidLogicSingleton.store.fetcher.nowOrWhenFetched(creator.doc(), undefined, function (
_ok,
_body
) {
nickAnchor.textContent = nick(creator)
})
}
td1.appendChild(dom.createElement('br'))
td1.appendChild(anchor(date, message))
}
// ///////////////////////////////////////////////////////////////////////
function syncMessages (about, messageTable) {
const displayed = {}
let ele, ele2
for (ele = messageTable.firstChild; ele; ele = ele.nextSibling) {
if (ele.AJAR_subject) {
displayed[ele.AJAR_subject.uri] = true
}
}
const messages = kb.each(about, ns.wf('message'))
const stored = {}
messages.forEach(function (m) {
stored[m.uri] = true
if (!displayed[m.uri]) {
addMessage(m)
}
})
for (ele = messageTable.firstChild; ele;) {
ele2 = ele.nextSibling
if (ele.AJAR_subject && !stored[ele.AJAR_subject.uri]) {
messageTable.removeChild(ele)
}
ele = ele2
}
}
const deleteMessage = function (message) {
const deletions = kb
.statementsMatching(message)
.concat(kb.statementsMatching(undefined, undefined, message))
updater.update(deletions, [], function (uri, ok, body) {
if (!ok) {
announce.error('Cant delete messages:' + body)
} else {
syncMessages(subject, messageTable)
}
})
}
const addMessage = function (message) {
const bindings = {
'?msg': message,
'?creator': kb.any(message, ns.foaf('maker')),
'?date': kb.any(message, DCT('created')),
'?content': kb.any(message, ns.sioc('content'))
}
renderMessage(bindings, true) // fresh from elsewhere
}
const renderMessage = function (bindings, fresh) {
const creator = bindings['?creator']
const message = bindings['?msg']
const date = bindings['?date']
const content = bindings['?content']
const dateString = date.value
const tr = dom.createElement('tr')
tr.AJAR_date = dateString
tr.AJAR_subject = message
let done = false
for (let ele = messageTable.firstChild; ; ele = ele.nextSibling) {
if (!ele) {
// empty
break
}
if (
(dateString > ele.AJAR_date && newestFirst) ||
(dateString < ele.AJAR_date && !newestFirst)
) {
messageTable.insertBefore(tr, ele)
done = true
break
}
}
if (!done) {
messageTable.appendChild(tr)
}
const td1 = dom.createElement('td')
tr.appendChild(td1)
creatorAndDate(td1, creator, UI.widgets.shortDate(dateString), message)
const td2 = dom.createElement('td')
tr.appendChild(td2)
const pre = dom.createElement('p')
pre.setAttribute(
'style',
messageBodyStyle +
(fresh ? 'background-color: #e8ffe8;' : 'background-color: #white;')
)
td2.appendChild(pre)
pre.textContent = content.value
const td3 = dom.createElement('td')
tr.appendChild(td3)
const delButton = dom.createElement('button')
td3.appendChild(delButton)
delButton.textContent = '-'
tr.setAttribute('class', 'hoverControl') // See tabbedtab.css (sigh global CSS)
delButton.setAttribute('class', 'hoverControlHide')
delButton.setAttribute('style', 'color: red;')
delButton.addEventListener(
'click',
function (_event) {
td3.removeChild(delButton) // Ask -- are you sure?
const cancelButton = dom.createElement('button')
cancelButton.textContent = 'cancel'
td3.appendChild(cancelButton).addEventListener(
'click',
function (_event) {
td3.removeChild(sureButton)
td3.removeChild(cancelButton)
td3.appendChild(delButton)
},
false
)
const sureButton = dom.createElement('button')
sureButton.textContent = 'Delete message'
td3.appendChild(sureButton).addEventListener(
'click',
function (_event) {
td3.removeChild(sureButton)
td3.removeChild(cancelButton)
deleteMessage(message)
},
false
)
},
false
)
}
// Messages with date, author etc
messageTable = dom.createElement('table')
messageTable.fresh = false
div.appendChild(messageTable)
messageTable.setAttribute('style', 'width: 100%;') // fill that div!
const tr = newMessageForm()
if (newestFirst) {
messageTable.insertBefore(tr, messageTable.firstChild) // If newestFirst
} else {
messageTable.appendChild(tr) // not newestFirst
}
let query
// Do this with a live query to pull in messages from web
if (options.query) {
query = options.query
} else {
query = new rdf.Query('Messages')
const v = {} // semicolon needed
const vs = ['msg', 'date', 'creator', 'content']
vs.forEach(function (x) {
query.vars.push((v[x] = rdf.variable(x)))
})
query.pat.add(subject, WF('message'), v.msg)
query.pat.add(v.msg, ns.dct('created'), v.date)
query.pat.add(v.msg, ns.foaf('maker'), v.creator)
query.pat.add(v.msg, ns.sioc('content'), v.content)
}
function doneQuery () {
messageTable.fresh = true // any new are fresh and so will be greenish
}
kb.query(query, renderMessage, undefined, doneQuery)
div.refresh = function () {
syncMessages(subject, messageTable)
}
// syncMessages(subject, messageTable) // no the query will do this async
return div
}