Zionetrix::Git
Repositories
Help
Report an Issue
MyCo
Code
Commits
Branches
Tags
Search
Tree:
8a949d3
Branches
Tags
master
MyCo
inc
myco.js
Fix error on add_group_modal reset form on close
Benjamin Renard
commited
8a949d3
at 2014-01-06 01:11:09
myco.js
Blame
History
Raw
refresh_group_list=function() { grouplist=$('#group-choice')[0]; $('a.group-choice').each(function(idx,a) { $(a).parent().remove(); }); if (groups.count()==0) { $(grouplist).prepend('<li><a class="group-choice">Aucune groupe</a></li>'); } else { groups.each(function(idx,group) { $(grouplist).prepend('<li><a class="group-choice">'+group.name+'</a></li>'); }); $('a.group-choice').each(function(idx,a) { $(a).bind('click',on_group_choice_click); }); } } /**************** * Add group ****************/ on_show_add_group_modal=function(e) { $('#add_group_modal #add_group_name').focus(); } on_valid_add_group_modal=function () { var name=$('#add_group_name')[0].value; if (name=='') { alert('Vous devez saisir un nom !'); return; } if (groups[name]!==undefined) { alert('Ce groupe exite déjà !'); return; } groups[name]=new Group(name); refresh_group_list(); $('#add_group_modal').modal('hide'); groups.save(); view_group(groups[name]); } on_close_add_group_modal=function () { $('#add_group_modal form')[0].reset(); } /**************** * Nav bars ****************/ on_group_choice_click=function(e) { view_group(groups[$(e.target).html()]); } /**************** * View group ****************/ view_group=function(group) { $('#view-group h1').html(group.name); set_contributors(group); on_contributor_change(); $('#view-group').css('display','block'); } set_contributors=function(group) { $('#view-group #contributor option').remove(); if(group.contributors.length==0) { $('#view-group #contributor').append('<option>Aucun participant</option>'); } else { $(group.contributors).each(function(idx,c) { $('#view-group #contributor').append('<option value="'+c.name+'">'+c.name+'</option>'); }); } } on_contributor_change=function(e) { show_contributions(groups[$('#view-group h1').html()],$('#view-group #contributor')[0].value); } show_contributions=function(group,contributor_name) { var tbody=$($('#view-group #contributions tbody')[0]); tbody.html(''); total=$($('#view-group #total-value')[0]); contributions=group.contributionsByContributorName(contributor_name); if (contributions.length==0) { tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>'); total.html('0,00 €'); } else { sum=0; for (idx in contributions) { col_actions='<td><div class="btn-group" data-id="'+contributions[idx].id+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>'; tbody.append('<tr><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost+' €</td>'+col_actions+'</tr>'); sum+=contributions[idx].cost; } total.html(sum+' €'); } $('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click); $('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click); } /********************* * Add contributor *********************/ on_click_add_contributor_btn=function() { $('#add_contributor_modal #group_name')[0].value=$($('#view-group h1')[0]).html(); $('#add_contributor_modal').modal('show'); } on_show_add_contributor_modal=function(e) { $('#add_contributor_modal #add_contributor_name').focus(); } on_valid_add_contributor_modal=function() { var group=groups[$('#add_contributor_modal #group_name')[0].value]; var name=$('#add_contributor_name')[0].value; if (name=='') { alert('Vous devez saisir un nom !'); return; } if (group.contributorByName(name)!=undefined) { alert('Un contributor porte déjà ce nom !'); return; } var email=$('#add_contributor_email')[0].value; if (email=='') { alert('Vous devez saisir un email !'); return; } if (group.contributorByEmail(email)!=undefined) { alert('Un contributor utilise déjà cet email !'); return; } group.contributors.push(new Contributor(name,email)); set_contributors(group); $('#add_contributor_modal').modal('hide'); $('#add_contributor_name')[0].value=''; $('#add_contributor_email')[0].value=''; groups.save(); } on_close_add_contributor_modal=function () { $('#add_contributor_name')[0].value=''; $('#add_contributor_email')[0].value=''; } on_click_del_contributor_btn=function() { contributor=$('#view-group #contributor')[0].value; question='Voulez-vous vraiment supprimer '+contributor+' ?'; myconfirm(question,on_confirm_del_contributor,null,contributor); } on_confirm_del_contributor=function(contributor) { var group=groups[$($('#view-group h1')[0]).html()]; group.removeContributor(contributor); set_contributors(group); groups.save(); } /******************************** * Add/edit/delete contribution ********************************/ on_show_add_contribution_modal=function(e) { var cs=$($('#add_contribution_modal #add_contribution_contributor_name')[0]); cs.html(''); $('#view-group #contributor option').each(function(idx,option) { cs.append($(option).clone()); }); cs[0].value = $('#view-group #contributor')[0].value; $('#add_contribution_modal #add_contribution_title').focus(); } on_click_add_contribution_btn=function() { $('#add_contribution_modal #group_name')[0].value=$($('#view-group h1')[0]).html(); $('#add_contribution_modal #edit_id')[0].value='-1'; $('#add_contribution_modal').modal('show'); } on_valid_add_contribution_modal=function(e) { e.preventDefault(); var group=groups[$('#add_contribution_modal #group_name')[0].value]; var contributor_name=$('#add_contribution_contributor_name')[0].value; var contributor=group.contributorByName(contributor_name); if (contributor==undefined) { alert('Participant inconu !'); return; } var title=$('#add_contribution_title')[0].value; if (title=='') { $('#add_contribution_title')[0].focus(); alert('Vous devez saisir un titre !'); return; } var cost=$('#add_contribution_cost')[0].value; if (cost=='') { $('#add_contribution_cost')[0].focus(); alert('Vous devez saisir un coût !'); return; } else if(!RegExp('^[0-9]*(,[0-9]{1,2})*$').test(cost)) { $('#add_contribution_cost')[0].focus(); alert('Coût incorrect !'); return; } cost=parseFloat(cost.replace(',','.')); var date=$('#add_contribution_date')[0].value; if (date!='') { if(!RegExp('^[0-3][0-9]\/[0-1][0-9]\/[0-2][0-9][0-9][0-9]$').test(date)) { $('#add_contribution_date')[0].focus(); alert('Date incorrect !'); return; } date=moment(date,'DD-MM-YYYY').toDate(); if (jQuery.type(date)!='date') { alert('Date non-reconnu !'); return; } } else { date=new Date(); } if($('#add_contribution_modal #edit_id')[0].value=='-1') { group.addContribution(new Contribution(contributor,cost,title,date)); } else { contribution_id=$('#add_contribution_modal #edit_id')[0].value; group.replaceContribution(contribution_id,new Contribution(contributor,cost,title,date)); } show_contributions(group,contributor_name); $('#add_contribution_modal').modal('hide'); $('#add_contribution_modal form')[0].reset(); groups.save(); } on_close_add_contribution_modal=function () { $('#add_contribution_modal form')[0].reset(); } on_contribution_delete_btn_click=function(e) { var group=groups[$($('#view-group h1')[0]).html()]; contribution_id=$($(e.target).parents('div')[0]).data('id'); contribution=group.contributions[contribution_id]; myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_id':contribution_id, 'contribution': contribution} ); } on_confirm_contribution_delete=function(data) { delete data.group.contributions[data.contribution_id]; groups.save(); on_contributor_change(); } on_contribution_edit_btn_click=function(e) { var group=groups[$($('#view-group h1')[0]).html()]; $('#add_contribution_modal #group_name')[0].value=$($('#view-group h1')[0]).html(); contribution_id=$($(e.target).parents('div')[0]).data('id'); contribution=group.contributions[contribution_id]; $('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name; console.log($('#add_contribution_modal #add_contribution_contributor_name')[0]); console.log(contribution.contributor.name); $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle(); $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost; $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY'); $('#add_contribution_modal #edit_id')[0].value=contribution_id; $('#add_contribution_modal').modal('show'); } /******************** * Clear local data ********************/ clear_local_data=function() { myconfirm('Etes-vous sûre de vouloir supprimer les données locales ?',on_confirm_clear_local_data); } on_confirm_clear_local_data=function(data) { localStorage.clear(); location.reload(); } /********************* * View balance *********************/ on_display_balance_btn_click=function(e) { group=groups[$($('#view-group h1')[0]).html()]; display_balance(group); } display_balance=function(group) { bal=group.balance(); tbody=$($('#display_balance_modal tbody')[0]); tbody.html(''); sum=0; for (c in bal) { tbody.append('<tr><td>'+c+'</td><td>'+bal[c]+' €</td></tr>'); sum+=bal[c]; } $('#display_balance_modal #total-value').html(sum+' €'); $('#display_balance_modal').modal('show'); } /********************* * Remove group *********************/ on_remove_group_btn_click=function(e) { group=groups[$($('#view-group h1')[0]).html()]; myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group); } on_confirm_remove_group=function(group) { if(groups.removeGroup(group.name)) { groups.save(); location.reload(); } else { alert('Erreur en supprimant le groupe '+group.name+' :('); } } /********************* * Activate *********************/ $( document ).ready( function() { if(typeof(Storage)!=="undefined"){ groups=new GroupList(); groups.loadFromLocalStorage(); refresh_group_list(); } else { alert('Local storage not supported !'); } $('#add_group_submit').bind('click',on_valid_add_group_modal); $("#add_group_modal").on('shown.bs.modal',on_show_add_group_modal); $("#add_group_modal").on('hidden.bs.modal',on_close_add_group_modal); $("#add_group_modal form").on('submit',on_valid_add_group_modal); $('#clear_local_data').bind('click',clear_local_data); $('#view-group #contributor').bind('change',on_contributor_change); $('#add_contributor_btn').bind('click',on_click_add_contributor_btn); $('#add_contributor_submit').bind('click',on_valid_add_contributor_modal); $("#add_contributor_modal").on('shown.bs.modal',on_show_add_contributor_modal); $("#add_contributor_modal").on('hidden.bs.modal',on_close_add_contributor_modal); $("#add_contributor_modal form").on('submit',on_valid_add_contributor_modal); $('#del_contributor_btn').bind('click',on_click_del_contributor_btn); $('#add_contribution_btn').bind('click',on_click_add_contribution_btn); $('#add_contribution_submit').bind('click',on_valid_add_contribution_modal); $("#add_contribution_modal").on('shown.bs.modal',on_show_add_contribution_modal); $("#add_contribution_modal").on('hidden.bs.modal',on_close_add_contribution_modal); $("#add_contribution_modal form").on('submit',on_valid_add_contribution_modal); $("#display_balance_btn").bind('click',on_display_balance_btn_click); $('#remove_group_btn').bind('click',on_remove_group_btn_click); } );