-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.html
491 lines (437 loc) · 18.4 KB
/
config.html
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<!DOCTYPE html>
<html lang="en">
<head>
<title>POS Lite</title>
<link rel="icon" type="image/png" sizes="16x16"
href="https://cdn0.iconfinder.com/data/icons/kameleon-free-pack-rounded/110/Shop-16.png">
<link rel="icon" type="image/png" sizes="32x32"
href="https://cdn0.iconfinder.com/data/icons/kameleon-free-pack-rounded/110/Shop-32.png">
<link rel="icon" type="image/png" sizes="96x96"
href="https://cdn0.iconfinder.com/data/icons/kameleon-free-pack-rounded/110/Shop-96.png">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta name="Description" content="POS Lite" />
<meta name="author" content="POS Lite">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<ul class="nav nav-pills justify-content-end p-2" style="width:100%; background-color:aliceblue">
<li class="nav-item">
<a class="nav-link" href="./index.html">Sales</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./product.html">Product</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Config</a>
</li>
</ul>
</div>
<div class="col-lg-12 mt-3">
<h2 class="alert alert-primary text-center">
Configuration
</h2>
</div>
<div class="col-lg-12">
<div class="card">
<div class="card-body p-3">
<div class="row">
<div class="col-lg-4 mb-2">
<div class="row no-gutters">
<div class="col mr-1">
<button type="button" class="btn btn-info btn-block" onclick="onDownloadConfig()">Download
Config</button>
</div>
<div class="col">
<button type="button" class="btn btn-info btn-block" onclick="onUploadConfig()">Upload
Config</button>
</div>
</div>
<div class="input-group-sm mt-2 mb-2" style="display:none" id="config_upload_form">
<input type="file" class="form-control" id="config_file">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" data-toggle="modal"
data-target="#exampleModal">Upload</button>
</div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Do you really want to replace all config?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal"
onclick="onUploadConfigProcess()">Save changes</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4 offset-lg-4 mb-2">
<div class="input-group">
<input id="filter_value" type="text" class="form-control" placeholder="Filter Value"
aria-label="Filter Value" aria-describedby="basic-addon2">
<div class="input-group-append">
<select id="filter_type" class="custom-select" id="inputGroupSelect04">
<option value="default" selected>Choose filter</option>
<option value="key">Key</option>
<option value="value">Value</option>
</select>
<button class="btn btn-info btn-rounded" onclick="onSearchConfig()">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
</div>
</div>
</div>
</div>
<div class="table-responsive">
<table id="datatable" class="table table-sm table-striped mb-0">
<thead>
<tr>
<th>ID</th>
<th>Key</th>
<th>Value</th>
<th style="text-align:center">Operation</th>
</tr>
</thead>
<tbody id="config_list"></tbody>
<tfoot id="new_config" class="bg-info">
<tr>
<td></td>
<td>
<input id="new_config_key" type="text" class="form-control">
</td>
<td>
<input id="new_config_value" type="text" class="form-control">
</td>
<td style="text-align:center"><button type="button" class="btn btn-success btn-rounded btn-sm my-0"
style="border-color:antiquewhite" onclick="onAddConfig()">
<i class="fas fa-plus"></i>
</button></td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<div class="col-lg-12">
<div class="card mt-3 mb-3">
<div class="card-body p-3">
<div>
<button type="button" class="btn btn-info" data-toggle="modal"
data-target="#resetModal">
Factory Reset
</button>
<div class="modal fade" id="resetModal" tabindex="-1" role="dialog"
aria-labelledby="resetModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="resetModalLabel">Warning</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
This action will delete all your data, make sure it's already backup
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal"
onclick="onFactoryResetClicked()">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Custom Alert -->
<div id="customAlert"></div>
</div>
<!-- Import JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="js/localstoragedb.min.js"></script>
<script src="js/localstoragedbcustom.js"></script>
<script src="js/customalert.js"></script>
<!-- Custom JS -->
<script type="text/javascript">
allConfigs() // init page
function allConfigs() {
var listConfig = lsDB.queryAll("config")
if (listConfig.length == 0) {
return
}
configList = ``
listConfig.forEach(function (value) {
var singleDataValues = Object.values(value)
var singleDataKeys = Object.keys(value)
var singleDataRow = ``
singleDataValues.forEach(function (va, i) {
singleDataRow += `<td id="config_${singleDataKeys[i]}_${singleDataValues[0]}">${va}</td>`
})
configList += `
<tr id="config_data_${singleDataValues[0]}">
${singleDataRow}
<td style="text-align:center">
<button class="btn btn-sm btn-secondary" onclick="onEditConfig(${singleDataValues[0]})">
<i class="fa fa-edit"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteConfig(${singleDataValues[0]})">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>`
})
document.getElementById("config_list").innerHTML = configList
}
function onAddConfig() {
var configKey = document.getElementById("new_config_key").value
var configValue = document.getElementById("new_config_value").value
if (configKey == "" || configValue == "") {
alertE("Config key or config Value should not empty")
return
}
var registeredConfig = lsDB.queryAll("config", {
query: { key: configKey }
})
if (registeredConfig.length > 0) {
alertE("Config key already used")
return
}
lsDB.insert("config", {
key: configKey,
value: configValue,
})
lsDB.commit()
allConfigs()
document.getElementById("new_config_value").value = ""
document.getElementById("new_config_key").value = ""
}
function onEditConfig(id) {
var currentConfigID = document.getElementById("config_ID_" + id).innerHTML
var currentConfigKey = document.getElementById("config_key_" + id).innerHTML
var currentConfigValue = document.getElementById("config_value_" + id).innerHTML
document.getElementById("config_data_" + id).innerHTML = `
<td id="config_ID_${id}">${currentConfigID}</td>
<td id="config_key_${id}" contenteditable=true style="border:groove">${currentConfigKey}</td>
<td id="config_value_${id}" contenteditable=true style="border:groove">${currentConfigValue}</td>
<td style="text-align:center">
<button class="btn btn-sm btn-primary" onclick="onSaveConfig(${id})">
<i class="fa fa-save"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteConfig(${id})">
<i class="fas fa-trash"></i>
</button>
</td>`
}
function onSaveConfig(id) {
var changeConfigID = document.getElementById("config_ID_" + id).innerHTML
var changeConfigKey = document.getElementById("config_key_" + id).innerHTML
var changeConfigValue = document.getElementById("config_value_" + id).innerHTML
if (changeConfigKey == "" || changeConfigValue == "") {
alertE("Config key, config name or price should not empty")
return
}
var registeredConfig = lsDB.queryAll("config", {
query: { key: changeConfigKey }
})
if (registeredConfig.length > 0) {
if (registeredConfig[0].config_key == changeConfigKey && registeredConfig[0].ID != id) {
alertE("Config key already used")
return
}
}
document.getElementById("config_data_" + id).innerHTML = `
<td id="config_ID_${id}">${changeConfigID}</td>
<td id="config_key_${id}">${changeConfigKey}</td>
<td id="config_value_${id}">${changeConfigValue}</td>
<td style="text-align:center">
<button class="btn btn-sm btn-secondary" onclick="onEditConfig(${id})">
<i class="fa fa-edit"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteConfig(${id})">
<i class="fas fa-trash"></i>
</button>
</td>`
lsDB.update("config", { ID: id }, function (row) {
row.key = changeConfigKey
row.value = changeConfigValue
return row
})
lsDB.commit()
}
function onDeleteConfig(id) {
lsDB.deleteRows("config", { ID: id })
lsDB.commit()
document.getElementById("config_data_" + id).remove()
}
function onSearchConfig() {
var filterType = document.getElementById("filter_type").value
var filterValue = document.getElementById("filter_value").value
var newQuery = {}
if (filterType == "default") {
document.getElementById("filter_value").value = ""
allConfigs()
return
} else {
newQuery = function (row) {
if (row[filterType].toLowerCase().match(filterValue.toLowerCase())) {
return true
} else {
return false
}
}
}
var listConfig = lsDB.queryAll("config", {
query: newQuery
})
if (listConfig.length == 0) {
document.getElementById("config_list").innerHTML = ""
return
}
configList = ``
listConfig.forEach(function (value) {
var singleDataValues = Object.values(value)
var singleDataKeys = Object.keys(value)
var singleDataRow = ``
singleDataValues.forEach(function (va, i) {
singleDataRow += `<td id="config_${singleDataKeys[i]}_${singleDataValues[0]}">${va}</td>`
})
configList += `
<tr id="config_data_${singleDataValues[0]}">
${singleDataRow}
<td style="text-align:center">
<button class="btn btn-sm btn-danger" onclick="onDeleteConfig(${singleDataValues[0]})">
<i class="fas fa-trash"></i>
</button>
<button class="btn btn-sm btn-success" onclick="onEditConfig(${singleDataValues[0]})">
<i class="fa fa-edit"></i>
</button>
</td>
</tr>`
})
document.getElementById("config_list").innerHTML = configList
}
function onDownloadConfig() {
var listConfig = lsDB.queryAll("config")
if (listConfig.length == 0) {
return
}
var header = Object.keys(listConfig[0]).join(",") + "\n"
var configCSV = ""
listConfig.forEach(function (value, i) {
var { ID, ...valueWithoutID } = value
if (i == 0) { // first iteration
configCSV += Object.keys(valueWithoutID).join(",") + "\n"
configCSV += Object.values(valueWithoutID).join(",") + "\n"
}
else if (i + 1 == listConfig.length) { // last iteration
configCSV += Object.values(valueWithoutID).join(",")
} else {
configCSV += Object.values(valueWithoutID).join(",") + "\n"
}
})
var csv_file, download_link
csv_file = new Blob([configCSV], { type: "text/csv" })
download_link = document.createElement("a")
download_link.download = "config.csv"
download_link.href = window.URL.createObjectURL(csv_file)
download_link.style.display = "none"
document.body.appendChild(download_link)
download_link.click()
}
function onUploadConfig() {
var formUploadConfig = document.getElementById("config_upload_form")
if (formUploadConfig.style.display === "none") {
formUploadConfig.style.display = "flex"
} else {
formUploadConfig.style.display = "none"
}
}
function onUploadConfigProcess() {
var configFile = document.getElementById("config_file")
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/
if (regex.test(configFile.value.toLowerCase())) {
if (typeof (FileReader) != "undefined") {
var reader = new FileReader()
reader.onload = function (e) {
var configJSON = csvToJSON(e.target.result)
if (configJSON) {
lsDB.truncate("config")
configJSON.forEach(function (value) {
lsDB.insert("config", value)
})
lsDB.commit()
allConfigs()
} else {
alertE("Error, please re-validate your file");
}
}
reader.readAsText(configFile.files[0]);
} else {
alertE("This browser does not support HTML5");
}
} else {
alertE("Please upload a valid CSV file");
}
}
function csvToJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",")
for (var j = 0; j < headers.length; j++) {
var val = currentline[j]
if (j + 1 == 2 && currentline.length > 2) { // indicating value contains comma in value
currentline.shift() // remove first array
val = currentline.join(",") // then compact it
}
obj[headers[j]] = val
}
result.push(obj)
}
const unique = new Set()
const anyDuplicateConfigKey = result.some(element => unique.size === unique.add(element.key).size)
if (anyDuplicateConfigKey) {
return false
}
return result
}
function onFactoryResetClicked() {
lsDB.drop("poslite")
location.reload()
}
</script>
</body>
</html>