-
Notifications
You must be signed in to change notification settings - Fork 1
/
jstemplate.js
169 lines (167 loc) · 4.22 KB
/
jstemplate.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
const maxDuration = 180;
const listOfChoices = [
{
name: 'Workshop 1',
user: 'John Doe',
duration: 180
},
{
name: 'Workshop 2',
user: 'Willem deSantos',
duration: 90
},
{
name: 'Workshop 3',
user: 'Viola Zerbst',
duration: 90
},
{
name: 'Workshop 4',
user: 'Loretta Norris',
duration: 90
},
{
name: 'Talk 5',
user: 'André Grienz',
duration: 45
},
{
name: 'Talk 6',
user: 'Meredith Winter',
duration: 45
},
{
name: 'Talk 7',
user: 'Enid Tobler',
duration: 45
},
{
name: 'Demo 8',
user: 'Roland Romain',
duration: 30
},
{
name: 'Demo 9',
user: 'Werner Molder',
duration: 30
},
{
name: 'Demo 10',
user: 'Lara Bloom',
duration: 30
},
];
const TMPL_INPUT_ENTRY = `
<div class="row align-items-center h-100">
<div class="col-1 mx-auto text-right">
<input name="choices[{num}]" type="checkbox" class="custom-choice">
</div>
<div class="col-11 mx-auto">
<span class="choice-name">{name}</span>
<span class="choice-user">{user}</span>
<span class="choice-duration">{duration}</span>
</div>
</div>
`;
const TMPL_LIST_ENTRY = `
<div class="row">
<div class="col-sm-10 col-md-9 col-lg-8">{name}</div>
<div class="col-sm-2 col-md-3 col-lg-4">{votes}</div>
</div>
`;
const TMPL_ITEM_ENTRY = `
<div class="mt-4">
<span class="choice-name">{name}</span>
<span class="choice-user">{user}</span>
<span class="choice-duration">{duration}</span>
</div>
`;
function displayChoices() {
let html = '';
let num = 0;
for (const choice of listOfChoices) {
let snippet = TMPL_INPUT_ENTRY;
['name', 'user', 'duration'].forEach((n) => {
snippet = snippet.replace('{' + n + '}', choice[n]);
})
html += snippet.replace('{num}', num);
num++;
}
return html;
};
function getChoiceName(val) {
return listOfChoices[val] ? listOfChoices[val].name : val;
}
function getChoiceDuration(val) {
return listOfChoices[val] ? listOfChoices[val].duration : 0;
}
function getSelectedChoices() {
const vals = Array.from(
document.getElementById('defaulttemplate-addentry').querySelectorAll('input:checked')
).map(o => o.name.replace('choices[', '').replace(']', ''));
const duration = vals.reduce((sum, i) => {
return sum + getChoiceDuration(i);
}, 0);
return { items: vals, duration: duration };
}
function displaySelectedChoices(val) {
let a;
try {
a = JSON.parse(val.replaceAll('"', ''));
} catch (e) {
console.log('Could not parse: ' + val);
return 'Error decoding value';
}
if (!Array.isArray(a)) {
console.log('Parsed value is not an array: ' + a);
return 'Error decoding value';
}
let html = '';
for (const i of a) {
if (typeof listOfChoices[i] === 'undefined') {
console.log(`Item ${i} not found in choices`);
continue;
}
let snippet = TMPL_ITEM_ENTRY;
['name', 'user', 'duration'].forEach((n) => {
snippet = snippet.replace('{' + n + '}', listOfChoices[i][n]);
})
html += snippet;
}
return html;
}
function displayVotes(votes) {
let html = TMPL_LIST_ENTRY.replace('{votes}', '<h3>Participants</h3>')
.replace('{name}', '<h3>Name</h3>');
for (let i = 0; i < listOfChoices.length; i++) {
html += TMPL_LIST_ENTRY.replace('{votes}', votes[i] ?? 0).replace('{name}', getChoiceName(i));
}
return html;
}
function buildCsvData(choices) {
const data = [];
const header = [];
header.push('Name');
header.push('Link');
for (const choice of listOfChoices) {
header.push('"' + choice.name.replace(/"/g, '""') + '"')
}
data.push(header.join(';'))
for (const choice of choices) {
const row = [];
row.push('"' + choice.u.replace(/<[^>]*>/g, '') + '"')
const m = choice.u.match(/href="([^"]*)"/)
row.push('"' + m[1] + '"')
for (let i = 0; i < listOfChoices.length; i++) {
row.push(choice.v.includes(i.toString()) ? 'X' : '')
}
data.push(row.join(';'))
}
const url = URL.createObjectURL(new Blob([data.join("\n")], { type: 'text/csv' }));
const a = document.createElement('a');
a.href = url;
a.download = 'data-choices.csv';
a.click();
URL.revokeObjectURL(url);
return false;
}