bh 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -1
  3. data/CHANGELOG.md +19 -0
  4. data/README.md +51 -18
  5. data/bh.gemspec +13 -9
  6. data/examples/middleman/.gitignore +18 -0
  7. data/examples/middleman/Gemfile +6 -0
  8. data/examples/middleman/config.rb +1 -0
  9. data/examples/middleman/source/index.html.erb +158 -0
  10. data/examples/padrino/Gemfile +9 -0
  11. data/examples/padrino/app/app.rb +16 -0
  12. data/examples/padrino/app/views/index.html.erb +155 -0
  13. data/examples/padrino/config.ru +5 -0
  14. data/examples/padrino/config/apps.rb +5 -0
  15. data/examples/padrino/config/boot.rb +8 -0
  16. data/examples/rails/.gitignore +12 -0
  17. data/examples/rails/Gemfile +5 -0
  18. data/examples/rails/app/controllers/application_controller.rb +6 -0
  19. data/examples/rails/app/controllers/users_controller.rb +2 -0
  20. data/examples/rails/app/models/user.rb +15 -0
  21. data/examples/rails/app/views/application/index.html.erb +160 -0
  22. data/examples/rails/bin/rails +4 -0
  23. data/examples/rails/config.ru +4 -0
  24. data/examples/rails/config/application.rb +12 -0
  25. data/examples/rails/config/boot.rb +3 -0
  26. data/examples/rails/config/environment.rb +5 -0
  27. data/examples/rails/config/environments/development.rb +7 -0
  28. data/examples/rails/config/routes.rb +4 -0
  29. data/examples/rails/config/secrets.yml +22 -0
  30. data/examples/rails/log/.keep +0 -0
  31. data/gemfiles/Gemfile.rails-3.x +2 -0
  32. data/gemfiles/Gemfile.rails-4.x +2 -0
  33. data/lib/bh.rb +15 -4
  34. data/lib/bh/classes/alert_box.rb +35 -0
  35. data/lib/bh/classes/base.rb +125 -0
  36. data/lib/bh/classes/button.rb +58 -0
  37. data/lib/bh/classes/button_to.rb +33 -0
  38. data/lib/bh/classes/cdn.rb +35 -0
  39. data/lib/bh/classes/dropdown.rb +56 -0
  40. data/lib/bh/classes/icon.rb +34 -0
  41. data/lib/bh/classes/link_to.rb +22 -0
  42. data/lib/bh/classes/modal.rb +68 -0
  43. data/lib/bh/classes/nav.rb +37 -0
  44. data/lib/bh/classes/navbar.rb +78 -0
  45. data/lib/bh/classes/panel.rb +72 -0
  46. data/lib/bh/classes/panel_row.rb +13 -0
  47. data/lib/bh/classes/progress_bar.rb +89 -0
  48. data/lib/bh/classes/stack.rb +19 -0
  49. data/lib/bh/classes/vertical.rb +27 -0
  50. data/lib/bh/core_ext/middleman.rb +24 -0
  51. data/lib/bh/core_ext/padrino.rb +25 -0
  52. data/lib/bh/core_ext/rails/base_helper.rb +21 -0
  53. data/lib/bh/{helpers → core_ext/rails}/form/base_helper.rb +3 -1
  54. data/lib/bh/{helpers → core_ext/rails}/form/check_box_helper.rb +1 -1
  55. data/lib/bh/{helpers → core_ext/rails}/form/field_helper.rb +1 -1
  56. data/lib/bh/{helpers → core_ext/rails}/form/fields_for_helper.rb +1 -1
  57. data/lib/bh/{helpers → core_ext/rails}/form/fieldset_helper.rb +1 -1
  58. data/lib/bh/{helpers → core_ext/rails}/form/file_field_helper.rb +1 -1
  59. data/lib/bh/{helpers → core_ext/rails}/form/legend_helper.rb +1 -1
  60. data/lib/bh/{helpers → core_ext/rails}/form/radio_button_helper.rb +1 -1
  61. data/lib/bh/{helpers → core_ext/rails}/form/select_helper.rb +1 -1
  62. data/lib/bh/{helpers → core_ext/rails}/form/static_control_helper.rb +1 -1
  63. data/lib/bh/{helpers → core_ext/rails}/form/submit_helper.rb +1 -1
  64. data/lib/bh/{form_builders → core_ext/rails}/form_builder.rb +11 -10
  65. data/lib/bh/core_ext/rails/form_for_helper.rb +32 -0
  66. data/lib/bh/core_ext/railtie.rb +27 -0
  67. data/lib/bh/helpers/alert_box_helper.rb +40 -0
  68. data/lib/bh/helpers/button_helper.rb +35 -58
  69. data/lib/bh/helpers/button_to_helper.rb +48 -10
  70. data/lib/bh/helpers/cdn_helper.rb +11 -35
  71. data/lib/bh/helpers/dropdown_helper.rb +29 -62
  72. data/lib/bh/helpers/glyphicon_helper.rb +7 -13
  73. data/lib/bh/helpers/horizontal_helper.rb +35 -0
  74. data/lib/bh/helpers/icon_helper.rb +15 -29
  75. data/lib/bh/helpers/link_to_helper.rb +52 -41
  76. data/lib/bh/helpers/modal_helper.rb +49 -79
  77. data/lib/bh/helpers/nav_helper.rb +25 -40
  78. data/lib/bh/helpers/navbar_helper.rb +36 -170
  79. data/lib/bh/helpers/panel_helper.rb +45 -72
  80. data/lib/bh/helpers/panel_row_helper.rb +20 -38
  81. data/lib/bh/helpers/progress_bar_helper.rb +48 -59
  82. data/lib/bh/helpers/vertical_helper.rb +33 -0
  83. data/lib/bh/version.rb +1 -1
  84. data/lib/bh/views/bh/_alert_dismiss_button.html +4 -0
  85. data/lib/bh/views/bh/_dropdown.html.erb +5 -5
  86. data/lib/bh/views/bh/_dropdown_split.html.erb +5 -5
  87. data/lib/bh/views/bh/_modal.html.erb +5 -8
  88. data/lib/bh/views/bh/_navbar.html.erb +5 -0
  89. data/spec/padrino/button_to_helper.rb +35 -0
  90. data/spec/padrino_spec.rb +28 -0
  91. data/spec/rails/button_to_helper.rb +31 -0
  92. data/spec/{helpers → rails}/form/check_box_helper_spec.rb +3 -3
  93. data/spec/{helpers → rails}/form/field_helper_spec.rb +3 -3
  94. data/spec/{helpers → rails}/form/fields_for_helper_spec.rb +3 -3
  95. data/spec/{helpers → rails}/form/fieldset_helper_spec.rb +3 -3
  96. data/spec/{helpers → rails}/form/file_field_helper_spec.rb +3 -3
  97. data/spec/{helpers → rails}/form/legend_helper_spec.rb +3 -3
  98. data/spec/{helpers → rails}/form/radio_button_helper_spec.rb +3 -3
  99. data/spec/{helpers → rails}/form/select_helper_spec.rb +3 -3
  100. data/spec/{helpers → rails}/form/static_control_helper_spec.rb +3 -3
  101. data/spec/{helpers → rails}/form/submit_helper_spec.rb +3 -3
  102. data/spec/{helpers → rails}/form_for_helper_spec.rb +3 -3
  103. data/spec/rails_helper.rb +25 -0
  104. data/spec/rails_spec.rb +28 -0
  105. data/spec/shared/alert_box_helper.rb +62 -0
  106. data/spec/shared/button_helper.rb +52 -0
  107. data/spec/shared/button_to_helper.rb +42 -0
  108. data/spec/shared/cdn_helper.rb +36 -0
  109. data/spec/shared/dropdown_helper.rb +112 -0
  110. data/spec/shared/glyphicon_helper.rb +21 -0
  111. data/spec/shared/horizontal_helper.rb +37 -0
  112. data/spec/shared/icon_helper.rb +31 -0
  113. data/spec/shared/link_to_helper.rb +62 -0
  114. data/spec/shared/modal_helper.rb +104 -0
  115. data/spec/shared/nav_helper.rb +49 -0
  116. data/spec/shared/navbar_helper.rb +63 -0
  117. data/spec/shared/panel_helper.rb +76 -0
  118. data/spec/shared/panel_row_helper.rb +21 -0
  119. data/spec/shared/progress_bar_helper.rb +103 -0
  120. data/spec/shared/vertical_helper.rb +43 -0
  121. data/spec/spec_helper.rb +6 -29
  122. data/spec/support/matchers.rb +38 -0
  123. data/spec/support/padrino.rb +33 -0
  124. data/spec/support/rails.rb +27 -0
  125. metadata +183 -94
  126. data/config.rb +0 -6
  127. data/lib/bh/helpers/alert_helper.rb +0 -77
  128. data/lib/bh/helpers/base_helper.rb +0 -39
  129. data/lib/bh/helpers/form_for_helper.rb +0 -30
  130. data/lib/bh/middleman.rb +0 -39
  131. data/lib/bh/railtie.rb +0 -42
  132. data/spec/dummy/index.html.erb +0 -60
  133. data/spec/dummy/layouts/default.erb +0 -17
  134. data/spec/helpers/alert_helper_spec.rb +0 -84
  135. data/spec/helpers/button_helper_spec.rb +0 -100
  136. data/spec/helpers/button_to_helper_spec.rb +0 -25
  137. data/spec/helpers/cdn_helper_spec.rb +0 -100
  138. data/spec/helpers/dropdown_helper_spec.rb +0 -146
  139. data/spec/helpers/glyphicon_helper_spec.rb +0 -21
  140. data/spec/helpers/icon_helper_spec.rb +0 -45
  141. data/spec/helpers/link_to_helper_spec.rb +0 -124
  142. data/spec/helpers/modal_helper_spec.rb +0 -133
  143. data/spec/helpers/nav_helper_spec.rb +0 -53
  144. data/spec/helpers/navbar_helper_spec.rb +0 -194
  145. data/spec/helpers/panel_helper_spec.rb +0 -95
  146. data/spec/helpers/panel_row_helper_spec.rb +0 -27
  147. data/spec/helpers/progress_bar_helper_spec.rb +0 -114
