Use delegated events instead of local handlers

This commit is contained in:
Paul Chavard 2018-08-09 11:19:56 +02:00
parent 8bf5dedd80
commit ea68c75361
4 changed files with 48 additions and 59 deletions

View file

@ -1,52 +1,47 @@
$(document).on('turbolinks:load', init_admin); $(document).on('click', '.delete', function() {
$(this).hide();
$(this)
.closest('td')
.find('.confirm')
.show();
});
function init_admin(){ $(document).on('click', '.cancel', function() {
destroy_action(); $(this)
on_change_type_de_champ_select(); .closest('td')
} .find('.delete')
.show();
$(this)
.closest('td')
.find('.confirm')
.hide();
});
function destroy_action(){ $(document).on('change', 'select.form-control.type-champ', function() {
$(".delete").on('click', function(){ var parent = $(this)
$(this).hide(); .parent()
$(this).closest('td').find(".confirm").show(); .parent();
});
$(".cancel").on('click', function(){ parent.removeClass('header-section');
$(this).closest('td').find(".delete").show(); parent.children('.drop-down-list').removeClass('show-inline');
$(this).closest('td').find(".confirm").hide(); parent.children('.pj-template').removeClass('show-inline');
});
$("#liste-gestionnaire #libelle").on('click', function(){ $('.mandatory', parent).show();
setTimeout(destroy_action, 500);
});
}
function on_change_type_de_champ_select (){ switch (this.value) {
$("select.form-control.type-champ").on('change', function(e){ case 'header_section':
parent.addClass('header-section');
parent = $(this).parent().parent(); break;
case 'drop_down_list':
parent.removeClass('header-section'); case 'multiple_drop_down_list':
parent.children(".drop-down-list").removeClass('show-inline'); case 'linked_drop_down_list':
parent.children(".pj-template").removeClass('show-inline'); parent.children('.drop-down-list').addClass('show-inline');
break;
$('.mandatory', parent).show(); case 'piece_justificative':
parent.children('.pj-template').addClass('show-inline');
switch(this.value){ break;
case 'header_section': case 'explication':
parent.addClass('header-section'); $('.mandatory', parent).hide();
break; break;
case 'drop_down_list': }
case 'multiple_drop_down_list': });
case 'linked_drop_down_list':
parent.children(".drop-down-list").addClass('show-inline');
break;
case 'piece_justificative':
parent.children(".pj-template").addClass('show-inline');
break;
case 'explication':
$('.mandatory', parent).hide();
break;
}
});
}

View file

@ -1,10 +1,6 @@
$(document).on('turbolinks:load', link_init); $(document).on('click', '#dossiers-list tr', function(event) {
var href = $(this).data('href');
function link_init() { if (href && event.target.tagName !== 'A') {
$('#dossiers-list tr').on('click', function(event) { location.href = href;
var href = $(this).data('href'); }
if (href && event.target.tagName !== 'A') { });
location.href = href;
}
});
}

View file

@ -1,2 +1 @@
<%= smart_listing_update :procedures %> <%= smart_listing_update :procedures %>
link_init();

View file

@ -1,3 +1,2 @@
<%= render_flash(timeout: 3000, sticky: true) %> <%= render_flash(timeout: 3000, sticky: true) %>
<%= render_to_element('#liste-champ', partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, types_de_champ: @types_de_champ }) %> <%= render_to_element('#liste-champ', partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, types_de_champ: @types_de_champ }) %>
on_change_type_de_champ_select();