From 3ef052076d372ae8f1ac87f60f92fb7c719b04aa Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 10 Sep 2017 19:05:18 +0200 Subject: [PATCH] Improve jQuery usage --- inc/myco.js | 118 ++++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/inc/myco.js b/inc/myco.js index bda33a2..5a43042 100644 --- a/inc/myco.js +++ b/inc/myco.js @@ -1,15 +1,15 @@ refresh_group_list=function() { - grouplist=$('#group-choice')[0]; + grouplist=$('#group-choice'); $('a.group-choice').each(function(idx,a) { $(a).parent().remove(); }); if (groups.count()==0) { - $(grouplist).prepend('
  • Aucune groupe
  • '); + grouplist.prepend('
  • Aucune groupe
  • '); } else { groups.each(function(idx,group) { - $(grouplist).prepend('
  • '+group.name+'
  • '); + grouplist.prepend('
  • '+group.name+'
  • '); }); $('a.group-choice').each(function(idx,a) { @@ -34,7 +34,7 @@ on_show_add_group_modal=function(e) { on_valid_add_group_modal=function (e) { e.preventDefault(); - var name=$('#add_group_name')[0].value; + var name=$('#add_group_name').val(); if (name=='') { alert('Vous devez saisir un nom !'); return; @@ -172,14 +172,14 @@ on_contributor_change=function(e) { $('#group-navbar-collapse-1').collapse('hide'); } $('#view-group #group-username').html($('#view-group #contributor option:selected').text()); - show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value); - $('#nav-search-form input')[0].value=''; + show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor').val()); + $('#nav-search-form input').val(''); } _current_displayed_contributions=[]; _current_displayed_contributions_group=null; show_contributions=function(group,contributor_email,contributions) { - var tbody=$($('#view-group #contributions tbody')[0]); + var tbody=$('#view-group #contributions tbody').first(); tbody.html(''); if (!contributions) { contributions=group.contributionsByContributorEmail(contributor_email); @@ -192,7 +192,7 @@ show_contributions=function(group,contributor_email,contributions) { show_more_contributions=function(count) { if (!_current_displayed_contributions_group) return; - var tbody=$($('#view-group #contributions tbody')[0]); + var tbody=$('#view-group #contributions tbody').first(); if (_current_displayed_contributions.length==0) { tbody.html(''); tbody.append('Aucune contributions'); @@ -311,7 +311,7 @@ on_categories_go_back_group_btn_click=function(e) { on_categories_group_add_btn_click=function(e) { e.preventDefault(); - name=$('#add_category input')[0].value; + name=$('#add_category input').val(); if (jQuery.type(name)!='string' || name=='') { return; @@ -324,7 +324,7 @@ on_categories_group_add_btn_click=function(e) { } else { group.addCategory(new Category(name)); - $('#add_category input')[0].value=''; + $('#add_category input').val(''); refresh_group_categories(group); $('#add_category input').focus(); } @@ -349,7 +349,7 @@ on_nav_search_form_input=function(e) { var pattern=ipt.val(); group=groups[$('#view-group').data('uuid')]; - contributor_email=$('#view-group #contributor')[0].value; + contributor_email=$('#view-group #contributor').val(); var found=group.searchContributions(pattern,contributor_email); show_contributions(group,null,found); @@ -362,7 +362,7 @@ on_nav_search_form_input=function(e) { on_trash_group_btn_click=function(e) { group=groups[$('#view-group').data('uuid')]; - contributor_email=$('#view-group #contributor')[0].value; + contributor_email=$('#view-group #contributor').val(); bottom_navbar_collapse_hide(); view_group_trash(group,contributor_email); } @@ -396,7 +396,7 @@ view_group_trash=function(group,contributor_email) { on_contribution_restore_btn_click=function(e) { var group=groups[$('#view-group-trash').data('uuid')]; - contribution_uuid=$($(e.target).parents('tr')[0]).data('uuid'); + contribution_uuid=$(e.target).parents('tr').first().data('uuid'); contribution=group.importContribution(group.deletedContributions[contribution_uuid]); 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} ); } @@ -438,7 +438,7 @@ view_group_trash_contributors=function(group) { on_contributor_restore_btn_click=function(e) { var group=groups[$('#view-group-trash-contributors').data('uuid')]; - contributor_email=$($(e.target).parents('tr')[0]).data('email'); + contributor_email=$(e.target).parents('tr').first().data('email'); contributor=group.importContributor(group.deletedContributors[contributor_email]); myconfirm('Etes-vous sûre de vouloir restaurer '+contributor.name+' ?',on_confirm_contributor_restore,null,{'group':group,'contributor_email':contributor_email, 'contributor': contributor} ); } @@ -454,8 +454,8 @@ on_confirm_contributor_restore=function(data) { ******************************/ on_click_add_contributor_btn=function() { $('#add_contributor_modal').data('group-uuid',$('#view-group').data('uuid')) - $($('#add_contributor_modal h4')[0]).html('Ajouter un participant'); - $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter'); + $('#add_contributor_modal h4').first().html('Ajouter un participant'); + $('#add_contributor_modal #add_contributor_submit').first().html('Ajouter'); $('#add_contributor_modal').data('edit-id','-1'); $('#add_contributor_modal #add_contributor_email').attr('disabled',false); $('#add_contributor_modal').modal('show'); @@ -473,13 +473,13 @@ validateEmail=function(email) { on_valid_add_contributor_modal=function(e) { e.preventDefault(); var group=groups[$('#add_contributor_modal').data('group-uuid')]; - var name=$('#add_contributor_name')[0].value; + var name=$('#add_contributor_name').val(); var edit_id=$('#add_contributor_modal').data('edit-id'); if (name=='') { alert('Vous devez saisir un nom !'); return; } - var email=$('#add_contributor_email')[0].value; + var email=$('#add_contributor_email').val(); if (email=='') { alert('Vous devez saisir un email !'); return; @@ -506,27 +506,27 @@ on_valid_add_contributor_modal=function(e) { } on_close_add_contributor_modal=function () { - $('#add_contributor_name')[0].value=''; - $('#add_contributor_email')[0].value=''; + $('#add_contributor_name').val(''); + $('#add_contributor_email').val(''); } on_edit_contributor_btn_click=function(e) { var group=groups[$('#view-group').data('uuid')]; $('#add_contributor_modal').data('group-uuid',group.uuid); - contributor_email=$('#view-group #contributor')[0].value; + contributor_email=$('#view-group #contributor').val(); contributor=group.contributorByEmail(contributor_email); $('#add_contributor_modal').data('edit-id',contributor_email); - $('#add_contributor_modal #add_contributor_name')[0].value=contributor.name; - $('#add_contributor_modal #add_contributor_email')[0].value=contributor.email; + $('#add_contributor_modal #add_contributor_name').val(contributor.name); + $('#add_contributor_modal #add_contributor_email').val(contributor.email); $('#add_contributor_modal #add_contributor_email').attr('disabled',true); - $($('#add_contributor_modal h4')[0]).html('Editer un participant'); - $($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider'); + $('#add_contributor_modal h4').html('Editer un participant'); + $('#add_contributor_modal #add_contributor_submit').html('Valider'); $('#add_contributor_modal').modal('show'); } on_click_del_contributor_btn=function() { var group=groups[$('#view-group').data('uuid')]; - contributor=group.contributorByEmail($('#view-group #contributor')[0].value); + contributor=group.contributorByEmail($('#view-group #contributor').val()); question='Voulez-vous vraiment supprimer '+contributor.name+' ?'; myconfirm(question,on_confirm_del_contributor,null,contributor); } @@ -544,7 +544,7 @@ on_confirm_del_contributor=function(contributor) { last_selected_date=moment().format('DD/MM/YYYY'); on_show_add_contribution_modal=function(e) { $('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html()); - $('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value; + $('#add_contribution_modal #add_contribution_contributor_email').val($('#view-group #contributor').val()); gid=$('#add_contribution_modal').data('group-uuid'); group=groups[gid]; populate_add_contribution_categories(group); @@ -566,7 +566,7 @@ populate_add_contribution_categories=function(group,select) { gid=$('#add_contribution_modal').data('group-uuid'); group=groups[gid]; } - current_cat=$('#add_contribution_modal #add_contribution_category')[0].value; + current_cat=$('#add_contribution_modal #add_contribution_category').val(); cats=""; for (u in group.getSortedCategories()) { cats+=""; @@ -588,11 +588,11 @@ on_select_contribution_suggestion=function(event,choice,name) { on_click_add_contribution_btn=function() { $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid')); - $('#add_contribution_modal #edit_uuid')[0].value='-1'; + $('#add_contribution_modal #edit_uuid').val('-1'); $('#add_contribution_date').val(last_selected_date); $('#add_contribution_date').data('value',last_selected_date); - $($('#add_contribution_modal h4')[0]).html('Ajouter une contribution'); - $($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter'); + $('#add_contribution_modal h4').html('Ajouter une contribution'); + $('#add_contribution_modal #add_contribution_submit').html('Ajouter'); $('#add_contribution_modal').modal('show'); $('#add_contribution_title').val('').focus(); } @@ -600,19 +600,19 @@ on_click_add_contribution_btn=function() { on_valid_add_contribution_modal=function(e) { e.preventDefault(); var group=groups[$('#add_contribution_modal').data('group-uuid')]; - var contributor_email=$('#add_contribution_contributor_email')[0].value; + var contributor_email=$('#add_contribution_contributor_email').val(); var contributor=group.contributorByEmail(contributor_email); if (contributor==undefined) { alert('Participant inconu !'); return; } - var title=$('#add_contribution_title')[0].value; + var title=$('#add_contribution_title').val(); if (title=='') { $('#add_contribution_title').focus(); alert('Vous devez saisir un titre !'); return; } - var cost=$('#add_contribution_cost')[0].value; + var cost=$('#add_contribution_cost').val(); if (cost=='') { $('#add_contribution_cost').focus(); alert('Vous devez saisir un coût !'); @@ -625,7 +625,7 @@ on_valid_add_contribution_modal=function(e) { } cost=parseFloat(cost.replace(',','.')); - var date=$('#add_contribution_date')[0].value; + var date=$('#add_contribution_date').val(); date.replace(/ /g,''); if (date!='') { if(!RegExp('^[0-3][0-9]\/[0-1][0-9]\/[0-2][0-9][0-9][0-9]$').test(date)) { @@ -644,20 +644,20 @@ on_valid_add_contribution_modal=function(e) { } last_selected_date=moment(date).format('DD/MM/YYYY'); - var cat=$('#add_contribution_category')[0].value; + var cat=$('#add_contribution_category').val(); if (cat != '' && !jQuery.type(group.categories[cat])) { alert('Category incorrect'); return; } - if($('#add_contribution_modal #edit_uuid')[0].value=='-1') { + if($('#add_contribution_modal #edit_uuid').val()=='-1') { group.addContribution(new Contribution(contributor,cost,title,date,cat)); } else { - contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value; + contribution_uuid=$('#add_contribution_modal #edit_uuid').val(); group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date,cat)); } - show_contributions(group,$('#view-group #contributor')[0].value); + show_contributions(group,$('#view-group #contributor').val()); $('#add_contribution_modal').modal('hide'); $('#add_contribution_modal form')[0].reset(); groups.save(); @@ -669,7 +669,7 @@ on_close_add_contribution_modal=function () { on_contribution_delete_btn_click=function(e) { var group=groups[$('#view-group').data('uuid')]; - contribution_uuid=$($(e.target).parents('div')[0]).data('uuid'); + contribution_uuid=$(e.target).parents('div').first().data('uuid'); contribution=group.contributions[contribution_uuid]; myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} ); } @@ -683,19 +683,19 @@ on_confirm_contribution_delete=function(data) { on_contribution_edit_btn_click=function(e) { var group=groups[$('#view-group').data('uuid')]; $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid')); - $($('#add_contribution_modal h4')[0]).html('Editer une contribution'); - $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider'); - contribution_uuid=$($(e.target).parents('div')[0]).data('uuid'); + $('#add_contribution_modal h4').first().html('Editer une contribution'); + $('#add_contribution_modal #add_contribution_submit').html('Valider'); + contribution_uuid=$(e.target).parents('div').first().data('uuid'); contribution=group.contributions[contribution_uuid]; - $('#add_contribution_modal #add_contribution_contributor_email')[0].value=contribution.contributor.email; - $('#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 #add_contribution_contributor_email').val(contribution.contributor.email); + $('#add_contribution_modal #add_contribution_title').val(contribution.getTitle()); + $('#add_contribution_modal #add_contribution_cost').val(contribution.cost); + $('#add_contribution_modal #add_contribution_date').val(moment(contribution.date).format('DD/MM/YYYY')); $('#add_contribution_modal #add_contribution_date').data('value',moment(contribution.date).format('DD/MM/YYYY')); if (contribution.category) { populate_add_contribution_categories(group,contribution.category); } - $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid; + $('#add_contribution_modal #edit_uuid').val(contribution_uuid); $('#add_contribution_modal').modal('show'); } @@ -725,7 +725,7 @@ on_display_balance_btn_click=function(e) { display_balance=function(group) { bal=group.balance(); - tbody=$($('#display_balance_modal tbody')[0]); + tbody=$('#display_balance_modal tbody').first(); tbody.html(''); for (c in bal['balance']) { if(bal['balance'][c]['diff']<0) { @@ -770,9 +770,9 @@ on_close_login_modal=function(e) { sync_server=false; on_valid_login_modal=function(e) { e.preventDefault(); - email=$('#login_modal #login_email')[0].value; - pass=$('#login_modal #login_pass')[0].value; - server=$('#login_modal #login_server')[0].value; + email=$('#login_modal #login_email').val(); + pass=$('#login_modal #login_pass').val(); + server=$('#login_modal #login_server').val(); $('#login_modal').modal('hide'); pleaseWaitShow(); sync_server.login(server,email,pass, @@ -815,10 +815,10 @@ on_close_subscribe_modal=function(e) { sync_server=false; on_valid_subscribe_modal=function(e) { e.preventDefault(); - email=$('#subscribe_modal #subscribe_email')[0].value; - name=$('#subscribe_modal #subscribe_name')[0].value; - pass=$('#subscribe_modal #subscribe_pass')[0].value; - server=$('#subscribe_modal #subscribe_server')[0].value; + email=$('#subscribe_modal #subscribe_email').val(); + name=$('#subscribe_modal #subscribe_name').val(); + pass=$('#subscribe_modal #subscribe_pass').val(); + server=$('#subscribe_modal #subscribe_server').val(); $('#subscribe_modal').modal('hide'); pleaseWaitShow(); sync_server.subscribe(server,email,name,pass, @@ -902,7 +902,7 @@ on_export_all_btn_click=function(e) { a.attr('href',window.URL.createObjectURL(blob)); a.css('display','none'); $('body').append(a); - a[0].click(); + a.click(); a.remove(); } @@ -915,7 +915,7 @@ on_import_all_btn_click=function(e) { var input=$(''); input.css('display','none'); $('body').append(input); - input[0].click(); + input.click(); input.bind('change',{'input': input},function(e) { pleaseWaitShow(); var file=input.prop('files')[0]; @@ -983,7 +983,7 @@ logged_out_menu=function() { on_click_myaccount_btn=function() { $('#myaccount_modal #myaccount_email').html(user.email); - $('#myaccount_modal #myaccount_name')[0].value=user.name; + $('#myaccount_modal #myaccount_name').val(user.name); $('#myaccount_modal #myaccount_server').html(user.server); if (user.lastsync) { $('#myaccount_modal #myaccount_lastsync').html(moment(user.lastsync).format('DD/MM/YYYY HH:mm:ss'));