]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blob - templates/main.js
Update web pages a bit
[coffee/coffee-flask.git] / templates / main.js
1 var flask = "{{ url_for('hello', _external=True) }}"
2
3 var updateRemote = undefined;
4 var loggedIn = false;
5
6 console.log("hello from flask");
7 //sendJSON("{\"type\":\"empty\"}");
8
9 function update(id, msg) {
10     document.getElementById(id).innerHTML = msg;
11 }
12
13 function loadLocalStorage() {
14     if (localStorage) {
15         if (localStorage.length) {
16             for (var i = 0; i < localStorage.length; i++) {
17                 var value = localStorage.getItem(localStorage.key(i));
18                 try {
19                     updateRemote(value);
20                 } catch (err) {
21                     console.log("no json: " + value)
22                 }
23             }
24             localStorage.clear();
25         }
26     }
27 }
28
29 function hiddenUpdateRemote(json) {
30     var msg = JSON.parse(json);
31
32     //update("json", "json: " + JSON.stringify(msg))
33
34     switch(msg.type) {
35         case "empty":
36             //update("json","");
37             break;
38         case "rfid":
39             /*update("json",
40                 "uid: " + msg.uid + "<br>" +
41                 "card type: " + msg.card_type + "<br>" +
42                 "uid size: " + msg.size + " bytes<br>" +
43                 "sak: " + msg.sak
44             );*/
45             login(msg.uid);
46             break;
47         case "keys":
48             //update("json", "key: " + msg.key);
49             if (loggedIn) {
50                 var flavor = getFlavor(msg.key);
51                 if (flavor !== "") {
52                     addCoffee(flavor);
53                     loggedIn = false;
54                 }
55             }
56             break;
57         case "fuck":
58             ajax(msg.method, msg.route, msg.data, msg.id);
59             break;
60     }
61     sendLog(json);
62 }
63
64 function loadRemote(string) {
65     var xhr = new XMLHttpRequest();
66     xhr.onreadystatechange = function() {
67         if (this.readyState == 4 && this.status == 200) {
68             update("remote", this.responseText);
69             updateRemote = hiddenUpdateRemote;
70             loadLocalStorage();
71             if (loggedIn) {
72                 document.getElementById("local").style.display = "none";
73             }
74         }
75     };
76     xhr.open("GET", flask, true);
77     xhr.send();
78 }
79
80 loadRemote();
81
82 function ajax(method, route, data, id) {
83     var xhr = new XMLHttpRequest();
84     xhr.onreadystatechange = function() {
85         if (this.readyState == 4) {
86             if (this.status == 200) {
87                 update(id, this.responseText);
88             } else {
89                 updateRemote = undefined;
90                 update("remote", "<center>Server offline...</center>");
91                 document.getElementById("local").style.display = "block";
92                 if (localStorage) {
93                     var now = Date.now();
94                     var fuck = JSON.stringify({
95                         type: "fuck",
96                         method: method,
97                         route: route,
98                         data: data,
99                         id: id
100                     });
101                     localStorage.setItem(now, fuck);
102                     console.log(now + ": " + fuck);
103                 }
104             }
105         }
106     };
107     xhr.open(method, flask + route, true);
108     xhr.withCredentials = true;
109     xhr.setRequestHeader("Content-type", "application/json");
110     if (method === "POST") {
111         xhr.send(data);
112     } else {
113         xhr.send();
114     }
115 }
116
117 //var timer;
118
119 function login(id) {
120     ajax("POST", "login", id, "user");
121     loggedIn = true;
122     document.getElementById("local").style.display = "none";
123     //timer = setTimeout(logout, 20000);
124 }
125
126 function logout() {
127     //clearTimeout(timer);
128     sendReset();
129     ajax("GET", "logout", "", "user");
130     loggedIn = false;
131     document.getElementById("local").style.display = "block";
132 }
133
134 function renameUser() {
135     ajax("GET", "user/rename?name=" +  document.getElementById("username").value, "", "user");
136 }
137
138 function getFlavor(letter) {
139     switch (letter) {
140         case "E": return "espresso";
141         case "C": return "cappuccino";
142         case "B": return "latte macchiato";
143         case "D": return "espresso lungo";
144         default: return "";
145     }
146 }
147
148 function addCoffee(flavor) {
149     var data = JSON.stringify({
150         time: new Date().toISOString(),
151         flavor: flavor
152     });
153     ajax("POST", "coffee/add", data, "user");
154 }
155
156 function sendLog(json) {
157     ajax("POST", "log", json, "log");
158 }