aboutsummaryrefslogtreecommitdiff
path: root/render.js
blob: 50064e8685c1d451a997ba15dfd97d833c9393d1 (plain)
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
function renderMessage (post) {
  var messageDiv = h('messageDiv', {id: post.key})
  var message = h('div', {classList: 'message'})

  var pubkey = nacl.util.decodeBase64(post.author.substring(1))
  var sig = nacl.util.decodeBase64(post.signature)
  post.content = JSON.parse(nacl.util.encodeUTF8(nacl.sign.open(sig, pubkey)))


  if (post.content.type == 'name') {
    var mini = h('span', [
      ' identified as ',
      post.content.name
    ])

    message.appendChild(getHeader(post, mini))

    messageDiv.appendChild(message)
  }


  if (post.content.type == 'post') {

    localforage.getItem('log', function (err, log) {
      if (log) {
        for (var i = log.length - 1; i >= 0; --i) {
         
          if (log[i].content.reply == post.key) {
            var nextPost = log[i]
            var messageExists = (document.getElementById(nextPost.key) !== null);
            if (!messageExists) {
              messageDiv.appendChild(h('div', {classList: 'submessage'}, [
                renderMessage(nextPost)
              ]))
            }
          }
        }
      }
    })
  
    var renderer = new marked.Renderer();
    renderer.link = function(href, title, text) {
        if ((href[0] == '@') || (href[0] == '%')) {
          href = '#' + href
        }
        var link = marked.Renderer.prototype.link.call(this, href, title, text);
        return link
    }
    
    marked.setOptions({
        renderer: renderer
    });

    message.appendChild(getHeader(post))
    
    if (post.content.reply) {
      message.appendChild(h('span', [
        're: ',
        h('a', {href: '#' + post.content.reply}, [post.content.reply.substring(0, 10) + '...'])
      ]))
    }

    message.appendChild(h('div', {innerHTML: marked(post.content.text)}))

    message.appendChild(h('span', {id: post.key + 'src', classList: 'right'}, [
      h('a', {
        onclick: function () {
          message.appendChild(h('pre', [JSON.stringify(post)]))
          var span = document.getElementById(post.key + 'src') 
          span.parentNode.removeChild(span)
        }
      }, ['[src]'])
    ]))

    var gotName = getName(post.content.author)

    localforage.getItem('id', function (err, keys) {

      var publishButton = h('button', {
        onclick: function () {
          if (textarea.value) {
            var content = {
              author: keys.publicKey,
              type: 'post',
              text: textarea.value,
              reply: post.key,
              timestamp: Date.now()
            }
            publish(content, keys)
            message.removeChild(textarea)
            message.removeChild(publishButton)
          }
        }
      }, ['Publish'])

      var textarea = h('textarea', {placeholder: 'Reply to this bog post'}, ['['+ gotName.textContent + '](' + post.content.author + ')'])

      var replyButton = h('button', {
        classList: 'replyButton:' + post.key,
        onclick: function () {
          message.removeChild(replyButton)
          message.appendChild(textarea)
          message.appendChild(publishButton)
          
        }
      }, ['Reply'])

      message.appendChild(replyButton)
    })

    messageDiv.appendChild(message)
  }

  return messageDiv
}

function getHeader (post, mini) {
  var inner 
  if (mini) {
    var inner = mini
  }

  var head = h('span', [
    h('a', {href: '#' + post.key}, [
      h('p', {classList: 'right'}, [human(new Date(post.content.timestamp))]),
    ]),
    h('p', [
      h('a', {href: '#' + post.content.author}, [
        getName(post.content.author)
      ]),
      inner
    ])
  ])
  return head
}