Ruby helpers for js.erb responses

This commit is contained in:
Paul Chavard 2018-08-09 11:15:23 +02:00
parent 9fed32456c
commit 8bf5dedd80
9 changed files with 53 additions and 36 deletions

View file

@ -178,7 +178,8 @@ div.pagination {
}
.alert.alert-success.move-up,
.alert.alert-danger.siret {
.alert.alert-danger.siret,
.alert.sticky {
position: fixed;
top: 0px;
left: 0;

View file

@ -7,13 +7,43 @@ module ApplicationHelper
end
end
def flash_class(level)
def flash_class(level, sticky = false)
case level
when "notice" then "alert-success"
when "alert" then "alert-danger"
when "notice" then "alert-success#{sticky ? ' sticky' : ''}"
when "alert" then "alert-danger#{sticky ? ' sticky' : ''}"
end
end
def render_to_element(selector, partial:, outer: false, locals: {})
method = outer ? 'outerHTML' : 'innerHTML'
html = escape_javascript(render partial: partial, locals: locals)
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{selector}').#{method} = \"#{html}\";")
# rubocop:enable Rails/OutputSafety
end
def render_flash(timeout: false, sticky: false)
if flash.any?
html = render_to_element('#flash_messages', partial: 'layouts/flash_messages', locals: { sticky: sticky }, outer: true)
flash.clear
if timeout
html += remove_element('#flash_messages', timeout: timeout, inner: true)
end
html
end
end
def remove_element(selector, timeout: 0, inner: false)
script = "(function() {";
script << "var el = document.querySelector('#{selector}');"
method = (inner ? "el.innerHTML = ''" : "el.parentNode.removeChild(el)")
script << "setTimeout(function() { #{method}; }, #{timeout});";
script << "})();"
# rubocop:disable Rails/OutputSafety
raw(script);
# rubocop:enable Rails/OutputSafety
end
def current_email
current_user&.email ||
current_gestionnaire&.email ||

View file

@ -1,4 +1,2 @@
<% flash.each do |type, message| %>
$("#flash_message").html("<div class=\"alert alert-success move-up\" style=\"display: block:\"> <%= sanitize(message) %></div>").children().fadeOut(5000)
<% end %>
$('#piece_justificative_form').html("<%= escape_javascript(render partial: 'form', locals: { procedure: @procedure } ) %>");
<%= render_flash(timeout: 3000, sticky: true) %>
<%= render_to_element('#piece_justificative_form', partial: 'admin/pieces_justificatives/form', locals: { procedure: @procedure }) %>

View file

@ -1,9 +1,7 @@
<%- if response.status == 404 %>
transfer_errors_message(true);
<%- else %>
$("#main-container").prepend("<div class='row'><div id='flash_message'></div></div>");
$("#flash_message").prepend("<div class=\"alert alert-success\"> <%= sanitize(flash.notice) %></div>");
<% flash.clear %>
<%= render_flash %>
transfer_errors_message(false);
$("#email_admin").val('');

View file

@ -1,5 +1,3 @@
<% flash.each do |type, message| %>
$("#flash_message").html("<div class=\"alert alert-success move-up\" style=\"display: block:\"> <%= sanitize(message) %></div>").children().fadeOut(5000)
<% end %>
$('#liste-champ').html("<%= escape_javascript(render partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, types_de_champ: @types_de_champ } ) %>");
on_change_type_de_champ_select ();
<%= 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 }) %>
on_change_type_de_champ_select();

View file

@ -1,8 +1,5 @@
(function() {
<% if @blank || @error %>
var html = "<%= escape_javascript(render partial: 'shared/champs/siret/delete_etablissement', locals: { message: @error, position: @champ.order_place, etablissement: @etablissement }) %>";
<% else %>
var html = "<%= escape_javascript(render partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>";
<% end %>
document.querySelector("#etablissement-for-<%= @champ.id %>").innerHTML = html;
})();
<% if @blank || @error %>
<%= render_to_element("#etablissement-for-#{@champ.id}", partial: 'shared/champs/siret/delete_etablissement', locals: { message: @error, position: @champ.order_place, etablissement: @etablissement }) %>
<% else %>
<%= render_to_element("#etablissement-for-#{@champ.id}", partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>
<% end %>

View file

@ -1,6 +1,2 @@
var formView = "<%= escape_javascript(render partial: 'invites/form', locals: { dossier: @dossier }) %>";
document.querySelector("#invites-form").outerHTML = formView;
var flashMessagesView = "<%= escape_javascript(render partial: 'layouts/flash_messages') %>";
document.querySelector("#flash_messages").outerHTML = flashMessagesView;
<% flash.clear %>
<%= render_to_element('#invites-form', partial: 'invites/form', locals: { dossier: @dossier }, outer: true) %>
<%= render_flash %>

View file

@ -2,11 +2,12 @@
- if flash.any?
#flash_message.center
- flash.each do |key, value|
- sticky = defined?(sticky) ? sticky : false
- if value.class == Array
.alert{ class: flash_class(key) }
.alert{ class: flash_class(key, sticky) }
- value.each do |message|
= sanitize(message)
%br
- else
.alert{ class: flash_class(key) }
.alert{ class: flash_class(key, sticky) }
= sanitize(value)

View file

@ -1,4 +1,2 @@
document.querySelector('#user-satisfaction').innerHTML = '';
var flashMessagesView = "<%= escape_javascript(render partial: 'layouts/flash_messages') %>";
document.querySelector("#flash_messages").outerHTML = flashMessagesView;
<% flash.clear %>
<%= remove_element('#user-satisfaction') %>
<%= render_flash %>