diff --git a/README.md b/README.md index 2c509e5..b79ca04 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,6 @@ Include BootstrapHelper in Gemfile: gem 'bootstrap-helper',git: 'git://github.com/niedhui/bootstrap-helper.git' ``` -# topbar(Navigation) -``` ruby -- topbar do |bar| - = bar.brand_name "YourBrandName",root_path - = bar.nav do - = bar.item 'label1', "/url/url" - = bar.dropdown "dropdown_menu" do - = bar.item 'label2', "/url2/url2" - = bar.item 'label3', "/url3/url3" - = bar.second_nav do - = bar.item 'label4', "/url4/url4" -``` - # modal ``` ruby - modal id: "my_modal" do |m| @@ -43,3 +30,96 @@ gem 'bootstrap-helper',git: 'git://github.com/niedhui/bootstrap-helper.git' %li.divider %li= link_to "nihao" ``` + +# tabs +```ruby + <% tabs do |tab| %> + <%= tab.item "Tabs 1", { :active => true } do %> +

Title 1

+

Content 1

+ <% end %> + <%= tab.item "Tabs 2" do %> +

Title 2

+

Content 2

+ <% end %> + <% end %> +``` + +# navbar + +Simple use : + +```ruby + <% navbar do |bar| %> + + <%= bar.brand_name "YourBrandName", root_path %> + + <%= bar.nav do %> + <%= bar.item 'item', '#', { :active => true } %> + <%= bar.divider( :vertical => true ) %> + <%= bar.dropdown 'Title 1' do %> + <%= bar.header "Items a" %> + <%= bar.item 'item 1', '#' %> + <%= bar.item 'item 2', '#' %> + <%= bar.divider %> + <%= bar.item 'item 3', '#' %> + <%= bar.header "Items b" %> + <%= bar.item 'item 4', '#' %> + <%= bar.item 'item 5', '#' %> + <% end %> + <% end %> + + <% end %> +``` + +Or you can use like this : + + +```ruby + <% topbar( :inverse => true, :id => 'topBar' ) do |bar| %> + + <%= bar.collapse %> + <%= bar.brand_name "YourBrandName", root_path %> + + <%= bar.nav( :right => true ) do %> + <%= bar.dropdown( :class => 'highlight' ) do %> + <%= bar.dropdown_title do %> + 10 + Title 2 + <% end %> + <%= bar.dropdown_menu( :right => true ) do %> + <%= bar.header do %> + Items a + <% end %> + <%= bar.item do %> + Item 3 + <% end %> + <%= bar.item 'item 4', '#' %> + <%= bar.header "Items b" %> + <%= bar.item '/link' do %> + Item 5 + 15 + <% end %> + <% end %> + <% end %> + <% end %> + + <% end %> +``` + +Optional display variations : + + +```ruby +<% navbar do |bar| %> + ... +<% end %> + +<% topbar do |bar| %> + ... +<% end %> + +<% bottombar do |bar| %> + ... +<% end %> +``` \ No newline at end of file diff --git a/lib/bootstrap-helper/builders/alert.rb b/lib/bootstrap-helper/builders/alert.rb new file mode 100644 index 0000000..98a7926 --- /dev/null +++ b/lib/bootstrap-helper/builders/alert.rb @@ -0,0 +1,27 @@ +module BootstrapHelper + module Builders + class Alert + attr_accessor :template, :items + + def initialize(template, options = {}, &proc) + @template, @items = template, [] + render(options, &proc) + end + + def render(options, &proc) + buffer = template.capture(self, &proc) + template.concat(wrapper(options,buffer)) + end + + def wrapper(options, buffer) + klass = options[:type].nil? ? ['alert'] : ["alert alert-#{options[:type]}"] + klass << options[:klass] || '' + + template.content_tag(:div, class: klass) do + template.content_tag(:button, template.raw('×'), :type => 'button', class: 'close', 'data-dismiss' => 'alert') + buffer + end + end + + end + end +end \ No newline at end of file diff --git a/lib/bootstrap-helper/builders/navbar.rb b/lib/bootstrap-helper/builders/navbar.rb index 19ea670..e0543db 100644 --- a/lib/bootstrap-helper/builders/navbar.rb +++ b/lib/bootstrap-helper/builders/navbar.rb @@ -10,48 +10,114 @@ def initialize(template, options = {}, &proc) def render(options, &proc) buffer = template.capture(self, &proc) - template.concat(wrapper(options,buffer)) + template.concat(wrapper(options, buffer)) end - def wrapper(options ,buffer) - navbar_class = ["navbar"] - navbar_class << "navbar-fixed-top" if options[:top] - template.content_tag(:div, class: navbar_class, "data-dropdown" => "dropdown") do - template.content_tag(:div, class: "navbar-inner") do - template.content_tag(:div, buffer,class: "container") - end + def wrapper(options, buffer) + navbar_class = ['navbar'] + navbar_class << 'navbar-fixed-top' if options[:top] + navbar_class << 'navbar-fixed-bottom' if options[:bottom] + navbar_class << 'navbar-inverse' if options[:inverse] + id = options[:id] || '' + + template.content_tag(:div, class: navbar_class, 'data-dropdown' => 'dropdown', :id => id) do + template.content_tag(:div, buffer, class: 'navbar-inner') end end - def brand_name(name,link = "#",options = {}) + def brand_name(name, link = '#', options = {}) template.link_to name, link, class: 'brand' end - - def nav(options = {},&block) + + def nav(options = {}, &block) buffer = template.capture(self, &proc) - template.content_tag(:ul,buffer, class: "nav") + klass = ['nav'] + klass << 'pull-right' if options[:right] + klass << 'pull-left' if options[:left] + klass_ul = ['nav'] + klass_ul << options[:class] || '' + + template.content_tag( :nav, class: klass ) do + template.content_tag( :ul, buffer, class: klass_ul) + end end - def second_nav(options = {},&block) + def second_nav(options = {}, &block) + ActiveSupport::Deprecation.warn("use nav instead of second_nav") + nav(options.merge(:right => true), &block) + end + + def dropdown(*args, &proc) + if args.first.class == Hash + options = args.first + else + title = args.first + options = args.second || {} + end + + body = template.capture(self, &proc) + klass = ['dropdown'] + klass << options[:class] unless options[:class].nil? + + unless title.nil? + body = dropdown_title(title, options, &proc) + body << dropdown_menu(options, &proc) + end + + template.content_tag(:li, body, class: klass) + end + + def dropdown_title(title = nil, options = {}, &block) + body = title.nil? ? template.capture(self, &proc) : title.html_safe + body << template.content_tag(:b, nil, class: 'caret') + + template.link_to(body, '#', class: 'dropdown-toggle', 'data-toggle' => 'dropdown') + end + + def dropdown_menu(options = {}, &block) buffer = template.capture(self, &proc) - template.content_tag(:ul,buffer, class: ["nav","pull-right"]) + klass = ['dropdown-menu'] + klass << 'pull-right' if options[:right] + + template.content_tag(:ul, buffer, class: klass) end - def dropdown(title,options = {},&proc) - buffer = template.capture(self, &proc) - template.content_tag(:li,class: 'dropdown') do - link = template.link_to "#",class: 'dropdown-toggle',"data-toggle" => "dropdown" do - title.html_safe + template.content_tag(:b,"",class: 'caret') - end - link << template.content_tag(:ul,buffer, class: "dropdown-menu") + def item(*args, &block) + if block_given? + body = template.capture(self, &proc) + url = args.first || '#' + options = args.second || {} + else + body = args[0] + url = args[1] || '#' + options = args[2] || {} + end + + klass = options[:class] || [] + klass << 'active' if options[:active] + data_toggle = options[:url_data_toggle] || '' + + template.content_tag(:li, class: klass) do + template.link_to(body, url, 'data-toggle' => data_toggle) end end - def item(title,link,options = {}) - template.content_tag(:li,template.link_to(title,link)) + def collapse + template.content_tag(:button, nil, :class => 'btn btn-navbar collapsed', "data-toggle" => 'collapse', 'data-target' => '.nav-collapse') do + 3.times.map { template.content_tag :span, nil, class: 'icon-bar' }.inject(:+) + end end - + + def divider(options = {}) + klass = "divider#{options[:vertical] ? '-vertical' : ''}" + template.content_tag :li, nil, class: klass + end + + def header(title = nil, &block) + body = title.nil? ? template.capture(self, &proc) : title + template.content_tag :li, body, class: 'nav-header' + end + end end -end - +end \ No newline at end of file diff --git a/lib/bootstrap-helper/builders/tabs.rb b/lib/bootstrap-helper/builders/tabs.rb new file mode 100644 index 0000000..ff68151 --- /dev/null +++ b/lib/bootstrap-helper/builders/tabs.rb @@ -0,0 +1,69 @@ +module BootstrapHelper + module Builders + class Tabs + attr_accessor :template, :items + + def initialize(template, options = {}, &proc) + @template, @items = template, [] + render(options, &proc) + end + + def render(options, &proc) + buffer = template.capture(self, &proc) + template.concat(wrapper(options,buffer)) + end + + def wrapper(options, buffer) + klass = options[:class] || 'nav-collapse' + + template.content_tag( :div, class: "tabbable" ) do + content = template.content_tag( :div, :class => 'navbar' ) do + content_div = collapse({:class => klass}) + content_div << tabs_titles({:class => klass}) do + content_items = ''.html_safe + items.each do |item| + content_items << item_title( item[:name], item[:id], item[:klass], item[:title] ) + end + content_items + end + end + + content << template.content_tag( :div, class: "tab-content" ) do + buffer + end + end + end + + def collapse(options = {}) + template.content_tag(:button, nil, :class => 'btn btn-navbar', "data-toggle" => 'collapse', 'data-target' => ".#{options[:class]}", :type => 'button') do + 3.times.map { template.content_tag :span, nil, class: 'icon-bar' }.inject(:+) + end + end + + def tabs_titles(options = {}, &block) + buffer = template.capture( self, &proc ) + template.content_tag( :ul, buffer, class: "nav nav-tabs #{options[:class]} collapse" ) + end + + def item_title(name, id, klass, title) + template.content_tag( :li, nil, class: klass, :title => title ) do + template.link_to name, "##{id}", :data => { :toggle => 'tab' } + end + end + + def item(name, options = {}, &block) + klass = options[:active] ? "active" : "" + klass += " #{options[:class]}" unless options[:class].nil? + title = options[:title] || '' + + id = name.downcase.parameterize.gsub(/-/, '_') + + items << { :name => name, :id => id, :klass => klass, :title => title } + + buffer = template.capture( self, &proc ) + template.content_tag( :div, buffer, class: "tab-pane #{klass}", :id => id ) + end + + end + end +end \ No newline at end of file diff --git a/lib/bootstrap-helper/helpers.rb b/lib/bootstrap-helper/helpers.rb index a477d21..7aa58ce 100644 --- a/lib/bootstrap-helper/helpers.rb +++ b/lib/bootstrap-helper/helpers.rb @@ -1,14 +1,18 @@ module BootstrapHelper module Helpers autoload :Navbar, 'bootstrap-helper/helpers/navbar' + autoload :Tabs, 'bootstrap-helper/helpers/tabs' autoload :Modal, 'bootstrap-helper/helpers/modal' autoload :Link, 'bootstrap-helper/helpers/link' autoload :Table, 'bootstrap-helper/helpers/table' autoload :Buttons, 'bootstrap-helper/helpers/buttons' + autoload :Alert, 'bootstrap-helper/helpers/alert' include Navbar + include Tabs include Table include Modal include Link include Buttons + include Alert end end \ No newline at end of file diff --git a/lib/bootstrap-helper/helpers/alert.rb b/lib/bootstrap-helper/helpers/alert.rb new file mode 100644 index 0000000..16e2fad --- /dev/null +++ b/lib/bootstrap-helper/helpers/alert.rb @@ -0,0 +1,22 @@ +require "bootstrap-helper/builders/alert" +module BootstrapHelper + module Helpers + module Alert + def bt_alert(options = {}, &proc) + ::BootstrapHelper::Builders::Alert.new(self,options,&proc) + end + def bt_alert_success(options = {}, &proc) + bt_alert(options.merge(type: 'success'), &proc) + end + def bt_alert_error(options = {}, &proc) + bt_alert(options.merge(type: 'error'), &proc) + end + def bt_alert_warning(options = {}, &proc) + bt_alert(options.merge(type: 'block'), &proc) + end + def bt_alert_info(options = {}, &proc) + bt_alert(options.merge(type: 'info'), &proc) + end + end + end +end \ No newline at end of file diff --git a/lib/bootstrap-helper/helpers/navbar.rb b/lib/bootstrap-helper/helpers/navbar.rb index b93927a..807581d 100644 --- a/lib/bootstrap-helper/helpers/navbar.rb +++ b/lib/bootstrap-helper/helpers/navbar.rb @@ -7,7 +7,11 @@ def navbar(options = {},&proc) end def topbar(options = {}, &proc) - ::BootstrapHelper::Builders::Navbar.new(self,options.merge(top: true),&proc) + navbar(options.merge(top: true),&proc) + end + + def bottombar(options = {}, &proc) + navbar(options.merge(bottom: true),&proc) end end end diff --git a/lib/bootstrap-helper/helpers/tabs.rb b/lib/bootstrap-helper/helpers/tabs.rb new file mode 100644 index 0000000..61d34c6 --- /dev/null +++ b/lib/bootstrap-helper/helpers/tabs.rb @@ -0,0 +1,10 @@ +require "bootstrap-helper/builders/tabs" +module BootstrapHelper + module Helpers + module Tabs + def tabs(options = {}, &proc) + ::BootstrapHelper::Builders::Tabs.new(self,options,&proc) + end + end + end +end \ No newline at end of file