render.jsView |
---|
2 | 2 | var pull = require('pull-stream') |
3 | 3 | var human = require('human-time') |
4 | 4 | |
5 | 5 | var sbot = require('./scuttlebot') |
6 | | -var composer = require('./compose') |
7 | 6 | var tools = require('./tools') |
8 | 7 | |
9 | 8 | var config = require('./config')() |
10 | 9 | var id = require('./keys').id |
11 | 10 | |
| 11 | +var mime = require('simple-mime')('application/octect-stream') |
| 12 | +var split = require('split-buffer') |
| 13 | + |
| 14 | +function file_input (onAdded) { |
| 15 | + return h('label.btn', 'Upload file', |
| 16 | + h('input', { type: 'file', hidden: true, |
| 17 | + onchange: function (ev) { |
| 18 | + var file = ev.target.files[0] |
| 19 | + if (!file) return |
| 20 | + var reader = new FileReader() |
| 21 | + reader.onload = function () { |
| 22 | + pull( |
| 23 | + pull.values(split(new Buffer(reader.result), 64*1024)), |
| 24 | + sbot.addblob(function (err, blob) { |
| 25 | + if(err) return console.error(err) |
| 26 | + onAdded({ |
| 27 | + link: blob, |
| 28 | + name: file.name, |
| 29 | + size: reader.result.length || reader.result.byteLength, |
| 30 | + type: mime(file.name) |
| 31 | + }) |
| 32 | + }) |
| 33 | + ) |
| 34 | + } |
| 35 | + reader.readAsArrayBuffer(file) |
| 36 | + } |
| 37 | + })) |
| 38 | +} |
| 39 | + |
| 40 | +function composeButtons (msg, opts) { |
| 41 | + var files = [] |
| 42 | + var filesById = {} |
| 43 | + |
| 44 | + var buttons = h('div.controls') |
| 45 | + |
| 46 | + var previewBtn = h('button.btn', 'Preview', { |
| 47 | + onclick: function () { |
| 48 | + var draft = {} |
| 49 | + draft.value = { |
| 50 | + "author": id, |
| 51 | + "content": { |
| 52 | + "type": opts.type, |
| 53 | + "root": opts.root |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (opts.original) |
| 58 | + msg.value.content.original = opts.original |
| 59 | + if (opts.updated) |
| 60 | + msg.value.content.updated = opts.updated |
| 61 | + |
| 62 | + draft.value.content.text = textarea.value |
| 63 | + console.log(draft) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + |
| 68 | + onclick: function () { |
| 69 | + |
| 70 | + } |
| 71 | + })*/ |
| 72 | + |
| 73 | + buttons.appendChild(previewBtn) |
| 74 | + |
| 75 | + buttons.appendChild( |
| 76 | + file_input(function (file) { |
| 77 | + files.push(file) |
| 78 | + filesById[file.link] = file |
| 79 | + var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
| 80 | + textarea.value += embed + '['+file.name+']('+file.link+')' |
| 81 | + }) |
| 82 | + ) |
| 83 | + return buttons |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +function defaultButtons (msg, reply, edit) { |
| 89 | + |
| 90 | + var buttons = h('div.controls') |
| 91 | + |
| 92 | + var replyBtn = h('button.btn', 'Reply', { |
| 93 | + onclick: function () { |
| 94 | + var textarea = h('textarea.compose', {placeholder: 'Reply to this message'}) |
| 95 | + var reply = h('div.reply', |
| 96 | + textarea, |
| 97 | + composeButtons(msg, reply) |
| 98 | + ) |
| 99 | + |
| 100 | + buttons.parentNode.replaceChild(reply, buttons) |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + var editBtn = h('button.btn', 'Edit', { |
| 105 | + onclick: function () { |
| 106 | + var textarea = h('textarea.compose', edit.messageText) |
| 107 | + var editor = h('div.edit', |
| 108 | + textarea, |
| 109 | + composeButtons(msg, edit) |
| 110 | + ) |
| 111 | + |
| 112 | + var prevMessage = buttons.parentNode.childNodes[2] |
| 113 | + buttons.parentNode.replaceChild(editor, prevMessage) |
| 114 | + buttons.parentNode.removeChild(buttons) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + buttons.appendChild(replyBtn) |
| 119 | + |
| 120 | + if (msg.value.author == id) |
| 121 | + buttons.appendChild(editBtn) |
| 122 | + |
| 123 | + return buttons |
| 124 | +} |
| 125 | + |
| 126 | + |
12 | 127 | module.exports = function (msg) { |
13 | 128 | var edit = {} |
14 | 129 | var reply = {} |
15 | 130 | |
16 | 131 | var message = h('div.message') |
17 | 132 | |
18 | 133 | if (msg.value.content.type == 'post') { |
19 | | - |
20 | 134 | reply.type = 'post' |
21 | 135 | reply.branch = msg.key |
22 | 136 | |
23 | 137 | if (msg.value.content.root) |
49 | 163 | h('span.timestamp', 'Edited: ', h('a', {href: '#' + update.key}, human(new Date(update.value.timestamp)))) |
50 | 164 | ) |
51 | 165 | var num = message.childNodes.length |
52 | 166 | if (msg.value.author == id) |
53 | | - var act = num - 3 |
| 167 | + var act = num - 2 |
54 | 168 | else |
55 | 169 | var act = num - 2 |
56 | 170 | message.replaceChild(latest, message.childNodes[act]) |
57 | 171 | edit.messageText = update.value.content.text |
58 | 172 | edit.original = msg.value.content.original |
59 | 173 | }) |
60 | 174 | ) |
61 | | - |
62 | | - message.appendChild(h('button.btn', 'Reply', { |
63 | | - onclick: function () { |
64 | | - var compose = composer(reply) |
65 | | - message.replaceChild(compose, message.lastElementChild) |
66 | | - } |
67 | | - })) |
68 | | - if (msg.value.author == id) |
69 | | - message.appendChild(h('button.btn', 'Edit', { |
70 | | - onclick: function () { |
71 | | - var compose = h('div.message', composer(edit)) |
72 | | - message.parentNode.replaceChild(compose, message) |
73 | | - } |
74 | | - })) |
| 175 | + message.appendChild(defaultButtons(msg, reply, edit)) |
75 | 176 | return message |
76 | 177 | |
77 | 178 | } else if (msg.value.content.type == 'vote') { |
78 | 179 | message.appendChild(tools.header(msg)) |