From 0b8d66fbf5bf74ec0d5aabf69810e133bc1ca034 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 19 Jan 2014 14:48:41 +0100 Subject: [PATCH] Manage contributor by their email --- inc/myco.js | 102 +++++++++++++++++++++----------------------- inc/myco_objects.js | 73 +++++++++++++++---------------- index.html | 2 +- 3 files changed, 84 insertions(+), 93 deletions(-) diff --git a/inc/myco.js b/inc/myco.js index b4cb6bb..2525f05 100644 --- a/inc/myco.js +++ b/inc/myco.js @@ -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=''+balances[g]['balance'][user.name]['diff'].toFixed(2)+''; + 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=''+balances[g]['balance'][user.email]['diff'].toFixed(2)+''; } else { - value=balances[g]['balance'][user.name]['total']-balances[g]['min']; + value=balances[g]['balance'][user.email]['total']-balances[g]['min']; sum+=value; diff='+'+value.toFixed(2)+' €'; } @@ -143,9 +143,9 @@ set_contributors=function(group) { $('#view-group #contributor').append(''); } else { - $(group.contributors).each(function(idx,c) { - $('#view-group #contributor').append(''); - }); + for (email in group.contributors) { + $('#view-group #contributor').append(''); + } } } @@ -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('Aucune contributions'); @@ -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=''; } - tbody.append(''+c+''+bal['balance'][c]['total'].toFixed(2)+' €'+diff+''); + tbody.append(''+bal['balance'][c]['name']+''+bal['balance'][c]['total'].toFixed(2)+' €'+diff+''); } $('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €'); $('#display_balance_modal').modal('show'); @@ -523,28 +514,33 @@ 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) { - console.log(data); - if (data.groups) { - groups.import(data.groups); - groups.save(); - refresh_group_list(); - pleaseWaitHide(); - alert('Groupes synchronisés'); - } - else { - pleaseWaitHide(); - console.log(data); - alert('Erreur durant la synchronisation :('); - } - }, - function(data) { + onsuccess=function(data) { + console.log(data); + if (data.groups) { + groups.import(data.groups); + groups.save(); + refresh_group_list(); pleaseWaitHide(); - console.log(data); - alert('Impossible de contacter le serveur :('); + 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 :('); + } + } + sync_server.sync(user.server, user.email, user.password, groups.export(),onsuccess,onerror); } else { alert("Vous devez vous connecter pour commencer"); diff --git a/inc/myco_objects.js b/inc/myco_objects.js index 9e6206f..e628885 100644 --- a/inc/myco_objects.js +++ b/inc/myco_objects.js @@ -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
Participant * - +