-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplum.js
363 lines (275 loc) · 8.12 KB
/
plum.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
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
var API_PATH = "api.php";
var API_ASYNC = false;
var RELOAD_INTERVAL = 5500;
State = {
overview : 1,
user : null,
locSelection : -1,
status: 1
}
var loginForm;
var rendering = false;
var sid = $.cookie("sid");
$( document ).ready( function () {
display();
setInterval(display, RELOAD_INTERVAL);
});
function myAjax( ajax ) {
//alert(ajax.url);
var result = $.ajax( ajax );
if ( result.responseJSON.error == "auth_failed" ) {/*alert("authfailed");*/ sid = ""; document.location = "http://plum.faui2k13.de/login.php"; return;}
return result;
}
function api_login(name, pw) {
return myAjax({
url: API_PATH + "?/login",
type: "POST",
data: {
name: loginForm.find( "input[name='name']" ).val(),
pw: loginForm.find( "input[name='pw']" ).val()
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_me() {
return myAjax({
url: API_PATH + "?/get/me",
type: "POST",
data: {
sid: sid
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_getLocationTypes() {
return myAjax({
url: API_PATH + "?/get/location_types",
type: "POST",
data: {
sid: sid
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_getLocations(type) {
return myAjax({
url: API_PATH + "?/get/locations",
type: "POST",
data: {
sid: sid,
type_id: type
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_getLocation(id) {
return myAjax({
url: API_PATH + "?/get/location",
type: "POST",
data: {
sid: sid,
id: id
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_getUsers(loc) {
return myAjax({
url: API_PATH + "?/get/users",
type: "POST",
data: {
sid: sid,
location_id: loc
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_checkin(loc, posx, posy) {
return myAjax({
url: API_PATH + "?/checkin",
type: "POST",
data: {
sid: sid,
location_id: loc,
posx: posx,
posy: posy
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_checkout() {
return myAjax({
url: API_PATH + "?/checkout",
type: "POST",
data: {
sid: sid,
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_updateStatus(status) {
return myAjax({
url: API_PATH + "?/set/status",
type: "POST",
data: {
sid: sid,
status: status
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
function api_logout() {
return myAjax({
url: API_PATH + "?/halt",
type: "POST",
data: {
sid: sid
},
dataType: "json",
async: API_ASYNC
}).responseJSON;
}
/*function startup_login() {
loginForm = $( "#loginForm" );
loginForm.on( "submit", function() {
result = api_login(
loginForm.find( "input[name='name']" ).val(),
loginForm.find( "input[name='pw']" ).val()
)
if ( result.error != "" ) {
alert("NOPE");
return false;
} else {
sid = result.result.sid;
user = api_me().result;
loginForm.fadeOut( 400, function() {
loginForm = loginForm.detach();
});
$( "body" ).append($( "<p id='welcome'>Welcome, " + user.name + "</p>"));
}
state = "location_table";
display();
return false;
});
loginForm.find( "input.btnRegister" ).on( "click", function () {
alert( "YOU SHALL NOT PASS!!!11");
});
}*/
function display() {
if (rendering || State.typing) return;
rendering = true;
State.user = api_me().result;
State.checkedInAt = State.user.checked_in_at == null ? -1 : State.user.checked_in_at;
var body = $( "body" );
var contentDiv = $( document.createElement("div") ).attr( "id", "content" );
if ( State.overview ) {
var locTable = $( document.createElement("table") );
var locTypes = api_getLocationTypes().result;
var tableContent = "";
var totalUsers = 0;
locTypes.forEach( function ( locType ) {
tableContent += "<tr class='locType'><td colspan=2>" + locType.name + "</td><td>" + (locType.usercount == null ? 0 : locType.usercount) + "</td></tr>";
var locs = api_getLocations(locType.id).result;
locs.forEach( function ( loc ) {
var checkinLink = loc.id == State.checkedInAt
? "<a href='' onclick='checkout(); return false'>checkout</a>"
: "<a href='' onClick='checkin(" + loc.id + "); return false;'>checkin</a>";
var locNameWithLink = loc.usercount <= 0 ? loc.name : "<a href='' onClick='listUsers(" + loc.id + "); return false;'>" + loc.name + "</a>";
tableContent += "<tr class='loc'><td class='btn_checkin'>" + checkinLink + "</td><td>" + locNameWithLink + "</td><td>" + loc.usercount + "</td></tr>";
totalUsers += parseInt(loc.usercount);
});
});
tableContent += "<tr class='total'><td colspan=2>total users:</td><td>" + totalUsers + " of max</td></tr>";
locTable.html( tableContent );
var locDiv = $( document.createElement("div") );
locDiv.attr({"id": "locs"});
locDiv.append( locTable );
contentDiv.append( locDiv );
}
if ( State.locSelection != -1 ) {
var loc = api_getLocation(State.locSelection).result;
if ( loc.usercount > 0 ) {
var userTable = $( document.createElement("table") );
var users = api_getUsers(State.locSelection).result;
var tableContent = "";
tableContent += "<tr><th colspan=3>" + loc.name + "</th><th>" + loc.usercount + "</th></tr>";
users.forEach( function ( user ) {
var date = new Date( user.checked_in_at_time * 1000 );
date = date.getFullYear() + "/" + (leading0(date.getMonth()+1)) + "/" + leading0(date.getDate()) + " " + leading0(date.getHours()) + ":" + leading0(date.getMinutes()) + ":" + leading0(date.getSeconds());
tableContent += "<tr><td>" + user.name + "</td><td>" + date + "</td><td class='status'>"
+ escape(user.status) + "</td></tr>";
});
userTable.html( tableContent );
var userDiv = $( document.createElement("div") );
userDiv.attr({"id": "loc"});
userDiv.append( userTable );
contentDiv.append( userDiv );
}
}
if ( State.status ) {
$( document.createElement("div") )
.attr("id", "status")
.append( $( document.createElement("table") )
.html("<tr><td>current status:</td><td>" + escape(State.user.status) + "</td></tr>"
+ "<tr><td>new status:</td><td><input name='newStatus' type='text' onfocus='State.typing = true' onblur='State.typing = false' /></td><td><button onClick='updateStatus(); return false' class='button-xsmall pure-button pure-button-primary btn'>update</button></td></tr>")
)
.appendTo( contentDiv );
}
body.html( "" )
.append( "<div id='logo'> <span class='logo'>plum<span class='underscore'>_</span></span></div>" )
.append( "<div id='welcome'>welcome, " + State.user.name + "</div>" )
.append( contentDiv )
.append( "<div id='logout'><button class='button-xsmall pure-button pure-button-primary btn' onClick='logout(); return false'>logout</button></div>" )
.append( "<div id='css_toggle'><label class='switch'><input type='checkbox' class='switch-input' name='switch-input' id='switch-input'"+($.cookie("switch-input") == "true" ? " checked='checked'":"")+"><span class='switch-label' data-on='on' data-off='off'></span><span class='switch-handle'></span></label></div>" );
rendering = false;
}
function checkin(loc) {
var result = api_checkin(loc, 0, 0);
if ( result.error != "" ) {
alert("Error occurred while checking in!\n" + result.error);
} else {
State.checkedInAt = loc;
//alert("Successfully checked in!");
display();
}
}
function checkout() {
api_checkout();
State.checkedInAt = -1;
display();
}
function listUsers(loc) {
State.locSelection = loc;
display();
}
function updateStatus() {
var newStatus = $( "input[name='newStatus']" ).val();
if ( api_updateStatus(newStatus).error != "" ) {
alert("Error occurred while updating! Please check your status.");
return;
}
display();
}
function logout() {
api_logout();
sid = "";
display();
}
function escape(str) {
return $( document.createElement("td") ).text( str ).html();
}
function leading0(time) {
return ( time < 10 ? "0" : "" ) + time;
}
function displayError(errorMsg) {
$( document.createElement("div") ).attr( "class", "error" ).text( errorMsg ).prependTo( $( "body" ) );
}