module BootstrapBaseHelper
extend ActiveSupport::Concern
include BootstrapHelper
class List
attr_accessor :collection, :options, :template, :block, :li_options
def initialize(templte, *args)
self.template = templte
self.options = args.extract_options!.dup
self.li_options = options.delete(:li_options) || {}
self.collection = args
end
def add(*args)
li_opts = args.extract_options!
collection << {options: li_opts, content: args.join}
end
alias_method :<<, :add
def to_s
type = options.delete(:type) || 'unordered'
tag = (type == 'ordered') ? 'ol' : 'ul'
unstyled_class = (type == 'unstyled') ? 'unstyled ' : ''
template.content_tag(tag, nil, template.merge_predef_class(unstyled_class, options)) do
ul_content = ''
collection.each do |obj|
if obj.is_a?(Hash)
if obj.has_key?(:options) && obj.has_key?(:content)
ul_content << template.content_tag('li', nil, obj[:options].reverse_merge!(li_options)) do
obj[:content].html_safe
end
else
nested_collection = obj.to_a.first
ul_content << template.content_tag('li', nil, li_options) do
(nested_collection.first + template.list(*nested_collection.last, options)).html_safe
end
end
elsif obj.is_a?(Array)
ul_content << template.list(*obj, options)
else
ul_content << template.content_tag('li', nil, li_options) do
obj.html_safe
end
end
end
ul_content.html_safe
end
end
end
# Typography
# Headings not implemented
# Lead not implemented
# Small not implemented
# Bold not implemented
# Italics not implemented
# muted, text-warning, text-error, text-info, text-success not implemented
# Abbreviations not implemented
# Addresses not implemented
# Blockquotes not implemented
# Lists
##############################################################################
# bootstrap Typography List
#
# collection - content to be displayed, can be a nested array like this:
# [
# 'list1',
# 'list2',
# {'list3' => [
# 'sublist1',
# 'sublist2',
# {'sublist3' => [
# 'sublist3.1',
# 'sublist3.2'
# ]}
# ]},
# 'list4'
# ]
#
#
# options - a hash
# type: unordered, ordered, unstyled. default: unordered
# the other html options can be accepted by ul or ol
# li_options: common li options in ul
def list(*args, &block)
builder = List.new(self, *args)
capture(builder, &block) if block_given?
builder.to_s
end
# Inline not implemented
# Description not implemented
# Code
# not implemented
# Tables
# see Bootstrap_table_helper.rb
# Forms
# see ext_form gem
# Buttons
# see bootstrap_button_helper.rb
# Images
# 圆角图片
# 和 image_tag 一样用
def r_image_tag(source, options={})
image_tag(source, merge_predef_class('img-rounded', options))
end
# 圆形图片
# 和 image_tag 一样用
def c_image_tag(source, options={})
image_tag(source, merge_predef_class('img-circle', options))
end
# 带边框图片
# 和 image_tag 一样用
def p_image_tag(source, options={})
image_tag(source, merge_predef_class('img-polaroid', options))
end
# glyph_icon bootstrap icon
#
# Example:
# glyph_icon(:plus)
# # =>
#
# glyph_icon(:align_justify, :white)
# # =>
def glyph_icon(*names)
content_tag :i, nil, :class => names.flatten.map { |name| "icon-#{name.to_s.gsub('_', '-')}" }
end
end