Fix category management
Benjamin Renard

Benjamin Renard commited on 2014-07-19 21:58:59
Showing 3 changed files, with 85 additions and 42 deletions.

... ...
@@ -133,12 +133,10 @@ view_home=function() {
133 133
 on_home_grp_cat_btn_click=function(e) {
134 134
   grp_id=$(e.target).parents('div.btn-group').data('grp');
135 135
   grp=groups[grp_id];
136
-  console.log(grp.getCategories());
137 136
   ul=$('#grp_cat_modal ul');
138 137
   html="";
139
-  cats=grp.getCategories();
140
-  for(cid in cats) {
141
-    html+="<li><span class='cat-color' style='background-color: "+cats[cid]['color']+"'></span> "+cats[cid]['name']+"</li>";
138
+  for(cid in grp.categories) {
139
+    html+="<li><span class='cat-color' style='background-color: "+grp.categories[cid]['color']+"'></span> "+grp.categories[cid]['name']+"</li>";
142 140
   }
143 141
   ul.html(html);
144 142
   $('#grp_cat_modal').modal('show');
... ...
@@ -191,9 +189,8 @@ show_contributions=function(group,contributor_email) {
191 189
     sum=0;
192 190
     for (idx in contributions) {
193 191
       var cat='';
194
-      console.log(contributions[idx].categorie);
195
-      if (contributions[idx].categorie && jQuery.type(group.categories[contributions[idx].categorie])) {
196
-		cat='<br/><span class="categorie"><span class="cat-color" style="background-color: '+group.categories[contributions[idx].categorie]['color']+'"></span> '+group.categories[contributions[idx].categorie]['name']+"</span>";
192
+      if (contributions[idx].category && jQuery.type(group.categories[contributions[idx].category])) {
193
+		cat='<br/><span class="category"><span class="cat-color" style="background-color: '+group.categories[contributions[idx].category]['color']+'"></span> '+group.categories[contributions[idx].category]['name']+"</span>";
197 194
 	  }
198 195
       col_actions='<td><div class="btn-group" data-uuid="'+contributions[idx].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>';
199 196
       tbody.append('<tr><td>'+contributions[idx].getTitle()+cat+'</td><td>'+contributions[idx].cost.toFixed(2)+' €<br/><span class="date">'+moment(contributions[idx].date).format('DD/MM/YYYY')+'</span></td>'+col_actions+'</tr>');
... ...
@@ -399,7 +396,7 @@ on_show_add_contribution_modal=function(e) {
399 396
   for (u in group.categories) {
400 397
 	cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>";
401 398
   }
402
-  $('#add_contribution_modal #add_contribution_categorie').html(cats);
399
+  $('#add_contribution_modal #add_contribution_category').html(cats);
403 400
   $('#add_contribution_modal #add_contribution_title').focus();
404 401
 }
405 402
 
... ...
@@ -458,9 +455,9 @@ on_valid_add_contribution_modal=function(e) {
458 455
     date=new Date();
459 456
   }
460 457
   
461
-  var cat=$('#add_contribution_categorie')[0].value;
458
+  var cat=$('#add_contribution_category')[0].value;
462 459
   if (cat != '' && !jQuery.type(group.categories[cat])) {
463
-	alert('Categorie incorrect');
460
+	alert('Category incorrect');
464 461
 	return;
465 462
   }
466 463
   
... ...
@@ -505,8 +502,8 @@ on_contribution_edit_btn_click=function(e) {
505 502
   $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
506 503
   $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
507 504
   $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
508
-  if (contribution.categorie) {
509
-	$('#add_contribution_modal #add_contribution_categorie')[0].value=contribution.categorie;
505
+  if (contribution.category) {
506
+	$('#add_contribution_modal #add_contribution_category')[0].value=contribution.category;
510 507
   }
511 508
   $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
512 509
   $('#add_contribution_modal').modal('show');
... ...
@@ -122,6 +122,7 @@ function Group(uuid,name,data) {
122 122
   this.contributions={};
123 123
   this.deletedContributions={};
124 124
   this.categories={};
125
+  this.deletedCategories={};
125 126
 
126 127
   this.isGroup=function() {
127 128
     return true;
... ...
@@ -136,6 +137,10 @@ function Group(uuid,name,data) {
136 137
     for (uuid in this.contributions) {
137 138
       contributions[uuid]=this.contributions[uuid].export();
138 139
     }
140
+    var categories={}
141
+    for (uuid in this.categories) {
142
+      categories[uuid]=this.categories[uuid].export();
143
+    }
139 144
     return {
140 145
       'uuid': this.uuid,
141 146
       'name': encodeURIComponent(this.name),
... ...
@@ -143,7 +148,8 @@ function Group(uuid,name,data) {
143 148
       'deletedContributors': this.deletedContributors,
144 149
       'contributions': contributions,
145 150
       'deletedContributions': this.deletedContributions,
146
-      'categories': this.categories
151
+      'categories': categories,
152
+      'deletedCategories': this.deletedCategories
147 153
     };
148 154
   }
149 155
 
... ...
@@ -207,6 +213,13 @@ function Group(uuid,name,data) {
207 213
     this.contributors[c.email]=c;
208 214
   }
209 215
   
216
+  this.importContributor=function(data) {
217
+    return new Contributor(
218
+      decodeURIComponent(data.name),
219
+      data.email
220
+    );
221
+  }
222
+
210 223
   /*
211 224
    * Contributions
212 225
    */
... ...
@@ -240,7 +253,7 @@ function Group(uuid,name,data) {
240 253
           this.deletedContributions[uuid].cost,
241 254
           decodeURIComponent(this.deletedContributions[uuid].title),
242 255
           this.deletedContributions[uuid].date,
243
-          this.deletedContributions[uuid].categorie,
256
+          this.deletedContributions[uuid].category,
244 257
           uuid,
245 258
           this.deletedContributions[uuid].lastChange
246 259
         ));
... ...
@@ -281,25 +294,51 @@ function Group(uuid,name,data) {
281 294
     delete this.deletedContributions[uuid];
282 295
   }
283 296
 
284
-  this.importContributor=function(data) {
285
-    return new Contributor(
286
-      decodeURIComponent(data.name),
287
-      data.email
288
-    );
289
-  }
290
-
291 297
   this.importContribution=function(data) {
292 298
     return new Contribution(
293 299
       this.contributorByEmail(data.contributor),
294 300
       data.cost,
295 301
       decodeURIComponent(data.title),
296 302
       data.date,
297
-      data.categorie,
303
+      data.category,
298 304
       data.uuid,
299 305
       data.lastChange
300 306
     );
301 307
   }
302 308
   
309
+  /*
310
+   * Categories
311
+   */
312
+
313
+  this.addCategory=function(c) {
314
+    this.categories[c.uuid]=c;
315
+  }
316
+
317
+  this.updateCategory=function(uuid,c) {
318
+    c.uuid=uuid;
319
+    this.categories[uuid]=c;
320
+  }
321
+
322
+  this.deleteCategory=function(uuid,time) {
323
+    this.categories[uuid].lastChange=time || new Date().getTime();
324
+    this.deletedCategory[uuid]=this.categories[uuid].export();
325
+    delete this.categories[uuid];
326
+  }
327
+
328
+  this.restoreCategory=function(uuid) {
329
+    this.deletedCategory[uuid].lastChange=new Date().getTime();
330
+    this.categories[uuid]=this.importCategory(this.deletedCategory[uuid]);
331
+    delete this.deletedCategory[uuid];
332
+  }
333
+
334
+  this.importCategory=function(data) {
335
+    return new Category(
336
+      decodeURIComponent(data.name),
337
+      data.color,
338
+      data.lastChange
339
+    );
340
+  }
341
+
303 342
   /*
304 343
    * Balance
305 344
    */
... ...
@@ -339,13 +378,6 @@ function Group(uuid,name,data) {
339 378
     };
340 379
   }
341 380
 
342
-  /*
343
-   * Categories
344
-   */
345
-  this.getCategories=function() {
346
-    return this.categories;
347
-  }
348
-
349 381
   /*
350 382
    * Contructor
351 383
    */
... ...
@@ -373,12 +405,10 @@ function Group(uuid,name,data) {
373 405
           this.deletedContributions[uuid]=data.deletedContributions[uuid];
374 406
         }
375 407
       }
408
+      console.log(data.categories);
376 409
       if (jQuery.type(data.categories) == 'object') {
377
-        for (cid in data.categories) {
378
-          if (jQuery.type(data.categories[cid]['color'])!='string') {
379
-            data.categories[cid]['color']='#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
380
-          }
381
-          this.categories[cid]=data.categories[cid];
410
+        for (uuid in data.categories) {
411
+          this.categories[uuid]=this.importCategory(data.categories[uuid]);
382 412
         }
383 413
       }
384 414
       else {
... ...
@@ -393,14 +423,17 @@ function Group(uuid,name,data) {
393 423
           'Cadeau': '#a700fa'
394 424
         };
395 425
         for (c in categories) {
396
-		  this.categories[generate_uuid()]={
397
-	        'name': c,
398
-	        'color': categories[c]
399
-		  };	
426
+		  this.categories[generate_uuid()]=new Category(c,categories[c]);
427
+	    }
428
+      }
429
+      if (jQuery.type(data.deletedCategories) == 'object') {
430
+        for (uuid in data.deletedCategories) {
431
+          this.deletedCategories[uuid]=data.deletedCategories[uuid];
400 432
         }
401 433
       }
402 434
     }
403 435
     catch (e) {
436
+	  console.log(e);
404 437
       alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
405 438
     }
406 439
   }
... ...
@@ -417,6 +450,19 @@ function Contributor(name,email) {
417 450
   }
418 451
 }
419 452
 
453
+function Category(name,color,lastChange) {
454
+  this.name=name;
455
+  this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
456
+  this.lastChange=lastChange || new Date().getTime();
457
+  this.export=function() {
458
+    return {
459
+      'name': encodeURIComponent(this.name),
460
+      'color': this.color,
461
+      'lastChange': this.lastChange
462
+    };
463
+  }
464
+}
465
+
420 466
 function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
421 467
   this.contributor=contributor;
422 468
   this.cost=cost;
... ...
@@ -426,7 +472,7 @@ function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
426 472
   }
427 473
   this.date=date;
428 474
   this.uuid=uuid || generate_uuid();
429
-  this.categorie=cat;
475
+  this.category=cat;
430 476
   this.lastChange=lastChange || new Date().getTime();
431 477
   this.export=function() {
432 478
     return {
... ...
@@ -435,7 +481,7 @@ function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
435 481
       'cost': this.cost,
436 482
       'title': encodeURIComponent(this.title),
437 483
       'date': this.date.getTime(),
438
-      'categorie': this.categorie,
484
+      'category': this.category,
439 485
       'lastChange': this.lastChange,
440 486
     };
441 487
   }
... ...
@@ -55,7 +55,7 @@ body{
55 55
   text-transform: capitalize;
56 56
 }
57 57
 
58
-#view-group span.date, #view-group span.categorie {
58
+#view-group span.date, #view-group span.category {
59 59
   color: #999;
60 60
   font-size: 0.8em;
61 61
   font-style: italic;
... ...
@@ -408,7 +408,7 @@ span.cat-color {
408 408
           <div class="form-group">
409 409
             <div class="input-group">
410 410
               <span class="input-group-addon">Catégorie </span>
411
-              <select id='add_contribution_categorie' class="form-control"></select>
411
+              <select id='add_contribution_category' class="form-control"></select>
412 412
             </div>
413 413
           </div>
414 414
           <div class="form-group">
415 415