ccs-frontend_helpers 0.1.0.rc.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -5
  3. data/CHANGELOG.md +2 -4
  4. data/Gemfile +1 -15
  5. data/Gemfile.lock +80 -81
  6. data/ccs-frontend_helpers.gemspec +10 -0
  7. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +79 -0
  8. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +119 -6
  9. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +183 -6
  10. data/lib/ccs/{components/ccs → frontend_helpers/ccs_frontend}/logo.rb +12 -10
  11. data/lib/ccs/frontend_helpers/ccs_frontend.rb +4 -2
  12. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +95 -8
  13. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +17 -6
  14. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +54 -6
  15. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +105 -6
  16. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +114 -6
  17. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +23 -7
  18. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +32 -11
  19. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +65 -13
  20. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +165 -0
  21. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +200 -0
  22. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +153 -0
  23. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +83 -0
  24. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +153 -0
  25. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +201 -0
  26. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +124 -0
  27. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +106 -0
  28. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +213 -0
  29. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +48 -7
  30. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +161 -6
  31. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +24 -13
  32. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +139 -6
  33. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +17 -8
  34. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +21 -7
  35. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +70 -7
  36. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +113 -7
  37. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +314 -6
  38. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +28 -7
  39. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +27 -8
  40. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +18 -6
  41. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +186 -7
  42. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +204 -6
  43. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +100 -11
  44. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +73 -6
  45. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +21 -7
  46. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +30 -7
  47. data/lib/ccs/frontend_helpers/govuk_frontend.rb +19 -17
  48. data/lib/ccs/frontend_helpers/shared_methods.rb +27 -0
  49. data/lib/ccs/frontend_helpers/version.rb +1 -1
  50. metadata +142 -112
  51. data/.ruby-version +0 -1
  52. data/lib/ccs/components/base.rb +0 -64
  53. data/lib/ccs/components/ccs/dashboard_section/panel.rb +0 -57
  54. data/lib/ccs/components/ccs/dashboard_section.rb +0 -71
  55. data/lib/ccs/components/ccs/footer/link.rb +0 -55
  56. data/lib/ccs/components/ccs/footer/meta.rb +0 -59
  57. data/lib/ccs/components/ccs/footer/navigation.rb +0 -60
  58. data/lib/ccs/components/ccs/footer.rb +0 -95
  59. data/lib/ccs/components/ccs/header/link.rb +0 -60
  60. data/lib/ccs/components/ccs/header/navigation.rb +0 -98
  61. data/lib/ccs/components/ccs/header/service_authentication.rb +0 -53
  62. data/lib/ccs/components/ccs/header.rb +0 -110
  63. data/lib/ccs/components/govuk/accordion/section/content.rb +0 -53
  64. data/lib/ccs/components/govuk/accordion/section/header.rb +0 -57
  65. data/lib/ccs/components/govuk/accordion/section.rb +0 -61
  66. data/lib/ccs/components/govuk/accordion.rb +0 -58
  67. data/lib/ccs/components/govuk/back_link.rb +0 -51
  68. data/lib/ccs/components/govuk/breadcrumbs/link.rb +0 -57
  69. data/lib/ccs/components/govuk/breadcrumbs.rb +0 -55
  70. data/lib/ccs/components/govuk/button.rb +0 -107
  71. data/lib/ccs/components/govuk/cookie_banner/action.rb +0 -60
  72. data/lib/ccs/components/govuk/cookie_banner/message.rb +0 -80
  73. data/lib/ccs/components/govuk/cookie_banner.rb +0 -55
  74. data/lib/ccs/components/govuk/details.rb +0 -53
  75. data/lib/ccs/components/govuk/error_message.rb +0 -60
  76. data/lib/ccs/components/govuk/error_summary/item.rb +0 -54
  77. data/lib/ccs/components/govuk/error_summary.rb +0 -70
  78. data/lib/ccs/components/govuk/field/input/character_count.rb +0 -129
  79. data/lib/ccs/components/govuk/field/input/file_upload.rb +0 -44
  80. data/lib/ccs/components/govuk/field/input/select.rb +0 -82
  81. data/lib/ccs/components/govuk/field/input/text_input/fix.rb +0 -55
  82. data/lib/ccs/components/govuk/field/input/text_input.rb +0 -97
  83. data/lib/ccs/components/govuk/field/input/textarea.rb +0 -59
  84. data/lib/ccs/components/govuk/field/input.rb +0 -62
  85. data/lib/ccs/components/govuk/field/inputs/checkboxes.rb +0 -69
  86. data/lib/ccs/components/govuk/field/inputs/date_input/item.rb +0 -65
  87. data/lib/ccs/components/govuk/field/inputs/date_input.rb +0 -89
  88. data/lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb +0 -47
  89. data/lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb +0 -43
  90. data/lib/ccs/components/govuk/field/inputs/item/checkbox.rb +0 -50
  91. data/lib/ccs/components/govuk/field/inputs/item/divider.rb +0 -50
  92. data/lib/ccs/components/govuk/field/inputs/item/radio/form.rb +0 -44
  93. data/lib/ccs/components/govuk/field/inputs/item/radio/tag.rb +0 -43
  94. data/lib/ccs/components/govuk/field/inputs/item/radio.rb +0 -50
  95. data/lib/ccs/components/govuk/field/inputs/item.rb +0 -111
  96. data/lib/ccs/components/govuk/field/inputs/radios.rb +0 -69
  97. data/lib/ccs/components/govuk/field/inputs.rb +0 -57
  98. data/lib/ccs/components/govuk/field.rb +0 -108
  99. data/lib/ccs/components/govuk/fieldset/legend.rb +0 -65
  100. data/lib/ccs/components/govuk/fieldset.rb +0 -54
  101. data/lib/ccs/components/govuk/footer/link.rb +0 -55
  102. data/lib/ccs/components/govuk/footer/meta.rb +0 -59
  103. data/lib/ccs/components/govuk/footer/navigation.rb +0 -60
  104. data/lib/ccs/components/govuk/footer.rb +0 -131
  105. data/lib/ccs/components/govuk/form_group.rb +0 -60
  106. data/lib/ccs/components/govuk/header/link.rb +0 -56
  107. data/lib/ccs/components/govuk/header/navigation.rb +0 -71
  108. data/lib/ccs/components/govuk/header.rb +0 -107
  109. data/lib/ccs/components/govuk/hint.rb +0 -49
  110. data/lib/ccs/components/govuk/inset_text.rb +0 -52
  111. data/lib/ccs/components/govuk/label.rb +0 -85
  112. data/lib/ccs/components/govuk/notification_banner.rb +0 -107
  113. data/lib/ccs/components/govuk/pagination/increment/next.rb +0 -44
  114. data/lib/ccs/components/govuk/pagination/increment/previous.rb +0 -43
  115. data/lib/ccs/components/govuk/pagination/increment.rb +0 -98
  116. data/lib/ccs/components/govuk/pagination/item/ellipsis.rb +0 -28
  117. data/lib/ccs/components/govuk/pagination/item/form.rb +0 -49
  118. data/lib/ccs/components/govuk/pagination/item/tag.rb +0 -47
  119. data/lib/ccs/components/govuk/pagination/item.rb +0 -64
  120. data/lib/ccs/components/govuk/pagination.rb +0 -96
  121. data/lib/ccs/components/govuk/panel.rb +0 -62
  122. data/lib/ccs/components/govuk/phase_banner.rb +0 -69
  123. data/lib/ccs/components/govuk/skip_link.rb +0 -51
  124. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list/item.rb +0 -53
  125. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list.rb +0 -50
  126. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/paragraph.rb +0 -45
  127. data/lib/ccs/components/govuk/step_by_step_navigation/section/content.rb +0 -63
  128. data/lib/ccs/components/govuk/step_by_step_navigation/section/heading.rb +0 -69
  129. data/lib/ccs/components/govuk/step_by_step_navigation/section.rb +0 -56
  130. data/lib/ccs/components/govuk/step_by_step_navigation.rb +0 -59
  131. data/lib/ccs/components/govuk/summary_list/action/link.rb +0 -59
  132. data/lib/ccs/components/govuk/summary_list/card/actions.rb +0 -59
  133. data/lib/ccs/components/govuk/summary_list/card/title.rb +0 -51
  134. data/lib/ccs/components/govuk/summary_list/card.rb +0 -63
  135. data/lib/ccs/components/govuk/summary_list/row/actions.rb +0 -59
  136. data/lib/ccs/components/govuk/summary_list/row/key.rb +0 -47
  137. data/lib/ccs/components/govuk/summary_list/row/value.rb +0 -47
  138. data/lib/ccs/components/govuk/summary_list/row.rb +0 -67
  139. data/lib/ccs/components/govuk/summary_list.rb +0 -74
  140. data/lib/ccs/components/govuk/table/body/data_cell.rb +0 -53
  141. data/lib/ccs/components/govuk/table/body/head_cell.rb +0 -52
  142. data/lib/ccs/components/govuk/table/header/head_cell.rb +0 -54
  143. data/lib/ccs/components/govuk/table.rb +0 -111
  144. data/lib/ccs/components/govuk/tabs/panel.rb +0 -58
  145. data/lib/ccs/components/govuk/tabs/tab.rb +0 -56
  146. data/lib/ccs/components/govuk/tabs.rb +0 -66
  147. data/lib/ccs/components/govuk/tag.rb +0 -51
  148. data/lib/ccs/components/govuk/warning_text.rb +0 -61
  149. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_section.rb +0 -27
  150. data/lib/ccs/frontend_helpers/govuk_frontend/character_count.rb +0 -28
  151. data/lib/ccs/frontend_helpers/govuk_frontend/checkboxes.rb +0 -28
  152. data/lib/ccs/frontend_helpers/govuk_frontend/date_input.rb +0 -28
  153. data/lib/ccs/frontend_helpers/govuk_frontend/file_upload.rb +0 -28
  154. data/lib/ccs/frontend_helpers/govuk_frontend/input.rb +0 -28
  155. data/lib/ccs/frontend_helpers/govuk_frontend/radios.rb +0 -28
  156. data/lib/ccs/frontend_helpers/govuk_frontend/select.rb +0 -28
  157. data/lib/ccs/frontend_helpers/govuk_frontend/textarea.rb +0 -28
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/label'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
@@ -10,18 +12,79 @@ module CCS
10
12
  # This helper is used for generating the label component from the Government Design Systems
