Fix global jQuery usage
This commit is contained in:
parent
8b23cbd272
commit
872e07b5e6
5 changed files with 44 additions and 20 deletions
19
app/javascript/manager/fields/features.js
Normal file
19
app/javascript/manager/fields/features.js
Normal 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 }
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
1
app/javascript/packs/manager.js
Normal file
1
app/javascript/packs/manager.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
import '../manager/fields/features';
|
|
@ -4,5 +4,5 @@
|
||||||
<% else %>
|
<% else %>
|
||||||
var html = "<%= escape_javascript(render partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>";
|
var html = "<%= escape_javascript(render partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>";
|
||||||
<% end %>
|
<% end %>
|
||||||
$("#etablissement-for-<%= @champ.id %>").html(html);
|
document.querySelector("#etablissement-for-<%= @champ.id %>").innerHTML = html;
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -5,22 +5,3 @@
|
||||||
%td= feature.title
|
%td= feature.title
|
||||||
%td
|
%td
|
||||||
= check_box_tag "enable-feature", "enable", field.data[feature.name], data: { url: enable_feature_manager_administrateur_path(field.resource.id), key: feature.key }
|
= 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
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
23
app/views/manager/application/_javascript.html.erb
Normal file
23
app/views/manager/application/_javascript.html.erb
Normal 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 %>
|
Loading…
Reference in a new issue