-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
298 lines (236 loc) · 7.46 KB
/
renderer.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const config = require('config')
var jQuery = require('jquery')
var Swal = require('sweetalert2')
var ko = require('knockout')
var $ = jQuery
var tagreader = require('./tagreader.js')
var data = require('./data.js')
const db = new data.DB(defaultIfUndefined(process.env.SK_MongoDB, config.get('mongo')))
const DEBUG = defaultIfUndefined(process.env.SK_DEBUG, config.get('debug'))
function defaultIfUndefined(vvalue, def){
return vvalue != undefined ? vvalue : def
}
//var logoutTimeoutHandler = null
//################################################################
// View model
//################################################################
function ViewModel(){
this.uid = ko.observable(undefined)
this.credit = ko.observable(undefined)
this.footerText = ko.observable("Scan RFID-Tag to login")
}
var viewModel = new ViewModel();
exports.viewModel = viewModel;
ko.applyBindings(viewModel);
//################################################################
// Inputs
//################################################################
// set what to do when a tag is read
tagreader.setOnTagReadCallback(loginByUid)
async function loginByUid(uid) {
try {
var account = await db.getAccountByTagID(uid)
} catch (err) {
await Swal({
position: 'top-end',
type: 'error',
title: err,
showConfirmButton: false,
toast: true,
timer: 2000
})
return
}
if (account == null){
await db.createAccount(uid)
await Swal({
title: "Account not found. Creating new one!",
type: "error",
timer: 4000
})
await Swal({
title: "Created account. Please retry!",
type: "info",
timer: 2000
})
return
}
onLogin(account)
}
function logoutKeyListener(ev){
if (ev.key == "/"){
var event = new Event('logout')
window.dispatchEvent(event)
}
}
function addCreditKeyListener(ev){
if (ev.key == "+"){
onAddCredit()
}
}
function removeCreditKeyListener(ev){
if (ev.key == "-"){
onRemoveCredit()
}
}
function debugLoginListener(ev){
if (ev.key == "l"){
onDebugLogin()
}
}
//################################################################
// User interface
//################################################################
function loggedInState(){
emptyCartState()
$("#vspaceone_logo > g > path").css({
fill: "rgb(8, 160, 89)",
transition: "400ms"
})
$('#list').css({
display: "block"
})
$("header").css({
"background-color": "rgb(8, 160, 89)",
transition: "400ms"
})
window.addEventListener('keyup', logoutKeyListener, true)
window.addEventListener('keyup', removeCreditKeyListener, true)
window.addEventListener('keyup', addCreditKeyListener, true)
}
function emptyCartState(){
viewModel.footerText("/ to logout | - to remove credit | + to add credit")
}
function loggedOutState(){
viewModel.footerText("Scan RFID-Tag to login")
$("#vspaceone_logo > g > path").css({
fill: "rgb(111, 121, 144)",
transition: "400ms"
})
$('#list').css({
display: "none"
})
$("header").css({
"background-color": "rgb(71, 78, 93)",
transition: "400ms"
})
window.removeEventListener('keyup', logoutKeyListener)
window.removeEventListener('keyup', removeCreditKeyListener)
window.removeEventListener('keyup', addCreditKeyListener)
}
//################################################################
// Callbacks
//################################################################
// just creating functions instead of event handlers seems to work better if data needs to be passed
function onLogin(account){
console.log(account)
/*logoutTimeoutHandler = setTimeout(function(){
var event = new Event('logout')
window.dispatchEvent(event)
},10000)*/
updateAccountInfo(account)
loggedInState()
}
function updateAccountInfo(account){
viewModel.uid(account.tagID)
viewModel.credit(getEurosFromCent(account.credit) + '€')
}
window.addEventListener('logout', function(){
viewModel.uid(undefined)
viewModel.credit(undefined)
loggedOutState()
});
window.addEventListener('keyup', debugLoginListener, true)
/*window.addEventListener('keyup', function(){
clearTimeout(logoutTimeoutHandler)
logoutTimeoutHandler = setTimeout(function(){
var event = new Event('logout')
window.dispatchEvent(event)
},20000)
}, true)*/
/**
* Shows debug login dialog circumventing the need for a tag reader
*/
async function onDebugLogin(){
if (DEBUG){
var {value : id} = await Swal({
title: 'DEBUG: Enter ID',
type: 'warning',
showConfirmButton: true,
input: 'text',
inputPlaceholder: 'ID',
inputAttributes: {
autocapitalize: 'off',
autocorrect: 'off'
}
})
loginByUid(id)
}
}
async function onAddCredit(){
var {value : amount} = await Swal({
title: 'Enter amount to add',
type: 'question',
showConfirmButton: true,
input: 'number',
inputPlaceholder: 'Amount',
inputAttributes: {
autocapitalize: 'off',
autocorrect: 'off'
}
})
amount = Number.parseInt(amount.replaceWith(",", "."))
var centAmount = getCentsFromEuros(amount);
if (centAmount >= 0 && !(isNaN(centAmount))){ // sane bounds check to prevent nonsens like NaN and negative numbers
updateAccountInfo(await db.updateCredit(viewModel.uid(), centAmount))
}
}
async function onRemoveCredit(){
var {value : amount} = await Swal({
title: 'Enter amount to remove',
type: 'question',
showConfirmButton: true,
input: 'number',
inputPlaceholder: 'Amount',
inputAttributes: {
autocapitalize: 'off',
autocorrect: 'off'
}
})
amount = Number.parseInt(amount.replaceWith(",", "."))
var centAmount = getCentsFromEuros(amount);
if (centAmount >= 0 && !(isNaN(centAmount))){ // sane bounds check to prevent nonsens like NaN and negative numbers
updateAccountInfo(await db.updateCredit(viewModel.uid(), -centAmount))
}
}
async function getCentsFromEuros(amount){
return amount*100;
}
async function getEurosFromCent(amount){
return amount/100;
}
//################################################################
// SVG Replacer
//################################################################
jQuery('img.svg').each(function() {
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
jQuery.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = jQuery(data).find('svg');
// Add replaced image's ID to the new SVG
if (typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if (typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass + ' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});