@@ -1,48 +1,59 @@
1
- require 'bh/helpers/base_helper'
1
+ require 'bh/classes/link_to'
2
+ require 'bh/classes/alert_box'
3
+ require 'bh/classes/dropdown'
4
+ require 'bh/classes/nav'
5
+ require 'bh/classes/stack'
6
+ require 'bh/classes/vertical'
2
7
 
3
8
  module Bh
4
- module LinkToHelper
5
- include BaseHelper
6
-
7
- # Overrides ActionView +link_to+ to be able to add the 'alert-link' class
8
- # to the link in case the link is inside of an alert.
9
- # @see http://getbootstrap.com/components/#alerts-links
10
- # Overrides ActionView +link_to+ to be able to surround the link in a
11
- # '<li>' item in case the link is inside of a nav.
12
- # Overrides ActionView +link_to+ to be able to add the 'navbar-brand'
13
- # class to the link in case the link is inside of an alert.
9
+ module Helpers
10
+ # Overrides `link_to` to display a Bootstrap-styled link.
11
+ # Can only be used in Ruby frameworks that provide the `link_to` method.
12
+ # @see http://getbootstrap.com/components/#dropdowns
13
+ # @see http://getbootstrap.com/components/#nav
14
+ # @see http://getbootstrap.com/components/#navbar-brand-image
15
+ # @see http://getbootstrap.com/components/#navbar-links
16
+ # @see http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
17
+ # @see http://www.rubydoc.info/github/padrino/padrino-framework/Padrino/Helpers/AssetTagHelpers#link_to-instance_method
18
+ # @return [String] the HTML to display a Bootstrap-styled link.
19
+ # @overload link_to(caption, url, options = {})
20
+ # @param [#to_s] caption the caption to display in the link.
21
+ # @param [#to_s] url the URL to link to.
22
+ # @param [Hash] options the options for the original `link_to` method.
23
+ # @example Display a plain-text link inside an alert-box.
24
+ # alert_box do
25
+ # link_to 'Check the terms and conditions', '/#terms'
26
+ # end
27
+ # @overload button_to(url, options = {}, &block)
28
+ # @param [#to_s] url the URL to link to (see above).
29
+ # @param [Hash] options the options for the original `link_to` method.
30
+ # @yieldreturn [#to_s] the caption to display in the link.
31
+ # @example Display a link with HTML inside a dropdown.
32
+ # dropdown 'Menu' do
33
+ # link_to '/#terms' do
34
+ # content_tag :strong, 'Check the terms and conditions'
35
+ # end
36
+ # end
14
37
  def link_to(*args, &block)
