-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepposcript.js
278 lines (264 loc) · 8.97 KB
/
stepposcript.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
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
var resultat;
var lan = new Object();
var kommun = new Object();
var skola = new Object();
var days = ["Mån", "Tis", "Ons", "Tors", "Fre"];
var meny;
var backbutton;
var startPos;
var weekIx;
function clear () {
var anchor = document.getElementById("header_left");
while (anchor.firstChild) {
anchor.removeChild(anchor.firstChild);
}
var anchor = document.getElementById("header_center");
while (anchor.firstChild) {
anchor.removeChild(anchor.firstChild);
}
var anchor = document.getElementById("header_right");
while (anchor.firstChild) {
anchor.removeChild(anchor.firstChild);
}
var anchor = document.getElementById("main");
while (anchor.firstChild) {
anchor.removeChild(anchor.firstChild);
}
}
/* section to be added back - and buttons removed - when 'touchend' event fires correctly on real device
function touchStart (e) {
console.log("x-pos: " + e.touches[0].pageX + " y-pos: " + e.touches[0].pageY);
startPos = e;
}
function touchEnd (e) {
console.log("x-pos: " + e.touches[0].pageX + " y-pos: " + e.touches[0].pageY);
if (e.touches[0].pageX > (startPos.touches[0].pageX + 30)) {
console.log("right swipe detected");
if (weekIx > 0) {
weekIx--;
display_meny(weekIx);
}
} else if ((e.touches[0].pageX + 30) < startPos.touches[0].pageX) {
console.log("leftt swipe detected");
if (weekIx < 4) {
weekIx++;
display_meny(weekIx);
}
}
}
function detectSwipe () {
var touchzone = document.getElementById("main");
touchzone.addEventListener("touchstart", touchStart, false);
touchzone.addEventListener("touchend", touchEnd, false);
}*/
function senare_vecka () {
if (weekIx < 4) {
weekIx++;
display_meny(weekIx);
}
}
function tidigare_vecka () {
if (weekIx > 0) {
weekIx--;
display_meny(weekIx);
}
}
function display_meny_page () {
clear();
console.log("skola vald");
backbutton = new Image();
backbutton.src = "back.png";
backbutton.addEventListener("click", display_skolor);
document.getElementById("header_left").appendChild(backbutton);
var para = document.createElement("p");
para.innerHTML = skola.name;
para.style.fontSize = "18px";
document.getElementById("header_center").appendChild(para);
//document.getElementById("header_center").appendChild(document.createElement("br"));
para = document.createElement("p");
//ktext.style.color = "red";
para.innerHTML = kommun.name;
document.getElementById("header_center").appendChild(para);
//document.getElementById("header_center").appendChild(document.createElement("br"));
para = document.createElement("p");
para.id = "weekNo";
document.getElementById("header_center").appendChild(para);
var logo = new Image();
logo.src = kommun.listOfSchools[0].logo_url;
document.getElementById("header_right").appendChild(logo);
var tabell = document.createElement("table");
tabell.id = "tabellen";
document.getElementById("main").appendChild(tabell);
var row, cell;
days.forEach(function (el, ix, arr) {
row = document.createElement("tr");
row.id = el;
row.className = "day_row";
tabell.appendChild(row);
cell = document.createElement("td");
cell.id = el + "date";
cell.className = "day_date_cell";
row.appendChild(cell);
para = document.createElement("p");
para.id = el + "dateday";
para.className = "day_date_day";
cell.appendChild(para);
para = document.createElement("p");
para.id = el + "datedate";
para.className = "day_date_date";
cell.appendChild(para);
cell = document.createElement("td");
cell.id = el + "meal";
cell.className = "day_meal_cell";
row.appendChild(cell);
});
var btn=document.createElement("input");
btn.type = "button";
btn.value = "tidigare";
btn.id = "tidigarebtn";
btn.addEventListener("click", tidigare_vecka);
document.getElementById("main").appendChild(btn);
btn=document.createElement("input");
btn.type = "button";
btn.value = "senare";
btn.id = "senarebtn";
btn.addEventListener("click", senare_vecka);
document.getElementById("main").appendChild(btn);
}
function display_meny (weekIndex) {
if (weekIndex == 2) {
document.body.style.background = "#99CCFF"
} else {
document.body.style.background = "#CCFFFF";
}
document.getElementById("weekNo").innerHTML = "Vecka " + meny.weeks[weekIndex].week;
for (var i = 0; i < days.length; i++) {
document.getElementById(days[i] + "dateday").innerHTML = days[i];
document.getElementById(days[i] + "datedate").innerHTML = meny.weeks[weekIndex].days[i].date;
if (meny.weeks[weekIndex].days[i].items) {
for (var j = 0; j < meny.weeks[weekIndex].days[i].items.length; j++) {
document.getElementById(days[i] + "meal").innerHTML = meny.weeks[weekIndex].days[i].items[j];
document.getElementById(days[i] + "meal").innerHTML += "<br />";
}
} else if (meny.weeks[weekIndex].days[i].reason) {
document.getElementById(days[i] + "meal").innerHTML = meny.weeks[weekIndex].days[i].reason;
}
}
document.getElementById("senarebtn").enabled = true;
document.getElementById("tidigarebtn").enabled = true;
if (weekIndex == 0) document.getElementById("tidigarebtn").enabled = false;
if (weekIndex == 4) document.getElementById("senarebtn").enabled = false;
}
function skola_vald () {
skola = kommun.listOfSchools[this.skola];
weekIx = 2;
display_meny_page();
function reqListener () {
console.log("got response2");
console.log(this.responseText);
meny = JSON.parse(this.responseText);
display_meny(weekIx);
//detectSwipe(); to be added back - and buttons removed - when 'touchend' event fires correctly on real device
}
console.log("got to get matsedel");
var oReq = new XMLHttpRequest({mozSystem: true});
oReq.onload = reqListener;
oReq.open("get", skola.url + "?fmt=json&v=2&limit=5&offset=-2", true);
oReq.send();
}
function display_skolor () {
clear();
console.log("kommun vald");
backbutton = new Image();
backbutton.src = "back.png";
backbutton.addEventListener("click", display_kommuner);
document.getElementById("header_left").appendChild(backbutton);
var textnode = document.createTextNode(kommun.name);
document.getElementById("header_center").appendChild(textnode);
document.getElementById("header_center").appendChild(document.createElement("br"));
var logo = new Image();
logo.src = kommun.listOfSchools[0].logo_url;
document.getElementById("header_right").appendChild(logo);
var anchor = document.getElementById("main");
for (var index in kommun.listOfSchools) {
console.log("another skola");
var node=document.createElement("LI");
var btn=document.createElement("input");
btn.type = "button";
btn.value = kommun.listOfSchools[index].name;
btn.id = "mainbtn";
btn.skola = index;
btn.addEventListener("click", skola_vald);
node.appendChild(btn);
anchor.appendChild(node);
}
}
function kommun_vald () {
kommun.listOfSchools = lan.listOfKommuner[this.kommun];
kommun.name = this.value;
display_skolor();
}
function display_kommuner () {
clear();
console.log("län valt");
backbutton = new Image();
backbutton.src = "back.png";
backbutton.addEventListener("click", display_lan);
document.getElementById("header_left").appendChild(backbutton);
var textnode = document.createTextNode(lan.name);
document.getElementById("header_center").appendChild(textnode);
document.getElementById("header_center").appendChild(document.createElement("br"));
var logo = new Image();
logo.src = "mat90.png";
document.getElementById("header_right").appendChild(logo);
var anchor = document.getElementById("main");
for (var index in lan.listOfKommuner) {
console.log("another kommun");
var node=document.createElement("LI");
btn=document.createElement("input");
btn.type = "button";
btn.value = index;
btn.kommun = index;
btn.id = "mainbtn";
btn.addEventListener("click", kommun_vald);
node.appendChild(btn);
anchor.appendChild(node);
}
}
function lan_valt () {
lan.listOfKommuner = resultat.provinces[this.lan];
lan.name = this.value;
display_kommuner();
}
function display_lan () {
clear();
var logo = new Image();
logo.src = "mat90.png";
document.getElementById("header_right").appendChild(logo);
var anchor = document.getElementById("main");
for (var index in resultat.provinces) {
var node=document.createElement("LI");
var btn=document.createElement("input");
btn.type = "button";
btn.value = index;
btn.id = "mainbtn";
btn.lan = index;
btn.addEventListener("click", lan_valt);
node.appendChild(btn);
anchor.appendChild(node);
}
}
function start () {
function reqListener () {
console.log("got response");
console.log(this.responseText);
resultat = JSON.parse(this.responseText);
display_lan();
}
console.log("got to get json");
var oReq = new XMLHttpRequest({mozSystem: true});
oReq.onload = reqListener;
oReq.open("get", "http://meny.dinskolmat.se/?fmt=json", true);
oReq.send();
}
window.addEventListener('load', start);