Files
fetsite/app/helpers/html_helper.rb

68 lines
2.6 KiB
Ruby

require 'nokogiri'
module HtmlHelper
def wrapper_html(tag,&block)
content_tag(tag,nil, class:"test_wrapper", &block)
end
# make a ul with <li><a> structure a bootstrap nav list
def bs_make_ul_nav(options={},&block)
r=capture(&block)
n=Nokogiri.XML(r)
u= n.xpath("/ul").first
u['class']=u['class'].to_s + " nav"
n.xpath("/ul/li").each do |l|
l['class'] = l['class'].to_s + " nav-item hello"
l.xpath("a").each do |a|
a['class']=a['class'].to_s+" nav-link"
end
end
return raw(n.to_html)
end
# Generate Bootstrap Dropdown with toggler as the main button. and a block containing links
def bs_dropdown_menu(toggler="",options={}, &block)
id=options[:id] || SecureRandom.hex(12)
if options[:nav]
tag=:li
toggler=content_tag(:a, toggler, {:role => "button","id"=>id, "aria-haspopup"=>"true", "aria-expanded"=>"false", "data-toggle"=> "dropdown", "href"=>"#","class"=>"dropdown-toggle nav-link"})
c = "dropdown nav-item"
else
toggler=content_tag(:a, toggler, {:role => "button","id"=>id, "aria-haspopup"=>"true", "aria-expanded"=>"false", "data-toggle"=> "dropdown", "href"=>"#","class"=>"btn dropdown-toggle"})
c="dropdown"
tag=(options[:tag].nil? ? :div : options[:tag])
end
ctn= content_tag(:div, capture(&block), {class:"dropdown-menu", "aria-labelledby": id} )
ctn=inject_html_class(:to_links,ctn, "dropdown-item")
content_tag(tag, toggler+ ctn.html_safe, class: c)
end
def bs_modal(toggler="", options={}, &block)
id=options[:id] || SecureRandom.hex(12)
title=options[:title]|| "Hello World!"
ctn=capture(&block)
toggler=content_tag(:button, toggler,{type: "button", class: "btn", "data-toggle": "modal", "data-target":"#"+id})
close_btn=content_tag(:button, content_tag(:span,"&times;".html_safe, {"aria-hidden": "true"}), {type: "button", class: "close", "data-dismiss":"modal", "aria-label": "Close"})
ctn=content_tag(:div,content_tag(:div, content_tag(:h5,title,{class: "modal-title"})+close_btn,{class: "modal-header"})+content_tag(:div, ctn,{class: "modal-body"}), {class: "modal-content"})
toggler+content_tag(:div , content_tag(:div, ctn, {role: "document", class: "modal-dialog"}),{class: "modal fade", id: id, tabindex: "-1", role: "dialog", "aria-labelledby": id+"Label", "aria-hidden": "true"})
end
private
def inject_html_class(mode,html,c)
n=Nokogiri.XML(html)
if mode==:to_links
n.xpath("//a").each do |e|
e['class'] = e['class'].to_s+" " + c
end
return n.to_html
end
raise Error("wrong options for inject_html_class")
end
end