Class: LogicalTabs::TabbedPanel

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @view = view
  @tabs = []
  @base_id = options[:base_id] || build_generic_id(options)
end

Instance Attribute Details

#base_idObject (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

#tabsObject (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, options = {},  &block)
  # debugger
  options[:content] ||= v.capture(&block)
  tab = TabPane.new(self, name, options)
  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(options)
  base_id = "tabbed_panel".html_safe
  base_id += "_#{options[:seq]}".html_safe if options[:seq]
  base_id
end

#renderObject

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.(:div,
    render_tabs + render_panes,
    :id => @base_id.to_s.html_safe,
    :class => 'tabbed_panel'.html_safe
  ).html_safe
  out
end

#render_panesObject

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.(:div,
    @tabs.map{ |tab| tab.render_pane(selected?(tab)) }.join.html_safe,
    :class => 'panes'.html_safe
  ).html_safe
end

#render_tabsObject

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.(:ul,
    @tabs.map{ |tab| tab.render_tab(selected?(tab)) }.join.html_safe,
    :class => 'tabs'.html_safe
  ).html_safe
end

#selected?(tab) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/logical_tabs/tabbed_panel.rb', line 85

def selected?(tab)
  tab == selected_tab
end

#selected_tabObject

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

#vObject

shortcut to the View



95
# File 'lib/logical_tabs/tabbed_panel.rb', line 95

def v; @view end