This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.js
189 lines (158 loc) · 4.62 KB
/
ui.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
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
182
183
184
185
186
187
188
189
/* global Event CustomEvent */
if (!atc) var atc = {}
if (!atc.setup) atc.setup = {}
atc.setup.userInterface = function () {
var dom = {}
function getElements () {
var collection = {}
for (var id of arguments) {
collection[id] = document.getElementById(id)
}
return collection
}
// run when the dom is loaded
function setup (event) {
dom = getElements('username', 'device', 'to',
'compose', 'list', 'view', 'preferences', 'setup',
'explanation')
dom.list.addEventListener('showMessage', showMessage)
dom.list.addEventListener('selected', function (e) {
clearCompose()
populateList()
})
dom.view.addEventListener('reply', function (e) {
send(dom.compose, 'reply', e.detail)
send(dom.compose, 'select')
})
dom.compose.addEventListener('toChanged', updateCompose)
dom.compose.addEventListener('send', sendmail)
dom.preferences.addEventListener('togglePrefer', togglePrefer)
dom.preferences.addEventListener('toggleEnable', toggleEnable)
dom.preferences.addEventListener('setupMessage', setupMessage)
dom.setup.addEventListener('initialSetup', initialSetup)
dom.setup.addEventListener('multiSetup', multiSetup)
dom.setup.addEventListener('multiImport', multiImport)
dom.username.addEventListener('toggled', changeUser)
dom.device.addEventListener('toggled', changeDevice)
initClient()
}
function showMessage (e) {
var message = e.detail.message;
atc.client.decryptMessage(message)
var from_me = (message.from == atc.us.current().name)
send(dom.view, 'show', {
message: message,
from_me: from_me
})
send(dom.view, 'select')
}
function populateList () {
send(dom.list, 'populate', {messages: atc.msgs.messages})
}
function togglePrefer(e) {
atc.client.prefer(e.detail.prefer)
}
function toggleEnable(e) {
if (e.detail.enable) {
send(dom.setup, 'select')
}
else {
atc.client.enable(e.detail.enable)
}
}
function initialSetup(e) {
atc.client.enable(true)
send(dom.preferences, 'select')
}
function multiSetup(e) {
if (atc.client.hasSetupMail()) {
send(dom.setup, 'import')
} else
{
send(dom.setup, 'check')
}
}
function multiImport(e) {
atc.client.importSecretKey()
send(dom.list, 'select')
}
function sendmail (e) {
var msg = {
from: atc.us.current().name,
to: e.detail.to,
subject: e.detail.subject,
body: e.detail.body,
encrypted: e.detail.encrypted,
date: new Date()
}
atc.client.prepareOutgoing(msg)
atc.provider.send(msg)
send(dom.list, 'select')
}
function setupMessage (e) {
var msg = {
from: atc.us.current().name,
to: atc.us.current().name,
subject: 'Autocrypt Setup Message',
body: 'This message allows setting up autocrypt using the setup code',
date: new Date(),
encrypted: false,
setupMessage: true
}
atc.client.prepareOutgoing(msg)
atc.provider.send(msg)
send(dom.list, 'select')
}
function clearCompose () {
send(dom.compose, 'clear')
}
function updateCompose (e) {
send(dom.compose, 'update', {
toggle: atc.client.encryptOptionTo(e.detail.to)
})
}
function toggleEncrypted (e) {
var enc = e.detail
if (enc.checked && atc.client.isEnabled() === false) {
if (confirm('Please only enable Autocrypt on one device.\n\n' +
'Are you sure you want to enable Autocrypt on this device?')) {
atc.client.enable(true)
send(dom.preferences, 'reset', atc.client)
} else {
enc.checked = false
}
}
send(dom.compose, 'set', {
explanation: atc.client.explain(enc) || '',
checked: enc.checked
})
}
function changeDevice () {
atc.dev.next()
send(dom.device, 'select', atc.dev.current())
initClient()
}
function changeUser () {
atc.us.next()
send(dom.username, 'select', atc.us.current())
initClient()
}
function initClient () {
var user = atc.us.current()
var device = atc.dev.current()
atc.client = atc.clients.get(user.id, device.id)
atc.msgs.messages = []
// console.log(atc.msgs.msgStore)
atc.provider.reload(user.id)
send(dom.compose, 'reset', user)
send(dom.preferences, 'reset', atc.client)
send(dom.list, 'select')
}
function send (element, type, detail) {
var e = new CustomEvent(type, { detail: detail || {} })
element.dispatchEvent(e)
}
// push setup in the inits for the DOM ready event
if (!atc.setup.inits) atc.setup.inits = []
atc.setup.inits.push({name: 'setup ui', setup: setup})
}