11
13
 
12
14
  module Label
15
+ include SharedMethods
16
+ include ActionView::Context
17
+ include ActionView::Helpers::TagHelper
18
+ include ActionView::Helpers::TextHelper
19
+ include ActionView::Helpers::FormTagHelper
20
+
21
+ # Generates the HTML for the GOV.UK label component
22
+ #
23
+ # @param attribute [String, Symbol] the attribute of the input that requires a label
24
+ # @param label_text [String] the label text, it is ignored if a block is given
25
+ # @param govuk_label_options [Hash] options that will be used in customising the HTML
26
+ #
27
+ # @option govuk_label_options [String] :classes additional CSS classes for the label HTML
28
+ # @option govuk_label_options [Boolean] :is_page_heading (false) if the legend is also the heading it will rendered in a h1
29
+ # @option govuk_label_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create the label
30
+ # @option govuk_label_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
31
+ #
32
+ # @yield HTML that will be contained within the 'govuk-label' label. Ignored if label text is given
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_label(attribute, label_text = nil, **govuk_label_options, &block)
38
+ initialise_attributes_and_set_classes(govuk_label_options, 'govuk-label')
39
+
40
+ label_html = if govuk_label_options[:form]
41
+ govuk_label_form(attribute, label_text, **govuk_label_options, &block)
42
+ else
43
+ govuk_label_tag(attribute, label_text, **govuk_label_options, &block)
44
+ end
45
+
46
+ if govuk_label_options[:is_page_heading]
47
+ tag.h1(label_html, class: 'govuk-label-wrapper')
48
+ else
49
+ label_html
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # Generates the HTML for the GOV.UK label component
56
+ # using the inbuilt rails method +label+
57
+ #
58
+ # @param (see govuk_label)
59
+ #
60
+ # @option (see govuk_label)
61
+ #
62
+ # @yield (see govuk_label)
63
+ #
64
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
65
+ # which can then be rendered on the page
66
+
67
+ def govuk_label_tag(attribute, label_text, **govuk_label_options)
68
+ label_tag(attribute, **govuk_label_options[:attributes]) do
69
+ label_text || yield
70
+ end
71
+ end
72
+
13
73
  # Generates the HTML for the GOV.UK label component
