Sha256: 829500ce38cdb28a4602325a83fe6e92371501f077638681c1a51635f41a5b16

Contents?: true

Size: 1.76 KB

Versions: 9

Compression:

Stored size: 1.76 KB

Contents

module Symphonia
  module BootstrapModalHelper

    # Ruby shortcut for generate modals
    #
    # <%= render_modal title: t(:title) do %>
    # <% render(template: 'path_to_template_or_partial', formats: [:html]) %>
    # <% end %>

    # template_or_partial example:

    # <%= 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 -%>


    #
    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

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
symphonia-3.1.2 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-3.1.1 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-3.1.0 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-3.0.3 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-3.0.2 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-2.2.1 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-3.0.1 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-3.0.0 app/helpers/symphonia/bootstrap_modal_helper.rb
symphonia-2.1.8 app/helpers/symphonia/bootstrap_modal_helper.rb