Use UUID as Contribution identifier and keep trace of deleted contributions
This commit is contained in:
parent
d56d8f069a
commit
4baf634bdb
4 changed files with 65 additions and 44 deletions
6
inc/lib/uuid.js
Normal file
6
inc/lib/uuid.js
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
30
inc/myco.js
30
inc/myco.js
|
@ -162,10 +162,10 @@ show_contributions=function(group,contributor_name) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sum=0;
|
sum=0;
|
||||||
for (idx in contributions) {
|
for (uuid 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>';
|
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[idx].getTitle()+'</td><td>'+contributions[idx].cost+' €</td>'+col_actions+'</tr>');
|
tbody.append('<tr><td>'+contributions[uuid].getTitle()+'</td><td>'+contributions[uuid].cost+' €</td>'+col_actions+'</tr>');
|
||||||
sum+=contributions[idx].cost;
|
sum+=contributions[uuid].cost;
|
||||||
}
|
}
|
||||||
total.html(sum+' €');
|
total.html(sum+' €');
|
||||||
}
|
}
|
||||||
|
@ -271,7 +271,7 @@ on_show_add_contribution_modal=function(e) {
|
||||||
|
|
||||||
on_click_add_contribution_btn=function() {
|
on_click_add_contribution_btn=function() {
|
||||||
$('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
|
$('#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 h4')[0]).html('Ajouter une contribution');
|
||||||
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
|
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
|
||||||
$('#add_contribution_modal').modal('show');
|
$('#add_contribution_modal').modal('show');
|
||||||
|
@ -322,12 +322,12 @@ on_valid_add_contribution_modal=function(e) {
|
||||||
date=new Date();
|
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));
|
group.addContribution(new Contribution(contributor,cost,title,date));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
contribution_id=$('#add_contribution_modal #edit_id')[0].value;
|
contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
|
||||||
group.replaceContribution(contribution_id,new Contribution(contributor,cost,title,date));
|
group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
|
||||||
}
|
}
|
||||||
show_contributions(group,contributor_name);
|
show_contributions(group,contributor_name);
|
||||||
$('#add_contribution_modal').modal('hide');
|
$('#add_contribution_modal').modal('hide');
|
||||||
|
@ -341,13 +341,13 @@ on_close_add_contribution_modal=function () {
|
||||||
|
|
||||||
on_contribution_delete_btn_click=function(e) {
|
on_contribution_delete_btn_click=function(e) {
|
||||||
var group=groups[$('#view-group #group_name')[0].value];
|
var group=groups[$('#view-group #group_name')[0].value];
|
||||||
contribution_id=$($(e.target).parents('div')[0]).data('id');
|
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
|
||||||
contribution=group.contributions[contribution_id];
|
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_id':contribution_id, 'contribution': contribution} );
|
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) {
|
on_confirm_contribution_delete=function(data) {
|
||||||
delete data.group.contributions[data.contribution_id];
|
data.group.deleteContribution(data.contribution_uuid);
|
||||||
groups.save();
|
groups.save();
|
||||||
on_contributor_change();
|
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 #group_name')[0].value=$('#view-group #group_name')[0].value;
|
||||||
$($('#add_contribution_modal h4')[0]).html('Editer une contribution');
|
$($('#add_contribution_modal h4')[0]).html('Editer une contribution');
|
||||||
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
|
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
|
||||||
contribution_id=$($(e.target).parents('div')[0]).data('id');
|
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
|
||||||
contribution=group.contributions[contribution_id];
|
contribution=group.contributions[contribution_uuid];
|
||||||
$('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name;
|
$('#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_title')[0].value=contribution.getTitle();
|
||||||
$('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
|
$('#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 #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');
|
$('#add_contribution_modal').modal('show');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,8 @@ function GroupList() {
|
||||||
function Group(name,data) {
|
function Group(name,data) {
|
||||||
this.name=name;
|
this.name=name;
|
||||||
this.contributors=[];
|
this.contributors=[];
|
||||||
this.contributions=[];
|
this.contributions={};
|
||||||
|
this.deletedContributions={};
|
||||||
|
|
||||||
|
|
||||||
this.isGroup=function() {
|
this.isGroup=function() {
|
||||||
|
@ -108,14 +109,15 @@ function Group(name,data) {
|
||||||
for (idx in this.contributors) {
|
for (idx in this.contributors) {
|
||||||
contributors.push(this.contributors[idx].export());
|
contributors.push(this.contributors[idx].export());
|
||||||
}
|
}
|
||||||
var contributions=[];
|
var contributions={}
|
||||||
for (idx in this.contributions) {
|
for (uuid in this.contributions) {
|
||||||
contributions.push(this.contributions[idx].export());
|
contributions[uuid]=this.contributions[uuid].export();
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
'name': encodeURIComponent(this.name),
|
'name': encodeURIComponent(this.name),
|
||||||
'contributors': contributors,
|
'contributors': contributors,
|
||||||
'contributions': contributions
|
'contributions': contributions,
|
||||||
|
'deletedContributions': this.deletedContributions
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,25 +158,31 @@ function Group(name,data) {
|
||||||
* Contributions
|
* Contributions
|
||||||
*/
|
*/
|
||||||
this.contributionsByContributorName=function(name) {
|
this.contributionsByContributorName=function(name) {
|
||||||
var ret=[];
|
var ret={};
|
||||||
for (idx in this.contributions) {
|
for (uuid in this.contributions) {
|
||||||
if (this.contributions[idx].contributor.name==name) {
|
if (this.contributions[uuid].contributor.name==name) {
|
||||||
ret.push(this.contributions[idx]);
|
ret[uuid]=this.contributions[uuid];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addContribution=function(c) {
|
this.addContribution=function(c) {
|
||||||
c.id=this.contributions.length;
|
this.contributions[c.uuid]=c;
|
||||||
this.contributions.push(c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.replaceContribution=function(idx,c) {
|
this.updateContribution=function(uuid,c) {
|
||||||
c.id=idx;
|
c.uuid=uuid;
|
||||||
this.contributions[idx]=c;
|
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
|
* Balance
|
||||||
*/
|
*/
|
||||||
|
@ -186,8 +194,8 @@ function Group(name,data) {
|
||||||
var sum=0;
|
var sum=0;
|
||||||
c=this.contributors[idx].name;
|
c=this.contributors[idx].name;
|
||||||
cl=this.contributionsByContributorName(c);
|
cl=this.contributionsByContributorName(c);
|
||||||
for (idc in cl) {
|
for (uuid in cl) {
|
||||||
sum+=cl[idc].cost;
|
sum+=cl[uuid].cost;
|
||||||
}
|
}
|
||||||
if (min==-1 || min>sum) {
|
if (min==-1 || min>sum) {
|
||||||
min=sum;
|
min=sum;
|
||||||
|
@ -229,16 +237,21 @@ function Group(name,data) {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (jQuery.type(data.contributions) == 'array') {
|
if (jQuery.type(data.contributions) == 'object') {
|
||||||
for (idx in data.contributions) {
|
for (uuid in data.contributions) {
|
||||||
this.contributions.push(new Contribution(
|
this.contributions[uuid]=new Contribution(
|
||||||
this.contributorByName(data.contributions[idx].contributor),
|
this.contributorByName(data.contributions[uuid].contributor),
|
||||||
data.contributions[idx].cost,
|
data.contributions[uuid].cost,
|
||||||
decodeURIComponent(data.contributions[idx].title),
|
decodeURIComponent(data.contributions[uuid].title),
|
||||||
data.contributions[idx].date,
|
data.contributions[uuid].date,
|
||||||
idx,
|
uuid,
|
||||||
data.contributions[idx].lastChange
|
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.contributor=contributor;
|
||||||
this.cost=cost;
|
this.cost=cost;
|
||||||
this.title=title;
|
this.title=title;
|
||||||
this.date=date;
|
this.date=date;
|
||||||
this.id=id;
|
this.uuid=uuid || generate_uuid();
|
||||||
this.lastChange=lastChange || new Date().getTime();
|
this.lastChange=lastChange || new Date().getTime();
|
||||||
this.export=function() {
|
this.export=function() {
|
||||||
return {
|
return {
|
||||||
'contributor': encodeURIComponent(this.contributor.name),
|
'contributor': encodeURIComponent(this.contributor.name),
|
||||||
|
'uuid': this.uuid,
|
||||||
'cost': this.cost,
|
'cost': this.cost,
|
||||||
'title': encodeURIComponent(this.title),
|
'title': encodeURIComponent(this.title),
|
||||||
'date': this.date,
|
'date': this.date,
|
||||||
|
|
|
@ -270,7 +270,7 @@ body{
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<form class="form-horizontal" role="form">
|
<form class="form-horizontal" role="form">
|
||||||
<input type='hidden' id='group_name' />
|
<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="form-group">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<span class="input-group-addon">Participant *</span>
|
<span class="input-group-addon">Participant *</span>
|
||||||
|
@ -421,6 +421,7 @@ body{
|
||||||
<!-- Latest compiled and minified JavaScript -->
|
<!-- Latest compiled and minified JavaScript -->
|
||||||
<script src="inc/lib/bootstrap/js/bootstrap.min.js"></script>
|
<script src="inc/lib/bootstrap/js/bootstrap.min.js"></script>
|
||||||
<script src="inc/lib/moment.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_objects.js"></script>
|
||||||
<script src="inc/myco_confirm.js"></script>
|
<script src="inc/myco_confirm.js"></script>
|
||||||
<script src="inc/myco.js"></script>
|
<script src="inc/myco.js"></script>
|
||||||
|
|
Loading…
Reference in a new issue