-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
134 lines (122 loc) · 3.93 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
<html>
<head>
<meta charset="UTF-8">
<title>Was bringe ich mit?</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<style>
form {
display: grid;
grid-template-columns: 200px 300px;
column-gap: 10px;
}
form label, input, select {
width: 200px;
display: block;
margin: 10px;
}
label {
text-align: right;
}
button {
margin: 10px;
}
#list {
list-style-type:none;
}
#list li {
line-height:1.5em;
}
#list li:before {
content:'\25A2 ';
display:inline-block;
padding-right:5px;
}
</style>
</head>
<body>
<form onsubmit="return false;">
<label for="name">Name </label>
<input type="text" name="name" id="name">
<label for="id">Mitbringen.net Pin </label>
<input type="text" name="id" id="id">
<button type="reset" value="Reset">Zurücksetzen</button>
<button type="submit" value="Submit" id="btnerstelle">Erstelle meine Mitbringliste</button>
</form>
<ul id="list"></ul>
<script>
var mitbringenNet = 'https://mitbringen.net/',
dom = '',
searchParams = new URLSearchParams(window.location.search);
$(function() {
// Pin is in get-parameters, then autofill formield
// and get Names
if (searchParams.has('pin')) {
document.getElementById('id').value = searchParams.get('pin');
// Get all names from list
$.ajax( {
url: mitbringenNet + searchParams.get('pin'),
type: 'GET',
headers: {
'Accept-Language': 'de'
},
success: function(result) {
var bringers = new Set(['- Bitte wählen -']);
dom = $.parseHTML(result);
$(dom).find('[data-bringer]').each(function(idx,item) {
var b=item.textContent.replace(/\s*\(.*\)/,'');
bringers.add(b);
});
bringers = [...bringers].sort();
var select = '<select name="name" id="name">';
for (bringer of bringers) {
select += `<option value="${bringer}">${bringer}</option>`;
};
select += '</select>';
$('#name').replaceWith($(select));
}
} );
}
});
$('#btnerstelle').on('click', function(event) {
if ( dom == '' ) {
$.ajax( {
url: mitbringenNet + $('#id').val(),
type: 'GET',
headers: {
'Accept-Language': 'de'
},
success: function(result) { extractList($.parseHTML(result));},
error: function(error) {console.log(error) }
});
} else {
extractList(dom);
}
});
function extractList(result) {
var test = $(result),
rows = test.find('#itemList .row'),
items = [];
// Clear list before adding new Items
$('#list').find('li').remove();
rows.each( function(i, el) {
var item = $(this).find('.item div').text(),
bringerLi = $(this).find('.bringer ul li'),
amount = undefined,
unit = $(this).find('.quantity div')[0];
unit = $(unit).text()
unit = unit.replace(/\d+(.*)/, '$1');
bringerLi.each( function(index, element) {
var bringer = $(this).text().replace(/\s*\(.*\)/,'');
var who = $('#name').val()
if(bringer === who) {
amount = $(this).text().match(/(?:[^\d])+\((\d+).*\)/)[1] + unit;
}
});
if(amount !== undefined) {
$('#list').append(`<li><span><b>${item}</b> (${amount})</span></li>`);
}
});
}
</script>
</body>
</html>