Add Categories management
Benjamin Renard

Benjamin Renard commited on 2014-07-19 18:06:57
Showing 3 changed files, with 123 additions and 8 deletions.

... ...
@@ -103,7 +103,8 @@ view_home=function() {
103 103
           sum+=value;
104 104
           diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
105 105
         }
106
-        tbody.html(tbody.html()+'<tr><td><a class="group-link" data-uuid="'+g+'">'+balances[g].name+'</a></td>'+diff+'</tr>');
106
+        menu='<div class="btn-group" data-grp="'+g+'"><button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-cog"></span></button><ul class="dropdown-menu"><li><a class="home_grp_cat_btn"><span class="glyphicon glyphicon-th-list"></span> Catégories</a></li></ul></div>';
107
+        tbody.html(tbody.html()+'<tr><td><a class="group-link" data-uuid="'+g+'">'+balances[g].name+'</a></td>'+diff+'<td>'+menu+'</td></tr>');
107 108
       }
108 109
     }
109 110
     $('#view-home #mybalances a.group-link').bind('click',function(e) {
... ...
@@ -112,6 +113,9 @@ view_home=function() {
112 113
         view_group(groups[g]);
113 114
       }
114 115
     });
116
+    $('a.home_grp_cat_btn').each(function(idx,a) {
117
+      $(a).bind('click',on_home_grp_cat_btn_click);
118
+    });
115 119
     if (sum<0) {
116 120
       $('#view-home #mybalances #total-value').html('<span class="negative">'+sum.toFixed(2)+' €</span>');
117 121
     }
... ...
@@ -126,6 +130,20 @@ view_home=function() {
126 130
   view_part('#view-home');
127 131
 }
128 132
 
133
+on_home_grp_cat_btn_click=function(e) {
134
+  grp_id=$(e.target).parents('div.btn-group').data('grp');
135
+  grp=groups[grp_id];
136
+  console.log(grp.getCategories());
137
+  ul=$('#grp_cat_modal ul');
138
+  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>";
142
+  }
143
+  ul.html(html);
144
+  $('#grp_cat_modal').modal('show');
145
+}
146
+
129 147
 /****************
130 148
  * View group
131 149
  ****************/
... ...
@@ -172,8 +190,13 @@ show_contributions=function(group,contributor_email) {
172 190
   else {
173 191
     sum=0;
174 192
     for (idx in contributions) {
193
+      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>";
197
+	  }
175 198
       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>';
176
-      tbody.append('<tr><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost.toFixed(2)+' €<br/><span class="date">'+moment(contributions[idx].date).format('DD/MM/YYYY')+'</span></td>'+col_actions+'</tr>');
199
+      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>');
177 200
       sum+=contributions[idx].cost;
178 201
     }
179 202
     total.html(sum.toFixed(2)+' €');
... ...
@@ -370,6 +393,13 @@ on_confirm_del_contributor=function(contributor) {
370 393
 on_show_add_contribution_modal=function(e) {
371 394
   $('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html());
372 395
   $('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
396
+  gid=$('#add_contribution_modal').data('group-uuid');
397
+  group=groups[gid];
398
+  cats="<option value=''>Non définie</option>";
399
+  for (u in group.categories) {
400
+	cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>";
401
+  }
402
+  $('#add_contribution_modal #add_contribution_categorie').html(cats);
373 403
   $('#add_contribution_modal #add_contribution_title').focus();
374 404
 }
375 405
 
... ...
@@ -428,12 +458,18 @@ on_valid_add_contribution_modal=function(e) {
428 458
     date=new Date();
429 459
   }
430 460
   
461
+  var cat=$('#add_contribution_categorie')[0].value;
462
+  if (cat != '' && !jQuery.type(group.categories[cat])) {
463
+	alert('Categorie incorrect');
464
+	return;
465
+  }
466
+  
431 467
   if($('#add_contribution_modal #edit_uuid')[0].value=='-1') {
432
-    group.addContribution(new Contribution(contributor,cost,title,date));
468
+    group.addContribution(new Contribution(contributor,cost,title,date,cat));
433 469
   }
434 470
   else {
435 471
     contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
436
-    group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
472
+    group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date,cat));
437 473
   }
438 474
   show_contributions(group,contributor_email);
439 475
   $('#add_contribution_modal').modal('hide');
... ...
@@ -469,6 +505,9 @@ on_contribution_edit_btn_click=function(e) {
469 505
   $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
470 506
   $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
471 507
   $('#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;
510
+  }
472 511
   $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
473 512
   $('#add_contribution_modal').modal('show');
474 513
 }
... ...
@@ -121,7 +121,7 @@ function Group(uuid,name,data) {
121 121
   this.deletedContributors={};
122 122
   this.contributions={};
123 123
   this.deletedContributions={};
124
-
124
+  this.categories={};
125 125
 
126 126
   this.isGroup=function() {
127 127
     return true;
... ...
@@ -142,7 +142,8 @@ function Group(uuid,name,data) {
142 142
       'contributors': contributors,
143 143
       'deletedContributors': this.deletedContributors,
144 144
       'contributions': contributions,
145
-      'deletedContributions': this.deletedContributions
145
+      'deletedContributions': this.deletedContributions,
146
+      'categories': this.categories
146 147
     };
147 148
   }
148 149
 
... ...
@@ -239,6 +240,7 @@ function Group(uuid,name,data) {
239 240
           this.deletedContributions[uuid].cost,
240 241
           decodeURIComponent(this.deletedContributions[uuid].title),
241 242
           this.deletedContributions[uuid].date,
243
+          this.deletedContributions[uuid].categorie,
242 244
           uuid,
243 245
           this.deletedContributions[uuid].lastChange
244 246
         ));
... ...
@@ -292,6 +294,7 @@ function Group(uuid,name,data) {
292 294
       data.cost,
293 295
       decodeURIComponent(data.title),
294 296
       data.date,
297
+      data.categorie,
295 298
       data.uuid,
296 299
       data.lastChange
297 300
     );
... ...
@@ -336,6 +339,13 @@ function Group(uuid,name,data) {
336 339
     };
337 340
   }
338 341
 
342
+  /*
343
+   * Categories
344
+   */
345
+  this.getCategories=function() {
346
+    return this.categories;
347
+  }
348
+
339 349
   /*
340 350
    * Contructor
341 351
    */
... ...
@@ -363,6 +373,32 @@ function Group(uuid,name,data) {
363 373
           this.deletedContributions[uuid]=data.deletedContributions[uuid];
364 374
         }
365 375
       }
376
+      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];
382
+        }
383
+      }
384
+      else {
385
+		categories= {
386
+          'Alimentation': '#1f83db',
387
+          'Restaurant': '#f07305',
388
+          'Loisir': '#d413ce',
389
+          'Transport': '#13d413',
390
+          'Vacances': '#e9fa00',
391
+          'Maison': '#e9fa00',
392
+          'Rembourssement': '#8a8b8c',
393
+          'Cadeau': '#a700fa'
394
+        };
395
+        for (c in categories) {
396
+		  this.categories[generate_uuid()]={
397
+	        'name': c,
398
+	        'color': categories[c]
399
+		  };	
400
+	    }
401
+      }
366 402
     }
