-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleeve-card.html
329 lines (273 loc) · 8.19 KB
/
sleeve-card.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
<!doctype html>
<html>
<head>
<script src="lib/qrcode.min.js"></script>
<style>
@media print {
.no-print {
display: none;
}
}
html,
body {
padding: 0;
margin: 0;
font-family: serif;
}
label {
font-weight: bold;
flex-grow: 1;
}
.body {
display: inline;
padding: 0 0 0 0;
text-align: center;
}
.card {
width: 64mm;
height: 90mm;
border: 1px solid black;
margin: 5mm 0 0mm 3mm;
display: inline-block;
text-align: center;
}
.top {
width: 50mm;
height: 50mm;
border: 0px solid black;
margin-left: auto;
margin-right: auto;
margin-top: 6mm;
margin-bottom: 7mm;
}
.top > img {
width: inherit;
height: inherit;
}
.bottom {
margin-left: auto;
margin-right: auto;
margin-top: -2mm;
width: 55mm;
border: 0px solid black;
text-align: left;
font-size: 4mm;
padding: 1mm;
border-radius: 1mm;
}
.row {
display: flex;
flex-direction: row;
margin-top: 1mm;
margin-bottom: 1mm;
}
.border-bottom {
border-bottom: 1px solid #aaa;
}
.dialog {
text-align: left;
margin: 2em;
padding: 0.5em;
background-color: #fff;
position: fixed;
top: 20px;
left: 20px;
box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.2);
border: 1px solid black;
border-radius: 0.2em;
}
button {
margin: 0.2em;
background-color: #fefefe;
}
.primary {
background-color: #eee;
}
.flex {
display: flex;
}
.grow {
flex: 1;
}
</style>
</head>
<body>
<div class="body">
<div data-card-list>
</div>
<button class="no-print" onclick="showUserForm(true);">Add Person</button>
<button class="no-print" onclick="showCSVForm(true);">Add from CSV</button>
<div data-dialog-csv hidden class="dialog no-print">
<h3>Add from CSV</h3>
<p>Paste cells from Google Sheets here and click Submit.</p>
<div>
<label>CSV</label><br/>
<textarea cols="80" rows="20" data-csv></textarea>
</div>
<div class="flex">
<span class="grow"></span>
<button class="primary" onclick="processCSV(); showCSVForm(false);">Submit</button>
</div>
</div>
<div data-dialog-user hidden class=" dialog no-print">
<h3>Add Person <span></span></h3>
<div class="row">
<label>Name</label>
<input type="text" name="name"/>
</div>
<div class="row">
<label>Gender</label>
<radiogroup>
<input type="radio" name="gender" value="Male">Male</input>
<input type="radio" name="gender" value="Female">Female</input>
</radiogroup>
</div>
<div class="row">
<label>Member #</label>
<input type="text" name="member_no"/>
</div>
<div class="row">
<label>Discipline</label>
<radiogroup>
<input type="radio" name="discipline" value="Running">Run</input>
<input type="radio" name="discipline" value="Walking">Walk</input>
</radiogroup>
</div>
<div class="border-bottom"></div>
<div class="row">
<button onclick="showUserForm(false);">Cancel</button>
<button class="primary" onclick="addUserFromForm(event);">Submit</button>
</div>
</div>
<div data-template hidden>
<div class="card">
<div class="top" id="">
</div>
<div class="bottom">
<div class="row border-bottom">
<label>Name</label>
<div data-name>Johan Mynhardt</div>
</div>
<div class="row border-bottom">
<label>Gender</label>
<div data-gender>Male</div>
</div>
<div class="row border-bottom">
<label>Member #</label>
<div data-member_no>123456789</div>
</div>
<div class="row border-bottom">
<label>Discipline</label>
<div data-discipline>Running</div>
</div>
</div>
</div>
</div>
</div>
<script>
const uuid = function () {
var uuid = "",
i, random;
for (i = 0; i < 32; i++) {
random = Math.random() * 16 | 0;
if (i === 8 || i === 12 || i === 16 || i === 20) {
uuid += "-"
}
uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random)).toString(16);
}
return uuid;
};
const qr = (config = {
id: 'qrcode',
text: 'hello'
}) => {
const code = new QRCode(config.id);
code.makeCode(config.text);
};
const makeCard = user => {
let uuuid = uuid();
let template = document.querySelector('[data-template]');
let div = document.createElement('div');
div.classList.add('card');
div.innerHTML = template.querySelector('.card').innerHTML;
Object.keys(user).forEach(key => {
let sel = `[data-${key}]`;
div.querySelector(sel).innerText = user[key];
});
div.querySelector('.top').setAttribute('id', `member_no-${user.member_no}-uuid_${uuuid}`);
document.querySelector('[data-card-list]').appendChild(div);
qr({
id: `member_no-${user.member_no}-uuid_${uuuid}`,
text: `${user.name}:${user.member_no}`
});
};
const clearCards = () => {
document.querySelector('[data-card-list]').innerText = '';
};
const addUser = (name, gender, member_no, discipline) => {
users.push({
name: name,
gender: gender,
member_no: member_no,
discipline: discipline
});
};
const updateUsers = () => {
users.forEach(makeCard);
};
const showCSVForm = show => {
let dialog = document.querySelector('[data-dialog-csv]');
if (show) {
dialog.removeAttribute('hidden');
} else {
dialog.setAttribute('hidden', true);
}
};
const processCSV = () => {
clearCards();
users = [];
let csvData = document.querySelector('textarea[data-csv]').value;
csvData.split('\n')
.filter(row => row.indexOf('Membership No.') < 0)
.map(row => row.split('\t'))
.filter(record => record.filter(field => field).length > 0)
.forEach(record => addUser(record[0], record[1], record[2], record[3]));
updateUsers();
};
const showUserForm = (show) => {
let dialog = document.querySelector('[data-dialog-user]');
if (show) {
dialog.removeAttribute('hidden');
} else {
dialog.setAttribute('hidden', true);
}
};
const addUserFromForm = formEvent => {
formEvent.preventDefault();
formEvent.stopPropagation();
let dialog = document.querySelector('[data-dialog-user]');
let inputs = dialog.querySelectorAll('input');
let name = ([...inputs].filter(x => x.name === 'name')[0] || {}).value;
let gender = ([...inputs].filter(x => x.name === 'gender').filter(x => x.checked)[0] || {}).value;
let member_no = ([...inputs].filter(x => x.name === 'member_no')[0] || {}).value;
let discipline = ([...inputs].filter(x => x.name === 'discipline').filter(x => x.checked)[0] || {}).value;
let newUser = {
name: name,
gender: gender,
member_no: member_no,
discipline: discipline
};
console.info('user: ', newUser);
addUser(name, gender, member_no, discipline);
};
let users = [{
name: 'Johan Mynhardt',
gender: 'Male',
member_no: '239987',
discipline: 'Running'
}];
users.forEach(makeCard);
updateUsers();
</script>
</body>
</html>