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 "      function \#{function}() {\n        if (!window.symphoniaModals)\n          window.symphoniaModals = {}\n        if (!window.symphoniaModals['\#{element_id}']) {\n        window.symphoniaModals['\#{element_id}'] = new SymphoniaDialog('\#{element_id}', \#{raw modal_options.to_json});\n      }\n      window.symphoniaModals['\#{element_id}'].show()\n      }\n    EOF\n  end\nend\n"

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

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