This commit is contained in:
jelhan 2013-10-26 19:01:02 +02:00
commit 420e5538c3
2 changed files with 36 additions and 20 deletions

View file

@ -15,17 +15,17 @@
<div id="content"></div> <div id="content"></div>
<script type="text/html" id="Schedule_template"> <script type="text/html" id="Schedule_template">
<h1 id="title">{{title}}</h1> <h1 id="title">{{data.title}}</h1>
<p id="description">{{description}}</p> <p id="description">{{data.description}}</p>
<form id="addUserForm"> <form id="addUserForm">
<table id="choices"> <table id="choices">
<thead id="options"> <thead id="options">
<tr> <tr>
<td class="option option-name">User</td> <td class="option option-name">User</td>
{{#options}} {{#data.options}}
<td class="option">{{.}}</td> <td class="option">{{.}}</td>
{{/options}} {{/data.options}}
<td></td> <td></td>
</tr> </tr>
</thead> </thead>
@ -35,9 +35,9 @@
{{/user}} {{/user}}
<tr id="addUser"> <tr id="addUser">
<td><input type="text" name="name"/></td> <td><input type="text" name="name"/></td>
{{#options}} {{#data.options}}
<td><input type="text" name="selections[]"/></td> <td><input type="text" name="selections[]"/></td>
{{/options}} {{/data.options}}
<td><input type="submit" id="addUserFormSubmit" value="save"/></td> <td><input type="submit" id="addUserFormSubmit" value="save"/></td>
</tr> </tr>
</tbody> </tbody>

View file

@ -11,12 +11,19 @@ function DataHandler () {
}) })
.done(function(result) { .done(function(result) {
if (result.result === true) { if (result.result === true) {
result.data = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data)); result.data.data = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.data));
for (i = 0; i < result.data.user.length; i++) {
result.data.user[i] = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.user[i]));
}
done(result); done(result);
} }
else { else {
console.log ('Api hat einen Fehler gemeldet.'); console.log ('Api reported an error.');
console.log (result.errorMsg); console.log (result.errorMsg);
alert('Could not read requested data!\nerror message: ' + result.errorMsg);
} }
}) })
.fail(function(result) { .fail(function(result) {
@ -25,6 +32,13 @@ function DataHandler () {
}; };
this.write = function (id, version, data, done, fail) { this.write = function (id, version, data, done, fail) {
crypt_data = jQuery.extend(true, {}, data);
crypt_data.data = sjcl.encrypt($(location).attr('hash').substring(1), JSON.stringify(data.data));
for (i = 0; i < data.user.length; i++) {
crypt_data.user[i] = sjcl.encrypt($(location).attr('hash').substring(1), JSON.stringify(data.user[i]));
}
$.ajax({ $.ajax({
url: 'api.php', url: 'api.php',
type: 'POST', type: 'POST',
@ -33,7 +47,7 @@ function DataHandler () {
id: id, id: id,
action: 'set', action: 'set',
version: version, version: version,
data: sjcl.encrypt($(location).attr('hash').substring(1), JSON.stringify(data)) data: JSON.stringify(crypt_data)
} }
}) })
.done(function(result) { .done(function(result) {
@ -41,12 +55,14 @@ function DataHandler () {
done(result); done(result);
} }
else { else {
console.log('Api hat einen Fehler gemeldet.'); console.log('Api reported an error.');
console.log(result.errorMsg); console.log(result.errorMsg);
alert('Could not save data:\nerror message: ' + result.errorMsg);
} }
}) })
.fail(function(result) { .fail(function(result) {
fail(result) fail(result);
}); });
}; };
}; };
@ -86,7 +102,7 @@ function Schedule (id) {
this.Save = function() { this.Save = function() {
datahandler = new DataHandler(); datahandler = new DataHandler();
datahandler.write(self.id, self.version, self.data, this.Saved, this.Failed) datahandler.write(self.id, self.version, self.data, this.Saved, this.Failed);
}; };
this.Saved = function(result) { this.Saved = function(result) {
@ -102,8 +118,6 @@ function Schedule (id) {
new_user = AddUserGetDataByForm(); new_user = AddUserGetDataByForm();
self.data.user.push(new_user); self.data.user.push(new_user);
console.log(self.data);
self.Save(); self.Save();
$('#userlist #addUser').mustache('ScheduleUserlistUser_template', new_user, { method: 'before' }); $('#userlist #addUser').mustache('ScheduleUserlistUser_template', new_user, { method: 'before' });
$('#addUserForm')[0].reset(); $('#addUserForm')[0].reset();
@ -253,7 +267,7 @@ function ScheduleAdd(type) {
new_schedule = CreateScheduleGetDataByForm(); new_schedule = CreateScheduleGetDataByForm();
// check for atleast two options // check for atleast two options
if (new_schedule.options.length < 2) { if (new_schedule.data.options.length < 2) {
alert ('You have to add at least two options / dates.'); alert ('You have to add at least two options / dates.');
return false; // prevent form to be submitted return false; // prevent form to be submitted
} }
@ -268,19 +282,21 @@ function ScheduleAdd(type) {
function CreateScheduleGetDataByForm() { function CreateScheduleGetDataByForm() {
form = $('#addScheduleForm').serializeArray(); form = $('#addScheduleForm').serializeArray();
new_schedule = {}; new_schedule = {};
new_schedule.options = []; new_schedule.head = {};
new_schedule.data = {};
new_schedule.data.options = [];
$.each(form, function(key, value){ $.each(form, function(key, value){
switch (value.name) { switch (value.name) {
case 'title': case 'title':
new_schedule.title = value.value; new_schedule.data.title = value.value;
break; break;
case 'description': case 'description':
new_schedule.description = value.value; new_schedule.data.description = value.value;
break; break;
case 'option': case 'option':
if (value.value !== '') { new_schedule.options.push(value.value); } if (value.value !== '') { new_schedule.data.options.push(value.value); }
break; break;
} }
}); });
@ -358,7 +374,7 @@ function Startpage() {
$.Mustache.addFromDom(); $.Mustache.addFromDom();
id = $(location).attr('search').substring(1); id = $(location).attr('search').substring(1);
password = $(location).attr('hash').substring(1) password = $(location).attr('hash').substring(1);
if (id !== '' && password !== '') { if (id !== '' && password !== '') {
// show existing schedule // show existing schedule
schedule = new Schedule(id); schedule = new Schedule(id);