15
- if @alert_link
16
- super *add_link_class!('alert-link', *args, &block), &block
17
- elsif @navbar_vertical
18
- super *add_link_class!('navbar-brand', *args, &block), &block
19
- elsif @dropdown_link
20
- content_tag :li, role: :presentation do
21
- super *add_menu_item_attributes!(*args, &block), &block
22
- end
23
- elsif @nav_link
24
- content_tag :li, super(*args, &block), nav_item_options(*args, &block)
25
- else
26
- super *args, &block
27
- end
28
- end
29
-
30
- private
38
+ link_to = Bh::LinkTo.new self, *args, &block
31
39
 
32
- def add_link_class!(new_class, *args, &block)
33
- append_class_as! :class, new_class, *args, &block
34
- end
35
-
36
- def nav_item_options(*args, &block)
37
- options = (block_given? ? args[0] : args[1]) || {}
38
- {class: 'active'} if current_page? options
39
- end
40
+ link_to.append_class! :'alert-link' if Bh::Stack.find(Bh::AlertBox)
41
+ link_to.append_class! :'navbar-brand' if Bh::Stack.find(Bh::Vertical)
42
+ link_to.merge! role: :menuitem if Bh::Stack.find(Bh::Dropdown)
43
+ link_to.merge! tabindex: -1 if Bh::Stack.find(Bh::Dropdown)
44
+ html = super link_to.content, link_to.url, link_to.attributes, &nil
40
45
 
