Manage group contributors trash
Benjamin Renard

Benjamin Renard commited on 2014-01-30 23:41:22
Showing 3 changed files, with 120 additions and 8 deletions.

... ...
@@ -233,6 +233,48 @@ on_confirm_contribution_restore=function(data) {
233 233
   view_group(data.group,data.contribution.contributor.email);
234 234
 }
235 235
 
236
+on_trash_group_contributors_btn_click=function(e) {
237
+  group=groups[$('#view-group').data('uuid')];
238
+  view_group_trash_contributors(group);
239
+}
240
+
241
+on_go_back_group_trash_contributors_btn_click=function(e) {
242
+  group=groups[$('#view-group-trash-contributors').data('uuid')];
243
+  view_group(group);
244
+}
245
+
246
+view_group_trash_contributors=function(group) {
247
+  $('#view-group-trash-contributors').data('uuid',group.uuid);
248
+  var tbody=$('#view-group-trash-contributors #trash tbody');
249
+  tbody.html('');
250
+  contributors=group.getDeletedContributors();
251
+  if (contributors.length==0) {
252
+    tbody.append('<tr><td colspan=3>Aucun participant</td></tr>');
253
+  }
254
+  else {
255
+    for (idx in contributors) {
256
+      tbody.append('<tr data-email="'+contributors[idx].email+'"><td>'+contributors[idx].name+'</td><td><button type="button" class="btn btn-default contributor_restore_btn"><span class="glyphicon glyphicon-share"></span></button></td></tr>');
257
+    }
258
+  }
259
+
260
+  $('#view-group-trash-contributors .contributor_restore_btn').bind('click',on_contributor_restore_btn_click);
261
+
262
+  view_part('#view-group-trash-contributors');
263
+}
264
+
265
+on_contributor_restore_btn_click=function(e) {
266
+  var group=groups[$('#view-group-trash-contributors').data('uuid')];
267
+  contributor_email=$($(e.target).parents('tr')[0]).data('email');
268
+  contributor=group.importContributor(group.deletedContributors[contributor_email]);
269
+  myconfirm('Etes-vous sûre de vouloir restaurer '+contributor.name+' ?',on_confirm_contributor_restore,null,{'group':group,'contributor_email':contributor_email, 'contributor': contributor} );
270
+}
271
+
272
+on_confirm_contributor_restore=function(data) {
273
+  data.group.restoreContributor(data.contributor_email);
274
+  groups.save();
275
+  view_group(data.group,data.contributor_email);
276
+}
277
+
236 278
 /******************************
237 279
  * Add/Edit/remove contributor
238 280
  ******************************/
