Module: Symphonia::BootstrapModalHelper

Defined in:
app/helpers/symphonia/bootstrap_modal_helper.rb

Instance Method Summary collapse

Instance Method Details

render link and JS script tag with function for create SymphoniaDialog

Examples:

<%= link_to_modal icon("remove", t("button_reject")), "reject_dialog", modal: { title: t(:button_reject), submit: t(:button_reject) } %>
<div id="reject_dialog" style="display:none">
  <%= symphonia_form_tag url: reject_organization_path(@organization), method: "patch" do |f| %>
    <%= f.text_area :message, label: Organization.human_attribute_name(:reject_message) %>
  <% end %>
</div>

Parameters:

  • name (String)

    can be label or HTML text

  • element_id (String)

    same ID must have container with modal content

  • options (Hash)

    standard ‘link_to` options and

Options Hash (**options):

  • :modal (Hash)

    contains options for SymphoniaDialog



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/symphonia/bootstrap_modal_helper.rb', line 70

def link_to_modal(name, element_id, **options)
  modal_options = options.delete(:modal)
  function = "symphoniaModal#{name.hash.to_s.remove(/[^\d]/)}"
  link_to(name, "javascript:#{function}()", options) + \
    javascript_tag do
    raw <<~EOF
      function #{function}() {
        if (!window.symphoniaModals)
          window.symphoniaModals = {}
        if (!window.symphoniaModals['#{element_id}']) {
        window.symphoniaModals['#{element_id}'] = new SymphoniaDialog('#{element_id}', #{raw modal_options.to_json});
      }
      window.symphoniaModals['#{element_id}'].show()
      }
    EOF
  end
end

#render_modal(options = {}, &block) ⇒ Object

<%= title(@entity, back: !request.xhr?) %>

<%= symphonia_form_for(@entity, remote: request.xhr?) do |f| %>

<%= render(partial: 'form', locals: { f: f }) %>

<%= f.primary unless request.xhr? %>

<% end -%>



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/symphonia/bootstrap_modal_helper.rb', line 21

def render_modal(options = {}, &block)
  opts = options.slice(:id, :title, :submit, :large)
  opts[:submit] = t(:button_submit) unless opts.has_key?(:submit)

  id = options.delete(:id) || 'ad_hoc_modal'


  script = <<SCRIPT
    if (typeof(renderModal) == "undefined")
      var renderModal = {}
    if (renderModal["#{id}"])
      renderModal["#{id}"].destroy();
 renderModal["#{id}"] = new SymphoniaDialog("#{id}", #{raw opts.to_json});
SCRIPT
  script << %Q{        renderModal["#{id}"].body.innerHTML = "#{j(capture(&block))}"; } if block_given?
  script << <<SCRIPT
    var submitButton = renderModal["#{id}"].body.querySelector("input[type=submit]")
    if (submitButton)
      submitButton.remove()
SCRIPT
  unless opts[:title]
    script.concat <<EOF
    var title = document.getElementById(renderModal["#{id}"].id).querySelector("#page_header");
    if (title) {
      title.className = 'modal-title'; title.id = null;
      $(renderModal["#{id}"].title).replaceWith($(title));
      renderModal["#{id}"].title = title;
    }
EOF
  end

  script << %Q{renderModal["#{id}"].show();} unless opts.has_key?(:render_only)
  script.html_safe
end