41
- def add_menu_item_attributes!(*args, &block)
42
- html_options = (block_given? ? args[1] : args[2]) || {}
43
- html_options.reverse_merge! role: 'menuitem', tabindex: '-1'
44
- block_given? ? args[1] = html_options : args[2] = html_options
45
- args
46
+ if Bh::Stack.find(Bh::Dropdown)
47
+ container = Bh::Base.new(self) { html }
48
+ container.merge! role: :presentation
49
+ container.render_tag :li
50
+ elsif Bh::Stack.find(Bh::Nav)
51
+ container = Bh::Base.new(self) { html }
52
+ container.append_class! :active if link_to.current_page?
53
+ container.render_tag :li
54
+ else
55
+ html
56
+ end
46
57
  end
47
58
  end
48
- end
59
+ end
@@ -1,85 +1,55 @@
1
- require 'bh/helpers/base_helper'
1
+ require 'bh/classes/modal'
2
2
 
3
3
  module Bh
4
- # Provides methods to include modals.
5
- # @see http://getbootstrap.com/javascript/#modals
6
- module ModalHelper
7
- include BaseHelper
8
-
9
- # Returns an HTML block tag that follows the Bootstrap documentation
10
- # on how to display *modals*.
11
- #
12
- # The content of the modal can either be passed as the first parameter (in
13
- # which case, the options are the second parameter), or as a block (in
14
- # which case, the options are the first paramter).
15
- # @example An modal with plain-text content passed as the first parameter.
16
- # modal 'Your profile was updated!', context: :info, title: 'Profile'
17
- # @example A panel with HTML content passed as a block.
18
- # modal context: :info, title: 'Profile' do
19
- # content_tag :div, "Your profile was updated!", class: 'modal-footer'
20
- # end
21
- #
22
- # @return [String] an HTML block tag for a panel.
23
- # @param [String] content_or_options_with_block the content to display in
24
- # the panel.
25
- # @param [Hash] options the display options for the panel.
26
- # @option options [#to_s] :title ('Modal') the title to display inside the modal.
27
- # @option options [#to_s] :body if present, the panel will include the
28
- # provided text wrapped in a 'modal-body' block, for proper padding
29
- # @option options [#to_s] :size the size of the modal.
30
- # @option options [Hash] :button the options for the "toggle" button.
31
- # * :caption (#to_s) ('Modal') the caption for the "toggle" button.
32
- # * :context (#to_s) (:default) the context for the "toggle" button,
33
- # which determines the button color
34
- # * :size (#to_s) the size of the "toggle" button.
4
+ module Helpers
5
+ # Displays a Bootstrap-styled modal.
35
6
  # @see http://getbootstrap.com/javascript/#modals
