-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoutnet.html
551 lines (478 loc) · 15.4 KB
/
scoutnet.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<meta charset="utf-8" />
<title>Scoutnet CSV manipulation</title>
<!--<script src="jquery.min.js"></script> -->
<script src="jquery-1.8.3.js"></script>
<script src="jquery.csv-0.71.js"></script>
<script>
var scout_header = new Array();
var scout_data = new Array();
var scout_groups;
var scout_groupIndex;
var scout_modified_header;
var scout_modified_data;
var scout_emailIndex;
function sContains(theString, toCheck) {
return (theString.indexOf(toCheck) >= 0);
}
$(document).ready(function() {
if(isAPIAvailable()) {
$('#files').bind('change', handleFileSelect);
}
var css = '<link rel="stylesheet" href="style.css" type="text/css" />';
$('#tableframe').contents().find("head").append(css);
});
function isAPIAvailable() {
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
return true;
} else {
// source: File API availability - http://caniuse.com/#feat=fileapi
// source: <output> availability - http://html5doctor.com/the-output-element/
document.writeln('The HTML5 APIs used in this form are only available in the following browsers:<br />');
// 6.0 File API & 13.0 <output>
document.writeln(' - Google Chrome: 13.0 or later<br />');
// 3.6 File API & 6.0 <output>
document.writeln(' - Mozilla Firefox: 6.0 or later<br />');
// 10.0 File API & 10.0 <output>
document.writeln(' - Internet Explorer: Not supported (partial support expected in 10.0)<br />');
// ? File API & 5.1 <output>
document.writeln(' - Safari: Not supported<br />');
// ? File API & 9.2 <output>
document.writeln(' - Opera: Not supported');
return false;
}
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
var file = files[0];
// read the file metadata
var output = ''
output += '<span style="font-weight:bold;">' + escape(file.name) + '</span><br />\n';
output += ' - FileType: ' + (file.type || 'n/a') + '<br />\n';
output += ' - FileSize: ' + file.size + ' bytes<br />\n';
output += ' - LastModified: ' + (file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() : 'n/a') + '<br />\n';
// read the file contents
loadData(file);
// post the results
$('#list').html(output);
$('#update').click( function(event){handleUpdate();});
}
function loadData(file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event){
var csv = event.target.result;
var data = $.csv.toArrays(csv);
var first = 1;
scout_header = new Array();
scout_data = new Array();
for(row in data) {
if(first == 1)
{
scout_header = data[row];
first = 0;
}
else
{
scout_data.push(data[row])
}
}
addGroups();
addEmails();
handleUpdate();
};
reader.onerror = function(){ alert('Unable to read ' + file.fileName); };
}
function addGroups(){
scout_groups = new Array();
scout_groupIndex = -1;
for (var i=0; i < scout_header.length; i++)
{
if (scout_header[i].toLowerCase() === "avdelning" || scout_header[i].toLowerCase() === "avdelningstillhörighet")
{
scout_groupIndex = i;
break;
}
}
for (var i=0; i<scout_data.length; i++)
{
var g = scout_data[i][scout_groupIndex].toLowerCase();
if (scout_groups.indexOf(g) == -1)
{
scout_groups.push(g);
}
}
scout_groups.sort();
for (var i=0; i<scout_groups.length; i++)
{
var html = '<input type="checkbox" id="group'+i+'" checked="checked"/>'
html += '<label for="group'+i+'">'+scout_groups[i]+'</label>';
$('#groups').append(html);
}
}
function addEmails(){
scout_email = new Array();
for (var i=0; i < scout_header.length; i++)
{
if (sContains(scout_header[i].toLowerCase(), "e-post") &&
!sContains(scout_header[i].toLowerCase(), "distriktet") &&
!sContains(scout_header[i].toLowerCase(), "kåren"))
{
scout_email.push(scout_header[i].toLowerCase());
}
}
scout_email.sort();
for (var i=0; i < scout_email.length; i++)
{
var html = '<input type="checkbox" id="email'+i+'" checked="checked"/>'
html += '<label for="email'+i+'">'+scout_email[i]+'</label>, ';
$('#emails').append(html);
}
}
function printTable() {
var html = '<table class="data">';
html += '<tr>\r\n';
for(var item in scout_modified_header) {
html += '<td><b>' + scout_modified_header[item] + '</b></td>\r\n';
}
html += '</tr>\r\n';
for(var row in scout_modified_data) {
html += '<tr>\r\n';
for(var item in scout_modified_data[row]) {
html += '<td>' + scout_modified_data[row][item] + '</td>\r\n';
}
html += '</tr>\r\n';
}
html += '</html>\r\n';
$('#tableframe').contents().find("body").html(html);
}
function printCSV() {
var html = '<pre>';
for(var item in scout_modified_header) {
html += '"' + scout_modified_header[item] + '",';
}
html += '\r\n';
for(var row in scout_modified_data) {
for(var item in scout_modified_data[row]) {
html += '"' + scout_modified_data[row][item] + '",';
}
html += '\r\n';
}
html += '</pre>'
$('#csvframe').contents().find("body").html(html);
}
function printEmail()
{
var addressIndex = new Array();
var scout_emailIndex = new Array();
var addresses = new Array();
var returnAddresses = new Array();
for (var i=0; i<scout_email.length; i++)
{
for (var j=0; j<scout_modified_header.length; j++)
{
if (scout_modified_header[j].toLowerCase() ==
scout_email[i])
{
scout_emailIndex.push(j);
}
}
}
for(var row in scout_modified_data)
{
for(var i=0; i<scout_emailIndex.length; i++)
{
var j=scout_emailIndex[i];
if (scout_modified_data[row][j] != "")
{
addresses.push(scout_modified_data[row][j].toLowerCase());
}
}
}
addresses.sort();
var first = 1;
for(var i=0; i<addresses.length; i++)
{
if (first == 1)
{
returnAddresses.push(addresses[i]);
first = 0;
}
else if (addresses[i] != addresses[i-1])
{
returnAddresses.push(addresses[i]);
}
}
var ma = returnAddresses.join(", ");
$('#emailfield').val(ma);
var html = '<a href="mailto:' + ma +
'">skicka mail till valda addresser!</a>';
$('#mailto').html(html);
}
function removeGroups() {
var tmp = new Array();
for (var i=0; i<scout_modified_data.length; i++)
{
var g = scout_modified_data[i][scout_groupIndex].toLowerCase();
var id = scout_groups.indexOf(g);
if ($('#group'+id).is(':checked'))
{
tmp.push(scout_modified_data[i]);
}
}
scout_modified_data = tmp;
}
function handleCleanphone() {
var phoneLabels = Array();
/* find all labels that contains "telefon" or "mobil" */
for (var i=0; i < scout_header.length; i++)
{
if (sContains(scout_header[i], "telefon") ||
sContains(scout_header[i], "mobil"))
{
phoneLabels.push(i); /* compensate for new element */
}
}
for (var member=0; member < scout_modified_data.length; member++)
{
for (var label=0; label < phoneLabels.length; label++)
{
var l = phoneLabels[label];
if (scout_modified_data[member][l] == undefined ||
scout_modified_data[member][l] == "")
{
continue;
}
if (scout_modified_data[member][l].indexOf("46 ",0)===0)
{
scout_modified_data[member][l] =
scout_modified_data[member][l].substr(3);
}
else if (scout_modified_data[member][l].indexOf("+46 ",0)===0)
{
scout_modified_data[member][l] =
scout_modified_data[member][l].substr(4);
}
}
}
}
function handleMergephone() {
var phoneLabels = Array();
/* find all labels that contains "telefon" or "mobil"
* but not "kårlokal", "distriktskansli"
*/
for (var i=0; i < scout_header.length; i++)
{
if ((sContains(scout_header[i], "telefon") ||
sContains(scout_header[i], "mobil")) &&
(!sContains(scout_header[i], "kårlokal") &&
!sContains(scout_header[i], "distriktskansli")))
{
phoneLabels.push(i + 1); /* compensate for new element */
}
}
var totPhone = "totaltelefon";
scout_modified_header.unshift(totPhone);
for (var member=0; member < scout_modified_data.length; member++)
{
/* Insert first empty phone number */
scout_modified_data[member].unshift("");
for (var label=0; label < phoneLabels.length; label++)
{
var l = phoneLabels[label];
if (scout_modified_data[member][l] != "")
{
if (scout_modified_data[member][0] == "")
{
scout_modified_data[member][0] = (scout_modified_header[l] + ": " +
scout_modified_data[member][l]);
}
else
{
scout_modified_data[member][0] += (", " + scout_modified_header[l] + ": " +
scout_modified_data[member][l]);
}
}
}
}
}
function handleSort(a, b, s_index)
{
for (var i=0; i < s_index.length; i++)
{
if (a[s_index[i]] == undefined ||
b[s_index[i]] == undefined)
{
continue;
}
if (a[s_index[i]] == "" ||
b[s_index[i]] == "")
{
continue;
}
if (a[s_index[i]].toLowerCase() <
b[s_index[i]].toLowerCase())
{
return -1;
}
if (a[s_index[i]].toLowerCase() >
b[s_index[i]].toLowerCase())
{
return 1;
}
}
return 0;
}
function handleUpdate()
{
scout_modified_header = scout_header.slice();
scout_modified_data = new Array();
for (var i=0; i<scout_data.length; i++)
{
scout_modified_data.push(scout_data[i].slice());
}
removeGroups();
if ($('#cleanPhone').is(':checked'))
{
handleCleanphone();
}
if ($('#mergePhone').is(':checked'))
{
handleMergephone();
}
var sortOrder = $('#sort').val();
if (sortOrder != "scoutnet")
{
var s_index = Array();
var sortorder = Array();
if (sortOrder == "groupSort" )
{
sortorder = ["avdelning", "förnamn", "efternamn"];
}
else if (sortOrder == "addressSort")
{
sortorder = ["postnr",
"adress, rad 1",
"adress, rad 2",
"adress, rad 3",
"efternamn",
"förnamn"];
}
for (s in sortorder)
{
var ss = sortorder[s];
for (var i=0; i<scout_modified_header.length; i++)
{
if (scout_modified_header[i].toLowerCase() ===
sortorder[s].toLowerCase())
{
s_index.push(i);
}
}
}
scout_modified_data.sort(function(a,b){return handleSort(a,b,s_index)});
}
var columnChoice = $('#columns').val();
if (columnChoice != "scoutnet")
{
var s_index = Array();
var columns = Array();
if (columnChoice == "patrik" )
{
columns = ["avdelning",
"förnamn",
"efternamn",
"c/o (ange c/o i rutan)",
"adress, rad 1",
"adress, rad 2",
"adress, rad 3",
"postnr",
"postort",
"e-post",
"totaltelefon"];
}
for (var s=0; s < columns.length; s++)
{
for (var i=0; i<scout_modified_header.length; i++)
{
if (scout_modified_header[i].toLowerCase() ===
columns[s].toLowerCase())
{
s_index.push(i);
}
}
}
for (var i=0; i < scout_modified_data.length; i++)
{
scout_modified_data[i] =
s_index.map(function(x){return scout_modified_data[i][x];});
}
scout_modified_header =
s_index.map(function(x){return scout_modified_header[x];});
}
printTable();
printCSV();
printEmail();
}
</script>
</head>
<body>
<h1>Massör för scoutnet data</h1>
<p>
Senaste versionen av det här verktyget hittar du på <a href="https://github.com/hakankvist/scoutnet">github.com/hakankvist/scoutnet</a>.<br/>
Verktyget är skrivet Håkan Kvist (hagar -at- df.lth.se).
</p>
<p>
Verktyget har bara testats med moderna version av Firefox (version 17).
</p>
<h2>Data parametrar</h2>
Välj CSV-fil att läsa in: <input type=file id="files" name=files[] multiple /><br/>
<table class="invisible">
<tr><td>Slå ihop telefonnummer</td>
<td><input id="mergePhone" type="checkbox" /></td></tr>
<tr><td>Ta bort svensk landskod från telefonnummer ("+46"/"46")</td>
<td><input id="cleanPhone" type="checkbox" /></td></tr>
<tr><td>Sorteringsordning</td>
<td><select id="sort">
<option value="scoutnet">scoutnet</option>
<option value="groupSort">grupper</option>
<option value="addressSort">postnummer, adress, efternamn</option>
</select></td></tr>
<tr><td>Kolumner</td>
<td><select id="columns">
<option value="scoutnet">scoutnet</option>
<option value="patrik">namn, adress, ihopslagen telefon, e-post</option>
</select></td></tr>
</table>
<p id="groups">
Grupper:
</p>
<p id="emails">
E-post adresser i email-listan (längst ner på sidan):
</p>
<button id="update">Uppdatera</button>
<hr />
<output id=list>
</output>
<hr />
<h2>Data i tabell-form</h2>
<!--<table id=contents>
</table>-->
<iframe id="tableframe">
</iframe>
<h2>CSV-data</h2>
<p>
Kopiera allt innehåll i framen nedan (CTRL+A, CTRL+C) och klistra in i notepad. Spara som "valfritt filnamn.csv". Den sparade CSV-filen kan du sen öppna i excel eller openoffice.org.
</p>
<iframe id="csvframe">
</iframe>
<h2>E-post lista</h2>
<input type="text" id="emailfield" size="80"/>
<p id="mailto">
</p>
</body>