function changed_name() {
var username = localStorage.getItem("c3-rating-user");
var newname = document.getElementById('Username').value;
document.getElementById('Username').classList.toggle('changed', username != newname);
}
function changed_filter() {
var filtertext = document.getElementById('Filter').value.toLowerCase();
if (filtertext == '') {
document.querySelectorAll('.event-list-item.filtered').forEach(function(ev) {
ev.classList.remove("filtered");
});
return;
}
if (!window.inner_texts) {
window.inner_texts = {};
document.querySelectorAll('.event-list-item').forEach(function(ev) {
inner_texts[ev.getAttribute('id')] = ev.innerText.toLowerCase();
});
}
Object.keys(window.inner_texts).forEach(function (eid) {
var elem = document.getElementById(eid);
elem.classList.toggle('filtered', window.inner_texts[eid].indexOf(filtertext) < 0);
});
}
function confirm_name() {
localStorage.setItem("c3-rating-user", document.getElementById('Username').value);
document.getElementById('Username').classList.remove("changed");
update_status();
}
function toggleHidden(name) {
document.getElementById(name).classList.toggle("hidden");
}
function toggleEdit(eid) {
var username = document.getElementById('Username').value;
if (!username) {
alert( "Please set your name before rating.");
return;
}
var ev = document.getElementById('event-'+eid);
if (ev.classList.contains('editing')) {
ev.classList.toggle('editing', false);
return;
}
var other_in_edit = document.getElementsByClassName('editing');
if (other_in_edit.length)
other_in_edit[0].classList.remove('editing');
ev.classList.toggle('editing', true);
var own_rating = document.getElementById('event-own-rating');
ev.appendChild(own_rating);
var myrating = document.querySelectorAll('div#event-rating-'+eid+'[submitter="'+username+'"]');
var mycomment = '';
if (myrating.length) {
mycomment = myrating[0].getElementsByClassName('event-rating-comment')[0].innerHTML;
myrating[0].querySelectorAll('meter').forEach(function(meter) {
var category = meter.getAttribute('category');
document.querySelectorAll('#event-own-rating .slider input[category='+category+']')[0].value = meter.value;
changeVal('event-'+category+'-output', meter.value);
});
} else {
document.querySelectorAll('#event-own-rating .slider input').forEach(function(sl) {
sl.value = 0;
var category = sl.getAttribute('category');
changeVal('event-'+category+'-output', '0');
});
}
document.getElementById('event-comment').value = mycomment;
own_rating.querySelectorAll('button.remove-rating')[0].classList.toggle('hidden', !myrating.length);
}
function changeVal(el, value) {
document.getElementById(el).innerHTML = value.toString() + " %";
}
function twocol() {
document.body.classList.toggle('two-column');
document.body.classList.remove('three-column');
document.body.classList.remove('four-column');
}
function threecol() {
document.body.classList.remove('two-column');
document.body.classList.toggle('three-column');
document.body.classList.remove('four-column');
}
function fourcol() {
document.body.classList.remove('two-column');
document.body.classList.remove('three-column');
document.body.classList.toggle('four-column');
}
function invert_sort() {
var evl = document.getElementById('event-list');
var nodes = Array.prototype.slice.call(evl.getElementsByClassName('event-list-item')).reverse().forEach(function(el) {
evl.appendChild(el);
});
}
function sort_by(order_function) {
var evl = document.getElementById('event-list');
Array.prototype.slice.call(evl.getElementsByClassName('event-list-item')).sort(order_function).forEach(function(el) {
evl.appendChild(el);
});
}
function myrating_count_sort(elem1, elem2) {
var username = document.getElementById('Username').value;
return elem2.querySelectorAll('.event-rating[submitter="'+username+'"]').length - elem1.querySelectorAll('.event-rating[submitter="'+username+'"]').length;
}
function random_sort(elem1, elem2) { return !!Math.floor(Math.random() * 2) ? -1 : 1; }
function rating_count_sort(elem1, elem2) { return elem2.querySelectorAll('.event-rating').length - elem1.querySelectorAll('.event-rating').length; }
function rating_1_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-0')[0].getAttribute('value') - elem1.getElementsByClassName('meter-0')[0].getAttribute('value'); }
function rating_2_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-1')[0].getAttribute('value') - elem1.getElementsByClassName('meter-1')[0].getAttribute('value'); }
function rating_3_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-2')[0].getAttribute('value') - elem1.getElementsByClassName('meter-2')[0].getAttribute('value'); }
function rating_4_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-3')[0].getAttribute('value') - elem1.getElementsByClassName('meter-3')[0].getAttribute('value'); }
function rating_5_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-4')[0].getAttribute('value') - elem1.getElementsByClassName('meter-4')[0].getAttribute('value'); }
function coordinator_sort(elem1, elem2) { return get_coordinator(elem1).localeCompare(get_coordinator(elem2)); }
function state_sort(elem1, elem2) { return elem2.getAttribute('event_state').localeCompare(elem1.getAttribute('event_state')); }
function get_coordinator(elem) {
var coordinator = elem.getElementsByClassName('event-coordinator');
if (coordinator.length)
return coordinator[0].getAttribute('coordinator');
return '';
}
function do_remove_rating() {
if (confirm('are you sure?') == false)
return;
var in_edit = document.getElementsByClassName('editing');
if (!in_edit.length)
return;
var eid = in_edit[0].getAttribute('id').replace(/^event-/, '');
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "api/remove_rating/"+eid, true);
xhttp.setRequestHeader("Content-type", "application/json");
var username = document.getElementById('Username').value;
xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var myrating = document.querySelectorAll('div#event-rating-'+eid+'[submitter="'+username+'"]');
if(myrating.length)
myrating[0].parentNode.removeChild(myrating[0]);
toggleEdit(eid);
update_status();
}
}
xhttp.send( JSON.stringify( { 'author': username } ) );
}
function do_remove_event(eid) {
var ev = document.getElementById('event-'+eid);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "api/remove_event/"+eid, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
ev.parentNode.removeChild(ev);
}
}
xhttp.send();
}
function do_rate() {
var in_edit = document.getElementsByClassName('editing');
if (!in_edit.length)
return;
var eid = in_edit[0].getAttribute('id').replace(/^event-/, '');
var username = document.getElementById('Username').value;
if (!username) {
alert( "Please set your name before rating.");
return;
}
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "api/add_rating/"+eid, true);
xhttp.setRequestHeader("Content-type", "application/json");
ratings = {};
document.getElementById('event-own-rating').querySelectorAll('.category-slider input').forEach(function(ev) {
ratings[ev.getAttribute("category")] = ev.value;
});
var comment = document.getElementById('event-comment').value;
xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var myrating = document.querySelectorAll('div#event-rating-'+eid+'[submitter="'+username+'"]');
if (myrating.length) {
myrating = myrating[0];
} else {
myrating = document.getElementById('event-rating-new').cloneNode(true);
myrating.setAttribute('id', 'event-rating-'+eid);
myrating.classList.remove('hidden');
myrating.setAttribute('submitter', username);
myrating.getElementsByClassName('event-rating-submitter')[0].innerHTML = username + ':';
document.querySelectorAll('#event-'+eid+' .event-ratings')[0].append(myrating);
}
myrating.getElementsByClassName('event-rating-comment')[0].innerHTML = comment;
for (category in ratings) {
myrating.querySelectorAll('meter[category='+category+']')[0].value = ratings[category];
myrating.querySelectorAll('.event-rating-category-output[category='+category+']')[0].innerHTML = ' ' + categories[category] + ' ' + ratings[category] + ' %';
}
toggleEdit(eid);
update_status();
}
}
xhttp.send( JSON.stringify( {
'author': username,
'comment': comment,
'ratings': ratings
} ) );
}
function do_set_state(eid, state) {
if ( state == document.getElementById('event-'+eid).getAttribute('event_state'))
state = '';
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "api/set_event_state/"+eid, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
document.getElementById('event-'+eid).setAttribute('event_state', state);
update_status();
}
}
xhttp.send( JSON.stringify( { 'state': state } ) );
}
function do_take(eid) {
var username = document.getElementById('Username').value;
if (!username) {
alert( "Please set your name before taking an event.");
return;
}
var ev = document.getElementById('event-'+eid);
if (ev.classList.contains('i-am-coordinator'))
username = '';
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "api/set_event_coordinator/"+eid, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var coor = ev.getElementsByClassName('event-coordinator');
if (coor.length) {
if (username) {
coor[0].innerHTML = 'coordinator: '+username;
coor[0].setAttribute('coordinator', username);
} else
coor[0].parentNode.removeChild(coor[0]);
} else {
if (username) {
var pers = ev.getElementsByClassName('event-persons');
coor = document.createElement('div');
coor.classList.toggle('event-coordinator', true);
coor.setAttribute('coordinator', username);
coor.innerHTML = 'coordinator: '+username;
pers[0].insertBefore(coor, pers[0].firstChild);
}
}
update_status();
}
}
xhttp.send( JSON.stringify( { 'coordinator': username } ) );
}
function update_status() {
var accepted_count = document.querySelectorAll('.event-list-item[event_state=accepted]').length;
var rejected_count = document.querySelectorAll('.event-list-item[event_state=rejected]').length;
var taken_count = document.querySelectorAll('.event-list-item .event-coordinator').length;
var total_count = document.getElementsByClassName('event-list-item').length;
var total_voted_count = document.querySelectorAll('.event-rating:first-child').length;
var username = document.getElementById('Username').value;
var own_voted_count = 0;
if (username)
own_voted_count = document.querySelectorAll('.event-rating[submitter="'+username+'"]').length;
document.getElementById('status').innerHTML = total_count + ' events. ' + accepted_count + ' accepted. ' + rejected_count + ' rejected. ' + (total_count - own_voted_count) + ' todo. ' + (total_count - total_voted_count) + ' unvoted. ' + (total_count - taken_count) + ' untaken.';
/* Do the math */
document.querySelectorAll('.event-list-item').forEach(function(ev) {
if (username) {
ev.classList.toggle('has-own-rating', ev.querySelectorAll('.event-rating[submitter="'+username+'"]').length > 0);
ev.classList.toggle('i-am-coordinator', ev.querySelectorAll('.event-coordinator[coordinator="'+username+'"]').length > 0);
}
var counts = {};
var meters = ev.querySelectorAll('.event-rating meter');
if (!meters.length) {
ev.querySelectorAll('.top-meter').forEach(function(meter) {
meter.setAttribute('value', 0);
});
return;
}
meters.forEach(function(rat) {
var tmp = counts[rat.getAttribute('category')] || 0;
counts[rat.getAttribute('category')] = tmp + parseInt(rat.getAttribute('value'));
});
var total = 0, i = 0, divisor = meters.length / Object.keys(counts).length;
for (category in counts) {
var dest_meter = ev.getElementsByClassName('meter-'+i)[0];
dest_meter.setAttribute('value', counts[category] / divisor);
dest_meter.setAttribute('title', category + ': ' + counts[category] / divisor + ' %' );
total += counts[category] / divisor;
i++;
}
ev.getElementsByClassName('meter-4')[0].setAttribute('value', total / Object.keys(counts).length);
});
}
document.addEventListener('DOMContentLoaded', function () {
var username = localStorage.getItem("c3-rating-user");
if (username)
document.getElementById('Username').value = username;
if (window.safari !== undefined)
document.body.classList.add('safari');
update_status();
});