36
- def modal(content_or_options_with_block = nil, options = nil, &block)
37
- if block_given?
38
- modal_string content_or_options_with_block, &block
39
- elsif content_or_options_with_block.is_a?(Hash) && options.nil?
40
- modal_string content_or_options_with_block, &Proc.new { nil }
41
- else
42
- modal_string options, &Proc.new { content_or_options_with_block }
43
- end
44
- end
45
-
46
- private
47
-
48
- def modal_string(options = nil, &block)
49
- options ||= {}
50
- options[:id] ||= "modal-#{rand 10**10}"
51
- options[:title] ||= 'Modal'
52
- options[:body] = modal_body options[:body]
53
- options[:dialog_class] ||= dialog_class options[:size]
54
- options[:button] ||= {}
55
- options[:button][:caption] ||= options[:title]
56
- options[:button][:class] ||= button_class options[:button]
57
- render layout: 'bh/modal', locals: options, &block
58
- end
59
-
60
- def button_class(options = {})
61
- valid_contexts = %w(primary success info warning danger link)
62
- context = context_for options[:context], valid: valid_contexts
63
-
64
- size = case options[:size].to_s
65
- when 'lg', 'large' then 'btn-lg'
66
- when 'sm', 'small' then 'btn-sm'
67
- when 'xs', 'extra_small' then 'btn-xs'
68
- end
69
-
70
- ['btn', "btn-#{context}", size].compact.join ' '
71
- end
72
-
73
- def dialog_class(size = nil)
74
- size_class = case size.to_s
75
- when 'lg', 'large' then 'modal-lg'
76
- when 'sm', 'small' then 'modal-sm'
77
- end
78
- ['modal-dialog', size_class].compact.join ' '
79
- end
80
-
81
- def modal_body(body = nil)
82
- content_tag :div, body, class: 'modal-body' if body
7
+ # @return [String] the HTML to display a Bootstrap-styled modal.
8
+ # @overload modal(body, options = {})
9
+ # @param [#to_s] body the content to display as the modal body.
10
+ # @param [Hash] options the options for the modal. Any option not listed
11
+ # below is ignored, except for `:id` which is passed as an HTML
12
+ # attribute to the modal’s `<div>`.
13
+ # @option options [#to_s] :title ('Modal') the title of the modal.
14
+ # @option options [#to_s] :body the content to display as the modal body.
15
+ # Using this option is equivalent to passing the body as an argument.
16
+ # @option options [#to_s] :size the size of the modal. Can be `:large`
17
+ # (alias `:lg`) or `:small` (alias `:sm`).
18
+ # @option options [Hash] :button the options for the toggle button.
19
+ # * :caption (#to_s) ('Modal') the caption of the toggle button.
20
+ # * :context (#to_s) (:default) the contextual alternative to apply to
21
+ # the toggle button. Can be `:danger`, `:info`, `:link`, `:primary`,
22
+ # `:success` or `:warning`.
23
+ # * :size (#to_s) the size of the toggle button. Can be `:extra_small`
24
+ # (alias `:xs`), `:large` (alias `:lg`) or `:small` (alias `:sm`).
25
+ # * :layout (#to_s) if set to `:block`, span the button for the full
26
+ # width of the parent.
27
+ # @example Display a simple modal toggled by a blue button.
28
+ # modal 'You clicked me!', title: 'Click me', button: {context: :info}
29
+ # @overload modal(options = {}, &block)
30
+ # @param [Hash] options the options for the modal (see above).
31
+ # @yieldreturn [#to_s] the content to display in the modal.
32
+ # @example Display a modal with HTML content.
33
+ # modal title: 'Click me' do
34
+ # content_tag :div, class: 'modal-body' do
35
+ # content_tag :em, 'You clicked me!'
36
+ # end
37
+ # end
38
+ def modal(*args, &block)
39
+ modal = Bh::Modal.new self, *args, &block
40
+ modal.extract! :button, :size, :body, :title, :id
41
+
42
+ modal.extract_from :button, [:context, :size, :layout, :caption]
43
+ modal.append_class_to! :button, :btn
44
+ modal.append_class_to! :button, modal.button_context_class
45
+ modal.append_class_to! :button, modal.button_size_class
46
+ modal.merge! button: {caption: modal.caption}
47
+
48
+ modal.append_class_to! :div, :'modal-dialog'
49
+ modal.append_class_to! :div, modal.dialog_size_class
50
+ modal.merge! div: {title: modal.title, id: modal.id}
51
+
52
+ modal.render_partial 'modal'
83
53
  end
