Manage contributor by their email

This commit is contained in:
Benjamin Renard 2014-01-19 14:48:41 +01:00
parent bf9233401f
commit 0b8d66fbf5
3 changed files with 84 additions and 93 deletions

View file

@ -93,13 +93,13 @@ view_home=function() {
balances=groups.balances();
var sum=0;
for (g in balances) {
if (jQuery.type(balances[g]['balance'][user.name])!='undefined') {
if (balances[g]['balance'][user.name]['diff']<0) {
sum+=balances[g]['balance'][user.name]['diff'];
diff='<td class="negative">'+balances[g]['balance'][user.name]['diff'].toFixed(2)+'</td>';
if (jQuery.type(balances[g]['balance'][user.email])!='undefined') {
if (balances[g]['balance'][user.email]['diff']<0) {
sum+=balances[g]['balance'][user.email]['diff'];
diff='<td class="negative">'+balances[g]['balance'][user.email]['diff'].toFixed(2)+'</td>';
}
else {
value=balances[g]['balance'][user.name]['total']-balances[g]['min'];
value=balances[g]['balance'][user.email]['total']-balances[g]['min'];
sum+=value;
diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
}
@ -143,9 +143,9 @@ set_contributors=function(group) {
$('#view-group #contributor').append('<option>Aucun participant</option>');
}
else {
$(group.contributors).each(function(idx,c) {
$('#view-group #contributor').append('<option value="'+c.name+'">'+c.name+'</option>');
});
for (email in group.contributors) {
$('#view-group #contributor').append('<option value="'+email+'">'+group.contributors[email].name+'</option>');
}
}
}
@ -153,11 +153,11 @@ on_contributor_change=function(e) {
show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
}
show_contributions=function(group,contributor_name) {
show_contributions=function(group,contributor_email) {
var tbody=$($('#view-group #contributions tbody')[0]);
tbody.html('');
total=$($('#view-group #total-value')[0]);
contributions=group.contributionsByContributorName(contributor_name);
contributions=group.contributionsByContributorEmail(contributor_email);
contributions.reverse();
if (contributions.length==0) {
tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
@ -185,6 +185,7 @@ on_click_add_contributor_btn=function() {
$($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
$('#view-group #edit_id')[0].value='-1';
$('#add_contributor_modal #add_contributor_email').attr('disabled',false);
$('#add_contributor_modal').modal('show');
}
@ -201,18 +202,13 @@ on_valid_add_contributor_modal=function(e) {
alert('Vous devez saisir un nom !');
return;
}
duplicate=group.contributorByName(name);
if (duplicate!=undefined && duplicate.id!=edit_id) {
alert('Un contributor porte déjà ce nom !');
return;
}
var email=$('#add_contributor_email')[0].value;
if (email=='') {
alert('Vous devez saisir un email !');
return;
}
duplicate=group.contributorByEmail(email)
if (duplicate!=undefined && duplicate.id!=edit_id) {
if (duplicate!=undefined && edit=='-1') {
alert('Un contributor utilise déjà cet email !');
return;
}
@ -220,12 +216,10 @@ on_valid_add_contributor_modal=function(e) {
group.addContributor(new Contributor(name,email));
}
else {
group.replaceContributor(edit_id,new Contributor(name,email));
group.replaceContributor(email,new Contributor(name,email));
}
set_contributors(group);
$('#add_contributor_modal').modal('hide');
$('#add_contributor_name')[0].value='';
$('#add_contributor_email')[0].value='';
groups.save();
}
@ -237,11 +231,12 @@ on_close_add_contributor_modal=function () {
on_edit_contributor_btn_click=function(e) {
var group=groups[$('#view-group').data('uuid')];
$('#add_contributor_modal').data('group-uuid',group.uuid);
contributor_name=$('#view-group #contributor')[0].value;
contributor=group.contributorByName(contributor_name);
$('#view-group #edit_id')[0].value=contributor.id;
contributor_email=$('#view-group #contributor')[0].value;
contributor=group.contributorByEmail(contributor_email);
$('#view-group #edit_id')[0].value=contributor_email;
$('#add_contributor_modal #add_contributor_name')[0].value=contributor.name;
$('#add_contributor_modal #add_contributor_email')[0].value=contributor.email;
$('#add_contributor_modal #add_contributor_email').attr('disabled',true);
$($('#add_contributor_modal h4')[0]).html('Editer un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider');
$('#add_contributor_modal').modal('show');
@ -264,12 +259,8 @@ on_confirm_del_contributor=function(contributor) {
* Add/edit/delete contribution
********************************/
on_show_add_contribution_modal=function(e) {
var cs=$($('#add_contribution_modal #add_contribution_contributor_name')[0]);
cs.html('');
$('#view-group #contributor option').each(function(idx,option) {
cs.append($(option).clone());
});
cs[0].value = $('#view-group #contributor')[0].value;
$('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html());
$('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
$('#add_contribution_modal #add_contribution_title').focus();
}
@ -284,8 +275,8 @@ on_click_add_contribution_btn=function() {
on_valid_add_contribution_modal=function(e) {
e.preventDefault();
var group=groups[$('#add_contribution_modal').data('group-uuid')];
var contributor_name=$('#add_contribution_contributor_name')[0].value;
var contributor=group.contributorByName(contributor_name);
var contributor_email=$('#add_contribution_contributor_email')[0].value;
var contributor=group.contributorByEmail(contributor_email);
if (contributor==undefined) {
alert('Participant inconu !');
return;
@ -333,7 +324,7 @@ on_valid_add_contribution_modal=function(e) {
contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
}
show_contributions(group,contributor_name);
show_contributions(group,contributor_email);
$('#add_contribution_modal').modal('hide');
$('#add_contribution_modal form')[0].reset();
groups.save();
@ -363,7 +354,7 @@ on_contribution_edit_btn_click=function(e) {
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
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_contributor_email')[0].value=contribution.contributor.email;
$('#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');
@ -406,7 +397,7 @@ display_balance=function(group) {
else {
diff='<td><span class="glyphicon glyphicon-thumbs-up"></span></td>';
}
tbody.append('<tr><td>'+c+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>');
tbody.append('<tr><td>'+bal['balance'][c]['name']+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>');
}
$('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €');
$('#display_balance_modal').modal('show');
@ -523,8 +514,7 @@ on_click_sync_btn=function(e) {
navbar_collapse_hide();
if (user) {
pleaseWaitShow();
sync_server.sync(user.server, user.email, user.password, groups.export(),
function(data) {
onsuccess=function(data) {
console.log(data);
if (data.groups) {
groups.import(data.groups);
@ -534,17 +524,23 @@ on_click_sync_btn=function(e) {
alert('Groupes synchronisés');
}
else {
onerror(data);
}
}
onerror=function(data) {
pleaseWaitHide();
console.log(data);
if (jQuery.type(data['loginerror'])!='undefined') {
alert(data['loginerror']);
}
else if (jQuery.type(data['syncerror'])!='undefined') {
alert(data['syncerror']);
}
else {
alert('Erreur durant la synchronisation :(');
}
},
function(data) {
pleaseWaitHide();
console.log(data);
alert('Impossible de contacter le serveur :(');
}
);
sync_server.sync(user.server, user.email, user.password, groups.export(),onsuccess,onerror);
}
else {
alert("Vous devez vous connecter pour commencer");

View file

@ -116,7 +116,7 @@ function GroupList() {
function Group(uuid,name,data) {
this.uuid=uuid || generate_uuid();
this.name=name || false;
this.contributors=[];
this.contributors={};
this.contributions={};
this.deletedContributions={};
@ -126,9 +126,9 @@ function Group(uuid,name,data) {
}
this.export=function() {
var contributors=[];
for (idx in this.contributors) {
contributors.push(this.contributors[idx].export());
var contributors={};
for (email in this.contributors) {
contributors[email]=this.contributors[email].export();
}
var contributions={}
for (uuid in this.contributions) {
@ -147,42 +147,39 @@ function Group(uuid,name,data) {
* Contributors
*/
this.removeContributor=function(c) {
this.contributors=this.contributors.filter(function(v){
return (v.name!=c);
});
delete this.contributors[c.email];
}
this.contributorByName=function(name) {
for (c in this.contributors) {
if (this.contributors[c].name == name) return this.contributors[c];
for (email in this.contributors) {
if (this.contributors[email].name == name) return this.contributors[email];
}
return undefined;
}
this.contributorByEmail=function(email) {
for (c in this.contributors) {
if (this.contributors[c].email == email) return this.contributors[c];
if (jQuery.type(this.contributors[email])!='undefined') {
return this.contributors[email];
}
return undefined;
}
this.addContributor=function(c) {
c.id=this.contributors.length;
this.contributors.push(c);
this.contributors[c.email]=c;
}
this.replaceContributor=function(idx,c) {
c.id=idx;
this.contributors[idx]=c;
this.replaceContributor=function(email,c) {
delete this.contributors[email];
this.contributors[c.email]=c;
}
/*
* Contributions
*/
this.contributionsByContributorName=function(name) {
this.contributionsByContributorEmail=function(email) {
var ret=[];
for (uuid in this.contributions) {
if (this.contributions[uuid].contributor.name==name) {
if (this.contributions[uuid].contributor.email==email) {
ret.push(this.contributions[uuid]);
}
}
@ -223,10 +220,9 @@ function Group(uuid,name,data) {
total={}
min=-1;
max=0;
for (idx in this.contributors) {
for (email in this.contributors) {
var sum=0;
c=this.contributors[idx].name;
cl=this.contributionsByContributorName(c);
cl=this.contributionsByContributorEmail(email);
for (idx in cl) {
sum+=cl[idx].cost;
}
@ -236,16 +232,17 @@ function Group(uuid,name,data) {
if(max<sum) {
max=sum;
}
total[c]=sum;
total[email]=sum;
}
balance={}
var sum=0;
for (c in total) {
balance[c]={
'total': total[c],
'diff': total[c]-max,
for (email in total) {
balance[email]={
'name': this.contributors[email].name,
'total': total[email],
'diff': total[email]-max,
}
sum=sum+total[c];
sum=sum+total[email];
}
return {
'balance': balance,
@ -262,19 +259,18 @@ function Group(uuid,name,data) {
try {
this.uuid=data.uuid;
this.name=data.name;
if (jQuery.type(data.contributors) == 'array') {
for (idx in data.contributors) {
this.contributors.push(new Contributor(
decodeURIComponent(data.contributors[idx].name),
decodeURIComponent(data.contributors[idx].email),
idx
));
if (jQuery.type(data.contributors) == 'object') {
for (email in data.contributors) {
this.contributors[email]=new Contributor(
decodeURIComponent(data.contributors[email].name),
data.contributors[email].email
);
}
}
if (jQuery.type(data.contributions) == 'object') {
for (uuid in data.contributions) {
this.contributions[uuid]=new Contribution(
this.contributorByName(data.contributions[uuid].contributor),
this.contributorByEmail(data.contributions[uuid].contributor),
data.contributions[uuid].cost,
decodeURIComponent(data.contributions[uuid].title),
data.contributions[uuid].date,
@ -295,14 +291,13 @@ function Group(uuid,name,data) {
}
}
function Contributor(name,email,id) {
function Contributor(name,email) {
this.name=name;
this.email=email;
this.id=id;
this.export=function() {
return {
'name': encodeURIComponent(this.name),
'email': encodeURIComponent(this.email)
'email': this.email
};
}
}
@ -319,7 +314,7 @@ function Contribution(contributor,cost,title,date,uuid,lastChange) {
this.lastChange=lastChange || new Date().getTime();
this.export=function() {
return {
'contributor': encodeURIComponent(this.contributor.name),
'contributor': this.contributor.email,
'uuid': this.uuid,
'cost': this.cost,
'title': encodeURIComponent(this.title),

View file

@ -316,7 +316,7 @@ body{
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Participant *</span>
<select id='add_contribution_contributor_name' class="form-control"></select>
<select id='add_contribution_contributor_email' class="form-control"></select>
</div>
</div>
<div class="form-group">