74
+ # using the inbuilt rails ActionView::Helpers::FormBuilder
14
75
  #
15
- # @param (see CCS::Components::GovUK::Label#initialize)
76
+ # @param (see govuk_label)
16
77
  #
17
- # @option (see CCS::Components::GovUK::Label#initialize)
78
+ # @option (see govuk_label)
18
79
  #
19
- # @yield (see CCS::Components::GovUK::Label#render)
80
+ # @yield (see govuk_label)
20
81
  #
21
- # @return (see CCS::Components::GovUK::Label#render)
82
+ # @return (see govuk_label_tag)
22
83
 
23
- def govuk_label(attribute, text = nil, **options, &block)
24
- Components::GovUK::Label.new(context: self, attribute: attribute, text: text, **options).render(&block)
84
+ def govuk_label_form(attribute, label_text, **govuk_label_options)
85
+ govuk_label_options[:form].label(attribute, **govuk_label_options[:attributes]) do
86
+ label_text || yield
87
+ end
25
88
  end
26
89
  end
27
90
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/notification_banner'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
@@ -11,19 +13,123 @@ module CCS
11
13
  # {https://design-system.service.gov.uk/components/notification-banner GDS - Components - Notification banner}
12
14
 
13
15
  module NotificationBanner
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
14
21
  # Generates the HTML for the GOV.UK Notification banner component
15
22
  #