84
54
  end
85
55
  end
@@ -1,51 +1,36 @@
1
- require 'bh/helpers/base_helper'
1
+ require 'bh/classes/nav'
2
+ require 'bh/classes/navbar'
2
3
 
3
4
  module Bh
4
- # Provides methods to include navs.
5
- # @see http://getbootstrap.com/components/#nav
6
- module NavHelper
7
- include BaseHelper
8
-
9
- # Returns an HTML block tag that follows the Bootstrap documentation
10
- # on how to display *navs*.
11
- #
12
- # The skeleton of the nav is an unordered list; its content is passed as a
13
- # block as a list of navigation items.
14
- # Since the most common use for a nav is to display a menu of links, a
15
- # variable is set inside the block so that every call to +link_to+
16
- # generates a link *surrounded by a list item*.
17
- # @example An justified nav with two links.
18
- # nav layout: :justified do
5
+ module Helpers
6
+ # Displays a Bootstrap-styled nav.
7
+ # @see http://getbootstrap.com/components/#nav
8
+ # @return [String] the HTML to display a Bootstrap-styled nav.
9
+ # @param [Hash] options the options for the nav. Any option not listed below
10
+ # is passed as an HTML attribute to the alert’s `<ul>`.
11
+ # @option options [#to_s] :as (:tabs) the style of the nav. Can be `:tabs`
12
+ # or `:pills`.
13
+ # @option options [#to_s] :layout the layout of the nav. Can be
14
+ # `:justified` or `:stacked`.
15
+ # @yieldreturn [#to_s] the content to display in the nav.
16
+ # @example Display a pills-styled nav with a link.
17
+ # nav as: :pills do
19
18
  # link_to 'Home', '/'
20
- # link_to 'Profile', '/profile'
21
19
  # end
22
- #
23
- # @return [String] an HTML block tag for a nav.
24
- # @param [Hash] options the display options for the nav.
25
- # @option options [#to_s] :as ('tabs') the style to use for the nav.
26
- # Valid values are: :tabs and :pills.
27
- # @option options [#to_s] :layout (nil) if set, the layout of the nav.
28
- # Valid values are: :justified and :stacked.
29
- # @yield block the content of the nav
30
- # @see http://getbootstrap.com/components/#nav
31
20
  def nav(options = {}, &block)
32
- @nav_link = true
33
- nav = content_tag :ul, nav_options(options), &block
34
- nav.tap{ @nav_link = false }
35
- end
36
-
37
- private
21
+ nav = Bh::Nav.new(self, options, &block)
22
+ nav.extract! :as, :layout
38
23
 
39
- def nav_options(options = {})
40
- append_class! options, 'nav'
41
- if @navbar_id
42
- append_class! options, 'navbar-nav'
24
+ nav.append_class! :nav
25
+ if Bh::Stack.find(Bh::Navbar)
26
+ nav.append_class! :'navbar-nav'
43
27
  else
44
- options[:role] = 'tablist'
45
- append_class! options, "nav-#{options.fetch :as, 'tabs'}"
46
- append_class! options, "nav-#{options[:layout]}" if options[:layout]
28
+ nav.merge! role: :tablist
29
+ nav.append_class! nav.style_class
30
+ nav.append_class! nav.layout_class
47
31
  end
48
- options.slice :role, :class
32
+
33
+ nav.render_tag :ul
49
34
  end
50
35
  end
51
36
  end
@@ -1,182 +1,48 @@
1
- require 'bh/helpers/base_helper'
1
+ require 'bh/helpers/horizontal_helper'
2
+ require 'bh/helpers/vertical_helper'
2
3
 
3
4
  module Bh
