Use UUID as Contribution identifier and keep trace of deleted contributions
Benjamin Renard

Benjamin Renard commited on 2014-01-13 23:32:29
Showing 4 changed files, with 65 additions and 44 deletions.

... ...
@@ -0,0 +1,6 @@
1
+generate_uuid=function() {
2
+	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
3
+		var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
4
+		return v.toString(16);
5
+	});
6
+}
... ...
@@ -162,10 +162,10 @@ show_contributions=function(group,contributor_name) {
162 162
   }
163 163
   else {
164 164
     sum=0;
165
-    for (idx in contributions) {
166
-      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>';
167
-      tbody.append('<tr><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost+' €</td>'+col_actions+'</tr>');
168
-      sum+=contributions[idx].cost;
165
+    for (uuid in contributions) {
166
+      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>';
167
+      tbody.append('<tr><td>'+contributions[uuid].getTitle()+'</td><td>'+contributions[uuid].cost+' €</td>'+col_actions+'</tr>');
168
+      sum+=contributions[uuid].cost;
169 169
     }
170 170
     total.html(sum+' €');
171 171
   }
... ...
@@ -271,7 +271,7 @@ on_show_add_contribution_modal=function(e) {
271 271
 
272 272
 on_click_add_contribution_btn=function() {
273 273
   $('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
274
-  $('#add_contribution_modal #edit_id')[0].value='-1';
274
+  $('#add_contribution_modal #edit_uuid')[0].value='-1';
275 275
   $($('#add_contribution_modal h4')[0]).html('Ajouter une contribution');
276 276
   $($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
277 277
   $('#add_contribution_modal').modal('show');
... ...
@@ -322,12 +322,12 @@ on_valid_add_contribution_modal=function(e) {
322 322
     date=new Date();
323 323
   }
324 324
   
325
-  if($('#add_contribution_modal #edit_id')[0].value=='-1') {
325
+  if($('#add_contribution_modal #edit_uuid')[0].value=='-1') {
326 326
     group.addContribution(new Contribution(contributor,cost,title,date));
327 327
   }
328 328
   else {
329
-    contribution_id=$('#add_contribution_modal #edit_id')[0].value;
330
-    group.replaceContribution(contribution_id,new Contribution(contributor,cost,title,date));
329
+    contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
330
+    group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
331 331
   }
332 332
   show_contributions(group,contributor_name);
333 333
   $('#add_contribution_modal').modal('hide');
... ...
@@ -341,13 +341,13 @@ on_close_add_contribution_modal=function () {
341 341
 
342 342
 on_contribution_delete_btn_click=function(e) {
343 343
   var group=groups[$('#view-group #group_name')[0].value];
344
-  contribution_id=$($(e.target).parents('div')[0]).data('id');
345
-  contribution=group.contributions[contribution_id];
346
-  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} );
344
+  contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
345
+  contribution=group.contributions[contribution_uuid];
346
+  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} );
347 347
 }
348 348
 
349 349
 on_confirm_contribution_delete=function(data) {
350
-  delete data.group.contributions[data.contribution_id];
350
+  data.group.deleteContribution(data.contribution_uuid);
351 351
   groups.save();
352 352
   on_contributor_change();
353 353
 }
... ...
@@ -357,13 +357,13 @@ on_contribution_edit_btn_click=function(e) {
357 357
   $('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
358 358
   $($('#add_contribution_modal h4')[0]).html('Editer une contribution');
359 359
   $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
360
-  contribution_id=$($(e.target).parents('div')[0]).data('id');
361
-  contribution=group.contributions[contribution_id];
360
+  contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
361
+  contribution=group.contributions[contribution_uuid];
362 362
   $('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name;
363 363
   $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
364 364
   $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
365 365
   $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
366
-  $('#add_contribution_modal #edit_id')[0].value=contribution_id;
366
+  $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
367 367
   $('#add_contribution_modal').modal('show');
368 368
 }
369 369
 
... ...
@@ -96,7 +96,8 @@ function GroupList() {
96 96
 function Group(name,data) {
97 97
   this.name=name;
98 98
   this.contributors=[];
99
-  this.contributions=[];
99
+  this.contributions={};
100
+  this.deletedContributions={};
100 101
 
101 102
 
102 103
   this.isGroup=function() {
... ...
@@ -108,14 +109,15 @@ function Group(name,data) {
108 109
     for (idx in this.contributors) {
109 110
       contributors.push(this.contributors[idx].export());
110 111
     }
111
-    var contributions=[];
112
-    for (idx in this.contributions) {
113
-      contributions.push(this.contributions[idx].export());
112
+    var contributions={}
113
+    for (uuid in this.contributions) {
114
+      contributions[uuid]=this.contributions[uuid].export();
114 115
     }
115 116
     return {
116 117
       'name': encodeURIComponent(this.name),
117 118
       'contributors': contributors,
118
-      'contributions': contributions
119
+      'contributions': contributions,
120
+      'deletedContributions': this.deletedContributions
119 121
     };
120 122
   }
121 123
 
... ...
@@ -156,25 +158,31 @@ function Group(name,data) {
156 158
    * Contributions
157 159
    */
158 160
   this.contributionsByContributorName=function(name) {
159
-    var ret=[];
160
-    for (idx in this.contributions) {
161
-      if (this.contributions[idx].contributor.name==name) {
162
-        ret.push(this.contributions[idx]);
161
+    var ret={};
162
+    for (uuid in this.contributions) {
163
+      if (this.contributions[uuid].contributor.name==name) {
164
+        ret[uuid]=this.contributions[uuid];
163 165
       }
164 166
     }
165 167
     return ret;
166 168
   }
167 169
 
168 170
   this.addContribution=function(c) {
169
-    c.id=this.contributions.length;
170
-    this.contributions.push(c);
171
+    this.contributions[c.uuid]=c;
171 172
   }
172 173
 
173
-  this.replaceContribution=function(idx,c) {
174
-    c.id=idx;
175
-    this.contributions[idx]=c;
174
+  this.updateContribution=function(uuid,c) {
175
+    c.uuid=uuid;
176
+    this.contributions[uuid]=c;
176 177
   }
177 178
 
179
+  this.deleteContribution=function(uuid) {
180
+    this.contributions[uuid].lastChange=new Date().getTime();
181
+    this.deletedContributions[uuid]=this.contributions[uuid].export();
182
+    delete this.contributions[uuid];
183
+  }
184
+
185
+
178 186
   /*
179 187
    * Balance
180 188
    */
... ...
@@ -186,8 +194,8 @@ function Group(name,data) {
186 194
       var sum=0;
187 195
       c=this.contributors[idx].name;
188 196
       cl=this.contributionsByContributorName(c);
189
-      for (idc in cl) {
190
-        sum+=cl[idc].cost;
197
+      for (uuid in cl) {
198
+        sum+=cl[uuid].cost;
191 199
       }
192 200
       if (min==-1 || min>sum) {
193 201
           min=sum;
... ...
@@ -229,16 +237,21 @@ function Group(name,data) {
229 237
           ));
230 238
         }
231 239
       }
232
-      if (jQuery.type(data.contributions) == 'array') {
233
-        for (idx in data.contributions) {
234
-          this.contributions.push(new Contribution(
235
-            this.contributorByName(data.contributions[idx].contributor),
236
-            data.contributions[idx].cost,
237
-            decodeURIComponent(data.contributions[idx].title),
238
-            data.contributions[idx].date,
239
-            idx,
240
-            data.contributions[idx].lastChange
241
-          ));
240
+      if (jQuery.type(data.contributions) == 'object') {
241
+        for (uuid in data.contributions) {
242
+          this.contributions[uuid]=new Contribution(
243
+            this.contributorByName(data.contributions[uuid].contributor),
244
+            data.contributions[uuid].cost,
245
+            decodeURIComponent(data.contributions[uuid].title),
246
+            data.contributions[uuid].date,
247
+            uuid,
248
+            data.contributions[uuid].lastChange
249
+          );
250
+        }
251
+      }
252
+      if (jQuery.type(data.deletedContributions) == 'object') {
253
+        for (uuid in data.deletedContributions) {
254
+          this.deletedContributions[uuid]=data.deletedContributions[uuid];
242 255
         }
243 256
       }
244 257
     }
... ...
@@ -260,16 +273,17 @@ function Contributor(name,email,id) {
260 273
   }
261 274
 }
262 275
 
263
-function Contribution(contributor,cost,title,date,id,lastChange) {
276
+function Contribution(contributor,cost,title,date,uuid,lastChange) {
264 277
   this.contributor=contributor;
265 278
   this.cost=cost;
266 279
   this.title=title;
267 280
   this.date=date;
268
-  this.id=id;
281
+  this.uuid=uuid || generate_uuid();
269 282
   this.lastChange=lastChange || new Date().getTime();
270 283
   this.export=function() {
271 284
     return {
272 285
       'contributor': encodeURIComponent(this.contributor.name),
286
+      'uuid': this.uuid,
273 287
       'cost': this.cost,
274 288
       'title': encodeURIComponent(this.title),
275 289
       'date': this.date,
... ...
@@ -270,7 +270,7 @@ body{
270 270
       <div class="modal-body">
271 271
         <form class="form-horizontal" role="form">
272 272
           <input type='hidden' id='group_name' />
273
-          <input type='hidden' id='edit_id' value='-1' />
273
+          <input type='hidden' id='edit_uuid' value='-1' />
274 274
           <div class="form-group">
275 275
             <div class="input-group">
276 276
               <span class="input-group-addon">Participant *</span>
... ...
@@ -421,6 +421,7 @@ body{
421 421
   <!-- Latest compiled and minified JavaScript -->
422 422
   <script src="inc/lib/bootstrap/js/bootstrap.min.js"></script>
423 423
   <script src="inc/lib/moment.min.js"></script>
424
+  <script src="inc/lib/uuid.js"></script>
424 425
   <script src="inc/myco_objects.js"></script>
425 426
   <script src="inc/myco_confirm.js"></script>
426 427
   <script src="inc/myco.js"></script>
427 428