367 403
     catch (e) {
368 404
       alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
... ...
@@ -381,7 +417,7 @@ function Contributor(name,email) {
381 417
   }
382 418
 }
383 419
 
384
-function Contribution(contributor,cost,title,date,uuid,lastChange) {
420
+function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
385 421
   this.contributor=contributor;
386 422
   this.cost=cost;
387 423
   this.title=title;
... ...
@@ -390,6 +426,7 @@ function Contribution(contributor,cost,title,date,uuid,lastChange) {
390 426
   }
391 427
   this.date=date;
392 428
   this.uuid=uuid || generate_uuid();
429
+  this.categorie=cat;
393 430
   this.lastChange=lastChange || new Date().getTime();
394 431
   this.export=function() {
395 432
     return {
... ...
@@ -398,6 +435,7 @@ function Contribution(contributor,cost,title,date,uuid,lastChange) {
398 435
       'cost': this.cost,
399 436
       'title': encodeURIComponent(this.title),
400 437
       'date': this.date.getTime(),
438
+      'categorie': this.categorie,
401 439
       'lastChange': this.lastChange,
402 440
     };
403 441
   }
... ...
@@ -55,11 +55,22 @@ body{
55 55
   text-transform: capitalize;
56 56
 }
57 57
 
58
-#view-group span.date {
58
+#view-group span.date, #view-group span.categorie {
59 59
   color: #999;
60 60
   font-size: 0.8em;
61 61
   font-style: italic;
62 62
 }
63
+
64
+span.cat-color {
65
+  width: 0.8em;
66
+  height: 0.8em;
67
+  display: inline-block;
68
+}
69
+
70
+#grp_cat_modal ul {
71
+  list-style-type: none;
72
+  padding: 0;
73
+}
63 74
 </style>
64 75
   <body>
65 76
     <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
... ...
@@ -103,6 +114,7 @@ body{
103 114
       <tr>
104 115
         <th>Groupe</th>
105 116
         <th>Balance</th>
117
+        <th>&nbsp;</th>
106 118
       </tr>
107 119
     </thead>
108 120
     <tbody></tbody>
... ...
@@ -110,6 +122,7 @@ body{
110 122
       <tr>
111 123
         <td id='total-label'>Total :</td>
112 124
         <td id='total-value'></td>
125
+        <td>&nbsp;</td>
113 126
       </tr>
114 127
     </tfoot>
115 128
   </table>
... ...
@@ -392,6 +405,12 @@ body{
392 405
               <span class="input-group-addon">€</span>
393 406
             </div>
394 407
           </div>
408
+          <div class="form-group">
409
+            <div class="input-group">
410
+              <span class="input-group-addon">Catégorie </span>
411
+              <select id='add_contribution_categorie' class="form-control"></select>
412
+            </div>
413
+          </div>
395 414
           <div class="form-group">
396 415
             <div class="input-group">
397 416
               <span class="input-group-addon">Date</span>
... ...
@@ -515,6 +534,7 @@ body{
515 534
           </div>
516 535
         </div>
517 536
       </form>
537
+      </div>
518 538
       <div class="modal-footer">
519 539
         <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
520 540
       </div>
... ...
@@ -522,6 +542,24 @@ body{
522 542
   </div><!-- /.modal-dialog -->
523 543
 </div>
524 544
 
545
+<div class="modal fade" id="grp_cat_modal" tabindex="-1" role="dialog" aria-labelledby="grpCatModal" aria-hidden="true">
546
+  <div class="modal-dialog">
547
+    <div class="modal-content">
548
+      <div class="modal-header">
549
+        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
550
+        <h4 class="modal-title">Catégories</h4>
551
+      </div>
552
+      <div class="modal-body">
553
+        <ul id='grp_cat_list'></ul>
554
+      </div>
555
+      <div class="modal-footer">
556
+        <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
557
+      </div>
558
+    </div><!-- /.modal-content -->
559
+  </div><!-- /.modal-dialog -->
560
+</div>
561
+
562
+
525 563
 </div>
526 564
   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
527 565
   <script src="inc/lib/jquery-1.10.2.min.js"></script>
528 566