Module: BootstrapComponentHelper

Extended by:
ActiveSupport::Concern
Includes:
BootstrapHelper
Defined in:
app/helpers/bootstrap_component_helper.rb

Instance Method Summary collapse

Methods included from BootstrapHelper

#div, #javascript_include_tag_with_p, #merge_predef_class

Instance Method Details

#badge(text, type = 'default') ⇒ Object Also known as: b



118
119
120
# File 'app/helpers/bootstrap_component_helper.rb', line 118

def badge(text, type='default')
  ('span', text, class: "badge badge-#{type.to_s}")
end

#btn_group(opts = {}, &block) ⇒ Object Also known as: bg

Button groups 就是一个div, 里面的btn还是自己写 opts:

type: vertical 默认为空,竖着排。。。诡异


12
13
14
15
16
17
# File 'app/helpers/bootstrap_component_helper.rb', line 12

def btn_group(opts={}, &block)
  class_str = 'btn-group'
  class_str << ' btn-group-vertical' if opts.delete(:type) == 'vertical'

  div(merge_predef_class(class_str, opts), &block)
end

#btn_toolbar(opts = {}, &block) ⇒ Object Also known as: bt

可以组合多个btn group



22
23
24
# File 'app/helpers/bootstrap_component_helper.rb', line 22

def btn_toolbar(opts={}, &block)
  div(merge_predef_class('btn-toolbar', opts), &block)
end

#f(message = nil, options = {}, &block) ⇒ Object

Alerts, Styles for success, warning, and error messages

message - the message of alert options -

type - type of alerts: error, warning, success, info. default: warning
block: true, false, add padding to alert component. default: false
closable: true, false, add a close button to alert. default: true
the other options that can be accepted by div


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/helpers/bootstrap_component_helper.rb', line 222

def f(message=nil, options={}, &block)
  message, options = nil, message if message.is_a?(Hash)

  default_options = {closable: true, block: false}
  options.reverse_merge!(default_options)

  flash_class = %w(alert)
  flash_class << "alert-#{options.delete(:type).to_s}" if options[:type]
  flash_class << 'alert-block' if options.delete(:block)
  flash_class << "#{options.delete(:class)}"

  closable = options.delete(:closable)
  (:div, nil, options.merge!(class: flash_class)) do
    content = []
    content << '<a href="#" class="close" data-dismiss="alert">&times;</a>' if closable
    content << message if message
    content << capture(&block) if block_given?
    content.join.html_safe
  end
end

#hero_unit(*args, &block) ⇒ Object Also known as: hu

Typographic components

title - hero unit’s title, presented as h1 description - description of hero unit, presented as p options - any html options that can be accepted by div



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/bootstrap_component_helper.rb', line 129

def hero_unit(*args, &block)
  options = args.extract_options!
  title = args.try(:first)
  description = args.try(:second)

  div(merge_predef_class('hero-unit', options)) do
    content = []
    content << ('h1', title) unless title.blank?
    content << ('p', description) unless description.blank?
    content << capture(&block) if block_given?
    content.compact.join.html_safe
  end
end

#l(text, type = 'default') ⇒ Object

Labels and badges type: success, warning, important, info, inverse



114
115
116
# File 'app/helpers/bootstrap_component_helper.rb', line 114

def l(text, type='default')
  ('span', text, class: "label label-#{type.to_s}")
end

bootstrap modal see bootstrap-modal-rails gem

options -

header - header of modal, replace default header.
footer_text - text of footer, default: Close
footer -  footer of modal, replace default footer.
width - numbers
static - true, false. default: true
fullwidth - true, false, default: false
long - true, false, default: false


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'app/helpers/bootstrap_component_helper.rb', line 303

def modal(id, header_text, options={}, &block)
  default_options = {
      header: "<button type='button' data-dismiss='modal' class='close'>&times;</button><h3>#{header_text}</h3>",
      footer: "<button class=\"btn\" data-dismiss=\"modal\" type=\"button\">#{options.delete(:footer_text)||'Close'}</button>",
      fullwidth: false, long: false, static: true
  }

  options = default_options.merge(options)

  footer = options.delete(:footer)
  header = options.delete(:header)

  options.merge!({'data-backdrop' => 'static', 'data-keyboard' => 'false'}) if options.delete(:static)
  options.merge!({'data-replace' => 'true'}) if options.delete(:long)
  options.merge!({'data-width' => options.delete(:width)}) if options[:width]

  div(merge_predef_class("modal hide fade #{options.delete(:fullwidth) ? 'container' : ''}", options).merge!(id: id)) do
    content = ''
    content << div(class: 'modal-header') do
      header.html_safe
    end
    content << div(class: 'modal-body') do
      capture(&block) if block_given?
    end
    content << div(class: 'modal-footer') do
      footer.html_safe
    end
    content.html_safe
  end
end

options:

type - a or b, a is link, b is button. default: a
icon - glyph icon of trigger


337
338
339
340
341
342
343
344
345
346
347
348
# File 'app/helpers/bootstrap_component_helper.rb', line 337

def modal_trigger(id, value, options={})
  default_options = {name: "trigger-#{id}", id: "trigger-#{id}", 'data-toggle' => 'modal', type: 'a'}
  options = default_options.merge(options)
  merge_predef_class('btn', options) if options.delete(:type) == 'b'

  link_to("##{id}", options) do
    content = ''
    content << glyph_icon(options.delete(:icon)) if options[:icon]
    content << value
    content.html_safe
  end
end


350
351
352
# File 'app/helpers/bootstrap_component_helper.rb', line 350

def modal_with_trigger(id, header_text, trigger_value, options={}, trigger_options={}, &block)
  modal(id, header_text, options, &block) + modal_trigger(id, trigger_value, trigger_options)