4
- # Provides methods to include navbars.
5
- # @see http://getbootstrap.com/components/#navbar
6
- module NavbarHelper
7
- include BaseHelper
8
-
9
- # Returns an HTML block tag that follows the Bootstrap documentation
10
- # on how to display *navbars*.
11
- #
12
- # The skeleton of the nav is a <div> container inside a <nav> tag; its
13
- # content is passed as a block.
14
- #
15
- # Bootstrap allows to differentiate between the content of a navtab that
16
- # should remain visible even on a mobile (in a 'vertical' layout) and the
17
- # content that should only be visible when there is enough horizontal space
18
- # (in a 'horizontal') layout.
19
- #
20
- # The +vertical+ and +horizontal+ helpers can be used inside a navbar to
21
- # specify these types of content.
22
- # @example An inverted navbar with the company logo and two links.
23
- # navbar inverted: false do
24
- # vertical image_tag('logo')
25
- # horizontal nav do
26
- # link_to 'Home', '/'
27
- # link_to 'Profile', '/profile'
28
- # end
29
- # end
30
- #
31
- # @return [String] an HTML block tag for a navbar.
32
- # @param [Hash] options the display options for the nav.
5
+ module Helpers
6
+ # Displays a Bootstrap-styled navbar.
7
+ # @see http://getbootstrap.com/components/#navbar
8
+ # @return [String] the HTML to display a Bootstrap-styled navbar.
9
+ # @param [Hash] options the options for the navbar. Any option not listed
10
+ # below is ignored, except for `:id` which is passed as an HTML
11
+ # attribute to the navbar’s collapsable `<div>`.
33
12
  # @option options [Boolean] :fluid (false) whether to use a fluid container
34
13
  # to surround the navbar content.
35
14
  # @option options [Boolean] :inverted (false) whether to use an inverted
36
15
  # palette of colors.
37
- # @option options [#to_s] :position (nil) if set, the position of the
38
- # navbar. Valid values are: :top (alias :fixed_top), :bottom
39
- # (alias :fixed_bottom) and :static (alias :static_top).
16
+ # @option options [#to_s] :position the position of the navbar. Can be
17
+ # `:top` (alias `:fixed_top`), `:bottom` (alias `:fixed_bottom`) or
18
+ # `:static` (alias `:static_top`).
40
19
  # @option options [#to_s] :padding (70) if position is set to :top or
41
- # :bottom, then this padding will be added at the top (or bottom) of
42
- # <body> to prevent the navbar from overlaying the content.
43
- # @yield block the content of the nav
44
- # @see http://getbootstrap.com/components/#navbar
20
+ # :bottom, the padding to at the top (or bottom) of <body> to prevent the
21
+ # navbar from overlaying the content.
22
+ # @yieldreturn [#to_s] the content to display in the navbar.
23
+ # @example Display an inverted navbar with three links.
24
+ # navbar inverted: true do
25
+ # vertical do
26
+ # image_tag('logo')
27
+ # end
28
+ # horizontal do
29
+ # nav do
30
+ # link_to 'Home', '/'
31
+ # link_to 'Profile', '/profile'
32
+ # end
33
+ # end
34
+ # end
45
35
  def navbar(options = {}, &block)
46
- @navbar_id = rand 10**7
47
- nav_tag = navbar_string options, &block
48
- navbar = safe_join [navbar_style_tag(options), nav_tag].compact, "\n"
49
- navbar.tap{ @navbar_id = nil }
50
- end
51
-
52
- def vertical(content_or_options_with_block = nil, options = nil, &block)
53
- if block_given?
54
- vertical_string (content_or_options_with_block || {}), &block
55
- else
56
- vertical_string (options || {}), &Proc.new { content_or_options_with_block }
57
- end
58
- end
59
-
60
- def horizontal(content_or_options_with_block = nil, options = nil, &block)
61
- if block_given?
62
- horizontal_string (content_or_options_with_block || {}), &block
63
- else
64
- horizontal_string (options || {}), &Proc.new { content_or_options_with_block }
65
- end
66
- end
67
-
68
- private
69
-
70
- def navbar_string(options = {}, &block)
71
- content_tag :nav, role: 'navigation', class: navbar_class(options) do
72
- content_tag :div, class: navbar_container_class(options), &block
73
- end
74
- end
75
-
76
- # @private
77
- # The fixed navbar will overlay your other content, unless you add padding
78
- # to the top or bottom of the <body>. Try out your own values or use our
79
- # snippet below. Tip: By default, the navbar is 50px high.
80
- # @see http://getbootstrap.com/components/#navbar-fixed-top
81
- def navbar_style_tag(options = {})
82
- padding_value = options.fetch :padding, 70
83
- if padding_value && padding_type = padding_type_for(options[:position])
84
- content_tag :style, "body {padding-#{padding_type}: #{padding_value}px}"
85
- end
86
- end
87
-
88
- def padding_type_for(position)
89
- /fixed-(?<type>top|bottom)$/ =~ navbar_position_class_for(position)
90
- type
91
- end
36
+ navbar = Bh::Navbar.new(self, options, &block)
37
+ navbar.extract! :inverted, :position, :padding, :fluid, :id
92
38
 
