Add possibility to view trash
Benjamin Renard

Benjamin Renard commited on 2014-01-29 23:30:05
Showing 3 changed files, with 134 additions and 13 deletions.

... ...
@@ -129,22 +129,28 @@ view_home=function() {
129 129
 /****************
130 130
  * View group
131 131
  ****************/
132
-view_group=function(group) {
132
+view_group=function(group,contributor_email) {
133 133
   $('#view-group').data('uuid',group.uuid);
134 134
   $('#view-group h1 span:first').html(group.name);
135
-  set_contributors(group);
135
+  set_contributors(group,contributor_email);
136 136
   on_contributor_change();
137 137
   view_part('#view-group');
138 138
 }
139 139
 
140
-set_contributors=function(group) {
140
+set_contributors=function(group,contributor_email) {
141 141
   $('#view-group #contributor option').remove();
142 142
   if(group.contributors.length==0) {
143 143
     $('#view-group #contributor').append('<option>Aucun participant</option>');
144 144
   }
145 145
   else {
146 146
     for (email in group.contributors) {
147
-      $('#view-group #contributor').append('<option value="'+email+'">'+group.contributors[email].name+'</option>');
147
+      if (email == contributor_email) {
148
+        sel=' selected';
149
+      }
150
+      else {
151
+        sel='';
152
+      }
153
+      $('#view-group #contributor').append('<option value="'+email+'"'+sel+'>'+group.contributors[email].name+'</option>');
148 154
     }
149 155
   }
150 156
 }
... ...
@@ -177,6 +183,56 @@ show_contributions=function(group,contributor_email) {
177 183
   $('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click);
178 184
 }
179 185
 
186
+/*****************************
187
+ * Trash
188
+ *****************************/
189
+
190
+on_trash_group_btn_click=function(e) {
191
+  group=groups[$('#view-group').data('uuid')];
192
+  contributor_email=$('#view-group #contributor')[0].value;
193
+  view_group_trash(group,contributor_email);
194
+}
195
+
196
+on_go_back_group_btn_click=function(e) {
197
+  group=groups[$('#view-group-trash').data('uuid')];
198
+  contributor_email=$('#view-group-trash').data('contributor-email');
199
+  view_group(group,contributor_email);
200
+}
201
+
202
+view_group_trash=function(group,contributor_email) {
203
+  $('#view-group-trash').data('uuid',group.uuid);
204
+  $('#view-group-trash').data('contributor-email',contributor_email);
205
+  var tbody=$('#view-group-trash #trash tbody');
206
+  tbody.html('');
207
+  contributions=group.deletedContributionsByContributorEmail(contributor_email);
208
+  contributions.reverse();
209
+  if (contributions.length==0) {
210
+    tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
211
+  }
212
+  else {
213
+    for (idx in contributions) {
214
+      tbody.append('<tr data-uuid="'+contributions[idx].uuid+'"><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><td><button type="button" class="btn btn-default contribution_restore_btn"><span class="glyphicon glyphicon-share"></span></button></td></tr>');
215
+    }
216
+  }
217
+  
218
+  $('#view-group-trash .contribution_restore_btn').bind('click',on_contribution_restore_btn_click);
219
+ 
220
+  view_part('#view-group-trash');
221
+}
222
+
223
+on_contribution_restore_btn_click=function(e) {
224
+  var group=groups[$('#view-group-trash').data('uuid')];
225
+  contribution_uuid=$($(e.target).parents('tr')[0]).data('uuid');
226
+  contribution=group.importContribution(group.deletedContributions[contribution_uuid]);
227
+  myconfirm('Etes-vous sûre de vouloir restaurer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_restore,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} );
228
+}
229
+
230
+on_confirm_contribution_restore=function(data) {
231
+  data.group.restoreContribution(data.contribution_uuid);
232
+  groups.save();
233
+  view_group(data.group,data.contribution.contributor.email);
234
+}
235
+
180 236
 /******************************
181 237
  * Add/Edit/remove contributor
182 238
  ******************************/
... ...
@@ -671,6 +727,9 @@ $( document ).ready( function() {
671 727
 
672 728
   $("#display_balance_btn").bind('click',on_display_balance_btn_click);
673 729
 
730
+  $("#view-group-trash #go-back-group").bind('click',on_go_back_group_btn_click);
731
+
732
+  $('#trash_group_btn').bind('click',on_trash_group_btn_click);
674 733
   $('#remove_group_btn').bind('click',on_remove_group_btn_click);
675 734
   view_home();
676 735
   pleaseWaitHide();
... ...
@@ -198,6 +198,34 @@ function Group(uuid,name,data) {
198 198
     return ret;
199 199
   }
200 200
 
201
+  this.deletedContributionsByContributorEmail=function(email) {
202
+    var ret=[];
203
+    for (uuid in this.deletedContributions) {
204
+      if (this.deletedContributions[uuid].contributor==email) {
205
+        ret.push(new Contribution(
206
+          this.contributorByEmail(email),
207
+          this.deletedContributions[uuid].cost,
208
+          decodeURIComponent(this.deletedContributions[uuid].title),
209
+          this.deletedContributions[uuid].date,
210
+          uuid,
211
+          this.deletedContributions[uuid].lastChange
212
+        ));
213
+      }
214
+    }
215
+    ret.sort(function(a,b) {
216
+      if (a.lastChange==b.lastChange) {
217
+        return 0;
218
+      }
219
+      else if(a.lastChange<b.lastChange) {
220
+        return -1;
221
+      }
222
+      else {
223
+        return 1;
224
+      }
225
+    });
226
+    return ret;
227
+  }
228
+
201 229
   this.addContribution=function(c) {
202 230
     this.contributions[c.uuid]=c;
203 231
   }
... ...
@@ -213,6 +241,22 @@ function Group(uuid,name,data) {
213 241
     delete this.contributions[uuid];
214 242
   }
215 243
 
244
+  this.restoreContribution=function(uuid) {
245
+    this.deletedContributions[uuid].lastChange=new Date().getTime();
246
+    this.contributions[uuid]=this.importContribution(this.deletedContributions[uuid]);
247
+    delete this.deletedContributions[uuid];
248
+  }
249
+
250
+  this.importContribution=function(data) {
251
+    return new Contribution(
252
+      this.contributorByEmail(data.contributor),
253
+      data.cost,
254
+      decodeURIComponent(data.title),
255
+      data.date,
256
+      data.uuid,
257
+      data.lastChange
258
+    );
259
+  }
216 260
 
217 261
   /*
218 262
    * Balance
... ...
@@ -270,14 +314,7 @@ function Group(uuid,name,data) {
270 314
       }
271 315
       if (jQuery.type(data.contributions) == 'object') {
272 316
         for (uuid in data.contributions) {
273
-          this.contributions[uuid]=new Contribution(
274
-            this.contributorByEmail(data.contributions[uuid].contributor),
275
-            data.contributions[uuid].cost,
276
-            decodeURIComponent(data.contributions[uuid].title),
277
-            data.contributions[uuid].date,
278
-            uuid,
279
-            data.contributions[uuid].lastChange
280
-          );
317
+          this.contributions[uuid]=this.importContribution(data.contributions[uuid]);
281 318
         }
282 319
       }
283 320
       if (jQuery.type(data.deletedContributions) == 'object') {
... ...
@@ -138,7 +138,8 @@ body{
138 138
         <span class="glyphicon glyphicon-cog"></span>
139 139
       </button>
140 140
       <ul class="dropdown-menu">
141
-        <li><a id='remove_group_btn'><span class='glyphicon glyphicon-trash'></span> Supprimer le groupe</span></a></li>
141
+        <li><a id='trash_group_btn'><span class='glyphicon glyphicon-trash'></span> Corbeille</span></a></li>
142
+        <li><a id='remove_group_btn'><span class='glyphicon glyphicon-floppy-remove'></span> Supprimer le groupe</span></a></li>
142 143
       </ul>
143 144
     </div>
144 145
   </div>
... ...
@@ -171,6 +172,30 @@ body{
171 172
   </table>
172 173
 </div>
173 174
 
175
+<div id='view-group-trash' class='part'>
176
+  <h1>Corbeille</h1>
177
+  <button type="button" class="btn btn-default" id="go-back-group"><span class="glyphicon glyphicon-arrow-left"> Retour</span></button>
178
+  <table id='trash' class="table">
179
+    <thead>
180
+      <tr>
181
+        <th>Dépence</th>
182
+        <th>Coût</th>
183
+        <th>Actions</th>
184
+      </tr>
185
+    </thead>
186
+    <tbody>
187
+      <tr>
188
+        <td>Label</td>
189
+        <td>XXX,XX€</td>
190
+        <td>
191
+          <button type="button" class="btn btn-default contribution_restore_btn"><span class='glyphicon glyphicon-share'></span></button>
192
+        </td>
193
+      </tr>
194
+    </tbody>
195
+  </table>
196
+
197
+</div>
198
+
174 199
 <div class="modal fade" id="add_group_modal" tabindex="-1" role="dialog" aria-labelledby="addGroupModal" aria-hidden="true">
175 200
   <div class="modal-dialog">
176 201
     <div class="modal-content">
177 202