forked from dominictarr/crdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.js
71 lines (61 loc) · 1.71 KB
/
debug.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
function findOrCreate(row) {
var id = row.id || row
//handle if an html element has been passed
if(row instanceof HTMLElement) return row
if(id)
el = document.getElementById(id)
if(!el) {
var el = document.createElement('div')
el.setAttribute('id', id)
}
el.classList.add('row')
return el
}
function css () {
return ' \
.row { \
border: solid 1px gray;\
margin: 4px; \
width: 400px \
} \
.row .key { \
min-width: 75px; \
display: inline-block; \
} \
.row .value { \
margin: 2px; \
display: inline-block; \
} ';
}
function render (row) {
var html = ''//'<div class=row id="' row.id + '">\n'
var o = row.toJSON()
for(var k in o) {
html += '<div class=pair>'
html += ' <div class=key id="'+ row.id +':key:'+ k +'">' + k + '</div>\n'
html += ' <div class=value id="'+ row.id +':value:'+ k +'">' + o[k] + '</div>\n'
html += '</div>'
}
return html
}
//would be good to refactor this out...
//and make it installable as a middleware...
//hmm, not that simple because will need:
//html, js, websocket to crdt documents.
//probably best to be something that can be
//added as an element to a page, for now.
module.exports = function (id, doc) {
if(!doc)
doc = id, id = 'crdt_debugger'
var parent = findOrCreate(id)
var style = document.createElement('style')
style.innerText = css()
document.head.appendChild(style)
function update (row) {
var el = findOrCreate(row)
el.innerHTML = render(row)
if(parent !== el.parentNode)
parent.appendChild(el)
}
doc.on('row_update', update)
}