Replace categories modal by view with categories management
Benjamin Renard

Benjamin Renard commited on 2014-07-22 22:16:12
Showing 3 changed files, with 122 additions and 42 deletions.

... ...
@@ -103,8 +103,7 @@ view_home=function() {
103 103
           sum+=value;
104 104
           diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
105 105
         }
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>');
106
+        tbody.html(tbody.html()+'<tr><td><a class="group-link" data-uuid="'+g+'">'+balances[g].name+'</a></td>'+diff+'</tr>');
108 107
       }
109 108
     }
110 109
     $('#view-home #mybalances a.group-link').bind('click',function(e) {
... ...
@@ -130,18 +129,6 @@ view_home=function() {
130 129
   view_part('#view-home');
131 130
 }
132 131
 
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
-  ul=$('#grp_cat_modal ul');
137
-  html="";
138
-  for(cid in grp.getSortedCategories()) {
139
-    html+="<li><span class='cat-color' style='background-color: "+grp.categories[cid]['color']+"'></span> "+grp.categories[cid]['name']+"</li>";
140
-  }
141
-  ul.html(html);
142
-  $('#grp_cat_modal').modal('show');
143
-}
144
-
145 132
 /****************
146 133
  * View group
147 134
  ****************/
... ...
@@ -207,6 +194,77 @@ show_contributions=function(group,contributor_email) {
207 194
   $('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click);
208 195
 }
209 196
 
197
+on_categories_group_btn_click=function(e) {
198
+  group=groups[$('#view-group').data('uuid')];
199
+  $('#view-group-categories').data('group-uuid',$('#view-group').data('uuid'));
200
+  refresh_group_categories(group);
201
+  view_part('#view-group-categories');
202
+}
203
+
204
+refresh_group_categories=function(group) {
205
+  ul=$('#view-group-categories ul');
206
+  html="";
207
+  for(cid in group.getSortedCategories()) {
208
+    html+="<li data-uuid='"+cid+"'><span class='cat-color' style='background-color: "+group.categories[cid]['color']+"'></span> "+group.categories[cid]['name']+"</li>";
209
+  }
210
+  ul.html(html);
211
+  $('#view-group-categories ul li').bind('click',on_categories_group_cat_click);
212
+}
213
+
214
+on_categories_group_cat_click=function(e) {
215
+  li=$(e.target);
216
+  if (li.prop("tagName")!='LI') {
217
+    return true;
218
+  }
219
+  cid=li.data('uuid');
220
+  group=groups[$('#view-group-categories').data('group-uuid')];
221
+  cat=group.categories[cid];
222
+  li.html("<span class='cat-color' style='background-color: "+cat['color']+"'></span> "+
223
+  "<input type='text' value=\""+cat.name+"\"/> "+
224
+  "<button class='btn btn-default btn-xs cat_edit'><span class='glyphicon glyphicon-ok'></span></button>"+
225
+  "<button class='btn btn-default btn-xs cat_delete'><span class='glyphicon glyphicon-trash'></span></button>");
226
+  li.children('button.cat_edit').bind('click',{'li': li,'group': group,'cid': cid},on_categories_group_cat_edit_valid_btn_click);
227
+  li.children('button.cat_delete').bind('click',{'li': li,'group': group,'cid': cid},on_categories_group_cat_delete_btn_click);
228
+}
229
+
230
+on_categories_group_cat_edit_valid_btn_click=function(e) {
231
+  name=e.data.li.children('input:first').val();
232
+  cat=e.data.group.categories[e.data.cid];
233
+  e.data.group.updateCategory(e.data.cid,new Category(name,cat.color));
234
+  groups.save();
235
+  refresh_group_categories(e.data.group);
236
+}
237
+
238
+on_categories_group_cat_delete_btn_click=function(e) {
239
+  e.data.group.deleteCategory(e.data.cid);
240
+  groups.save();
241
+  refresh_group_categories(e.data.group);
242
+}
243
+
244
+on_categories_go_back_group_btn_click=function(e) {
245
+  view_group(groups[$('#view-group-categories').data('group-uuid')]);
246
+}
247
+
248
+on_categories_group_add_btn_click=function(e) {
249
+  name=$('#add_category input')[0].value;
250
+  
251
+  if (jQuery.type(name)!='string' || name=='') {
252
+    return;
253
+  }
254
+  group_uuid=$('#view-group-categories').data('group-uuid');
255
+  group=groups[group_uuid];
256
+  
257
+  if (group.getCategoryByName(name,true)) {
258
+    alert('Cette catégorie existe déjà');
259
+  }
260
+  else {
261
+    group.addCategory(new Category(name));
262
+    $('#add_category input')[0].value='';
263
+    refresh_group_categories(group);
264
+  }
265
+}
266
+
267
+
210 268
 /*****************************
211 269
  * Trash
212 270
  *****************************/
... ...
@@ -856,8 +914,13 @@ $( document ).ready( function() {
856 914
   $("#view-group-trash #go-back-group").bind('click',on_go_back_group_btn_click);
857 915
   $("#view-group-trash-contributors #go-back-group").bind('click',on_go_back_group_trash_contributors_btn_click);
858 916
 
917
+  $('#categories_group_btn').bind('click',on_categories_group_btn_click);
859 918
   $('#trash_group_btn').bind('click',on_trash_group_btn_click);
860 919
   $('#remove_group_btn').bind('click',on_remove_group_btn_click);
920
+  
921
+  $('#view-group-categories span.input-group-addon').bind('click',on_categories_group_add_btn_click);
922
+  $("#view-group-categories button.go-back-group").bind('click',on_categories_go_back_group_btn_click);
923
+  
861 924
   view_home();
862 925
   pleaseWaitHide();
863 926
 } );
... ...
@@ -352,21 +352,39 @@ function Group(uuid,name,data) {
352 352
 
353 353
   this.deleteCategory=function(uuid,time) {
354 354
     this.categories[uuid].lastChange=time || new Date().getTime();
355
-    this.deletedCategory[uuid]=this.categories[uuid].export();
355
+    this.deletedCategories[uuid]=this.categories[uuid].export();
356 356
     delete this.categories[uuid];
357 357
   }
358 358
 
359 359
   this.restoreCategory=function(uuid) {
360
-    this.deletedCategory[uuid].lastChange=new Date().getTime();
361
-    this.categories[uuid]=this.importCategory(this.deletedCategory[uuid]);
362
-    delete this.deletedCategory[uuid];
360
+    this.deletedCategories[uuid].lastChange=new Date().getTime();
361
+    this.categories[uuid]=this.importCategory(this.deletedCategories[uuid]);
362
+    delete this.deletedCategories[uuid];
363
+  }
364
+  
365
+  this.getCategoryByName=function(name,approx) {
366
+    if(approx) {
367
+      name=String(name).replace(/^\s+|\s+$/g, '').toLowerCase();
368
+    }
369
+    for (uuid in this.categories) {
370
+      if (approx) {
371
+        if (String(this.categories[uuid].name).replace(/^\s+|\s+$/g, '').toLowerCase()==name) {
372
+          return this.categories[uuid];
373
+        }
374
+      }
375
+      else if(this.categories[uuid].name==name) {
376
+        return this.categories[uuid]
377
+      }
378
+    }
379
+    return false;
363 380
   }
364 381
 
365 382
   this.importCategory=function(data) {
366 383
     return new Category(
367 384
       decodeURIComponent(data.name),
368 385
       data.color,
369
-      data.lastChange
386
+      data.lastChange,
387
+      data.uuid
370 388
     );
371 389
   }
372 390
   
... ...
@@ -452,7 +470,6 @@ function Group(uuid,name,data) {
452 470
           this.deletedContributions[uuid]=data.deletedContributions[uuid];
453 471
         }
454 472
       }
455
-      console.log(data.categories);
456 473
       if (jQuery.type(data.categories) == 'object') {
457 474
         for (uuid in data.categories) {
458 475
           this.categories[uuid]=this.importCategory(data.categories[uuid]);
... ...
@@ -497,15 +514,17 @@ function Contributor(name,email) {
497 514
   }
498 515
 }
499 516
 
500
-function Category(name,color,lastChange) {
517
+function Category(name,color,lastChange,uuid) {
501 518
   this.name=name;
502 519
   this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
503 520
   this.lastChange=lastChange || new Date().getTime();
521
+  this.uuid=uuid || generate_uuid();
504 522
   this.export=function() {
505 523
     return {
506 524
       'name': encodeURIComponent(this.name),
507 525
       'color': this.color,
508
-      'lastChange': this.lastChange
526
+      'lastChange': this.lastChange,
527
+      'uuid': this.uuid
509 528
     };
510 529
   }
511 530
 }
... ...
@@ -67,7 +67,7 @@ span.cat-color {
67 67
   display: inline-block;
68 68
 }
69 69
 
70
-#grp_cat_modal ul {
70
+#view-group-categories ul {
71 71
   list-style-type: none;
72 72
   padding: 0;
73 73
 }
... ...
@@ -132,6 +132,10 @@ span.cat-color {
132 132
 .group-title {
133 133
   font-weight: bold;
134 134
 }
135
+
136
+.nav a, #mybalances a, #add_category span {
137
+  cursor: pointer;
138
+}
135 139
 </style>
136 140
   <body>
137 141
     <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
... ...
@@ -175,7 +179,6 @@ span.cat-color {
175 179
       <tr>
176 180
         <th>Groupe</th>
177 181
         <th>Balance</th>
178
-        <th>&nbsp;</th>
179 182
       </tr>
180 183
     </thead>
181 184
     <tbody></tbody>
... ...
@@ -183,7 +186,6 @@ span.cat-color {
183 186
       <tr>
184 187
         <td id='total-label'>Total :</td>
185 188
         <td id='total-value'></td>
186
-        <td>&nbsp;</td>
187 189
       </tr>
188 190
     </tfoot>
189 191
   </table>
... ...
@@ -256,6 +258,7 @@ span.cat-color {
256 258
       </div>
257 259
       <div class="collapse navbar-collapse" id="bottom-navbar-collapse-1">
258 260
         <ul class="nav navbar-nav navbar-right">
261
+          <li><a id='categories_group_btn'><span class='glyphicon glyphicon-th-list'></span> Catégories</span></a></li>
259 262
           <li><a id='trash_group_btn'><span class='glyphicon glyphicon-trash'></span> Corbeille</span></a></li>
260 263
           <li><a id='remove_group_btn'><span class='glyphicon glyphicon-floppy-remove'></span> Supprimer le groupe</span></a></li>
261 264
         </ul>
... ...
@@ -309,6 +312,18 @@ span.cat-color {
309 312
   </table>
310 313
 </div>
311 314
 
315
+<div id='view-group-categories' class='part row'>
316
+  <h1>Catégories <button type="button" class="btn btn-default go-back-group"><span class="glyphicon glyphicon-arrow-left"> Retour</span></button></h1>
317
+  <div class='col-xs-6'>
318
+    <ul>
319
+    </ul>
320
+    <div id="add_category" class='input-group'>
321
+      <input type='text' class="form-control" placeholder='nom'/>
322
+      <span class="input-group-addon"><span class="glyphicon glyphicon-plus"></span></span>
323
+    </div>
324
+  </div>
325
+</div>
326
+
312 327
 <div class="modal fade" id="add_group_modal" tabindex="-1" role="dialog" aria-labelledby="addGroupModal" aria-hidden="true">
313 328
   <div class="modal-dialog">
314 329
     <div class="modal-content">
... ...
@@ -615,23 +630,6 @@ span.cat-color {
615 630
   </div><!-- /.modal-dialog -->
616 631
 </div>
617 632
 
618
-<div class="modal fade" id="grp_cat_modal" tabindex="-1" role="dialog" aria-labelledby="grpCatModal" aria-hidden="true">
619
-  <div class="modal-dialog">
620
-    <div class="modal-content">
621
-      <div class="modal-header">
622
-        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
623
-        <h4 class="modal-title">Catégories</h4>
624
-      </div>
625
-      <div class="modal-body">
626
-        <ul id='grp_cat_list'></ul>
627
-      </div>
628
-      <div class="modal-footer">
629
-        <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
630
-      </div>
631
-    </div><!-- /.modal-content -->
632
-  </div><!-- /.modal-dialog -->
633
-</div>
634
-
635 633
 
636 634
 </div>
637 635
   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
638 636