-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
91 lines (79 loc) · 2.29 KB
/
index.js
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
var electron = require('electron')
var remote = require('remote')
var Client = require('electron-rpc/client')
var yo = require('yo-yo')
var extend = require('xtend')
var header = require('./components/header.js')
var body = require('./components/body.js')
var footer = require('./components/footer.js')
var client = new Client()
var previous = [] // experimental
var state = {
view: 'loading',
dats: []
}
var container = render()
document.body.appendChild(container)
listDats()
function onaction (action, params) {
if (!action || action.length === 0) return
if (action === 'home') return update({view: 'home'})
if (action === 'add') return chooseFiles()
if (action === 'settings') return update({view: 'settings'})
if (action === 'detail') {
var selected = state.dats[params.id]
if (!selected) return listDats('home')
params.dat = selected
return update({view: 'detail', params: params})
}
if (action === 'back') return update(previous[previous.length - 1]) // experimental
if (action === 'copy') return copy(params)
if (action === 'delete') return rpc('delete', params)
if (action === 'stop') return rpc('stop', params)
if (action === 'start') return rpc('start', params)
console.error('Unknown action:', action)
}
window.onaction = onaction // for debugging
function onerror (err) {
throw err
}
function render () {
return yo`<div id="app">
${header(state, onaction)}
${body(state, onaction)}
${footer(state, onaction)}
</div>`
}
function update (newState) {
if (newState) {
previous.push(state)
state = extend(state, newState)
}
yo.update(container, render())
}
function chooseFiles () {
remote.dialog.showOpenDialog({properties: ['openDirectory']}, function (directories) {
if (!directories.length) return
rpc('link', {dir: directories[0]})
})
}
function rpc (action, params) {
client.request(action, params, function (err, dat) {
if (err) return onerror(err)
listDats()
})
}
function listDats () {
client.request('status', function (err, status) {
if (err) return onerror(err)
update({view: 'home', dats: status.dats})
})
}
function copy (link) {
electron.clipboard.writeText(link)
var message = {
title: 'Dat',
message: 'A link has been copied to your clipboard.'
}
client.request('notify', message)
}