This repository was archived by the owner on Sep 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
181 lines (165 loc) · 4.54 KB
/
Copy pathmain.js
File metadata and controls
181 lines (165 loc) · 4.54 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var mkdirp = require('mkdirp')
var homedir = require('os').homedir()
var path = require('path')
var level = require('level')
var hyperlog = require('hyperlog')
var strftime = require('strftime')
var ipc = require('electron').ipcRenderer
var onend = require('end-of-stream')
var concat = require('concat-stream')
var minimist = require('minimist')
var argv = minimist(process.argv.slice(2), {
default: { configdir: path.join(homedir, '.config/odk-sync') },
alias: { c: 'configdir' }
})
mkdirp.sync(argv.configdir)
var Sync = require('./')
var sync = Sync({
db: level(path.join(argv.configdir, 'log')),
log: hyperlog(level(path.join(argv.configdir, 'index')),
{ valueEncoding: 'json' }),
dir: path.join(argv.configdir, 'blob')
})
window.sync = sync
sync.list({ live: true })
.on('data', addRow)
.on('end', function () {
if (!state.observations) state.observations = []
update()
})
function addRow (row) {
if (!state.observations) state.observations = []
state.observations.push(row)
update()
}
var freader = require('filereader-stream')
var dragDrop = require('drag-drop')
dragDrop(window, function (files, pos) {
state.loading = 'import'
update()
sync.importFiles(files, function (err, docs) {
if (err) return error(err)
notify('imported ' + docs.length + ' reports')
state.loading = null
update()
})
})
var html = require('yo-yo')
var root = document.querySelector('#content')
var state = {
observations: null,
loading: null,
errors: [],
messages: []
}
update(state)
ipc.on('select-import-dir', function (ev, dir) {
if (!dir) return
sync.importDevice(dir, function (errors) {
if (errors.length) return error(errors)
})
})
ipc.on('select-sync-dir', function (ev, dir) {
if (!dir) return
var ex = Sync({
db: level(path.join(dir, 'log')),
log: hyperlog(level(path.join(dir, 'index')),
{ valueEncoding: 'json' }),
dir: path.join(dir, 'blob')
})
var pending = 2
var rex = ex.replicate(function (err) {
if (err) error(err)
if (--pending === 0) done()
})
var r = sync.replicate(function (err) {
if (err) error(err)
if (--pending === 0) done()
})
rex.pipe(r).pipe(rex)
function done () {
console.log('sync complete')
}
})
function render (state) {
var observations = html`<table class="info">
<tr>
<th>form</th>
<th>date</th>
</tr>
${(state.observations || []).map(function (obs) {
var startTime = new Date(obs.info.start.split('.')[0])
return html`<tr>
<td>${obs.info.meta.formId} v${obs.info.meta.version}</td>
<td>${strftime('%F %T', startTime)}</td>
<td><ul>${obs.files.map(function (key) {
return html`<li>
<a onclick=${showPic}>${key.split('-').slice(5).join('-')}</a>
</li>`
function showPic (ev) {
ev.preventDefault()
sync.read(key, function (err, streams) {
if (err) return error(err)
streams[0].on('error', error)
streams[0].pipe(concat(ondata))
})
}
function ondata (buf) {
location.href = 'data:image/png;base64,' + buf.toString('base64')
}
})}</ul></td>
</tr>`
})}
</table>`
return html`<div>
<div class="errors">${state.errors.map(function (err) {
return html`<div class="error">
${err.message || err}
<button class="close" onclick=${closeError}>x</button>
</div>`
function closeError () {
var ix = state.errors.indexOf(err)
if (ix >= 0) state.errors.splice(ix, 1)
update()
}
})}</div>
<div class="messages">${state.messages.map(function (msg) {
return html`<div class="message">
${msg}
<button class="close" onclick=${closeMsg}>x</button>
</div>`
function closeMsg () {
var ix = state.messages.indexOf(msg)
if (ix >= 0) state.messages.splice(ix, 1)
update()
}
})}</div>
<table class="title">
<tr>
<td><h1>observations</h1></td>
<td class="import">
<button onclick=${syncOdk}>sync</button>
</td>
</tr>
</table>
<div>
${observations}
</div>
</div>`
function syncOdk (ev) {
ipc.send('open-sync-dir')
}
}
function update () {
html.update(root, render(state))
}
function error (errors) {
if (!Array.isArray(errors)) errors = [errors]
state.errors.push.apply(state.errors, errors)
update()
}
function notify (msgs) {
if (!Array.isArray(msgs)) msgs = [msgs]
state.messages.push.apply(state.messages, msgs)
update()
}