var username = "";
var appointments;
var categories;
var filter = 0;
var host = "//dhbw.t-battermann.de/calendar/redirect.php";
function showAlert( type, info, text ) {
div = document.createElement("div");
if ( type != "success" && type != "info" && type != "warning" && type != "danger")
type = "info";
div.className = "alert alert-" + type + " alert-dismissible";
div.innerHTML = '';
str = document.createElement("strong");
str.appendChild(document.createTextNode( info + " " ));
div.appendChild(str);
div.appendChild(document.createTextNode( text ));
e = document.getElementById("right");
e.insertBefore( div, e.firstChild );
}
function getListItems() {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
getListItemOnReadyStatusChangeEventHandler( request, filter );
};
request.open("GET", host + "?user="+username+"&format=json&action=list");
request.send();
}
function showCategory( id ) {
filter = id;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
getListItemOnReadyStatusChangeEventHandler( request, id );
};
request.open("GET", host + "?user="+username+"&format=json&action=list");
request.send();
}
function prettyDate(from, to, allday) {
var r = /(\d{4})-(\d{2})-0?([1-3]?\d)T(\d{2}):(\d{2})/;
var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var mf = r.exec(from);
var mt = r.exec(to);
if ( mf[1] == mt[1] && mf[2] == mt[2] && mf[3] == mt[3] ) {
if ( allday )
return month[mf[2]-1] + " " + mf[3] + ", " + mf[1];
return month[mf[2]-1] + " " + mf[3] + ", " + mf[1] + " " + mf[4] + ":" + mf[5] + " - " + mt[4] + ":" + mt[5];
}
if ( allday )
return month[mf[2]-1] + " " + mf[3] + ", " + mf[1] + " - " +
month[mt[2]-1] + " " + mt[3] + ", " + mt[1];
return month[mf[2]-1] + " " + mf[3] + ", " + mf[1] + " " + mf[4] + ":" + mf[5] + " - " +
month[mt[2]-1] + " " + mt[3] + ", " + mt[1] + " " + mt[4] + ":" + mt[5];
}
function hasCategory(appointment, id) {
for(var i = 0; i b.start });
var past = document.getElementById("showPastAppointments").checked;
if (!past || id != 0) {
var app = [];
var d = new Date();
d = d.getFullYear()+"-"+("0"+(d.getMonth()+1)).slice(-2)+"-"+("0"+d.getDate()).slice(-2)+"T"+d.getHours()+":"+d.getMinutes();
for(var i=0;i d) && (id == 0 || hasCategory(appointments[i], id)) ) {
app.push(appointments[i]);
}
}
appointments = app;
}
if(appointments.length == 0) {
e = document.createElement("p");
e.appendChild(document.createTextNode("There are no entries to display!"));
document.getElementById("content").appendChild(e);
return;
}
for(var i=0;i 12) {
media = document.createElement("div");
media.className = "media";
ml = document.createElement("div");
ml.className = "media-right";
img_a = document.createElement("a");
img_a.href = appointments[i].imageurl;
img_a.setAttribute("id","img-" + appointments[i].id);
img_a.setAttribute("target", "_blank");
img = document.createElement("img");
img.src = appointments[i].imageurl;
img.alt = "Image #" + appointments[i].id;
img_a.setAttribute("class", "small");
img_a.appendChild(img);
ml.appendChild(img_a);
body.appendChild(media);
body = document.createElement("div");
body.className = "media-body";
media.appendChild(body);
media.appendChild(ml);
}
// if there are any categories change "body"
elem = body;
if (appointments[i].categories.length > 0) {
elem = document.createElement("div");
elem.className = "row";
body.appendChild(elem);
body = elem;
elem = document.createElement("div");
elem.className = "col-xs-6";
body.appendChild(elem);
}
// info
info = document.createElement("ul");
// date
date = document.createElement("li");
date.appendChild(document.createTextNode( prettyDate( appointments[i].start, appointments[i].end, appointments[i].allday == 1) ));
date.className = "date";
info.appendChild(date);
// location
loc = document.createElement("li");
loc.appendChild(document.createTextNode(appointments[i].location));
loc.className = "loc";
info.appendChild(loc);
// organizer
org = document.createElement("li");
link = document.createElement("a");
link.href = "mailto:" + appointments[i].organizer;
link.appendChild(document.createTextNode(appointments[i].organizer));
org.appendChild(link);
org.className = "org";
info.appendChild(org);
// url
url = /https?:\/\/.{4}/;
if ( url.exec(appointments[i].webpage) != null ) {
urli = document.createElement("li");
url = document.createElement("a");
url.appendChild(document.createTextNode(appointments[i].webpage));
url.href = appointments[i].webpage;
urli.className = "url";
urli.appendChild(url);
info.appendChild(urli);
}
// actions
action = document.createElement("li");
action.className = "action";
// edit document
edit_a = document.createElement("a");
edit_img = document.createElement("img");
edit_img.src = "images/edit.png";
edit_img.alt = "edit";
edit_a.href = "#edit-" + appointments[i].id;
edit_a.setAttribute("onclick", "loadEditForm(" + appointments[i].id + ")");
edit_a.appendChild(edit_img);
action.appendChild(edit_a);
// delete appointment
del_a = document.createElement("a");
del_img = document.createElement("img");
del_img.src = "images/delete.png";
del_img.alt = "delete";
del_a.href = "#delete-" + appointments[i].id;
del_a.setAttribute("onclick", "deleteAppointment(" + appointments[i].id + ")");
del_a.appendChild(del_img);
action.appendChild(del_a);
// add image
img_a = document.createElement("a");
img_img = document.createElement("img");
img_img.src = "images/add-image.png";
img_img.alt = "add/change image";
img_a.href = "#image-" + appointments[i].id;
img_a.setAttribute("onclick", "showUploadImageForm(" + appointments[i].id + ")");
img_a.appendChild(img_img);
action.appendChild(img_a);
// add category
cat_a = document.createElement("a");
cat_img = document.createElement("img");
cat_img.src = "images/folder-txt.png";
cat_img.alt = "add category";
cat_a.href = "#add-category-" + appointments[i].id;
cat_a.setAttribute("onclick", "showAddCategoryForm(" + appointments[i].id + ")");
cat_a.appendChild(cat_img);
action.appendChild(cat_a);
// append actions
info.appendChild(action);
// add elements
elem.appendChild(info);
// categories
if ( appointments[i].categories.length != 0 ) {
body.appendChild(elem);
cat = document.createElement("div");
cat.className = "col-xs-6";
cat.appendChild(document.createTextNode("Categories:"));
cat2 = document.createElement("ul");
cat2.setAttribute("id", "cat-" + appointments[i].id);
cat2.className = "categories";
for(var j=0; j < appointments[i].categories.length; j++) {
dca = document.createElement("a");
dca.href = "#remove-category-" + appointments[i].categories[j].id + "-from-" + appointments[i].id;
dca.setAttribute("onclick", "deleteCategoryFromEvent( " + appointments[i].id + ", " + appointments[i].categories[j].id + ")");
dcimg = document.createElement("img");
dcimg.src = "images/delete.png";
dcimg.alt = "remove category from event";
dca.appendChild(dcimg);
cat3 = document.createElement("li");
cat3.setAttribute("id", "cat-" + appointments[i].id + "-" + appointments[i].categories[j].id);
cat3.appendChild(document.createTextNode(appointments[i].categories[j].name));
cat3.appendChild(dca);
cat2.appendChild(cat3);
}
cat.appendChild(cat2);
body.appendChild(cat);
}
// add appointment to list
document.getElementById("content").appendChild(calendarElement);
}
}
}
function createNewCategory() {
var name = document.getElementById("createCategory")["name"].value;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
createNewCategoryOnReadyStatusChangeEventHandler( request );
};
request.open("POST", host + "?user="+username+"&format=json&action=add-category&name="+name);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("name="+encodeURIComponent(name));
}
function createNewCategoryOnReadyStatusChangeEventHandler( request ) {
if( request.readyState == 4 && (request.status == 200 || request.status == 0) ) {
var c = eval('(' + request.responseText + ')');
if (typeof c.error != 'undefined') {
showAlert( "danger", "Error!", c.error.text );
}else{
getCategories();
}
}
}
function getCategories() {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
getCategoriesOnReadyStatusChangeEventHandler( request );
};
request.open("GET", host + "?user="+username+"&format=json&action=list-categories");
request.send();
}
function getCategoriesOnReadyStatusChangeEventHandler( request ) {
if( request.readyState == 4 && (request.status == 200 || request.status == 0) ) {
var c = eval('(' + request.responseText + ')');
var cats = c["categories"]["categories"];
categories = cats;
var p = document.getElementById("categoryList");
p.innerHTML = "";
for(var i=0;i