Skip to content

Instantly share code, notes, and snippets.

@ezfe
Created August 6, 2014 04:02
Show Gist options
  • Save ezfe/da1e11133672652ebece to your computer and use it in GitHub Desktop.
Save ezfe/da1e11133672652ebece to your computer and use it in GitHub Desktop.
var classes = {};
var times = {
"travel": 0,
"full": 1,
"am": 2,
"pm": 3
};
function getClassType(cl) {
if (cl.travel) {
return 'travel'
} else {
return cl['type'];
}
}
function getTimeIndex(cl) {
return times[getClassType(cl)];
}
function sendNetworkRequest(operation,user,cl,obj,index) {
$.ajax({
type: "POST",
url: "editAction.php",
data: { "operation": operation, "user": btoa(user), "class": btoa(cl), "index": index }
})
.done(function( msg ) {
var tr = obj.parent().parent();
if (msg == "TRUE" && operation == "remove") {
tr.html('<td width="20px"><span class="glyphicon glyphicon-ban-circle"></span></td><td class="text-danger">' + user + '</td>');
} else if (msg == "TRUE" && operation == "add") {
tr.before('<tr><td width="20px"><span class="studentremovebutton glyphicon glyphicon-remove-circle" netuser="'+user+'" netclass="'+cl+'"></span></td><td class="text-success">'+user+'</td></tr>');
$('#addUser'+obj.attr('netclass')).val('');
} else if (msg == "TRUE" && operation == "waitlist") {
var netclass = $(this).attr('netclass');
tr.remove();
$('#addUser'+obj.attr('netclass')).parent().parent().parent().before('<tr><td width="20px"><span class="studentremovebutton glyphicon glyphicon-remove-circle" netuser="'+user+'" netclass="'+cl+'"></span></td><td class="text-success">'+user+'</td></tr>');
} else {
alert(msg);
}
});
}
function addUser(user,cl) {
sendNetworkRequest('add',$('#').val(),cl);
}
$(document).ready(function(){
$(document).on('click','.studentremovebutton',function(){
if ($(this).attr('netuser') != undefined && $(this).attr('netclass') != undefined) {
sendNetworkRequest('remove',$(this).attr('netuser'),$(this).attr('netclass'),$(this));
} else {
return false;
}
});
$(document).on('click','.addstudentbutton',function(){
if ($(this).attr('netclass') != undefined && $('#addUser'+$(this).attr('netclass')).val() != "") {
sendNetworkRequest('add',$('#addUser'+$(this).attr('netclass')).val(),$(this).attr('netclass'),$(this));
} else {
return false;
}
});
$(document).on('click','.waitlistAction',function(){
if ($(this).attr('netclass') != undefined && $(this).attr('netindex') != undefined) {
sendNetworkRequest('waitlist',$(this).attr('netuser'),$(this).attr('netclass'),$(this),$(this).attr('netindex'));
} else {
return false;
}
});
$('.addUserForm').submit(function(event){
$('#'+$(this).attr('netid')).click();
event.preventDefault();
});
$('#content').html('');
$.ajax({
"type": "GET",
"url": "/api/classes"
}).done(function(content){
classes = content;
$('#content').html($('<table>').addClass('table table-bordered').attr('id','classes'));
$('#classes').append($('<tbody>'));
var tableBody = $('#classes').children().filter('tbody');
for (var i = 0; i < Object.keys(classes).length; i++) {
var classKey = Object.keys(classes)[i];
var classObject = classes[classKey];
tableBody.append($('<tr>').append($('<th>').attr('colspan','2').attr('id',classKey).append($('<a>').attr('href','/api/classes').html(classObject['displayname']))));
var enrolledList = '';
for (var x = 0; x < classObject['enrolled'].length; x++) {
var student = classObject['enrolled'][x];
enrolledList += '<tr><td width="20px">';
enrolledList += '<span class="studentremovebutton glyphicon glyphicon-remove-circle"netuser="' + student + '" netclass="' + classKey + '"></span>';
enrolledList += '</td><td>';
enrolledList += student;
enrolledList += '</td></tr>';
};
var waitlistTable = '';
if (classObject['waitlist']) {
if (classObject['waitlist'].length > 0) {
waitlistTable += '<table class="table"><tbody>';
waitlistTable += '<tr><th></th><th>Name</th><th>Other Requirement</th><th style="text-align:right;">Current</th><th style="text-align:left;">#</th></tr>';
for (var x = 0; x < classObject['waitlist'].length; x++) {
var studentObject = classObject['waitlist'][x];
waitlistTable += '<tr><td>';
waitlistTable += '<span netuser="' + studentObject['name'] + '" netclass="' + classKey + '" netindex="' + x + '" class="waitlistAction glyphicon glyphicon-chevron-left"></span>';
waitlistTable += '</td><td>' + studentObject.name + '</td>';
if (classObject['type'] == 'am') {
waitlistTable += '<td>' + studentObject['wantedpm'] + '</td>';
} else if (classObject['type'] == 'pm') {
waitlistTable += '<td>' + studentObject['wantedam'] + '</td>';
} else {
waitlistTable += '<td>';
}
waitlistTable += '<td>' + studentObject['currentClass'] + '</td>';
waitlistTable += '<td>' + studentObject['currentChoice'] + '</td>';
waitlistTable += '</tr>';
}
waitlistTable += '</tbody></table>';
}
}
tableBody.append($('<tr>').append($('<td>').width('50%').append('<table>').append($('<tbody>').append(enrolledList).append($('<tr>').append($('<td>').width('20%').append($('<span>').attr('id','addUserButton' + classKey).addClass('addstudentbutton glyphicon glyphicon-ok-circle').attr('netclass',classKey))).append($('<td>').append($('<form>').addClass('addUserForm').attr('netid','addUserButton' + classKey).append($('<input>').attr('id','addUser' + classKey).addClass('addUserField').attr('type', 'text').attr('name', 'user').attr('placeholder','Click to add a user...'))))))).append($('<td>').append(waitlistTable)));
};
setTimeout(function(){
$('#loader').hide();
$('#content').show();
},500);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment