Module: Card::Set::All::Bootstrap::Tabs::HtmlFormat

Extended by:
Card::Set::AbstractFormat
Defined in:
tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb

Instance Method Summary collapse

Instance Method Details

#lazy_loading_tabs(tabs, active_name, active_content = "", args = {}, &block) ⇒ HTML

beginning tab; other tabs get loaded via ajax when selected

Parameters:

  • tabs (Hash)

    keys are the views, values the title unless you pass a hash as value

  • active_name (String)

    label of the tab that should be active at the

  • active_content (String) (defaults to: "")

    content of the active tab can also be passed via a block

  • args (Hash) (defaults to: {})

    options

Options Hash (tabs):

  • :title (String)
  • :path (path)
  • :view (Symbol)
  • :html (HTML)

    if present use value as inner html for li tag and ignore the other tab options

Options Hash (args):

  • :type (String) — default: 'tabs'

    use pills or tabs

  • :panel_args (Hash)

    html args used for the panel div

  • :pane_args (Hash)

    html args used for the pane div

Returns:

  • (HTML)

    bootstrap tabs element with content only for the active



52
53
54
55
56
57
58
59
60
61
62
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 52

def lazy_loading_tabs tabs, active_name, active_content="", args={}, &block
  tab_buttons = ""
  tab_panes = ""
  standardize_tabs(tabs, active_name) do |tab_name, url, id, active_tab|
    tab_buttons += lazy_tab_button tab_name, id, url, active_tab
    tab_panes += lazy_tab_pane id, tab_name, active_tab, active_content,
                               args[:pane_args], &block
  end
  tab_type = args.delete(:type) || "tabs"
  tab_panel tab_buttons, tab_panes, tab_type, args[:panel_args]
end

#lazy_tab_button(tab_name, id, url, active_tab) ⇒ Object



64
65
66
67
68
69
70
71
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 64

def lazy_tab_button tab_name, id, url, active_tab
  return wrap_with(:li, tab_name, role: "presentation") unless url
  tab_button(
    "##{id}", tab_name, active_tab,
    "data-url" => url.html_safe,
    class: (active_tab ? nil : "load")
  )
end

#lazy_tab_pane(id, tab_name, active_tab, active_content, args) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 73

def lazy_tab_pane id, tab_name, active_tab, active_content, args
  tab_content =
    if active_tab
      block_given? ? yield : active_content
    else
      ""
    end
  tab_pane(id, tab_name, tab_content, active_tab, args)
end

#standardize_tabs(tabs, active_name) ⇒ Object



83
84
85
86
87
88
89
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 83

def standardize_tabs tabs, active_name
  tabs.each do |tab_view_name, tab_details|
    tab_title, url = tab_title_and_url(tab_details, tab_view_name)
    active_tab = (active_name == tab_view_name)
    yield tab_title, url, tab_id(tab_view_name), active_tab
  end
end

#static_tabs(tabs, active_name = nil, tab_type = "tabs", args = {}) ⇒ HTML

beginning (default is the first)

Parameters:

  • tab_type (String) (defaults to: "tabs")

    'tabs' or 'pills'

  • tabs (Hash)

    keys are the labels, values the content for the tabs

  • active_name (String) (defaults to: nil)

    label of the tab that should be active at the

Returns:

  • (HTML)

    bootstrap tabs element with all content preloaded



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 16

def static_tabs tabs, active_name=nil, tab_type="tabs", args={}
  tab_buttons = ""
  tab_panes = ""
  tabs.each do |tab_name, tab_content|
    active_name ||= tab_name
    active_tab = (tab_name == active_name)
    id = tab_id tab_name
    tab_content, button_attr =
      if tab_content.is_a?(Hash)
        [tab_content[:content], tab_content[:button_attr]]
      else
        [tab_content, {}]
      end
    tab_buttons += tab_button("##{id}", tab_name, active_tab, button_attr)
    tab_panes += tab_pane(id, tab_name, tab_content, active_tab, args[:pane])
  end
  tab_panel tab_buttons, tab_panes, tab_type
end

#tab_button(target, text, active = false, link_attr = {}) ⇒ Object



117
118
119
120
121
122
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 117

def tab_button target, text, active=false, link_attr={}
  add_class link_attr, "active" if active
  link = tab_button_link target, text, link_attr
  li_args = { role: :presentation, class: "nav-item" }
  wrap_with :li, link, li_args
end


124
125
126
127
128
129
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 124

def tab_button_link target, text, link_attr={}
  add_class link_attr, "nav-link"
  link_to text, link_attr.merge(
    path: target, role: "tab", "data-toggle" => "tab"
  )
end

#tab_id(tab_name) ⇒ Object



113
114
115
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 113

def tab_id tab_name
  "#{unique_id}-#{tab_name.to_name.safe_key}"
end

#tab_pane(id, tab_name, content, active = false, args = nil) ⇒ Object



131
132
133
134
135
136
137
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 131

def tab_pane id, tab_name, content, active=false, args=nil
  pane_args = { role: :tabpanel, id: id }
  pane_args.merge! args if args.present?
  add_class pane_args, "tab-pane tab-pane-#{tab_name.to_name.safe_key}"
  add_class pane_args, "active" if active
  wrap_with :div, content, pane_args
end

#tab_panel(tab_buttons, tab_panes, tab_type = "tabs", args = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 100

def tab_panel tab_buttons, tab_panes, tab_type="tabs", args=nil
  args ||= {}
  add_class args, "tabbable"
  args.reverse_merge! role: "tabpanel"
  wrap_with :div, args do
    [
      wrap_with(:ul, tab_buttons, class: "nav nav-#{tab_type}",
                                  role: "tablist"),
      wrap_with(:div, tab_panes, class: "tab-content")
    ]
  end
end

#tab_title_and_url(tab_details, tab_view_name) ⇒ Object



91
92
93
94
95
96
97
98
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/tabs.rb', line 91

def tab_title_and_url tab_details, tab_view_name
  if tab_details.is_a? Hash
    tab_details[:html] ||
      [tab_details[:title], tab_details[:path] || path(tab_details[:view])]
  else
    [tab_details, path(view: tab_view_name)]
  end
end