16
- # @param (see CCS::Components::GovUK::NotificationBanner#initialize)
23
+ # @param text [String] the text that will be used for the heading in the content section of the banner.
24
+ # It is ignored if a block is given
25
+ # @param success_banner [Boolean] will use the success banner options if this is set to true
26
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
27
+ #
28
+ # @option govuk_notification_banner_options [String] :classes additional CSS classes for the notification banner HTML
29
+ # @option govuk_notification_banner_options [String] :title_text ('Important') the title text shown at the top of the banner
30
+ # @option govuk_notification_banner_options [String] :title_id ('govuk-notification-banner-title') the ID for the title text
31
+ # @option govuk_notification_banner_options [String] :role ('region') the role for the banner
32
+ # @option govuk_notification_banner_options [String] :heading_level (2) the heading level for the title text
33
+ # @option govuk_notification_banner_options [Hash] :attributes ({data: { module: 'govuk-notification-banner' }, aria: { labelledby: 'govuk-notification-banner-title' }})
34
+ # any additional attributes that will added as part of the HTML
35
+ #
36
+ # @yield HTML that will be contained within the 'govuk-notification-banner__content' div
37
+ #
38
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Notification banner
39
+ # which can then be rendered on the page
40
+
41
+ def govuk_notification_banner(text = nil, success_banner = false, **govuk_notification_banner_options)
42
+ banner_options = fetch_banner_options(success_banner, govuk_notification_banner_options)
43
+
44
+ set_banner_classes(banner_options, govuk_notification_banner_options)
45
+ set_banner_attributes(banner_options, govuk_notification_banner_options)
46
+
47
+ tag.div(**govuk_notification_banner_options[:attributes]) do
48
+ concat(tag.div(class: 'govuk-notification-banner__header') do
49
+ tag.send(:"h#{govuk_notification_banner_options[:heading_level] || 2}", banner_options[:title_text], class: 'govuk-notification-banner__title', id: banner_options[:title_id])
50
+ end)
51
+ concat(tag.div(class: 'govuk-notification-banner__content') do
52
+ if block_given?
53
+ yield
54
+ else
55
+ tag.p(text, class: 'govuk-notification-banner__heading')
56
+ end
57
+ end)
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ # Determines the banner options to be used for {#govuk_notification_banner}
64
+ #
65
+ # @param success_banner [Boolean] will use the success banner options if this is set to true
66
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
67
+ #
68
+ # @option govuk_notification_banner_options [String] :title_text ('Important') the title text shown at the top of the banner
69
+ # @option govuk_notification_banner_options [String] :title_id ('govuk-notification-banner-title') the ID for the title text
70
+ # @option govuk_notification_banner_options [String] :role ('region') the role for the banner
71
+ #
72
+ # @return [Hash] contains the following options used in {#govuk_notification_banner}:
73
+ # - +classes+
74
+ # - +title_text+
75
+ # - +title_id+
76
+ # - +role+
77
+
78
+ def fetch_banner_options(success_banner, govuk_notification_banner_options)
79
+ banner_options = DEFAULT_OPTIONS.dup
80
+
81
+ banner_options.merge!(SUCCESS_BANNER_OPTIONS) if success_banner
82
+
83
+ govuk_notification_banner_options.each do |key, value|
84
+ banner_options[key] = value if banner_options.key?(key)
85
+ end
86
+
87
+ banner_options
88
+ end
89
+
90
+ # Sets the banner classes to be used for {#govuk_notification_banner}
91
+ #
92
+ # @param banner_options [Hash] this is the return value from {#fetch_banner_options}
93
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
17
94
  #
18
- # @option (see CCS::Components::GovUK::NotificationBanner#initialize)
95
+ # @option govuk_notification_banner_options [String] :classes additional CSS classes for the notification banner HTML
96
+
97
+ def set_banner_classes(banner_options, govuk_notification_banner_options)
98
+ initialise_attributes_and_set_classes(govuk_notification_banner_options, 'govuk-notification-banner')
99
+
100
+ govuk_notification_banner_options[:attributes][:class] << banner_options[:classes].to_s
101
+ end
102
+
103
+ # Sets the HTML attributes for {#govuk_notification_banner}
19
104
  #
20
- # @yield (see CCS::Components::GovUK::NotificationBanner#render)
105
+ # @param banner_options [Hash] this is the return value from {#fetch_banner_options}
106
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
21
107
  #
22
- # @return (see CCS::Components::GovUK::NotificationBanner#render)
108
+ # @option govuk_notification_banner_options [Hash] :attributes ({data: { module: 'govuk-notification-banner' }, aria: { labelledby: 'govuk-notification-banner-title' }})
109
+ # any additional attributes that will added as part of the HTML
23
110
 
24
- def govuk_notification_banner(text = nil, success_banner = nil, **options, &block)
25
- Components::GovUK::NotificationBanner.new(context: self, text: text, success_banner: success_banner, **options).render(&block)
111
+ def set_banner_attributes(banner_options, govuk_notification_banner_options)
112
+ set_data_module(govuk_notification_banner_options, 'govuk-notification-banner')
113
+
114
+ govuk_notification_banner_options[:attributes][:role] ||= banner_options[:role]
115
+ (govuk_notification_banner_options[:attributes][:aria] ||= {})[:labelledby] = banner_options[:title_id]
26
116
  end
117
+
118
+ # Default options used in normal versions of {#govuk_notification_banner}
119
+
120
+ DEFAULT_OPTIONS = {
121
+ title_text: 'Important',
122
+ title_id: 'govuk-notification-banner-title',
123
+ role: 'region'
124
+ }.freeze
125
+
126
+ # Options specific for the success version of {#govuk_notification_banner}
127
+
128
+ SUCCESS_BANNER_OPTIONS = {
129
+ classes: ' govuk-notification-banner--success',
130
+ title_text: 'Success',
131
+ role: 'alert'
132
+ }.freeze
27
133
  end