93
- # Yields the block as the vertical part of the navbar and sets a variable
94
- # so that every +link_to+ helper called inside the block gets the
95
- # "navbar-brand" class added, for links to look better.
96
- def vertical_string(options = {}, &block)
97
- @navbar_vertical = true
98
- append_class! options, 'navbar-header'
99
- vertical = content_tag :div, options do
100
- safe_join [toggle_button, capture(&block)], "\n"
101
- end
102
- vertical.tap{ @navbar_vertical = false }
103
- end
104
-
105
- def horizontal_string(options = {}, &block)
106
- append_class! options, 'collapse navbar-collapse'
107
- options[:id] = navbar_id
108
- content_tag :div, options, &block
109
- end
110
-
111
- def toggle_button(options = {})
112
- options['type'] = 'button'
113
- options['class'] = 'navbar-toggle'
114
- options['data-toggle'] = 'collapse'
115
- options['data-target'] = "##{navbar_id}"
116
- content_tag :button, options do
117
- safe_join [toggle_text, toggle_bar, toggle_bar, toggle_bar], "\n"
118
- end
119
- end
120
-
121
- def toggle_text
122
- content_tag :span, 'Toggle navigation', class: 'sr-only'
123
- end
124
-
125
- def toggle_bar
126
- content_tag :span, nil, class: 'icon-bar'
127
- end
128
-
129
- private
39
+ navbar.append_class_to! :navigation, :navbar
40
+ navbar.append_class_to! :navigation, navbar.style_class
41
+ navbar.append_class_to! :navigation, navbar.position_class
42
+ navbar.append_class_to! :div, navbar.layout_class
43
+ navbar.prepend_html! navbar.body_padding_style
130
44
 
131
- def navbar_id
132
- "navbar-collapse-#{@navbar_id}"
133
- end
134
-
135
- def navbar_class(options = {})
136
- style = options[:inverted] ? 'inverse' : 'default'
137
- position = navbar_position_class_for options[:position]
138
- append_class! options, 'navbar'
139
- append_class! options, "navbar-#{style}"
140
- append_class! options, "navbar-#{position}" if position
141
- options[:class]
142
- end
143
-
144
- def navbar_position_class_for(position)
145
- case position.to_s
146
- when 'static', 'static_top' then 'static-top'
147
- when 'top', 'fixed_top' then 'fixed-top'
148
- when 'bottom', 'fixed_bottom' then 'fixed-bottom'
149
- end
150
- end
151
-
152
- def navbar_container_class(options = {})
153
- options[:fluid] ? 'container-fluid' : 'container'
45
+ navbar.render_partial 'navbar'
154
46
  end
155
47
  end
156
- end
157
-
158
-
159
- # STILL TO DO
160
- # 2) Add class to form and button and nav and p inside navbar
161
-
162
-
163
- #
164
- # <%= navbar do %>
165
- # <%= vertical do %>
166
- # <%= link_to image_tag('/assets/fullscreen-standard-logo-white.png', alt: 'Fullscreen'), root_path %>
167
- # <% end %>
168
- # <%= horizontal do %>
169
- # <%= nav do %>
170
- # <%= link_to 'Campaigns', root_path %>
171
- # <%= link_to 'Users', users_path if current_user.admin? %>
172
- # <% end %>
173
- # <%= nav class: 'navbar-right' do %>
174
- # <!-- button_to needs to:
175
- # * be surrounded in <li> AND use navbar-form inside a navbar
176
- # * add navbar-btn is inside a navbar, else add btn
177
- # -->
178
- # <%= button_to 'Sign out', destroy_user_session_path, class: 'btn-link', method: :delete %>
179
- # <% end %>
180
- # <% end %>
181
- # <% end if user_signed_in? %>
182
- #
48
+ end