Class: LogicalTabs::TabbedPanel
- Inherits:
-
Object
- Object
- LogicalTabs::TabbedPanel
- Defined in:
- lib/logical_tabs/tabbed_panel.rb
Overview
a collection of tabs with their associated panes, I.E. a tabbed panel
Instance Attribute Summary collapse
-
#base_id ⇒ Object
readonly
Returns the value of attribute base_id.
-
#tabs ⇒ Object
readonly
returns the array of TabPanes.
Instance Method Summary collapse
-
#add_tab(name, options = {}, &block) ⇒ Object
Add a new tab (with pane) to this tabbed panel.
- #build_generic_id(options) ⇒ Object
-
#initialize(view, options = {}) ⇒ TabbedPanel
constructor
view must be an instance of ActionView::Base.
-
#render ⇒ Object
render the entire tabbed panel and all contents.
-
#render_panes ⇒ Object
Render a sequence of DIVs containing the panes.
-
#render_tabs ⇒ Object
Render a UL containing all the tabs as LIs.
- #selected?(tab) ⇒ Boolean
-
#selected_tab ⇒ Object
For the moment, the first tab is the one selected.
-
#v ⇒ Object
shortcut to the View.
Constructor Details
#initialize(view, options = {}) ⇒ TabbedPanel
view must be an instance of ActionView::Base. This class depends on it for access to the capture and content_tag methods
13 14 15 16 17 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 13 def initialize(view, = {}) @view = view @tabs = [] @base_id = [:base_id] || build_generic_id() end |
Instance Attribute Details
#base_id ⇒ Object (readonly)
Returns the value of attribute base_id.
9 10 11 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 9 def base_id @base_id end |
#tabs ⇒ Object (readonly)
returns the array of TabPanes
90 91 92 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 90 def tabs @tabs end |
Instance Method Details
#add_tab(name, options = {}, &block) ⇒ Object
Add a new tab (with pane) to this tabbed panel. Either pass a block with the content for the panel, or pass :content => ‘my_content’.
Name is required and serves as the internal identifier for the tab. By default, it is also the displayed text of the tab.
Other options:
:base_id The string used as the beginning of the ID for CSS
IDs for the tab and pane. :base_id => 'foo' will
generate a tab with ID 'foo_tab' and a panel with ID
'foo_panel'. Defaults to name.downcase
:tab_text The text to display in the tab. Defaults to name.
:content The content for the panel itself, in HTML
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 41 def add_tab(name, = {}, &block) # debugger [:content] ||= v.capture(&block) tab = TabPane.new(self, name, ) if @tabs.any?{ |t| t.base_id == tab.base_id } raise DuplicateTabID else @tabs << tab end return nil end |
#build_generic_id(options) ⇒ Object
19 20 21 22 23 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 19 def build_generic_id() base_id = "tabbed_panel".html_safe base_id += "_#{[:seq]}".html_safe if [:seq] base_id end |
#render ⇒ Object
render the entire tabbed panel and all contents.
71 72 73 74 75 76 77 78 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 71 def render out = v.content_tag(:div, render_tabs + render_panes, :id => @base_id.to_s.html_safe, :class => 'tabbed_panel'.html_safe ).html_safe out end |
#render_panes ⇒ Object
Render a sequence of DIVs containing the panes.
63 64 65 66 67 68 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 63 def render_panes v.content_tag(:div, @tabs.map{ |tab| tab.render_pane(selected?(tab)) }.join.html_safe, :class => 'panes'.html_safe ).html_safe end |
#render_tabs ⇒ Object
Render a UL containing all the tabs as LIs. Which tab is shown as selected is determined by the return value of selected?
55 56 57 58 59 60 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 55 def render_tabs v.content_tag(:ul, @tabs.map{ |tab| tab.render_tab(selected?(tab)) }.join.html_safe, :class => 'tabs'.html_safe ).html_safe end |
#selected?(tab) ⇒ Boolean
85 86 87 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 85 def selected?(tab) tab == selected_tab end |
#selected_tab ⇒ Object
For the moment, the first tab is the one selected
81 82 83 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 81 def selected_tab @tabs.first end |
#v ⇒ Object
shortcut to the View
95 |
# File 'lib/logical_tabs/tabbed_panel.rb', line 95 def v; @view end |