Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 93 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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 %>
<h1>Title 1</h1>
<p>Content 1</p>
<% end %>
<%= tab.item "Tabs 2" do %>
<h1>Title 2</h1>
<p>Content 2</p>
<% 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 %>
<span class="count">10</span>
<span class="title">Title 2</span>
<% end %>
<%= bar.dropdown_menu( :right => true ) do %>
<%= bar.header do %>
<span class="title">Items a</span>
<% end %>
<%= bar.item do %>
<span class="title">Item 3</span>
<% end %>
<%= bar.item 'item 4', '#' %>
<%= bar.header "Items b" %>
<%= bar.item '/link' do %>
<span class="title">Item 5</span>
<span class="count">15</span>
<% end %>
<% end %>
<% end %>
<% end %>

<% end %>
```

Optional display variations :


```ruby
<% navbar do |bar| %>
...
<% end %>

<% topbar do |bar| %>
...
<% end %>

<% bottombar do |bar| %>
...
<% end %>
```
27 changes: 27 additions & 0 deletions lib/bootstrap-helper/builders/alert.rb
Original file line number Diff line number Diff line change
@@ -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('&times;'), :type => 'button', class: 'close', 'data-dismiss' => 'alert') + buffer
end
end

end
end
end
118 changes: 92 additions & 26 deletions lib/bootstrap-helper/builders/navbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
69 changes: 69 additions & 0 deletions lib/bootstrap-helper/builders/tabs.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/bootstrap-helper/helpers.rb
Original file line number Diff line number Diff line change
@@ -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
Loading