... ...
@@ -728,6 +770,7 @@ $( document ).ready( function() {
728 770
   $("#add_contributor_modal form").bind('submit',on_valid_add_contributor_modal);
729 771
 
730 772
   $('#edit_contributor_btn').bind('click',on_edit_contributor_btn_click);
773
+  $('#trash_contributor_btn').bind('click',on_trash_group_contributors_btn_click);
731 774
   $('#del_contributor_btn').bind('click',on_click_del_contributor_btn);
732 775
 
733 776
   $('#add_contribution_btn').bind('click',on_click_add_contribution_btn);
... ...
@@ -739,6 +782,7 @@ $( document ).ready( function() {
739 782
   $("#display_balance_btn").bind('click',on_display_balance_btn_click);
740 783
 
741 784
   $("#view-group-trash #go-back-group").bind('click',on_go_back_group_btn_click);
785
+  $("#view-group-trash-contributors #go-back-group").bind('click',on_go_back_group_trash_contributors_btn_click);
742 786
 
743 787
   $('#trash_group_btn').bind('click',on_trash_group_btn_click);
744 788
   $('#remove_group_btn').bind('click',on_remove_group_btn_click);
... ...
@@ -118,6 +118,7 @@ function Group(uuid,name,data) {
118 118
   this.uuid=uuid || generate_uuid();
119 119
   this.name=name || false;
120 120
   this.contributors={};
121
+  this.deletedContributors={};
121 122
   this.contributions={};
122 123
   this.deletedContributions={};
123 124
 
... ...
@@ -139,6 +140,7 @@ function Group(uuid,name,data) {
139 140
       'uuid': this.uuid,
140 141
       'name': encodeURIComponent(this.name),
141 142
       'contributors': contributors,
143
+      'deletedContributors': this.deletedContributors,
142 144
       'contributions': contributions,
143 145
       'deletedContributions': this.deletedContributions
144 146
     };
... ...
@@ -148,7 +150,37 @@ function Group(uuid,name,data) {
148 150
    * Contributors
149 151
    */
150 152
   this.removeContributor=function(c) {
153
+    this.deletedContributors[c.email]=c.export();
154
+    time=new Date().getTime();
155
+    this.deletedContributors[c.email].deletionTime=time;
156
+    contributions=this.contributionsByContributorEmail(c.email);
157
+    for (idx in contributions) {
158
+      this.deleteContribution(contributions[idx].uuid,time);
159
+    }
151 160
     delete this.contributors[c.email];
161
+    return true;
162
+  }
163
+
164
+  this.restoreContributor=function(email) {
165
+    if (email in  this.contributors) return;
166
+    if (! email in this.deletedContributors) return;
167
+    this.contributors[email]=this.importContributor(this.deletedContributors[email]);
168
+    contributions=this.deletedContributionsByContributorEmail(email);
169
+    for (idx in contributions) {
170
+      if (contributions[idx].lastChange==this.deletedContributors[email].deletionTime) {
171
+        this.restoreContribution(contributions[idx].uuid);
172
+      }
173
+    }
174
+    delete this.deletedContributors[email];
175
+    return true;
176
+  }
177
+
178
+  this.getDeletedContributors=function() {
179
+    var ret=[];
180
+    for (email in this.deletedContributors) {
181
+      ret.push(this.importContributor(this.deletedContributors[email]));
182
+    }
183
+    return ret;
152 184
   }
153 185
 
154 186
   this.contributorByName=function(name) {
... ...
@@ -235,8 +267,8 @@ function Group(uuid,name,data) {
235 267
     this.contributions[uuid]=c;
236 268
   }
237 269
 
238
-  this.deleteContribution=function(uuid) {
239
-    this.contributions[uuid].lastChange=new Date().getTime();
270
+  this.deleteContribution=function(uuid,time) {
271
+    this.contributions[uuid].lastChange=time || new Date().getTime();
240 272
     this.deletedContributions[uuid]=this.contributions[uuid].export();
241 273
     delete this.contributions[uuid];
242 274
   }
... ...
@@ -247,6 +279,13 @@ function Group(uuid,name,data) {
247 279
     delete this.deletedContributions[uuid];
248 280
   }
249 281
 
282
+  this.importContributor=function(data) {
283
+    return new Contributor(
284
+      decodeURIComponent(data.name),
285
+      data.email
286
+    );
287
+  }
288
+
250 289
   this.importContribution=function(data) {
251 290
     return new Contribution(
252 291
       this.contributorByEmail(data.contributor),
... ...
@@ -306,10 +345,12 @@ function Group(uuid,name,data) {
306 345
       this.name=data.name;
307 346
       if (jQuery.type(data.contributors) == 'object') {
308 347
         for (email in data.contributors) {
309
-          this.contributors[email]=new Contributor(
310
-            decodeURIComponent(data.contributors[email].name),
311
-            data.contributors[email].email
312
-          );
348
+          this.contributors[email]=this.importContributor(data.contributors[email]);
349
+        }
350
+      }
351
+      if (jQuery.type(data.deletedContributors) == 'object') {
352
+        for (email in data.deletedContributors) {
353
+          this.deletedContributors[email]=data.deletedContributors[email];
313 354
         }
314 355
       }
315 356
       if (jQuery.type(data.contributions) == 'object') {
... ...
@@ -123,9 +123,15 @@ body{
123 123
         <select id='contributor' class="form-control">
124 124
         </select>
125 125
         <span class="input-group-btn">
126
-          <button type="button" class="btn btn-primary" id='edit_contributor_btn'><span class='glyphicon glyphicon-edit'></span></button>
127
-          <button type="button" class="btn btn-primary" id='del_contributor_btn'><span class='glyphicon glyphicon-trash'></span></button>
128 126
           <button type="button" class="btn btn-primary" id='add_contributor_btn'><span class='glyphicon glyphicon-plus'></span></button>
127
+            <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
128
+              <span class="glyphicon glyphicon-cog"></span>
129
+            </button>
130
+            <ul class="dropdown-menu">
131
+              <li><a id='edit_contributor_btn'><span class='glyphicon glyphicon-edit'></span> Modifier</span></a></li>
132
+              <li><a id='trash_contributor_btn'><span class='glyphicon glyphicon-trash'></span> Corbeille</span></a></li>
133
+              <li><a id='del_contributor_btn'><span class='glyphicon glyphicon-floppy-remove'></span> Supprimer le participant</span></a></li>
134
+            </ul>
129 135
         </span>
130 136
       </div>
131 137
     </div>
... ...
@@ -196,6 +202,27 @@ body{
196 202
 
197 203
 </div>
198 204
 
205
+<div id='view-group-trash-contributors' class='part'>
206
+  <h1>Corbeille</h1>
207
+  <button type="button" class="btn btn-default" id="go-back-group"><span class="glyphicon glyphicon-arrow-left"> Retour</span></button>
208
+  <table id='trash' class="table">
209
+    <thead>
210
+      <tr>
211
+        <th>Participant</th>
212
+        <th>Actions</th>
213
+      </tr>
214
+    </thead>
215
+    <tbody>
216
+      <tr>
217
+        <td>Nom</td>
218
+        <td>
219
+          <button type="button" class="btn btn-default contribution_restore_btn"><span class='glyphicon glyphicon-share'></span></button>
220
+        </td>
221
+      </tr>
222
+    </tbody>
223
+  </table>
224
+</div>
225
+
199 226
 <div class="modal fade" id="add_group_modal" tabindex="-1" role="dialog" aria-labelledby="addGroupModal" aria-hidden="true">
200 227
   <div class="modal-dialog">
201 228
     <div class="modal-content">
202 229