Add uuid to group and refer group in gouplist with their uuid instead of name
Benjamin Renard

Benjamin Renard commited on 2014-01-16 19:16:29
Showing 3 changed files, with 51 additions and 31 deletions.

... ...
@@ -9,7 +9,7 @@ refresh_group_list=function() {
9 9
   }
10 10
   else { 
11 11
     groups.each(function(idx,group) {
12
-      $(grouplist).prepend('<li><a class="group-choice">'+group.name+'</a></li>');
12
+      $(grouplist).prepend('<li><a class="group-choice" data-uuid="'+group.uuid+'">'+group.name+'</a></li>');
13 13
     });
14 14
 
15 15
     $('a.group-choice').each(function(idx,a) {
... ...
@@ -43,11 +43,11 @@ on_valid_add_group_modal=function (e) {
43 43
     alert('Ce groupe exite déjà !');
44 44
     return;
45 45
   }
46
-  groups[name]=new Group(name);
46
+  var uuid=groups.newGroup(name);
47 47
   refresh_group_list();
48 48
   $('#add_group_modal').modal('hide');
49 49
   groups.save();
50
-  view_group(groups[name]);
50
+  view_group(groups[uuid]);
51 51
 }
52 52
 
53 53
 on_close_add_group_modal=function () {
... ...
@@ -58,7 +58,7 @@ on_close_add_group_modal=function () {
58 58
  * Nav bars
59 59
  ****************/
60 60
 on_group_choice_click=function(e) {
61
-  view_group(groups[$(e.target).html()]);
61
+  view_group(groups[$(e.target).data('uuid')]);
62 62
   navbar_collapse_hide();
63 63
 }
64 64
 
... ...
@@ -102,7 +102,7 @@ view_home=function() {
102 102
           sum+=value;
103 103
           diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
104 104
         }
105
-        tbody.html(tbody.html()+'<tr><td><a class="group-link">'+g+'</a></td>'+diff+'</tr>');
105
+        tbody.html(tbody.html()+'<tr><td><a class="group-link">'+balances[g].name+'</a></td>'+diff+'</tr>');
106 106
       }
107 107
     }
108 108
     $('#view-home #mybalances a.group-link').bind('click',function(e) {
... ...
@@ -129,8 +129,8 @@ view_home=function() {
129 129
  * View group
130 130
  ****************/
131 131
 view_group=function(group) {
132
+  $('#view-group').data('uuid',group.uuid);
132 133
   $('#view-group h1 span:first').html(group.name);
133
-  $('#view-group #group_name')[0].value=group.name;
134 134
   set_contributors(group);
135 135
   on_contributor_change();
136 136
   view_part('#view-group');
... ...
@@ -149,7 +149,7 @@ set_contributors=function(group) {
149 149
 }
150 150
 
151 151
 on_contributor_change=function(e) {
152
-  show_contributions(groups[$('#view-group #group_name')[0].value],$('#view-group #contributor')[0].value);
152
+  show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
153 153
 }
154 154
 
155 155
 show_contributions=function(group,contributor_name) {
... ...
@@ -179,7 +179,7 @@ show_contributions=function(group,contributor_name) {
179 179
  * Add/Edit/remove contributor
180 180
  ******************************/
181 181
 on_click_add_contributor_btn=function() {
182
-  $('#add_contributor_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
182
+  $('#add_contributor_modal').data('group-uuid',('#view-group').data('uuid'))
183 183
   $($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
184 184
   $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
185 185
   $('#view-group #edit_id')[0].value='-1';
... ...
@@ -192,7 +192,7 @@ on_show_add_contributor_modal=function(e) {
192 192
 
193 193
 on_valid_add_contributor_modal=function(e) {
194 194
   e.preventDefault();
195
-  var group=groups[$('#add_contributor_modal #group_name')[0].value];
195
+  var group=groups[$('#add_contributor_modal').data('group-uuid')];
196 196
   var name=$('#add_contributor_name')[0].value;
197 197
   edit_id=$('#view-group #edit_id')[0].value;
198 198
   if (name=='') {
... ...
@@ -233,8 +233,8 @@ on_close_add_contributor_modal=function () {
233 233
 }
234 234
 
235 235
 on_edit_contributor_btn_click=function(e) {
236
-  var group=groups[$('#view-group #group_name')[0].value];
237
-  $('#add_contributor_modal #group_name')[0].value=group.name;
236
+  var group=groups[$('#view-group').data('uuid')];
237
+  $('#add_contributor_modal').data('group-uuid',group.uuid);
238 238
   contributor_name=$('#view-group #contributor')[0].value;
239 239
   contributor=group.contributorByName(contributor_name);
240 240
   $('#view-group #edit_id')[0].value=contributor.id;
... ...
@@ -252,7 +252,7 @@ on_click_del_contributor_btn=function() {
252 252
 }
253 253
 
254 254
 on_confirm_del_contributor=function(contributor) {
255
-  var group=groups[$('#view-group #group_name')[0].value];
255
+  var group=groups[$('#view-group').data('uuid')];
256 256
   group.removeContributor(contributor);
257 257
   set_contributors(group);
258 258
   groups.save();
... ...
@@ -272,7 +272,7 @@ on_show_add_contribution_modal=function(e) {
272 272
 }
273 273
 
274 274
 on_click_add_contribution_btn=function() {
275
-  $('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
275
+  $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
276 276
   $('#add_contribution_modal #edit_uuid')[0].value='-1';
277 277
   $($('#add_contribution_modal h4')[0]).html('Ajouter une contribution');
278 278
   $($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
... ...
@@ -281,7 +281,7 @@ on_click_add_contribution_btn=function() {
281 281
 
282 282
 on_valid_add_contribution_modal=function(e) {
283 283
   e.preventDefault();
284
-  var group=groups[$('#add_contribution_modal #group_name')[0].value];
284
+  var group=groups[$('#add_contribution_modal').data('group-uuid')];
285 285
   var contributor_name=$('#add_contribution_contributor_name')[0].value;
286 286
   var contributor=group.contributorByName(contributor_name);
287 287
   if (contributor==undefined) {
... ...
@@ -342,7 +342,7 @@ on_close_add_contribution_modal=function () {
342 342
 }
343 343
 
344 344
 on_contribution_delete_btn_click=function(e) {
345
-  var group=groups[$('#view-group #group_name')[0].value];
345
+  var group=groups[$('#view-group').data('uuid')];
346 346
   contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
347 347
   contribution=group.contributions[contribution_uuid];
348 348
   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} );
... ...
@@ -355,8 +355,8 @@ on_confirm_contribution_delete=function(data) {
355 355
 }
356 356
 
357 357
 on_contribution_edit_btn_click=function(e) {
358
-  var group=groups[$('#view-group #group_name')[0].value];
359
-  $('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
358
+  var group=groups[$('#view-group').data('uuid')];
359
+  $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
360 360
   $($('#add_contribution_modal h4')[0]).html('Editer une contribution');
361 361
   $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
362 362
   contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
... ...
@@ -389,7 +389,7 @@ on_confirm_clear_local_data=function(data) {
389 389
  *********************/
390 390
 
391 391
 on_display_balance_btn_click=function(e) {
392
-  group=groups[$('#view-group #group_name')[0].value];
392
+  group=groups[$('#view-group').data('uuid')];
393 393
   display_balance(group);
394 394
 }
395 395
 
... ...
@@ -415,12 +415,12 @@ display_balance=function(group) {
415 415
  *********************/
416 416
 
417 417
 on_remove_group_btn_click=function(e) {
418
-  group=groups[$('#view-group #group_name')[0].value];
418
+  group=groups[$('#view-group').data('uuid')];
419 419
   myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group);
420 420
 }
421 421
 
422 422
 on_confirm_remove_group=function(group) {
423
-  if(groups.removeGroup(group.name)) {
423
+  if(groups.removeGroup(group.uuid)) {
424 424
     groups.save();
425 425
     location.reload();
426 426
   }
... ...
@@ -8,7 +8,7 @@ function GroupList() {
8 8
         var data=JSON.parse(localStorage.groups);
9 9
         this.lastChange=data.lastChange;
10 10
         for (el in data.groups) {
11
-          this[el]=new Group(el,data.groups[el]);
11
+          this[el]=new Group(el,false,data.groups[el]);
12 12
         }
13 13
       }
14 14
       catch(e) {
... ...
@@ -41,7 +41,7 @@ function GroupList() {
41 41
       }
42 42
     }
43 43
     for (el in groups) {
44
-      this[el]=new Group(el,groups[el]);
44
+      this[el]=new Group(el,false,groups[el]);
45 45
     }
46 46
     return true;
47 47
   }
... ...
@@ -77,9 +77,21 @@ function GroupList() {
77 77
     return (jQuery.type(el)=='object' && jQuery.type(el.isGroup)=='function' && el.isGroup());
78 78
   }
79 79
 
80
-  this.removeGroup=function(name) {
81
-    if (this.isGroup(this[name])) {
82
-      delete this[name];
80
+  this.byName=function(name) {
81
+    for (el in this) {
82
+      if(this.isGroup(this[el])) {
83
+        if (this[el].name==name) {
84
+          return this[el];
85
+        }
86
+      }
87
+    }
88
+    return false;
89
+  }
90
+
91
+
92
+  this.removeGroup=function(uuid) {
93
+    if (this.isGroup(this[uuid])) {
94
+      delete this[uuid];
83 95
       return true;
84 96
     }
85 97
     return false;
... ...
@@ -87,14 +99,23 @@ function GroupList() {
87 99
 
88 100
   this.balances=function(fct) {
89 101
     return this.each(function(idx,group) {
90
-      return group.balance();
102
+      bal=group.balance();
103
+      bal.name=group.name;
104
+      return bal;
91 105
     });
92 106
   }
93 107
 
108
+  this.newGroup=function(name,uuid) {
109
+    var uuid=uuid||generate_uuid();
110
+    this[uuid]=new Group(uuid,name);
111
+    return uuid;
94 112
   }
95 113
 
96
-function Group(name,data) {
97
-  this.name=name;
114
+}
115
+
116
+function Group(uuid,name,data) {
117
+  this.uuid=uuid || generate_uuid();
118
+  this.name=name || false;
98 119
   this.contributors=[];
99 120
   this.contributions={};
100 121
   this.deletedContributions={};
... ...
@@ -114,6 +135,7 @@ function Group(name,data) {
114 135
       contributions[uuid]=this.contributions[uuid].export();
115 136
     }
116 137
     return {
138
+      'uuid': this.uuid,
117 139
       'name': encodeURIComponent(this.name),
118 140
       'contributors': contributors,
119 141
       'contributions': contributions,
... ...
@@ -227,6 +249,7 @@ function Group(name,data) {
227 249
    */
228 250
   if (jQuery.type(data)=='object') {
229 251
     try {
252
+      this.uuid=data.uuid;
230 253
       this.name=data.name;
231 254
       if (jQuery.type(data.contributors) == 'array') {
232 255
         for (idx in data.contributors) {
... ...
@@ -110,7 +110,6 @@ body{
110 110
 <div id='view-group' class='part'>
111 111
   <h1><span>Noël</span><small><span id='edit_group_btn' class='glyphicon glyphicon-edit btn btn-sm'></span></small></h1>
112 112
   <form class="form-horizontal" role="form">
113
-    <input type='hidden' id='group_name' value='Noël'/>
114 113
     <input type='hidden' id='edit_id' value='-1' />
115 114
     <div class="form-group">
116 115
       <div class="input-group">
... ...
@@ -200,7 +199,6 @@ body{
200 199
       </div>
201 200
       <div class="modal-body">
202 201
         <form class="form-horizontal" role="form">
203
-          <input type='hidden' id='group_name' />
204 202
           <div class="form-group">
205 203
             <div class="input-group">
206 204
               <span class="input-group-addon">Nom *</span>
... ...
@@ -269,7 +267,6 @@ body{
269 267
       </div>
270 268
       <div class="modal-body">
271 269
         <form class="form-horizontal" role="form">
272
-          <input type='hidden' id='group_name' />
273 270
           <input type='hidden' id='edit_uuid' value='-1' />
274 271
           <div class="form-group">
275 272
             <div class="input-group">
276 273