aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: b1c3d91c769f02468eb5e0d982e7891eb8dcbddc (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
var screen = h('div', {id: 'screen'})
document.body.appendChild(screen)

function composer (keys, reply, gotName) {
  var messageDiv = h('div')
  var message = h('div', {classList: 'message'})

  if (gotName) {
    console.log(gotName.textContent)
    var textarea = h('textarea', ['[' + gotName.textContent + '](' + reply.author + ')'])
  } else {

    var textarea = h('textarea', {placeholder: 'Write a new bog post...'})
  }



  var publisher = h('div', [
    textarea,
    h('button', {
      onclick: function () {
        if (textarea.value) {
          var content = {
            type: 'post',
            text: textarea.value
          }
          if (reply) {
            content.reply = reply.key
          }

          publish(content, keys, {preview: true}).then(post => {
            open(post).then(msg => {
              var preview = render(msg, keys, {preview: true})
              var cache = messageDiv.firstChild
              messageDiv.appendChild(preview)
              messageDiv.removeChild(messageDiv.firstChild)
              preview.firstChild.appendChild(h('button', {
                onclick: function () {
                  messageDiv.removeChild(messageDiv.firstChild)
                  messageDiv.appendChild(cache)
                }
              }, ['Cancel']))
              preview.firstChild.appendChild(h('button', {
                onclick: function () {
                  publish(content, keys).then(post => {
                    open(post).then(msg => {
                      textarea.value = ''
                      if (reply) {
                        messageDiv.removeChild(messageDiv.firstChild)
                      }
                      if (messageDiv.firstChild) {
                        messageDiv.removeChild(messageDiv.firstChild)
                        messageDiv = h('div')
                        messageDiv.appendChild(cache)
                        scroller.insertBefore(messageDiv, scroller.childNodes[2])
                        scroller.insertBefore(render(msg, keys), scroller.childNodes[3])
                      } else {
                        messageDiv.appendChild(render(msg, keys))
                      }
                    })
                  }) 
                }
              }, ['Publish']))
            })
          })
        }
      }
    }, ['Preview'])
  ])

  message.appendChild(publisher)
  messageDiv.appendChild(message)
  return messageDiv
}

function route (keys) {
  src = window.location.hash.substring(1)

  var scroller = h('div', {id: 'scroller'})
  var screen = document.getElementById('screen')
  screen.appendChild(scroller)

  if (src === 'key') {
    keyPage(keys)
  } else if (src === 'pubs') {
    pubs()
  } else if (src[0] === '@') {
    profilePage(src, keys)
  } else if (src[0] === '%') {
    threadPage(src, keys)
  } else {
    publicPage(keys)
  }
}

keys().then(key => { 

  var navbar = h('div', {classList: 'navbar'}, [
    h('div', {classList: 'internal'}, [
      h('li', [h('a', {href: '#'}, ['Home'])]),
      h('li', [h('a', {href: '#' + key.publicKey}, [getName(key.publicKey)])]),
      h('li', [h('a', {href: '#key'}, ['Key'])]),
      h('li', [h('a', {href: '#pubs'}, ['Pubs'])]),
      h('li', {classList: 'right'}, [h('a', {href: 'http://github.com/bogbook/bog/'}, ['Git'])])
    ])
  ])
  document.body.appendChild(navbar)

  route(key)
})

window.onhashchange = function () {
  keys().then(key => {
    var oldscreen = document.getElementById('screen')
    var newscreen = h('div', {id: 'screen'})
    oldscreen.parentNode.replaceChild(newscreen, oldscreen)

    route(key)
  })
}