28
134
  end
29
135
  end
@@ -1,28 +1,336 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/pagination'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
7
9
  module GovUKFrontend
10
+ # rubocop:disable Metrics/ModuleLength
11
+
8
12
  # = GOV.UK Pagination
9
13
  #
10
14
  # This helper is used for generating the pagination component from the
11
15
  # {https://design-system.service.gov.uk/components/pagination GDS - Components - Pagination}
12
16
 
13
17
  module Pagination
18
+ include SharedMethods
19
+ include ActionView::Context
20
+ include ActionView::Helpers::TagHelper
21
+ include ActionView::Helpers::TextHelper
22
+ include ActionView::Helpers::UrlHelper
23
+
14
24
  # Generates the HTML for the GOV.UK Pagination component
15
25
  #
16
- # @param (see CCS::Components::GovUK::Pagination#initialize)
26
+ # @param (see _govuk_pagination)
27
+ #
28
+ # @option (see _govuk_pagination)
29
+ #
30
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Pagination
31
+ # which can then be rendered on the page
32
+
33
+ def govuk_pagination(**govuk_pagination_options)
34
+ _govuk_pagination(**govuk_pagination_options) do |block_is_level|
35
+ concat(govuk_pagination_previous(block_is_level, form: govuk_pagination_options[:form], **govuk_pagination_options[:previous])) if govuk_pagination_options[:previous]
36
+ concat(govuk_pagination_items(govuk_pagination_options[:items], form: govuk_pagination_options[:form])) if govuk_pagination_options[:items]
37
+ concat(govuk_pagination_next(block_is_level, form: govuk_pagination_options[:form], **govuk_pagination_options[:next])) if govuk_pagination_options[:next]
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # Wrapper method used by {govuk_pagination}
44
+ #
45
+ # @param govuk_pagination_options [Hash] options that will be used in customising the HTML
46
+ #
47
+ # @option govuk_pagination_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create pagination buttons
48
+ # @option govuk_pagination_options [String] :classes additional CSS classes for the pagination HTML
49
+ # @option govuk_pagination_options [Array] :items an array of items for the pagination (see: {govuk_pagination_items})
50
+ # @option govuk_pagination_options [Hash] :previous the previous link (see: {govuk_pagination_previous})
51
+ # @option govuk_pagination_options [Hash] :next the next link (see: {govuk_pagination_next})
52
+ # @option govuk_pagination_options [Hash] :attributes ({role: 'navigation', aria: { label: 'results' }})
53
+ # any additional attributes that will added as part of the HTML
54
+ #
55
+ # @yield the pagination HTML generated by {govuk_pagination}
56
+ #
57
+ # @yieldparam block_is_level [Boolean] flag to indicate if there are no items
17
58
  #
18
- # @option (see CCS::Components::GovUK::Pagination#initialize)
59
+ # @return [ActiveSupport::SafeBuffer] the HTML for the Pagination
60
+ # which is used in {govuk_pagination}
61
+
62
+ def _govuk_pagination(**govuk_pagination_options)
63
+ initialise_attributes_and_set_classes(govuk_pagination_options, 'govuk-pagination')
64
+
65
+ block_is_level = govuk_pagination_options[:items].blank? && (govuk_pagination_options[:next].present? || govuk_pagination_options[:previous].present?)
66
+
67
+ govuk_pagination_options[:attributes][:class] << ' govuk-pagination--block' if block_is_level
68
+
69
+ govuk_pagination_options[:attributes][:role] = 'navigation'
70
+ (govuk_pagination_options[:attributes][:aria] ||= {})[:label] ||= 'results'
71
+
72
+ tag.nav(**govuk_pagination_options[:attributes]) do
73
+ yield(block_is_level)
74
+ end
75
+ end
76
+
77
+ # Generates the previous link for {govuk_pagination}
19
78
  #
20
- # @return (see CCS::Components::GovUK::Pagination#render)
79
+ # @param (see govuk_pagination_previous_content)
80
+ #
81
+ # @option (see govuk_pagination_previous_content)
82
+ #
83
+ # @return [ActiveSupport::SafeBuffer] the HTML for the previous link which is used in {govuk_pagination}
84
+
85
+ def govuk_pagination_previous(block_is_level, **govuk_pagination_previous_options)
86
+ govuk_pagination_previous_options[:attributes] ||= {}
87
+ govuk_pagination_previous_options[:attributes][:class] = "govuk-link govuk-pagination__link #{'pagination--button_as_link' if govuk_pagination_previous_options[:form]}".rstrip
88
+ govuk_pagination_previous_options[:attributes][:rel] = 'prev'
21
89
 