end

Nav : tabs, pills, and lists bootstrap navs



34
35
36
37
38
39
40
41
# File 'app/helpers/bootstrap_component_helper.rb', line 34

def nav(*args, &block)
  default_options = {class: 'nav'}
  options = args.extract_options!
  current_class = options.delete(:class) || ''
  options.reverse_merge!(default_options)[:class] << " #{current_class}"

  list(*(args << options), &block)
end

Navbar

options -

fix: top, bottom
static: top, bottom
fluid: true, false default: true
inverse: true, false default: false

&block ,navbar的内容



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/bootstrap_component_helper.rb', line 51

def navbar(options={}, &block)
  default_options = {fluid: true, inverse: false}
  options.reverse_merge!(default_options)
  fluid = options.delete(:fluid)

  result = ''
  navbar_class = 'navbar'
  case options.delete(:fix)
    when 'top'
      navbar_class << ' navbar-fixed-top'
      result << javascript_tag do
        raw <<-JS
            $(function(){
              if ($('.navbar-fixed-top').css('position') != 'static')
                $('body').css('padding-top','40px');
            });
        JS
      end
    when 'bottom'
      navbar_class << ' navbar-fixed-bottom'
      result << javascript_tag do
        raw <<-JS
            $(function(){
              if ($('.navbar-fixed-top').css('position') != 'static')
                $('body').css('padding-bottom','40px');
            });
        JS
      end
  end

  case options.delete(:static)
    when 'top'
      navbar_class << ' navbar-static-top'
    when 'bottom'
      navbar_class << ' navbar-static-bottom'
  end

  navbar_class << ' navbar-inverse' if options.delete(:inverse)

  options[:class] ||= ''
  options[:class] << " #{navbar_class}"

  result << div(options) do
    div(class: 'navbar-inner') do
      if fluid
        capture(&block) if block_given?
      else
        div(class: 'container') do
          capture(&block) if block_given?
        end
      end
    end
  end

  result.html_safe
end

#page_header(*args, &block) ⇒ Object Also known as: ph

Page header components

title - hero unit’s title, presented as h1 description - description of hero unit, presented as p options - any html options that can be accepted by div



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/helpers/bootstrap_component_helper.rb', line 150

def page_header(*args, &block)
  options = args.extract_options!
  title = args.try(:first)
  description = args.try(:second)

  div(merge_predef_class('page-header', options)) do
    if block_given?
      capture(&block)
    else
      ('h1') do
        [title,
         (('small', description) unless description.blank?)
        ].compact.join(' ').html_safe
      end unless title.blank?
    end
  end
end

#progress(*args) ⇒ Object Also known as: pg

Progress bars

the first parameter is a hash: active - true, false. it’s the active style of the bar. striped - true, false. it’s the striped style of the bar. in_table - true, false. If progress is in a table, set the margin-bottom to zero. the other options that can be acctpted by div

bar_options - hash or array. the bar options. If it is a stacked bar, options will be a hash, or will be an array

type        - bar's type: success, warning, danger, info
percentage  - the bar's percentage
content     - bar content
the other options that can be acctpted by div


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/helpers/bootstrap_component_helper.rb', line 256

def progress(*args)
  options = args.shift
  bar_options = args.flatten

  bar_options, options = [options], {} if bar_options.empty?

  progress_class = "progress #{options.delete(:striped) ? 'progress-striped' : ''} #{options.delete(:active) ? 'active' : ''}"
  progress_class << " #{options.delete(:class)}" if options[:class]

  progress_style = options.delete(:in_table) ? 'margin-bottom: 0;' : ''
  progress_style << " #{options.delete(:style)}" if options[:style]

  div(options.merge!({class: progress_class, style: progress_style})) do
    content = []
    bar_options.each do |opts|
      content << progress_bar(opts)
    end
    content.join.html_safe
  end
end

#progress_bar(options = {}) ⇒ Object



279
280
281
282
283
284
285
286
# File 'app/helpers/bootstrap_component_helper.rb', line 279

def progress_bar(options={})
  bar_class = "bar #{options[:type] ? 'bar-' + options[:type].to_s : ''}"
  bar_class << " #{options.delete(:class)}" if options[:class]

  bar_style = "width: #{options[:percentage].to_i}%;"
  bar_style << " #{options.delete(:style)}" if options[:style]
  ('div', options[:content], options.merge!({class: bar_class, style: bar_style}))
end

#thumbnail(span = 12, *options, &block) ⇒ Object Also known as: tn

Thumbnail element span - width of thumbnail, 1-12 title - title of thumbnail, h5 description - description of thumbnail, p li_options - any options that can be accepted by li tn_options - thumbnail options

tag: :div default.
any options that can be accepted by the tag


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/helpers/bootstrap_component_helper.rb', line 188

def thumbnail(span=12, *options, &block)
  tn_options = options.extract_options!
  li_options = options.first || {}

  li_options, tn_options = tn_options, {} if li_options.empty?

  li_options[:class] ||= ''
  li_options[:class] << " span#{span}"

  tn_options[:class] ||= ''
  tn_options[:class] << ' thumbnail'
  tn_options.reverse_merge!({tag: :div})

  ('li', nil, li_options) do
    (tn_options.delete(:tag), nil, tn_options) do
      capture(&block)
    end
  end
end

#thumbnails(options = {}, &block) ⇒ Object Also known as: tns

Thumbnails Grids of images, videos, text, and more options - any options that can be accepted by ul



172
173
174
175
176
177
178
# File 'app/helpers/bootstrap_component_helper.rb', line 172

def thumbnails(options={}, &block)
  options[:class] ||= ''
  options[:class] << ' thumbnails'
  ('ul', nil, options) do
    capture(&block)
  end
end