-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
408 lines (362 loc) · 13.1 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
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
<!DOCTYPE html>
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#7a99ac">
<link rel="icon" sizes="192x192" href="img/logo.png">
<html>
<head>
<title>
Mensaplan der Uni Ulm by FS-ET
</title>
<meta charset="utf-8" />
<script src="libs/jquery-1.9.1.js"></script>
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body onload="init()" onKeyDown="pressed(event)" onhashchange="init()">
<div class="navContainer">
<nav id="topNav">
<select id="selector">
<option id="facilityMensa">Mensa Uni</option>
<option id="facilityBistro">Bistro</option>
<option id="facilityBurgerbar">Burgerbar Cafeteria SouthSide</option>
<option id="facilityCB">Cafeteria B</option>
<option id="facilityWest">Cafeteria West</option>
<option id="facilityDiner">WestSideDiner</option>
<!--<option id="facilityHochschulleitung">Cafeteria Hochschulleitung</option>-->
<!--<option id="facilityHSOE">Hochschule Oberer Eselsberg</option>-->
<option id="facilityPrittwitzstr">Mensa Hochschule</option>
</select>
</nav>
<nav id="bottomNav">
<span id="1stWeek">
<a class="ahover" id="day0" onclick="openDay(0)">Mo</a>
<a class="ahover" id="day1" onclick="openDay(1)">Di</a>
<a class="ahover" id="day2" onclick="openDay(2)">Mi</a>
<a class="ahover" id="day3" onclick="openDay(3)">Do</a>
<a class="ahover" id="day4" onclick="openDay(4)">Fr</a>
</span>
<span id="seperator">|</span>
<span id="2ndWeek">
<a class="ahover" id="day5" onclick="openDay(5)">Mo</a>
<a class="ahover" id="day6" onclick="openDay(6)">Di</a>
<a class="ahover" id="day7" onclick="openDay(7)">Mi</a>
<a class="ahover" id="day8" onclick="openDay(8)">Do</a>
<a class="ahover" id="day9" onclick="openDay(9)">Fr</a>
</span>
</nav>
</div>
<div id="plan">
</div>
<div class="navContainer">
<div id="bottom">
<a href="https://github.com/seder/mensaplan-web-interface">Github</a> |
<a href="http://www.studentenwerk-ulm.de/index.php?id=25728">Quelle</a> |
<a href="http://www.uni-ulm.de/mensaplan/data/">Rohdaten</a> |
<a href="http://www.uni-ulm.de/mensaplan/impressum.html">Impressum</a>
</div>
</div>
<script>
var facility;
var date;
var refresh;
var daysArray = [];
var mensen = {"Mensa Uni":"Mensa", "Bistro":"Bistro", "Cafeteria West":"West", "WestSideDiner":"Diner",
"Burgerbar Cafeteria SouthSide":"Burgerbar","Mensa Hochschule":"Prittwitzstr",
"Cafeteria Hochschulleitung":"Hochschulleitung","Hochschule Oberer Eselsberg":"HSOE",
"Cafeteria B":"CB"};
var weekdays = ["Mo","Di","Mi","Do","Fr","Mo","Di","Mi","Do","Fr"];
var urlJSON = "./data/mensaplan.json";
var urlStaticJSON = "./data/mensaplan_static.json";
function init(){
parseAnchor();
if ( refresh != undefined ) timedRefresh(10 * 60 * 1000);//every 10 minutes
$.getJSON(urlJSON, function(data){
var noOfWeeks = 1;
while ( data.weeks[noOfWeeks] != undefined ) noOfWeeks++;
var coordinates = getCoordinatesInJSON(date,data,noOfWeeks);
date = coordinates.date;
reset();
if ( daysArray[0] == undefined ){//do this only when landing on page
initDaysList(data,noOfWeeks);
}
if ( !(facility == "Diner" || facility == "Burgerbar") ){
if ( coordinates.found == true ) {
printPlan(data.weeks[coordinates.week].days[coordinates.day][facility]);
}
}
else /* food plan for those facilities does not change --> different file */
{
$.getJSON(urlStaticJSON, function(staticData){
weekday = new Date(date).getDay()-1;
if ( weekday == -1 || weekday == 5 ){// weekend --> show monday
weekday = 0;
}
printPlan(staticData.weeks[0].days[weekday][facility]);
});
}
selectFacilityInDropdown();
var index = daysArray.indexOf(date);
if ( index != -1 )
document.getElementById("day"+index).className = "active";
});
}
/*
* sets global variables facility, date, and refresh according to anchor
*/
function parseAnchor(){
var anchor = window.location.hash.substr(1);
if ( anchor == "" ){ // default: Mensa, today
facility = 'Mensa';
date = getDayString(0);
} else {
facility = anchor.split('&')[0];
date = anchor.split('&')[1];
refresh = anchor.split('&')[2];
if ( date == "today" || date == "heute" || date == undefined )
date = getDayString(0);
if ( date == "tomorrow" || date == "morgen" )
date = getDayString(1);
if ( date == "yesterday" || date == "gestern" )
date = getDayString(-1);
if ( date == "next" )// shows today's plan during opening hours,
// tomorrows when facility is closed
// (exact to one hour)
{
var now = new Date().getHours();
var closingTime = 14;
if ( facility == "bistro" ) closingTime = 19;
if ( now < closingTime )
date = getDayString(0);
else
date = getDayString(1);
}
}
}
function initDaysList(data,noOfWeeks){
if ( noOfWeeks >= 2 ){
var mon1 = getDateOfISOWeek(data.weeks[0].weekNumber);
var mon2 = getDateOfISOWeek(data.weeks[1].weekNumber);
if (mon2<mon1) { // switch if weeks are out of order | TODO: what if more than 2 weeks?
var tmp = mon1;
mon1 = mon2;
mon2 = tmp;
}
addDateToWeekdays(mon1,0);
addDateToWeekdays(mon2,5);
} else {
document.getElementById('2ndWeek').style.display = "none";
document.getElementById('seperator').style.display = "none";
document.getElementById('1stWeek').style.width = "100%";
addDateToWeekdays(getDateOfISOWeek(data.weeks[0].weekNumber),0);
}
}
function getCoordinatesInJSON(date,data,noOfWeeks){
var x,y;
var originalDate = date;
var found = false;
for ( k=0; k<3; k++ ){
for ( i=0; i<noOfWeeks; i++ ){
for ( j=0; j<data.weeks[i].days.length; j++ ){
if(data.weeks[i].days[j].date == date){
found = true;
week = i;
day = j;
}
}
}
if ( found == false ) {//closed today, maybe plan for tomorrow?
date = getISOStringOfDate(new Date(new Date(date).getTime() + 24 * 60 * 60 * 1000));
}
else break;
}
if ( found == false ){// failsafe: requested date and the next two days not
// found, try today. If that doesn't work either
// just take the first one.
today = getISOStringOfDate(new Date());
if ( date != today && originalDate != today ){
return getCoordinatesInJSON(today, data, noOfWeeks);
} else {
return {
"week":0,
"day":0,
"date":date,
"found":found
};
}
}
return {
"week":week,
"day":day,
"date":date,
"found":found
};
}
function timedRefresh(timeoutPeriod) {
setInterval("init();",timeoutPeriod);
}
function getDayString(offset){
var day = new Date(new Date().getTime() + offset * 24 * 60 * 60 * 1000);
return getISOStringOfDate(day);
}
function addDateToWeekdays(startDate,startIndex){
var dateToAdd = startDate;
for ( i = startIndex; i < startIndex+5; i++ ){
daysArray[i] = getISOStringOfDate(dateToAdd);
var dayToShow = dateToAdd.getDate();
if ( dayToShow <=9 ) dayToShow = "0"+dayToShow;
document.getElementById("day"+i).textContent = dayToShow + " " + document.getElementById("day"+i).textContent;
dateToAdd = new Date(dateToAdd.getTime() + 24 * 60 * 60 * 1000);
}
}
function selectFacilityInDropdown() {
var sel = document.getElementById("selector");
var val = document.getElementById("facility"+ facility).value;
for(var i = 0, j = sel.options.length; i < j; ++i) {
if(sel.options[i].innerHTML === val) {
sel.selectedIndex = i;
break;
}
}
}
function printPlan(plan){
var oldTable = document.getElementById('plan'),
newTable = oldTable.cloneNode(true);
if ( plan != undefined && plan.meals != undefined ){
for(var i = 0; i < plan.meals.length; i++){
var tr = document.createElement('div');
tr.className = "mealFrame";
var mealWidth = "100%";
if( plan.meals[i].contains != undefined){
var img = document.createElement('img');
img.src = "img/" + plan.meals[i].contains + ".png";
img.alt = "This meal contains " + plan.meals[i].contains + ".";
img.className = "contains";
mealWidth = "90%";
tr.appendChild(img);
}
var td = document.createElement('div');
td.className = "category";
td.appendChild(document.createTextNode(plan.meals[i].category));
tr.appendChild(td);
var td = document.createElement('div');
td.className = "meal";
td.appendChild(document.createTextNode(plan.meals[i].meal));
td.style.width=mealWidth;
tr.appendChild(td);
var td = document.createElement('div');
td.className = "price";
if( plan.meals[i].price != undefined)
td.appendChild(document.createTextNode(plan.meals[i].price));
tr.appendChild(td);
newTable.appendChild(tr);
}
} else {
var tr = document.createElement('div');
tr.className = "mealFrame";
var mealWidth = "100%";
var td = document.createElement('div');
td.className = "category";
td.appendChild(document.createTextNode("geschlossen"));
tr.appendChild(td);
newTable.appendChild(tr);
}
oldTable.parentNode.replaceChild(newTable, oldTable);
}
document.getElementById("selector").onchange = function() {
facility = mensen[this.value];
setAnchor(facility,date,refresh);
};
function openDay(idx){
date = daysArray[idx];
setAnchor(facility,date,refresh);
}
function getISOStringOfDate(date2){
var dd = date2.getDate();
var mm = date2.getMonth()+1; //January is 0
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
var yyyy = date2.getFullYear();
return yyyy + '-' + mm + '-' + dd;
}
function getDateOfISOWeek(weekNumber) {
var year = new Date().getFullYear();
var simple = new Date(year, 0, 1 + (weekNumber - 1) * 7);
var dayOfWeek = simple.getDay();
var ISOweekStart = simple;
if (dayOfWeek <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}
function reset(){
for ( i = 0; i<10; i++ ){
document.getElementById("day"+i).className = "";
}
$( "div" ).remove( ".mealFrame" );
}
function setAnchor(facility,day,refresh){
if ( refresh != undefined )
window.location.assign('#'+facility+"&"+day+"&refresh");
else
window.location.assign('#'+facility+"&"+day);
}
/*
* Functions for touch and keyboard navigation
*/
// direction: -1 for yesterday, 1 for tomorrow
function adjacentDay(direction){
for ( i = 0; i<3; i++) {
var day = getISOStringOfDate(new Date(new Date(date).getTime() + direction * (i+1) * 24 * 60 * 60 * 1000));
if ( $.inArray(day, daysArray) != -1 ){
setAnchor(facility,day,refresh);
break;
}
}
}
// left/right swipe on mobile
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
var xDown = null;
var yDown = null;
function handleTouchStart(evt) {
xDown = evt.touches[0].clientX;
yDown = evt.touches[0].clientY;
};
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*ignore up / down swipe*/
if ( xDiff > 0 ) {
adjacentDay(1);
} else {
adjacentDay(-1);
}
}
/* reset values */
xDown = null;
yDown = null;
};
// left/right keyboard input
function pressed(e) {
cxc = e.keyCode;
if(cxc == 37 || cxc == 39){
e.preventDefault();
if(cxc == 37)//left
adjacentDay(-1);
if(cxc == 39)//right
adjacentDay(1);
}
}
</script>
<img src="http://mensaplan.fs-et.de/1x1.png" />
</body>
</html>