diff --git a/inc/lib/uuid.js b/inc/lib/uuid.js
new file mode 100644
index 0000000..08f662f
--- /dev/null
+++ b/inc/lib/uuid.js
@@ -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);
+ });
+}
diff --git a/inc/myco.js b/inc/myco.js
index e41290f..de74c53 100644
--- a/inc/myco.js
+++ b/inc/myco.js
@@ -162,10 +162,10 @@ show_contributions=function(group,contributor_name) {
}
else {
sum=0;
- for (idx in contributions) {
- col_actions='
| ';
- tbody.append(''+contributions[idx].getTitle()+' | '+contributions[idx].cost+' € | '+col_actions+'
');
- sum+=contributions[idx].cost;
+ for (uuid in contributions) {
+ col_actions=' | ';
+ tbody.append(''+contributions[uuid].getTitle()+' | '+contributions[uuid].cost+' € | '+col_actions+'
');
+ 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');
}
diff --git a/inc/myco_objects.js b/inc/myco_objects.js
index e490014..e74f839 100644
--- a/inc/myco_objects.js
+++ b/inc/myco_objects.js
@@ -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,
diff --git a/index.html b/index.html
index 371eea2..2b9e1d2 100644
--- a/index.html
+++ b/index.html
@@ -270,7 +270,7 @@ body{