[go: up one dir, main page]

Menu

[0413bb]: / js / menu.js  Maximize  Restore  History

Download this file

564 lines (514 with data), 21.6 kB

  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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**
* Used by the slide out menu
*/
var menuTimeout; // Will control if we are updating menu or not.
var getBackendRunningTimeout;
var focusedMenuItemName = 'none';
var buttonLocked;
var redMuted = false;
var redslider;
var yellowMuted = false;
var slider;
//Event Handler called when document is first loaded.
$(document).ready(function() {
//Event Handler when a user clicks on the menu. Opens the menu.
$('.menu').click(function() {
setMenuConfigInfo();
getBackendRunningTimeout = setTimeout(updateBackendRunning, 5000); //retrieve every 5 secs
//menuTimeout = setTimeout(setMenuConfigInfo, 5000);
$('nav').addClass('open');
$('body').addClass('menu-open');
//If tour is running, have it advance when menu is open.
if(!tour.ended())tour.next();
return false;
});
//Event Handler when a user clicks anywhere but the menu, when the menu is open. Closes the menu.
$(".grid").click(function() {
clearTimeout(getBackendRunningTimeout); // Remove the timer.
clearTimeout(menuTimeout); // Remove the timer.
$('body').removeClass('menu-open');
$('nav').removeClass('open');
$(".ajax-file-upload-container").fadeOut(); //cause upload messages to disappear
$("#eventsmessage").fadeOut(); //cause upload messages to disappear
});
//We need to check the class of id stopStartButton. and set the buttons text
buttonLocked = false; // don't allow ajax calls to be sent if buttonLock is true
var buttonClass = $("#stopStartButton").attr('class');
var newButtonText = (buttonClass == 'backendRunning' ? 'STOP' : 'START');
var newErrorText = (buttonClass == 'backendRunning' ? 'Backend Running' : 'Backend Stopped');
$("#stopStartButton").text(newButtonText);
$("#stopStartLabel").text(newButtonText + " Backend");
$(".startBackendErrorOutput").text(newErrorText);
/*setup alarms*/
redAudioElement = document.createElement('audio');
redAudioElement.setAttribute('src', 'alarms/'+redAlarm);
redAudioElement.setAttribute('preload', 'preload');
redAudioElement.setAttribute('id', 'redAudioElement');
yellowAudioElement = document.createElement('audio');
yellowAudioElement.setAttribute('src', 'alarms/'+yellowAlarm);
yellowAudioElement.setAttribute('preload', 'preload');
yellowAudioElement.setAttribute('id', 'yellowAudioElement');
$.get();
redAudioElement.addEventListener("load", function() {
}, true);
$('.playPager').click(function() {
redAudioElement.play();
});
$('.pausePager').click(function() {
redAudioElement.pause();
});
yellowAudioElement.addEventListener("load", function() {
}, true);
$('.playBleep').click(function() {
yellowAudioElement.play();
});
$('.pauseBleep').click(function() {
yellowAudioElement.pause();
});
//When the volume icon is clicked it toggles that alarms mute
$(".redvolume").click(function() {
if(redMuted){
console.log("unmute red Volume");
redMuted = false;
redAudioElement.volume = (redslider.slider('value') / 100);
$("#redMute").hide();
}else{
console.log("mute red volume");
redMuted = true;
redAudioElement.volume = 0;
$("#redMute").show();
}
});
$(".yellowvolume").click(function() {
if(yellowMuted){
console.log("unmute yellow Volume");
yellowMuted = false;
yellowAudioElement.volume = (slider.slider('value') / 100);
$("#yellowMute").hide();
}else{
console.log("mute yellow volume");
yellowMuted = true;
yellowAudioElement.volume = 0;
$("#yellowMute").show();
}
});
$("#yellowuploader").uploadFile({
url:"php/uploadFile.php",
acceptFiles: "audio/*",
fileName:"myfile",
formData: {alarmType: "yellow"},
onLoad:function(obj){
},
onSubmit:function(files){
if (files.toString().toLowerCase().indexOf("mp3") >= 0){
}else{
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Error, wrong file format. .mp3 ONLY!");
return false;
}
},
onSuccess:function(files,data,xhr,pd){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Success for: "+JSON.stringify(data));
yellowAlarm = files.toString();
yellowAudioElement.setAttribute('src', 'alarms/'+yellowAlarm);
},
afterUploadAll:function(obj){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>All files are uploaded");
},
onError: function(files,status,errMsg,pd){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Error for: "+JSON.stringify(errMsg));
},
onCancel:function(files,pd){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Canceled files: "+JSON.stringify(files));
}
});
$("#reduploader").uploadFile({
url:"php/uploadFile.php",
acceptFiles: "audio/*",
fileName:"myfile",
formData: {alarmType: "red"},
onLoad:function(obj){
},
onSubmit:function(files){
if (files.toString().toLowerCase().indexOf("mp3") >= 0){
}else{
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Error, wrong file format. .mp3 ONLY!");
return false;
}
},
onSuccess:function(files,data,xhr,pd){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Success for: "+JSON.stringify(data));
redAlarm = files.toString();
redAudioElement.setAttribute('src', 'alarms/'+redAlarm);
},
afterUploadAll:function(obj){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>All files are uploaded");
},
onError: function(files,status,errMsg,pd){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Error for: "+JSON.stringify(errMsg));
},
onCancel:function(files,pd){
$("#eventsmessage").html($("#eventsmessage").html()+"<br/>Canceled files: "+JSON.stringify(files));
}
});
//Setup the volume control functions
$(function() {
redslider = $('#redslider'),
redtooltip = $('.redtooltip');
redtooltip.hide();
redslider.slider({
range: "min",
min: 0,
max: 100,
step: 1,
value: 35,
start: function(event,ui) {
redtooltip.fadeIn('fast');
},
slide: function(event, ui) {
var value = redslider.slider('value'),
redvolume = $('.redvolume');
redtooltip.css('left', value).text(ui.value);
if(value <= 5) {
redvolume.css('background-position', '0 0');
}else if (value <= 25) {
redvolume.css('background-position', '0 -25px');
}else if (value <= 75) {
redvolume.css('background-position', '0 -50px');
}else{
redvolume.css('background-position', '0 -75px');
};
},
stop: function(event,ui) {
redtooltip.fadeOut('fast');
redAudioElement.volume = (redslider.slider('value') / 100);
console.log("Red Alarm Volume: " + redslider.slider('value'));
},
});
slider = $('#yellowslider'),
yellowtooltip = $('.yellowtooltip');
yellowtooltip.hide();
slider.slider({
range: "min",
min: 0,
max: 100,
step: 1,
value: 35,
start: function(event,ui) {
yellowtooltip.fadeIn('fast');
},
slide: function(event, ui) {
var value = slider.slider('value'),
volume = $('.yellowvolume');
yellowtooltip.css('left', value).text(ui.value);
if(value <= 5) {
volume.css('background-position', '0 0');
}else if (value <= 25) {
volume.css('background-position', '0 -25px');
}else if (value <= 75) {
volume.css('background-position', '0 -50px');
}else{
volume.css('background-position', '0 -75px');
};
},
stop: function(event,ui) {
yellowtooltip.fadeOut('fast');
yellowAudioElement.volume = (slider.slider('value') / 100);
console.log("Yellow Alarm Volume: " + slider.slider('value'));
},
});
});
});
/** Ajax call used to put the current backend status, "backendRunning" or "backendStopped
* in the startStopButton class" */
function updateBackendRunning(){
clearTimeout(getBackendRunningTimeout); // Remove the timer.
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
postData = {getBackendRunning:true};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
var jsonData = JSON.parse(result);
if(jsonData['success']){ // we succedded
var newButtonText = (jsonData['backendStatus'] == 'backendRunning' ? 'STOP' : 'START');
var newErrorText = (jsonData['backendStatus'] == 'backendRunning' ? 'Backend Running' : 'Backend Stopped');
$("#stopStartButton").removeClass("backendRunning");
$("#stopStartButton").removeClass("backendStopped");
$("#stopStartButton").addClass(jsonData['backendStatus']);
$("#stopStartButton").text(newButtonText);
$("#stopStartLabel").text(newButtonText + " Backend");
$(".startBackendErrorOutput").text(newErrorText);
$(".startBackendErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else{ //Not an admin, tell the user
$(".startBackendErrorOutput").text(jsonData['err']);
$(".startBackendErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}
},
error: function(xhr,status,error){
$(".startBackendErrorOutput").text("Error in updateBackendRunning ajax call.");
}
}); // End of ajax call.
})(); //End of worker thread.
getBackendRunningTimeout = setTimeout(updateBackendRunning, 5000);
}
/** Will send an ajax call to stop the backend if it is already started,
* or start the backend if it is stopped.
*/
function stopStartBackend(){
var buttonClass = $("#stopStartButton").attr('class');
//tells us if our backend is already running based on button class.
var backendRunning = (buttonClass == 'backendRunning' ? true : false);
if(!buttonLocked){ // We don't want to allow the user to continuously push a dangerous button.
buttonLocked = true; // start lock
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
var startOrStop = (buttonClass == 'backendRunning' ? 'stop' : 'start');
postData = {startStopBackend:startOrStop};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
var jsonData = JSON.parse(result);
$("#stopStartButton").text(jsonData['newButtonVal']);
$("#stopStartButton").toggleClass('backendRunning'); // change the button class
$("#stopStartButton").toggleClass('backendStopped');
$("#stopStartLabel").text(jsonData['newButtonVal'] + " Backend");
$('.startBackendErrorOutput').text(jsonData['returnVal']); // Report back
$(".startBackendErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
},
error: function(xhr,status,error){
$(".startBackendErrorOutput").text("Error in ajax call.");
$(".startBackendErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}
}); // End of ajax call.
})(); //End of worker thread.
}else{ // the button is currently locked
$(".startBackendErrorOutput").text("Don't Spam This Button."); //Scold the admin
}
setTimeout(clearButtonLock, 5000);
}
/** sets the buttonLocked variable to false. */
function clearButtonLock(){
$(".startBackendErrorOutput").text("-"); // set text back to default when lock is reset
buttonLocked = false; //stop lock
}
// A Set button was pressed on the config menu, we are setting that value in the db here.
function setConfigValue(configToSet){
var newVal = $('.'+configToSet).val();
//alert("setConfigValue: " + configToSet + " " + newVal);
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
postData = {setConfigValue:true,
name:configToSet,
value:newVal};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
//setMenuData(result); we might not need to do anything at all if successful.
setMenuItemDisplay(result);
},
complete: function(result) {
// Schedule the next request when the current one's complete
//alert("complete" + result);
//setMenuItemDisplay(result);
},
error: function(xhr,status,error){
alert("error" + error);
}
}); // End of ajax call.
})(); //End of worker thread.
}
/** Parses a single config value from json and updates the display on the menu. */
function setMenuItemDisplay(data){
var jsonData = JSON.parse(data);
var name = jsonData.name;
var value = jsonData.value;
$('.' + name).val(value);
$('.' + name).stop().css("background-color", "#3366FF")
.animate({ backgroundColor: "#FFFFFF"}, 1500); // Flashes a new color to the value changed.
//alert("menu item displayed");
}
/** Sets the menu's specific menu items, parses with json. */
function setMenuData(data){
var jsonData = JSON.parse(data);
for(var i = 0; i < jsonData.length; i++){
var nameSelector = jsonData[i].name;
var value = $(' '+jsonData[i].value);
var description = $(' '+jsonData[i].description);
if(nameSelector != focusedMenuItemName){ // Do not update the displayed value, if that input is focused.
$('.'+nameSelector).val(value.selector);
}
}
}
/** Retrieves the several menu config settings
* from the backend through a ajax and php db call.
* populates the values in the menu.
*/
function setMenuConfigInfo(norepeat){ // Called by grid.php and device.php document ready.
//alert("setMenuConfigInfo");
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
postData = {getConfigData:true};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
setMenuData(result);
},
complete: function(result) {
// Schedule the next request when the current one's complete
//alert("complete" + result);
},
error: function(xhr,status,error){
alert("error" + error);
}
}); // End of ajax call.
})(); //End of worker thread.
if(!norepeat) menuTimeout = setTimeout(setMenuConfigInfo, 5000);
}
/** Focus handler for menu input items. Needed to disable race condition.
* between timer updates, and human input.
* @param inputName The class name of the input we are focused on.
*/
function setMenuInputFocusIn(x){
//alert(inputName + " has focus.");
if($(x).is(":focus")){ // due to cross browswer difficulties, we need to check if focused or nto.
focusedMenuItemName = $(x).attr('class');
x.style.background = "yellow";
}else{
setMenuInputFocusOut(x); //if not we call this handler.
}
}
/** Focus handler for menu input items. Needed to disable race condition.
* between timer updates, and human input.
* @param x The Dom Object that lost focus.
*/
function setMenuInputFocusOut(x){
//console.log(x);
focusedMenuItemName = 'none';
x.style.background = "white";
}
/** Called When the users presses the password set button. */
function changePassword(){
var pass1 = $('.changePassword1').val();
var pass2 = $('.changePassword2').val();
if(pass1 == '' || pass2 == ''){
$('.errorOutput').text("Both Fields Must Contain a Password.");
$(".errorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else if(pass1 != pass2){
$('.errorOutput').text("Passwords Don't Match.");
$(".errorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else{
var newPassword = $.md5(pass1);
$('.errorOutput').text("Changing Password");
$(".errorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
postData = {changePassword:true,
newPass:newPassword};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
$('.errorOutput').text("Password Changed.");
$(".errorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
},
complete: function(result) {
//$('.errorOutput').text("Password Changed.");
},
error: function(xhr,status,error){
$('.errorOutput').text("Error Changing Password.");
$(".errorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}
}); // End of ajax call.
})(); //End of worker thread.
}
}
/** Called by the menu when the admin adds a new user. */
function addNewUser(){
var userName = $('.newUserName').val();
var pass = $('.newUserPass').val();
pass = $.md5(pass);
var adminLvl = $('.adminLvl').val();
// Can't add a user if any field is empty
if(userName == '' || pass == '' || adminLvl == ''){
$('.newUserErrorOutput').text("All fields must be filled.");
$(".newUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else if(isNaN(adminLvl)){
$('.newUserErrorOutput').text("Admin lvl must be between 0 and 10, inclusive.");
$(".newUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else if(parseInt(adminLvl) > 10 || parseInt(adminLvl) < 0){
$('.newUserErrorOutput').text("Admin lvl must be between 0 and 10, inclusive.");
$(".newUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else{
//$('.newUserErrorOutput').text("Adding user " + userName + " " + pass + " " + adminLvl);
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
postData = {addUser:true,
newUserName:userName,
newPassword:pass,
newAdminLvl:adminLvl};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
var jsonData = JSON.parse(result);
$('.newUserErrorOutput').text(jsonData['returnVal']);
$(".newUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
},
complete: function(result) {
var jsonData = JSON.parse(result);
$('.newUserErrorOutput').text(jsonData['returnVal']);
$(".newUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
},
error: function(xhr,status,error){
$('.newUserErrorOutput').text("Error Adding User.");
$(".newUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}
}); // End of ajax call.
})(); //End of worker thread.
}
}
/** Called by frontend when admin removes a user. */
function removeUser(){
var userName = $('.removeUsername').val();
if(userName == ''){
$('.removeUserErrorOutput').text("Username is Blank!");
$(".removeUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}else{
$('.removeUserErrorOutput').text("Removing User " + userName);
$(".removeUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
(function worker() { // Start a worker thread to grab the data so we don't freeze anything on our page.
postData = {removeUser:true,
removeUsername:userName};
// Send the request to the server.
$.ajax({
type:"POST",
data : postData,
url: 'php/menu-backend.php',
success: function(result,status,xhr) {
var jsonData = JSON.parse(result);
$('.removeUserErrorOutput').text(jsonData['returnVal']);
$(".removeUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
},
complete: function(result) {
var jsonData = JSON.parse(result);
$('.removeUserErrorOutput').text(jsonData['returnVal']);
$(".removeUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
},
error: function(xhr,status,error){
$('.removeUserErrorOutput').text("Error Removing.");
$(".removeUserErrorOutput").stop().css("color", "#FFFFFF").animate({ color: "#FF0000"}, 1500);
}
}); // End of ajax call.
})(); //End of worker thread.
}
}