Fix add contribution modal categories population
Benjamin Renard

Benjamin Renard commited on 2015-03-09 23:42:16
Showing 1 changed files, with 22 additions and 9 deletions.

... ...
@@ -541,13 +541,7 @@ on_show_add_contribution_modal=function(e) {
541 541
   $('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
542 542
   gid=$('#add_contribution_modal').data('group-uuid');
543 543
   group=groups[gid];
544
-  current_cat=$('#add_contribution_modal #add_contribution_category')[0].value;
545
-  cats="<option value=''>Non définie</option>";
546
-  for (u in group.getSortedCategories()) {
547
-	cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>";
548
-  }
549
-  $('#add_contribution_modal #add_contribution_category').html(cats);
550
-  $('#add_contribution_modal #add_contribution_category')[0].value=current_cat;
544
+  populate_add_contribution_categories(group);
551 545
   $('#add_contribution_modal #add_contribution_title').typeahead('destroy');
552 546
   $('#add_contribution_modal #add_contribution_title').typeahead({
553 547
     hint: true,
... ...
@@ -563,9 +557,28 @@ on_show_add_contribution_modal=function(e) {
563 557
   $('#add_contribution_modal #add_contribution_title').focus();
564 558
 }
565 559
 
560
+populate_add_contribution_categories=function(group,select) {
561
+  if (!group) {
562
+    gid=$('#add_contribution_modal').data('group-uuid');
563
+    group=groups[gid];
564
+  }
565
+  current_cat=$('#add_contribution_modal #add_contribution_category')[0].value;
566
+  cats="<option value=''>Non définie</option>";
567
+  for (u in group.getSortedCategories()) {
568
+	cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>";
569
+  }
570
+  $('#add_contribution_modal #add_contribution_category').html(cats);
571
+  if (select) {
572
+    $('#add_contribution_modal #add_contribution_category').val(select);
573
+  }
574
+  else {
575
+    $('#add_contribution_modal #add_contribution_category').val(current_cat);
576
+  }
577
+}
578
+
566 579
 on_select_contribution_suggestion=function(event,choice,name) {
567 580
   if (jQuery.type(choice['category'])=='string') {
568
-    $('#add_contribution_modal #add_contribution_category').val(choice['category']);
581
+    populate_add_contribution_categories(null,choice['category']);
569 582
   }
570 583
 }
571 584
 
... ...
@@ -672,7 +685,7 @@ on_contribution_edit_btn_click=function(e) {
672 685
   $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
673 686
   $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
674 687
   if (contribution.category) {
675
-	$('#add_contribution_modal #add_contribution_category')[0].value=contribution.category;
688
+    populate_add_contribution_categories(group,contribution.category);
676 689
   }
677 690
   $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
678 691
   $('#add_contribution_modal').modal('show');
679 692