Use UUID as Contribution identifier and keep trace of deleted contributions

This commit is contained in:
Benjamin Renard 2014-01-14 00:29:49 +01:00
parent d56d8f069a
commit 4baf634bdb
4 changed files with 65 additions and 44 deletions

6
inc/lib/uuid.js Normal file
View file

@ -0,0 +1,6 @@
generate_uuid=function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}

View file

@ -162,10 +162,10 @@ show_contributions=function(group,contributor_name) {
}
else {
sum=0;
for (idx in contributions) {
col_actions='<td><div class="btn-group" data-id="'+contributions[idx].id+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>';
tbody.append('<tr><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost+' €</td>'+col_actions+'</tr>');
sum+=contributions[idx].cost;
for (uuid in contributions) {
col_actions='<td><div class="btn-group" data-uuid="'+contributions[uuid].uuid+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>';
tbody.append('<tr><td>'+contributions[uuid].getTitle()+'</td><td>'+contributions[uuid].cost+' €</td>'+col_actions+'</tr>');
sum+=contributions[uuid].cost;
}
total.html(sum+' €');
}
@ -271,7 +271,7 @@ on_show_add_contribution_modal=function(e) {
on_click_add_contribution_btn=function() {
$('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
$('#add_contribution_modal #edit_id')[0].value='-1';
$('#add_contribution_modal #edit_uuid')[0].value='-1';
$($('#add_contribution_modal h4')[0]).html('Ajouter une contribution');
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
$('#add_contribution_modal').modal('show');
@ -322,12 +322,12 @@ on_valid_add_contribution_modal=function(e) {
date=new Date();
}
if($('#add_contribution_modal #edit_id')[0].value=='-1') {
if($('#add_contribution_modal #edit_uuid')[0].value=='-1') {
group.addContribution(new Contribution(contributor,cost,title,date));
}
else {
contribution_id=$('#add_contribution_modal #edit_id')[0].value;
group.replaceContribution(contribution_id,new Contribution(contributor,cost,title,date));
contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
}
show_contributions(group,contributor_name);
$('#add_contribution_modal').modal('hide');
@ -341,13 +341,13 @@ on_close_add_contribution_modal=function () {
on_contribution_delete_btn_click=function(e) {
var group=groups[$('#view-group #group_name')[0].value];
contribution_id=$($(e.target).parents('div')[0]).data('id');
contribution=group.contributions[contribution_id];
myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_id':contribution_id, 'contribution': contribution} );
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
contribution=group.contributions[contribution_uuid];
myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} );
}
on_confirm_contribution_delete=function(data) {
delete data.group.contributions[data.contribution_id];
data.group.deleteContribution(data.contribution_uuid);
groups.save();
on_contributor_change();
}
@ -357,13 +357,13 @@ on_contribution_edit_btn_click=function(e) {
$('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
$($('#add_contribution_modal h4')[0]).html('Editer une contribution');
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
contribution_id=$($(e.target).parents('div')[0]).data('id');
contribution=group.contributions[contribution_id];
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
contribution=group.contributions[contribution_uuid];
$('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name;
$('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
$('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
$('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
$('#add_contribution_modal #edit_id')[0].value=contribution_id;
$('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
$('#add_contribution_modal').modal('show');
}

View file

@ -96,7 +96,8 @@ function GroupList() {
function Group(name,data) {
this.name=name;
this.contributors=[];
this.contributions=[];
this.contributions={};
this.deletedContributions={};
this.isGroup=function() {
@ -108,14 +109,15 @@ function Group(name,data) {
for (idx in this.contributors) {
contributors.push(this.contributors[idx].export());
}
var contributions=[];
for (idx in this.contributions) {
contributions.push(this.contributions[idx].export());
var contributions={}
for (uuid in this.contributions) {
contributions[uuid]=this.contributions[uuid].export();
}
return {
'name': encodeURIComponent(this.name),
'contributors': contributors,
'contributions': contributions
'contributions': contributions,
'deletedContributions': this.deletedContributions
};
}
@ -156,25 +158,31 @@ function Group(name,data) {
* Contributions
*/
this.contributionsByContributorName=function(name) {
var ret=[];
for (idx in this.contributions) {
if (this.contributions[idx].contributor.name==name) {
ret.push(this.contributions[idx]);
var ret={};
for (uuid in this.contributions) {
if (this.contributions[uuid].contributor.name==name) {
ret[uuid]=this.contributions[uuid];
}
}
return ret;
}
this.addContribution=function(c) {
c.id=this.contributions.length;
this.contributions.push(c);
this.contributions[c.uuid]=c;
}
this.replaceContribution=function(idx,c) {
c.id=idx;
this.contributions[idx]=c;
this.updateContribution=function(uuid,c) {
c.uuid=uuid;
this.contributions[uuid]=c;
}
this.deleteContribution=function(uuid) {
this.contributions[uuid].lastChange=new Date().getTime();
this.deletedContributions[uuid]=this.contributions[uuid].export();
delete this.contributions[uuid];
}
/*
* Balance
*/
@ -186,8 +194,8 @@ function Group(name,data) {
var sum=0;
c=this.contributors[idx].name;
cl=this.contributionsByContributorName(c);
for (idc in cl) {
sum+=cl[idc].cost;
for (uuid in cl) {
sum+=cl[uuid].cost;
}
if (min==-1 || min>sum) {
min=sum;
@ -229,16 +237,21 @@ function Group(name,data) {
));
}
}
if (jQuery.type(data.contributions) == 'array') {
for (idx in data.contributions) {
this.contributions.push(new Contribution(
this.contributorByName(data.contributions[idx].contributor),
data.contributions[idx].cost,
decodeURIComponent(data.contributions[idx].title),
data.contributions[idx].date,
idx,
data.contributions[idx].lastChange
));
if (jQuery.type(data.contributions) == 'object') {
for (uuid in data.contributions) {
this.contributions[uuid]=new Contribution(
this.contributorByName(data.contributions[uuid].contributor),
data.contributions[uuid].cost,
decodeURIComponent(data.contributions[uuid].title),
data.contributions[uuid].date,
uuid,
data.contributions[uuid].lastChange
);
}
}
if (jQuery.type(data.deletedContributions) == 'object') {
for (uuid in data.deletedContributions) {
this.deletedContributions[uuid]=data.deletedContributions[uuid];
}
}
}
@ -260,16 +273,17 @@ function Contributor(name,email,id) {
}
}
function Contribution(contributor,cost,title,date,id,lastChange) {
function Contribution(contributor,cost,title,date,uuid,lastChange) {
this.contributor=contributor;
this.cost=cost;
this.title=title;
this.date=date;
this.id=id;
this.uuid=uuid || generate_uuid();
this.lastChange=lastChange || new Date().getTime();
this.export=function() {
return {
'contributor': encodeURIComponent(this.contributor.name),
'uuid': this.uuid,
'cost': this.cost,
'title': encodeURIComponent(this.title),
'date': this.date,

View file

@ -270,7 +270,7 @@ body{
<div class="modal-body">
<form class="form-horizontal" role="form">
<input type='hidden' id='group_name' />
<input type='hidden' id='edit_id' value='-1' />
<input type='hidden' id='edit_uuid' value='-1' />
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Participant *</span>
@ -421,6 +421,7 @@ body{
<!-- Latest compiled and minified JavaScript -->
<script src="inc/lib/bootstrap/js/bootstrap.min.js"></script>
<script src="inc/lib/moment.min.js"></script>
<script src="inc/lib/uuid.js"></script>
<script src="inc/myco_objects.js"></script>
<script src="inc/myco_confirm.js"></script>
<script src="inc/myco.js"></script>