module CMStyle module NavigationHelpers def build_opening_tag(name,pre_spaces=0,attributes=nil,newline="\n") buffer = " " * pre_spaces + "<" + name.to_s attributes.each_pair do |key,value| next if value == nil || value.empty? buffer += " #{key.to_s}=\"#{value.to_s}\"" end buffer += ">" buffer += newline.to_s end def build_closing_tag(name,pre_spaces,newline="\n") buffer = " " * pre_spaces + "" + name.to_s buffer += ">" buffer += newline.to_s end def build_list_item(item,pre_space,active_id) buffer = "" # prepare item classes item_class = [] item_class << 'active' if item[:id] == active_id if item[:class] item[:class].split(" ").each do |c| item_class << c end end # build attribute list for li node attributes = { :class => item_class.join(" "), :title => item[:tooltip]} # build li open tag buffer << build_opening_tag(:li,pre_space,attributes) # dont place an achnor tag when vertical divider unless item_class.include?('divider-vertical') || item_class.include?('divider') if item[:'data-toggle'] == 'modal' attributes = {:href => item[:href], :'data-toggle' => item[:'data-toggle'], :'data-target' => item[:'data-target']} else attributes = {:href => item[:href]} end buffer << build_opening_tag(:a,pre_space+2,attributes,nil) if item[:icon] buffer << build_opening_tag(:i,0,{:class => item[:icon]},nil) buffer << build_closing_tag(:i,0," ") end buffer << item[:title].to_s buffer << build_closing_tag(:a,0) end buffer << build_closing_tag(:li,pre_space) end # build navigation bar # # mandatory arguments # * definiton_array => array of hash entries of the form {:id => :first, :titel => 'nav1', :href => '#', :tooltip => 'This is nav1 tooltip', :class => nil , :icon => 'icon-user', :sub => {} } # optional arguments # * active_id => id of active navigation entry which should be highlighted def build_navbar(defintion_array, active_id = :none, custom_class = 'nav') return nil unless defintion_array.class == Array buffer = "
\n" render :inline => buffer end # build navigation (tabs, pills) # # mandatory arguments # * definiton_array => array of hash entries of the form {:id => :first, :titel => 'nav1', :href => '#', :tooltip => 'This is nav1 tooltip', :class => nil , :icon => 'icon-user', :sub => [{},{}] } # optional arguments # * active_id => id of active navigation entry which should be highlighted # * class => "nav nav-tabs" for tabbed navigation or "nav nav-pills" for pilled navigation def build_nav(definition_array,active_id = :none, custom_class = "nav nav-tabs") return nil unless definition_array.class == Array buffer = build_opening_tag(:ul,0,{:class => custom_class}) definition_array.each do |item,index| if item[:sub] == nil || item[:sub].class != Array buffer << build_list_item(item,2,active_id) else # we build a submenu buffer << build_opening_tag(:li,10,{:class => 'dropdown'}) buffer << build_opening_tag(:a,12,{:class => 'dropdown-toggle', 'data-toggle' => 'dropdown', :href => '#'},nil) buffer << item[:title].to_s buffer << build_opening_tag(:b,1,{:class => 'caret'},nil) buffer << build_closing_tag(:b,0) buffer << build_closing_tag(:a,12) buffer << build_opening_tag(:ul,12,{:class => 'dropdown-menu'}) # loop over submenu entries item[:sub].each do |item| buffer << build_list_item(item,14,active_id) end buffer << build_closing_tag(:ul,12) buffer << build_closing_tag(:li,10) end end buffer << build_closing_tag(:ul,0) render :inline => buffer end end # module end # module