22
- def govuk_pagination(**options)
23
- Components::GovUK::Pagination.new(context: self, **options).render
90
+ pagination_previous_content = govuk_pagination_previous_content(block_is_level, govuk_pagination_previous_options)
91
+
92
+ tag.div(class: 'govuk-pagination__prev') do
93
+ if govuk_pagination_previous_options[:form]
94
+ govuk_pagination_previous_options[:form].button(pagination_previous_content, **govuk_pagination_previous_options[:attributes])
95
+ else
96
+ link_to(pagination_previous_content, govuk_pagination_previous_options[:href], **govuk_pagination_previous_options[:attributes])
97
+ end
98
+ end
24
99
  end
100
+
101
+ # Used by {govuk_pagination_previous}
102
+ # to generate the previous link content
103
+ #
104
+ # @param block_is_level [Boolean] when there are no items, this will be true and will add extra classes
105
+ # to the link to make the next and previous pagination links level
106
+ # @param govuk_pagination_previous_options [Hash] options that will be used in customising the HTML
107
+ #
108
+ # @option govuk_pagination_previous_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create previous button
109
+ # @option govuk_pagination_previous_options [String] :href the URL for the link
110
+ # @option govuk_pagination_previous_options [String] :text ('Previous') the text for the link
111
+ # @option govuk_pagination_previous_options [String] :lable_text additional text for the link when the pagination block is level
112
+ # @option govuk_pagination_previous_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
113
+ #
114
+ # @return [ActiveSupport::SafeBuffer] the HTML for the previous link/button
115
+
116
+ def govuk_pagination_previous_content(block_is_level, govuk_pagination_previous_options)
117
+ capture do
118
+ concat(govuk_pagination_icon(:prev))
119
+ concat(tag.span(govuk_pagination_previous_options[:text] || 'Previous', class: pagination_next_and_previous_classes(block_is_level, govuk_pagination_previous_options)))
120
+ concat(govuk_pagination_icon_label_text(block_is_level, govuk_pagination_previous_options[:lable_text]))
121
+ end
122
+ end
123
+
124
+ # Generates the next link for {govuk_pagination}
125
+ #
126
+ # @param (see govuk_pagination_next_content)
127
+ #
128
+ # @option (see govuk_pagination_next_content)
129
+ #
130
+ # @return [ActiveSupport::SafeBuffer] the HTML for the next link which is used in {govuk_pagination}
131
+
132
+ def govuk_pagination_next(block_is_level, **govuk_pagination_next_options)
133
+ govuk_pagination_next_options[:attributes] ||= {}
134
+ govuk_pagination_next_options[:attributes][:class] = "govuk-link govuk-pagination__link #{'pagination--button_as_link' if govuk_pagination_next_options[:form]}".rstrip
135
+ govuk_pagination_next_options[:attributes][:rel] = 'next'
136
+
137
+ pagination_next_content = govuk_pagination_next_content(block_is_level, **govuk_pagination_next_options)
138
+
139
+ tag.div(class: 'govuk-pagination__next') do
140
+ if govuk_pagination_next_options[:form]
141
+ govuk_pagination_next_options[:form].button(pagination_next_content, **govuk_pagination_next_options[:attributes])
142
+ else
143
+ link_to(pagination_next_content, govuk_pagination_next_options[:href], **govuk_pagination_next_options[:attributes])
144
+ end
145
+ end
146
+ end
147
+
148
+ # Used by {govuk_pagination_next} to generate the next link content
149
+ #
150
+ # @param block_is_level [Boolean] when there are no items, this will be true and will add extra classe
151
+ # to the link to make the next and previous pagination links level
152
+ # @param govuk_pagination_next_options [Hash] options that will be used in customising the HTML
153
+ #
154
+ # @option govuk_pagination_next_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create next button
155
+ # @option govuk_pagination_next_options [String] :href the URL for the link
156
+ # @option govuk_pagination_next_options [String] :text ('Next') the text for the link
157
+ # @option govuk_pagination_next_options [String] :lable_text additional text for the link when the pagination block is level
158
+ # @option govuk_pagination_next_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
159
+ #
160
+ # @return [ActiveSupport::SafeBuffer] the HTML for the next link/button
161
+
162
+ def govuk_pagination_next_content(block_is_level, **govuk_pagination_next_options)
163
+ capture do
164
+ concat(govuk_pagination_icon(:next)) if block_is_level
165
+ concat(tag.span(govuk_pagination_next_options[:text] || 'Next', class: pagination_next_and_previous_classes(block_is_level, govuk_pagination_next_options)))
166
+ concat(govuk_pagination_icon_label_text(block_is_level, govuk_pagination_next_options[:lable_text]))
167
+ concat(govuk_pagination_icon(:next)) unless block_is_level
168
+ end
169
+ end
170
+
171
+ # Returns the classes for the previous and next link/button label
172
+ #
173
+ # @param (see govuk_pagination_next_content)
174
+
175
+ def pagination_next_and_previous_classes(block_is_level, govuk_pagination_options)
176
+ "govuk-pagination__link-title #{'govuk-pagination__link-title--decorated' if block_is_level && !govuk_pagination_options[:lable_text]}".rstrip
177
+ end
178
+
179
+ # Generates the item links for {govuk_pagination}
180
+ #
181
+ # @param (see _govuk_pagination_items)
182
+ # @param form [ActionView::Helpers::FormBuilder] optional form builder used to create the item buttons
183
+ #
184
+ # @return [ActiveSupport::SafeBuffer] the HTML for the lits of items which is used in {govuk_pagination}
185
+
186
+ def govuk_pagination_items(govuk_pagination_items, form:)
187
+ if form
188
+ _govuk_pagination_items(govuk_pagination_items) do |govuk_pagination_item|
189
+ govuk_pagination_item_number_form(form, govuk_pagination_item)
190
+ end
191
+ else
192
+ _govuk_pagination_items(govuk_pagination_items) do |govuk_pagination_item|
193
+ govuk_pagination_item_number_tag(govuk_pagination_item)
194
+ end
195
+ end
196
+ end
197
+
198
+ # Wrapper method used by {govuk_pagination_items}
199
+ # to generate the pagination items HTML
200
+ #
201
+ # @param govuk_pagination_items [Array(Hash)] an array of item hashes for the pagination.
202
+ # There are two types of item:
203
+ # - {govuk_pagination_item_ellipsis ellipsis item}
204
+ # - {govuk_pagination_item_number_tag number item}
205
+ #
206
+ # @yield the pagination number item HTML generated by {govuk_pagination_items}
207
+ #
208
+ # @yieldparam govuk_pagination_item [Hash] the current pagination item in the loop
209
+ #
210
+ # @return [ActiveSupport::SafeBuffer] the HTML for the lits of items which is
211
+ # used in {govuk_pagination_items}
212
+
213
+ def _govuk_pagination_items(govuk_pagination_items)
214
+ tag.ul(class: 'govuk-pagination__list') do
215
+ govuk_pagination_items.each do |govuk_pagination_item|
216
+ case govuk_pagination_item[:type]
217
+ when :ellipsis
218
+ concat(govuk_pagination_item_ellipsis)
219
+ when :number
220
+ concat(yield(govuk_pagination_item))
221
+ end
222
+ end
223
+ end
224
+ end
225
+
226
+ # Generates the icon for:
227
+ # - {govuk_pagination_previous}
228
+ # - {govuk_pagination_next}
229
+ #
230
+ # @param type [Symbol] the type of the pagination icon (+:prev+ or +:next+)
231
+ #
232
+ # @return [ActiveSupport::SafeBuffer]
233
+
234
+ def govuk_pagination_icon(type)
235
+ tag.svg(class: "govuk-pagination__icon govuk-pagination__icon--#{type}", xmlns: 'http://www.w3.org/2000/svg', height: '13', width: '15', aria: { hidden: 'true' }, focusable: 'false', viewBox: '0 0 15 13') do
236
+ tag.path(d: PAGINATION_ICON_PATHS[type])
237
+ end
238
+ end
239
+
240
+ # Generates the label text for:
241
+ # - {govuk_pagination_previous}
242
+ # - {govuk_pagination_next}
243
+ #
244
+ # @param block_is_level [Boolean] when there are no items, this will be true
245
+ # @param label_text [String] the additional text for the link
246
+ #
247
+ # @return [ActiveSupport::SafeBuffer]
248
+
249
+ def govuk_pagination_icon_label_text(block_is_level, label_text)
250
+ return unless block_is_level && label_text
251
+
252
+ concat(tag.span(':', class: 'govuk-visually-hidden'))
253
+ concat(tag.span(label_text, class: 'govuk-pagination__link-label'))
254
+ end
255
+
256
+ # Generates the ellipsis HTML for {govuk_pagination_items}
257
+ #
258
+ # @return [ActiveSupport::SafeBuffer] the HTML for an ellipsis item which is used in {govuk_pagination_items}
259
+
260
+ def govuk_pagination_item_ellipsis
261
+ tag.li('⋯', class: 'govuk-pagination__item govuk-pagination__item--ellipses')
262
+ end
263
+
264
+ # Generates the number item HTML for {govuk_pagination_items}
265
+ #
266
+ # @param (see _govuk_pagination_item_number)
267
+ #
268
+ # @option (see _govuk_pagination_item_number)
269
+ #
270
+ # @return [ActiveSupport::SafeBuffer] the HTML for an number item which is used in {govuk_pagination_items}
271
+
272
+ def govuk_pagination_item_number_tag(govuk_pagination_item)
273
+ _govuk_pagination_item_number(govuk_pagination_item) do
274
+ link_to(govuk_pagination_item[:number], govuk_pagination_item[:href], **govuk_pagination_item[:attributes])
275
+ end
276
+ end
277
+
278
+ # Generates the number item HTML for {govuk_pagination_items} when there is a ActionView::Helpers::FormBuilder
279
+ #
280
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the item button
281
+ # @param (see _govuk_pagination_item_number)
282
+ #
283
+ # @option (see _govuk_pagination_item_number)
284
+ #
285
+ # @return (see govuk_pagination_item_number_tag)
286
+
287
+ def govuk_pagination_item_number_form(form, govuk_pagination_item)
288
+ govuk_pagination_item[:classes] = 'pagination-number--button_as_link'
289
+
290
+ _govuk_pagination_item_number(govuk_pagination_item) do
291
+ form.button(govuk_pagination_item[:number], **govuk_pagination_item[:attributes])
292
+ end
293
+ end
294
+
295
+ # Wrapper method used by {govuk_pagination_item_number_tag} and {govuk_pagination_item_number_form}
296
+ # to generate the pagination number item HTML
297
+ #
298
+ # @param govuk_pagination_item [Hash] options that will be used in customising the HTML
299
+ #
300
+ # @option govuk_pagination_item [String] :href the URL for the link
301
+ # @option govuk_pagination_item [String] :number the number for the link
302
+ # @option govuk_pagination_item [String] :current is this item the current page
303
+ # @option govuk_pagination_item [Hash] :attributes ({aria: { label: 'Page <NUMBER>' } })
304
+ # any additional attributes that will added as part of the HTML
305
+ #
306
+ # @yield the pagination number item link (when {govuk_pagination_item_number_tag}) or button (when {govuk_pagination_item_number_form})
307
+ #
308
+ # @return [ActiveSupport::SafeBuffer] the HTML for an number item which is used
309
+ # in {govuk_pagination_item_number_tag} or {govuk_pagination_item_number_form}
310
+
311
+ def _govuk_pagination_item_number(govuk_pagination_item, &block)
312
+ initialise_attributes_and_set_classes(govuk_pagination_item, 'govuk-link govuk-pagination__link')
313
+
314
+ (govuk_pagination_item[:attributes][:aria] ||= {})[:label] ||= "Page #{govuk_pagination_item[:number]}"
315
+
316
+ govuk_pagination_list_classes = +'govuk-pagination__item'
317
+
318
+ if govuk_pagination_item[:current]
319
+ govuk_pagination_list_classes << ' govuk-pagination__item--current'
320
+ govuk_pagination_item[:attributes][:aria][:current] = 'page'
321
+ end
322
+
323
+ tag.li(class: govuk_pagination_list_classes, &block)
324
+ end
325
+
326
+ # The paths for the pagination next and previous icons
327
+
328
+ PAGINATION_ICON_PATHS = {
329
+ prev: 'm6.5938-0.0078125-6.7266 6.7266 6.7441 6.4062 1.377-1.449-4.1856-3.9768h12.896v-2h-12.984l4.2931-4.293-1.414-1.414z',
330
+ next: 'm8.107-0.0078125-1.4136 1.414 4.2926 4.293h-12.986v2h12.896l-4.1855 3.9766 1.377 1.4492 6.7441-6.4062-6.7246-6.7266z'
331
+ }.freeze
25
332
  end
