Manage contributor by their email
Benjamin Renard

Benjamin Renard commited on 2014-01-19 13:48:41
Showing 3 changed files, with 72 additions and 81 deletions.

... ...
@@ -93,13 +93,13 @@ view_home=function() {
93 93
     balances=groups.balances();
94 94
     var sum=0;
95 95
     for (g in balances) {
96
-      if (jQuery.type(balances[g]['balance'][user.name])!='undefined') {
97
-        if (balances[g]['balance'][user.name]['diff']<0) {
98
-          sum+=balances[g]['balance'][user.name]['diff'];
99
-          diff='<td class="negative">'+balances[g]['balance'][user.name]['diff'].toFixed(2)+'</td>';
96
+      if (jQuery.type(balances[g]['balance'][user.email])!='undefined') {
97
+        if (balances[g]['balance'][user.email]['diff']<0) {
98
+          sum+=balances[g]['balance'][user.email]['diff'];
99
+          diff='<td class="negative">'+balances[g]['balance'][user.email]['diff'].toFixed(2)+'</td>';
100 100
         }
101 101
         else {
102
-          value=balances[g]['balance'][user.name]['total']-balances[g]['min'];
102
+          value=balances[g]['balance'][user.email]['total']-balances[g]['min'];
103 103
           sum+=value;
104 104
           diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
105 105
         }
... ...
@@ -143,9 +143,9 @@ set_contributors=function(group) {
143 143
     $('#view-group #contributor').append('<option>Aucun participant</option>');
144 144
   }
145 145
   else {
146
-    $(group.contributors).each(function(idx,c) {
147
-      $('#view-group #contributor').append('<option value="'+c.name+'">'+c.name+'</option>');
148
-    });
146
+    for (email in group.contributors) {
147
+      $('#view-group #contributor').append('<option value="'+email+'">'+group.contributors[email].name+'</option>');
148
+    }
149 149
   }
150 150
 }
151 151
 
... ...
@@ -153,11 +153,11 @@ on_contributor_change=function(e) {
153 153
   show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
154 154
 }
155 155
 
156
-show_contributions=function(group,contributor_name) {
156
+show_contributions=function(group,contributor_email) {
157 157
   var tbody=$($('#view-group #contributions tbody')[0]);
158 158
   tbody.html('');
159 159
   total=$($('#view-group #total-value')[0]);
160
-  contributions=group.contributionsByContributorName(contributor_name);
160
+  contributions=group.contributionsByContributorEmail(contributor_email);
161 161
   contributions.reverse();
162 162
   if (contributions.length==0) {
163 163
     tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
... ...
@@ -185,6 +185,7 @@ on_click_add_contributor_btn=function() {
185 185
   $($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
186 186
   $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
187 187
   $('#view-group #edit_id')[0].value='-1';
188
+  $('#add_contributor_modal #add_contributor_email').attr('disabled',false);
188 189
   $('#add_contributor_modal').modal('show');
189 190
 }
190 191
 
... ...
@@ -201,18 +202,13 @@ on_valid_add_contributor_modal=function(e) {
201 202
     alert('Vous devez saisir un nom !');
202 203
     return;
203 204
   }
204
-  duplicate=group.contributorByName(name);
205
-  if (duplicate!=undefined && duplicate.id!=edit_id) {
206
-    alert('Un contributor porte déjà ce nom !');
207
-    return;
208
-  }
209 205
   var email=$('#add_contributor_email')[0].value;
210 206
   if (email=='') {
211 207
     alert('Vous devez saisir un email !');
212 208
     return;
213 209
   }
214 210
   duplicate=group.contributorByEmail(email)
215
-  if (duplicate!=undefined && duplicate.id!=edit_id) {
211
+  if (duplicate!=undefined && edit=='-1') {
216 212
     alert('Un contributor utilise déjà cet email !');
217 213
     return;
218 214
   }
... ...
@@ -220,12 +216,10 @@ on_valid_add_contributor_modal=function(e) {
220 216
     group.addContributor(new Contributor(name,email));
221 217
   }
222 218
   else {
223
-    group.replaceContributor(edit_id,new Contributor(name,email));
219
+    group.replaceContributor(email,new Contributor(name,email));
224 220
   }
225 221
   set_contributors(group);
226 222
   $('#add_contributor_modal').modal('hide');
227
-  $('#add_contributor_name')[0].value='';
228
-  $('#add_contributor_email')[0].value='';
229 223
   groups.save();
230 224
 }
231 225
 
... ...
@@ -237,11 +231,12 @@ on_close_add_contributor_modal=function () {
237 231
 on_edit_contributor_btn_click=function(e) {
238 232
   var group=groups[$('#view-group').data('uuid')];
239 233
   $('#add_contributor_modal').data('group-uuid',group.uuid);
240
-  contributor_name=$('#view-group #contributor')[0].value;
241
-  contributor=group.contributorByName(contributor_name);
242
-  $('#view-group #edit_id')[0].value=contributor.id;
234
+  contributor_email=$('#view-group #contributor')[0].value;
235
+  contributor=group.contributorByEmail(contributor_email);
236
+  $('#view-group #edit_id')[0].value=contributor_email;
243 237
   $('#add_contributor_modal #add_contributor_name')[0].value=contributor.name;
244 238
   $('#add_contributor_modal #add_contributor_email')[0].value=contributor.email;
239
+  $('#add_contributor_modal #add_contributor_email').attr('disabled',true);
245 240
   $($('#add_contributor_modal h4')[0]).html('Editer un participant');
246 241
   $($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider');
247 242
   $('#add_contributor_modal').modal('show');
... ...
@@ -264,12 +259,8 @@ on_confirm_del_contributor=function(contributor) {
264 259
  * Add/edit/delete contribution
265 260
  ********************************/
266 261
 on_show_add_contribution_modal=function(e) {
267
-  var cs=$($('#add_contribution_modal #add_contribution_contributor_name')[0]);
268
-  cs.html('');
269
-  $('#view-group #contributor option').each(function(idx,option) {
270
-    cs.append($(option).clone());
271
-  });
272
-  cs[0].value = $('#view-group #contributor')[0].value;
262
+  $('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html());
263
+  $('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
273 264
   $('#add_contribution_modal #add_contribution_title').focus();
274 265
 }
275 266
 
... ...
@@ -284,8 +275,8 @@ on_click_add_contribution_btn=function() {
284 275
 on_valid_add_contribution_modal=function(e) {
285 276
   e.preventDefault();
286 277
   var group=groups[$('#add_contribution_modal').data('group-uuid')];
287
-  var contributor_name=$('#add_contribution_contributor_name')[0].value;
288
-  var contributor=group.contributorByName(contributor_name);
278
+  var contributor_email=$('#add_contribution_contributor_email')[0].value;
279
+  var contributor=group.contributorByEmail(contributor_email);
289 280
   if (contributor==undefined) {
290 281
     alert('Participant inconu !');
291 282
     return;
... ...
@@ -333,7 +324,7 @@ on_valid_add_contribution_modal=function(e) {
333 324
     contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
334 325
     group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
335 326
   }
336
-  show_contributions(group,contributor_name);
327
+  show_contributions(group,contributor_email);
337 328
   $('#add_contribution_modal').modal('hide');
338 329
   $('#add_contribution_modal form')[0].reset();
339 330
   groups.save();
... ...
@@ -363,7 +354,7 @@ on_contribution_edit_btn_click=function(e) {
363 354
   $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
364 355
   contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
365 356
   contribution=group.contributions[contribution_uuid];
366
-  $('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name;
357
+  $('#add_contribution_modal #add_contribution_contributor_email')[0].value=contribution.contributor.email;
367 358
   $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
368 359
   $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
369 360
   $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
... ...
@@ -406,7 +397,7 @@ display_balance=function(group) {
406 397
     else {
407 398
       diff='<td><span class="glyphicon glyphicon-thumbs-up"></span></td>';
408 399
     }
409
-    tbody.append('<tr><td>'+c+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>');
400
+    tbody.append('<tr><td>'+bal['balance'][c]['name']+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>');
410 401
   }
411 402
   $('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €');
412 403
   $('#display_balance_modal').modal('show');
... ...
@@ -523,8 +514,7 @@ on_click_sync_btn=function(e) {
523 514
   navbar_collapse_hide();
524 515
   if (user) {
525 516
     pleaseWaitShow();
526
-    sync_server.sync(user.server, user.email, user.password, groups.export(),
527
-      function(data) {
517
+    onsuccess=function(data) {
528 518
       console.log(data);
529 519
       if (data.groups) {
530 520
         groups.import(data.groups);
... ...
@@ -534,17 +524,23 @@ on_click_sync_btn=function(e) {
534 524
         alert('Groupes synchronisés');
535 525
       }
536 526
       else {
527
+        onerror(data);
528
+      }
529
+    }
530
+    onerror=function(data) {
537 531
       pleaseWaitHide();
538 532
       console.log(data);
533
+      if (jQuery.type(data['loginerror'])!='undefined') {
534
+        alert(data['loginerror']);
535
+      }
536
+      else if (jQuery.type(data['syncerror'])!='undefined') {
537
+        alert(data['syncerror']);
538
+      }
539
+      else {
539 540
         alert('Erreur durant la synchronisation :(');
540 541
       }
541
-      },
542
-      function(data) {
543
-        pleaseWaitHide();
544
-        console.log(data);
545
-        alert('Impossible de contacter le serveur :(');
546 542
     }
547
-    );
543
+    sync_server.sync(user.server, user.email, user.password, groups.export(),onsuccess,onerror);
548 544
   }
549 545
   else {
550 546
     alert("Vous devez vous connecter pour commencer");
... ...
@@ -116,7 +116,7 @@ function GroupList() {
116 116
 function Group(uuid,name,data) {
117 117
   this.uuid=uuid || generate_uuid();
118 118
   this.name=name || false;
119
-  this.contributors=[];
119
+  this.contributors={};
120 120
   this.contributions={};
121 121
   this.deletedContributions={};
122 122
 
... ...
@@ -126,9 +126,9 @@ function Group(uuid,name,data) {
126 126
   }
127 127
 
128 128
   this.export=function() {
129
-    var contributors=[];
130
-    for (idx in this.contributors) {
131
-      contributors.push(this.contributors[idx].export());
129
+    var contributors={};
130
+    for (email in this.contributors) {
131
+      contributors[email]=this.contributors[email].export();
132 132
     }
133 133
     var contributions={}
134 134
     for (uuid in this.contributions) {
... ...
@@ -147,42 +147,39 @@ function Group(uuid,name,data) {
147 147
    * Contributors
148 148
    */
149 149
   this.removeContributor=function(c) {
150
-    this.contributors=this.contributors.filter(function(v){
151
-      return (v.name!=c);
152
-    });
150
+    delete this.contributors[c.email];
153 151
   }
154 152
 
155 153
   this.contributorByName=function(name) {
156
-    for (c in this.contributors) {
157
-      if (this.contributors[c].name == name) return this.contributors[c];
154
+    for (email in this.contributors) {
155
+      if (this.contributors[email].name == name) return this.contributors[email];
158 156
     }
159 157
     return undefined;
160 158
   }
161 159
 
162 160
   this.contributorByEmail=function(email) {
163
-    for (c in this.contributors) {
164
-      if (this.contributors[c].email == email) return this.contributors[c];
161
+    if (jQuery.type(this.contributors[email])!='undefined') {
162
+      return this.contributors[email];
165 163
     }
166 164
     return undefined;
167 165
   }
168 166
 
169 167
   this.addContributor=function(c) {
170
-    c.id=this.contributors.length;
171
-    this.contributors.push(c);
168
+    this.contributors[c.email]=c;
172 169
   }
173 170
 
174
-  this.replaceContributor=function(idx,c) {
175
-    c.id=idx;
176
-    this.contributors[idx]=c;
171
+  this.replaceContributor=function(email,c) {
172
+    delete this.contributors[email];
173
+    this.contributors[c.email]=c;
177 174
   }
178 175
 
179 176
   /*
180 177
    * Contributions
181 178
    */
182
-  this.contributionsByContributorName=function(name) {
179
+  this.contributionsByContributorEmail=function(email) {
183 180
     var ret=[];
184 181
     for (uuid in this.contributions) {
185
-      if (this.contributions[uuid].contributor.name==name) {
182
+      if (this.contributions[uuid].contributor.email==email) {
186 183
         ret.push(this.contributions[uuid]);
187 184
       }
188 185
     }
... ...
@@ -223,10 +220,9 @@ function Group(uuid,name,data) {
223 220
     total={}
224 221
     min=-1;
225 222
     max=0;
226
-    for (idx in this.contributors) {
223
+    for (email in this.contributors) {
227 224
       var sum=0;
228
-      c=this.contributors[idx].name;
229
-      cl=this.contributionsByContributorName(c);
225
+      cl=this.contributionsByContributorEmail(email);
230 226
       for (idx in cl) {
231 227
         sum+=cl[idx].cost;
232 228
       }
... ...
@@ -236,16 +232,17 @@ function Group(uuid,name,data) {
236 232
       if(max<sum) {
237 233
         max=sum;
238 234
       }
239
-      total[c]=sum;
235
+      total[email]=sum;
240 236
     }
241 237
     balance={}
242 238
     var sum=0;
243
-    for (c in total) {
244
-      balance[c]={
245
-        'total': total[c],
246
-        'diff': total[c]-max,
239
+    for (email in total) {
240
+      balance[email]={
241
+        'name': this.contributors[email].name,
242
+        'total': total[email],
243
+        'diff': total[email]-max,
247 244
       }
248
-      sum=sum+total[c];
245
+      sum=sum+total[email];
249 246
     }
250 247
     return {
251 248
       'balance': balance,
... ...
@@ -262,19 +259,18 @@ function Group(uuid,name,data) {
262 259
     try {
263 260
       this.uuid=data.uuid;
264 261
       this.name=data.name;
265
-      if (jQuery.type(data.contributors) == 'array') {
266
-        for (idx in data.contributors) {
267
-          this.contributors.push(new Contributor(
268
-            decodeURIComponent(data.contributors[idx].name),
269
-            decodeURIComponent(data.contributors[idx].email),
270
-            idx
271
-          ));
262
+      if (jQuery.type(data.contributors) == 'object') {
263
+        for (email in data.contributors) {
264
+          this.contributors[email]=new Contributor(
265
+            decodeURIComponent(data.contributors[email].name),
266
+            data.contributors[email].email
267
+          );
272 268
         }
273 269
       }
274 270
       if (jQuery.type(data.contributions) == 'object') {
275 271
         for (uuid in data.contributions) {
276 272
           this.contributions[uuid]=new Contribution(
277
-            this.contributorByName(data.contributions[uuid].contributor),
273
+            this.contributorByEmail(data.contributions[uuid].contributor),
278 274
             data.contributions[uuid].cost,
279 275
             decodeURIComponent(data.contributions[uuid].title),
280 276
             data.contributions[uuid].date,
... ...
@@ -295,14 +291,13 @@ function Group(uuid,name,data) {
295 291
   }
296 292
 }
297 293
 
298
-function Contributor(name,email,id) {
294
+function Contributor(name,email) {
299 295
   this.name=name;
300 296
   this.email=email;
301
-  this.id=id;
302 297
   this.export=function() {
303 298
     return {
304 299
       'name': encodeURIComponent(this.name),
305
-      'email': encodeURIComponent(this.email)
300
+      'email': this.email
306 301
     };
307 302
   }
308 303
 }
... ...
@@ -319,7 +314,7 @@ function Contribution(contributor,cost,title,date,uuid,lastChange) {
319 314
   this.lastChange=lastChange || new Date().getTime();
320 315
   this.export=function() {
321 316
     return {
322
-      'contributor': encodeURIComponent(this.contributor.name),
317
+      'contributor': this.contributor.email,
323 318
       'uuid': this.uuid,
324 319
       'cost': this.cost,
325 320
       'title': encodeURIComponent(this.title),
... ...
@@ -316,7 +316,7 @@ body{
316 316
           <div class="form-group">
317 317
             <div class="input-group">
318 318
               <span class="input-group-addon">Participant *</span>
319
-              <select id='add_contribution_contributor_name' class="form-control"></select>
319
+              <select id='add_contribution_contributor_email' class="form-control"></select>
320 320
             </div>
321 321
           </div>
322 322
           <div class="form-group">
323 323