Files: 4c19550a991fe099117a720ba1ac9e2b7db1fc6e / views.js
4020 bytesRaw
1 | var pull = require('pull-stream') |
2 | var sbot = require('./scuttlebot') |
3 | var hyperscroll = require('hyperscroll') |
4 | var More = require('pull-more') |
5 | var stream = require('hyperloadmore/stream') |
6 | var h = require('hyperscript') |
7 | var render = require('./render') |
8 | var ref = require('ssb-ref') |
9 | |
10 | var config = require('./config')() |
11 | |
12 | var fs = require('fs') |
13 | |
14 | var compose = require('./compose') |
15 | |
16 | var about = function () { |
17 | var screen = document.getElementById('screen') |
18 | |
19 | var about = require('./about') |
20 | |
21 | var content = h('div.content', about) |
22 | |
23 | screen.appendChild(hyperscroll(content)) |
24 | } |
25 | |
26 | var logStream = function () { |
27 | var content = h('div.content') |
28 | var screen = document.getElementById('screen') |
29 | screen.appendChild(hyperscroll(content)) |
30 | |
31 | function createStream (opts) { |
32 | return pull( |
33 | More(sbot.createLogStream, opts), |
34 | pull.map(function (msg) { |
35 | return render(msg) |
36 | }) |
37 | ) |
38 | } |
39 | |
40 | pull( |
41 | createStream({old: false, limit: 10}), |
42 | stream.top(content) |
43 | ) |
44 | |
45 | pull( |
46 | createStream({reverse: true, live: false, limit: 10}), |
47 | stream.bottom(content) |
48 | ) |
49 | } |
50 | |
51 | var userStream = function (src) { |
52 | var content = h('div.content') |
53 | var screen = document.getElementById('screen') |
54 | screen.appendChild(hyperscroll(content)) |
55 | function createStream (opts) { |
56 | return pull( |
57 | More(sbot.userStream, opts, ['value', 'sequence']), |
58 | pull.map(function (msg) { |
59 | return render(msg) |
60 | }) |
61 | ) |
62 | } |
63 | |
64 | pull( |
65 | createStream({old: false, limit: 10, id: src}), |
66 | stream.top(content) |
67 | ) |
68 | |
69 | pull( |
70 | createStream({reverse: true, live: false, limit: 10, id: src}), |
71 | stream.bottom(content) |
72 | ) |
73 | |
74 | } |
75 | |
76 | var msgThread = function(src) { |
77 | var content = h('div.content') |
78 | var screen = document.getElementById('screen') |
79 | screen.appendChild(hyperscroll(content)) |
80 | sbot.get(src, function (err, data) { |
81 | if (err) {console.log('could not find message') } |
82 | data.value = data |
83 | console.log(data) |
84 | var root = src |
85 | if (data.value.content.root) |
86 | root = data.value.content.root |
87 | sbot.get(root, function (err, data) { |
88 | if (err) { console.log('could not find root')} |
89 | data.value = data |
90 | data.key = root |
91 | content.appendChild(render(data)) |
92 | pull( |
93 | sbot.links({rel: 'root', dest: root, values: true, keys: true, live: true}), |
94 | pull.drain(function (msg) { |
95 | console.log(msg) |
96 | if (msg.value) |
97 | content.appendChild(render(msg)) |
98 | }) |
99 | ) |
100 | }) |
101 | }) |
102 | } |
103 | |
104 | var keyPage = function () { |
105 | var screen = document.getElementById('screen') |
106 | |
107 | var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'}) |
108 | |
109 | var content = h('div.content', |
110 | h('div.message#key', |
111 | h('h1', 'Your Key'), |
112 | h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'}, |
113 | h('button.btn', {onclick: function (e){ |
114 | localStorage[config.caps.shs +'/secret'] = '' |
115 | alert('Your public/private key has been deleted') |
116 | e.preventDefault() |
117 | location.hash = "" |
118 | location.reload() |
119 | }}, 'Delete Key') |
120 | ), |
121 | h('hr'), |
122 | h('form', |
123 | importKey, |
124 | h('button.btn', {onclick: function (e){ |
125 | if(importKey.value) { |
126 | localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ') |
127 | e.preventDefault() |
128 | alert('Your public/private key has been updated') |
129 | } |
130 | location.hash = "" |
131 | location.reload() |
132 | }}, 'Import key'), |
133 | ) |
134 | ) |
135 | ) |
136 | |
137 | screen.appendChild(hyperscroll(content)) |
138 | } |
139 | |
140 | |
141 | function hash () { |
142 | return window.location.hash.substring(1) |
143 | } |
144 | |
145 | module.exports = function () { |
146 | var src = hash() |
147 | |
148 | if (ref.isFeed(src)) { |
149 | userStream(src) |
150 | } else if (ref.isMsg(src)) { |
151 | msgThread(src) |
152 | } else if (src == 'about') { |
153 | about() |
154 | } else if (src == 'key') { |
155 | keyPage() |
156 | } else { |
157 | logStream() |
158 | } |
159 | } |
160 |
Built with git-ssb-web