333
+ # rubocop:enable Metrics/ModuleLength
26
334
  end
27
335
  end
28
336
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/panel'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
@@ -11,18 +13,37 @@ module CCS
11
13
  # {https://design-system.service.gov.uk/components/panel GDS - Components - Panel}
12
14
 
13
15
  module Panel
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
14
21
  # Generates the HTML for the GOV.UK Panel component
15
22
  #
16
- # @param (see CCS::Components::GovUK::Panel#initialize)
23
+ # @param title_text [String] title text for the panel which will be contained in haeding tags
24
+ # @param panel_text [String] text to use within the panel component
25
+ # @param govuk_panel_options [Hash] options that will be used in customising the HTML
17
26
  #
18
- # @option (see CCS::Components::GovUK::Panel#initialize)
27
+ # @option govuk_panel_options [String] :classes additional CSS classes for the panel HTML
28
+ # @option govuk_panel_options [Integer,String] :heading_level (default: 1) heading level for the panel title text
29
+ # @option govuk_panel_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
19
30
  #
20
- # @yield (see CCS::Components::GovUK::Panel#render)
31
+ # @yield HTML that will be contained within the panel body. Ignored if panel text is given
21
32
  #
22
- # @return (see CCS::Components::GovUK::Panel#render)
33
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Panel
34
+ # which can then be rendered on the page
35
+
36
+ def govuk_panel(title_text, panel_text = nil, **govuk_panel_options)
37
+ initialise_attributes_and_set_classes(govuk_panel_options, 'govuk-panel govuk-panel--confirmation')
23
38
 
24
- def govuk_panel(title_text, panel_text = nil, **options, &block)
25
- Components::GovUK::Panel.new(context: self, title_text: title_text, panel_text: panel_text, **options).render(&block)
39
+ tag.div(**govuk_panel_options[:attributes]) do
40
+ concat(tag.send(:"h#{govuk_panel_options[:heading_level] || 1}", title_text))
41
+ if panel_text || block_given?
42
+ concat(tag.div(class: 'govuk-panel__body') do
43
+ panel_text || yield
44
+ end)
45
+ end
46
+ end
26
47
  end
27
48
  end
28
49
  end