Merge pull request #2322 from tchak/fix-webpacker

Fix global jQuery usage
This commit is contained in:
Pierre de La Morinerie 2018-08-01 12:32:21 +02:00 committed by GitHub
commit 1d2fb73a4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 20 deletions

View file

@ -0,0 +1,19 @@
// Administrate injects its own copy of jQuery, and it is the one
// configured by rails to send csrf-token
const $ = window.$;
$(document).on('change', '#features input[type=checkbox]', ({ target }) => {
target = $(target);
const url = target.data('url');
const key = target.data('key');
const value = target.prop('checked');
$.ajax(url, {
method: 'put',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
features: { [key]: value }
})
});
});

View file

@ -0,0 +1 @@
import '../manager/fields/features';

View file

@ -4,5 +4,5 @@
<% else %>
var html = "<%= escape_javascript(render partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>";
<% end %>
$("#etablissement-for-<%= @champ.id %>").html(html);
document.querySelector("#etablissement-for-<%= @champ.id %>").innerHTML = html;
})();

View file

@ -5,22 +5,3 @@
%td= feature.title
%td
= check_box_tag "enable-feature", "enable", field.data[feature.name], data: { url: enable_feature_manager_administrateur_path(field.resource.id), key: feature.key }
:javascript
window.onload = function() {
$('#features input[type=checkbox]').on('change', function(evt) {
let url = $(evt.target).data('url');
let key = $(evt.target).data('key');
let features = {};
features[key] = $(evt.target).prop('checked');
$.ajax(url, {
method: 'put',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
features: features
})
});
});
};

View file

@ -0,0 +1,23 @@
<%#
# Javascript Partial
This partial imports the necessary javascript on each page.
By default, it includes the application JS,
but each page can define additional JS sources
by providing a `content_for(:javascript)` block.
%>
<% Administrate::Engine.javascripts.each do |js_path| %>
<%= javascript_include_tag js_path %>
<% end %>
<%= javascript_pack_tag 'manager' %>
<%= yield :javascript %>
<% if Rails.env.test? %>
<%= javascript_tag do %>
$.fx.off = true;
$.ajaxSetup({ async: false });
<% end %>
<% end %>