Add edit contributor feature
Benjamin Renard

Benjamin Renard commited on 2014-01-06 02:04:57
Showing 3 changed files, with 45 additions and 8 deletions.

... ...
@@ -105,11 +105,14 @@ show_contributions=function(group,contributor_name) {
105 105
   $('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click);
106 106
 }
107 107
 
108
-/*********************
109
- * Add contributor
110
- *********************/
108
+/******************************
109
+ * Add/Edit/remove contributor
110
+ ******************************/
111 111
 on_click_add_contributor_btn=function() {
112 112
   $('#add_contributor_modal #group_name')[0].value=$('#view-group #group_name')[0].value;
113
+  $($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
114
+  $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
115
+  $('#view-group #edit_id')[0].value='-1';
113 116
   $('#add_contributor_modal').modal('show');
114 117
 }
115 118
 
... ...
@@ -120,11 +123,13 @@ on_show_add_contributor_modal=function(e) {
120 123
 on_valid_add_contributor_modal=function() {
121 124
   var group=groups[$('#add_contributor_modal #group_name')[0].value];
122 125
   var name=$('#add_contributor_name')[0].value;
126
+  edit_id=$('#view-group #edit_id')[0].value;
123 127
   if (name=='') {
124 128
     alert('Vous devez saisir un nom !');
125 129
     return;
126 130
   }
127
-  if (group.contributorByName(name)!=undefined) {
131
+  duplicate=group.contributorByName(name);
132
+  if (duplicate!=undefined && duplicate.id!=edit_id) {
128 133
     alert('Un contributor porte déjà ce nom !');
129 134
     return;
130 135
   }
... ...
@@ -133,11 +138,17 @@ on_valid_add_contributor_modal=function() {
133 138
     alert('Vous devez saisir un email !');
134 139
     return;
135 140
   }
136
-  if (group.contributorByEmail(email)!=undefined) {
141
+  duplicate=group.contributorByEmail(email)
142
+  if (duplicate!=undefined && duplicate.id!=edit_id) {
137 143
     alert('Un contributor utilise déjà cet email !');
138 144
     return;
139 145
   }
140
-  group.contributors.push(new Contributor(name,email));
146
+  if(edit_id=='-1') {
147
+    group.addContributor(new Contributor(name,email));
148
+  }
149
+  else {
150
+    group.replaceContributor(edit_id,new Contributor(name,email));
151
+  }
141 152
   set_contributors(group);
142 153
   $('#add_contributor_modal').modal('hide');
143 154
   $('#add_contributor_name')[0].value='';
... ...
@@ -151,6 +162,19 @@ on_close_add_contributor_modal=function () {
151 162
   $('#add_contributor_email')[0].value='';
152 163
 }
153 164
 
165
+on_edit_contributor_btn_click=function(e) {
166
+  var group=groups[$('#view-group #group_name')[0].value];
167
+  $('#add_contributor_modal #group_name')[0].value=group.name;
168
+  contributor_name=$('#view-group #contributor')[0].value;
169
+  contributor=group.contributorByName(contributor_name);
170
+  $('#view-group #edit_id')[0].value=contributor.id;
171
+  $('#add_contributor_modal #add_contributor_name')[0].value=contributor.name;
172
+  $('#add_contributor_modal #add_contributor_email')[0].value=contributor.email;
173
+  $($('#add_contributor_modal h4')[0]).html('Editer un participant');
174
+  $($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider');
175
+  $('#add_contributor_modal').modal('show');
176
+}
177
+
154 178
 on_click_del_contributor_btn=function() {
155 179
   contributor=$('#view-group #contributor')[0].value;
156 180
   question='Voulez-vous vraiment supprimer '+contributor+' ?';
... ...
@@ -358,6 +382,7 @@ $( document ).ready( function() {
358 382
   $("#add_contributor_modal").on('hidden.bs.modal',on_close_add_contributor_modal);
359 383
   $("#add_contributor_modal form").on('submit',on_valid_add_contributor_modal);
360 384
 
385
+  $('#edit_contributor_btn').bind('click',on_edit_contributor_btn_click);
361 386
   $('#del_contributor_btn').bind('click',on_click_del_contributor_btn);
362 387
 
363 388
   $('#add_contribution_btn').bind('click',on_click_add_contribution_btn);
... ...
@@ -119,6 +119,15 @@ function Group(name,data) {
119 119
     this.contributions[idx]=c;
120 120
   }
121 121
 
122
+  this.addContributor=function(c) {
123
+    c.id=this.contributors.length;
124
+    this.contributors.push(c);
125
+  }
126
+
127
+  this.replaceContributor=function(idx,c) {
128
+    c.id=idx;
129
+    this.contributors[idx]=c;
130
+  }
122 131
 
123 132
   this.balance=function() {
124 133
     ret={}
... ...
@@ -141,7 +150,8 @@ function Group(name,data) {
141 150
       for (idx in data.contributors) {
142 151
         this.contributors.push(new Contributor(
143 152
           data.contributors[idx].name,
144
-          data.contributors[idx].email
153
+          data.contributors[idx].email,
154
+          idx
145 155
         ));
146 156
       }
147 157
     }
... ...
@@ -163,9 +173,10 @@ function Group(name,data) {
163 173
   }
164 174
 }
165 175
 
166
-function Contributor(name,email) {
176
+function Contributor(name,email,id) {
167 177
   this.name=name;
168 178
   this.email=email;
179
+  this.id=id;
169 180
   this.export=function() {
170 181
     return {
171 182
       'name': this.name,
... ...
@@ -77,6 +77,7 @@ body{
77 77
   <h1><span>Noël</span><small><span id='edit_group_btn' class='glyphicon glyphicon-edit btn btn-sm'></span></small></h1>
78 78
   <form class="form-horizontal" role="form">
79 79
     <input type='hidden' id='group_name' value='Noël'/>
80
+    <input type='hidden' id='edit_id' value='-1' />
80 81
     <div class="form-group">
81 82
       <div class="input-group">
82 83
         <span class="input-group-addon">Participant</span>
83 84