forked from jef/streetmerchant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
163 lines (141 loc) · 4.41 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>streetmerchant control</title>
<script type="text/javascript">
let config;
let brands;
let stores;
let series;
let models;
function renderList(id, elements, selectionArray, subfield) {
const list = document.getElementById(id);
list.innerHTML = '';
for (const element of elements) {
const name = `${id}_${element}`;
const htmlElement = document.createElement('input');
htmlElement.type = 'checkbox';
htmlElement.value = element;
htmlElement.innerHTML = element;
htmlElement.name = name;
htmlElement.id = name;
if (selectionArray.length === 0) {
htmlElement.checked = true;
} else if (subfield && selectionArray.find(obj => element === obj[subfield])) {
htmlElement.checked = true;
} else if (selectionArray.includes(element)) {
htmlElement.checked = true;
}
list.appendChild(htmlElement);
const htmlLabelElement = document.createElement('label');
htmlLabelElement.setAttribute('for', name);
htmlLabelElement.innerText = element;
list.appendChild(htmlLabelElement);
list.appendChild(document.createElement('br'));
}
}
function listToArray(id, subfield) {
const list = document.getElementById(id);
const resArray = [];
let allSelected = true;
for (const htmlElement of list.childNodes) {
if (htmlElement.checked) {
let obj;
if (subfield) {
obj = {};
obj[subfield] = htmlElement.value;
if (id === 'modelList') {
obj['series'] = '';
}
} else {
obj = htmlElement.value
}
resArray.push(obj);
} else {
allSelected = false;
}
}
if (allSelected) {
return [];
}
return resArray;
}
async function onReceiveConfig(resp) {
config = await resp.json();
renderList('storeList', stores, config.store.stores, 'name');
renderList('brandList', brands, config.store.showOnlyBrands);
renderList('seriesList', series, config.store.showOnlySeries);
renderList('modelList', models, config.store.showOnlyModels, 'name');
}
async function setConfig() {
if (!config) {
throw new Error('Config not loaded yet');
}
config.store.stores = listToArray('storeList', 'name');
config.store.showOnlyBrands = listToArray('brandList');
config.store.showOnlySeries = listToArray('seriesList');
config.store.showOnlyModels = listToArray('modelList', 'name');
const resp = await fetch('/api/config', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(config)
});
await onReceiveConfig(resp);
alert('Saved!');
}
async function loadScreenshots() {
const respScreenshots = await fetch('/api/screenshots');
const screenshots = await respScreenshots.json();
const screenshotContainer = document.getElementById('screenshots');
screenshotContainer.innerHTML = '';
for (const screenshot of screenshots) {
const htmlElement = document.createElement('a');
htmlElement.href = `/api/screenshots/${screenshot}`;
htmlElement.target = '_blank';
htmlElement.innerText = screenshot;
screenshotContainer.appendChild(htmlElement);
screenshotContainer.appendChild(document.createElement('br'));
}
}
async function loadConfig() {
const resp = await fetch('/api/config');
await onReceiveConfig(resp);
}
async function loadInitial() {
const respStores = await fetch('/api/stores');
stores = await respStores.json();
const respBrands = await fetch('/api/brands');
brands = await respBrands.json();
const respSeries = await fetch('/api/series');
series = await respSeries.json();
const respModels = await fetch('/api/models');
models = await respModels.json();
await loadConfig();
await loadScreenshots();
}
</script>
</head>
<body onload="loadInitial();">
<table>
<tr>
<th>Stores</th>
<th>Brands</th>
<th>Series</th>
<th>Models</th>
</tr>
<tr>
<td valign="top" id="storeList"></td>
<td valign="top" id="brandList"></td>
<td valign="top" id="seriesList"></td>
<td valign="top" id="modelList"></td>
</tr>
</table>
<br />
<input type="button" onclick="setConfig();" value="Save" />
<br /><br /><br /><br />
<b>Screenshots (<a href="javascript:loadScreenshots();">Refresh</a>)</b>
<div id="screenshots"></div>
</body>
</html>