govuk_publishing_components 27.13.0 → 27.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21c2a281f7de0f79303971394f9cad3102533d7e619f660809078457687552ce
4
- data.tar.gz: b246c494a88448ab43d2d2230c12a918b2107eda3217f4f44754ee5b2ef0f4de
3
+ metadata.gz: 59c59e65f1434263c7e36d4d89029083fe241185daba81730010af5b865ccabc
4
+ data.tar.gz: c4b9a4d0a73f9e0dd3068aeade2e04565e2d69b4739c9dc00c3a820c631089b3
5
5
  SHA512:
6
- metadata.gz: a1a5354695ffc567bfa7547af429dfdfa0145e8c009408a4a8200731e2677f883922b228dd4d3623770483ba704eafecfe68c1a895bf2dc6b978ac3da59b5c7c
7
- data.tar.gz: 344cee88bde5f7c64393028e7020dc62b62eb8794b568b05ecbf94746bb94381bf44cba4627332d8475fadb49cbdc1f79f51598bd7c7194c17eb157a925a0176
6
+ metadata.gz: 46b643cba9fcd36653300e4a11679f882eb1f1b5482e4811c38f580e6ad4526c60a3d2f61ecf9a4df13f5abef2c9d4b954ae4cec959159efced1c011b9dd458f
7
+ data.tar.gz: cd46ecdd5268be36578fecef171b2b86e7b000c750fbcc9e9000a51485e58559d14d9c9a32e9e623fff757f58559a90bab99f68aa87159bedc9737e3b29d082b
@@ -0,0 +1,48 @@
1
+ /* global XMLHttpRequest */
2
+ window.GOVUK = window.GOVUK || {}
3
+ window.GOVUK.Modules = window.GOVUK.Modules || {};
4
+
5
+ (function (Modules) {
6
+ function SinglePageNotificationButton ($module) {
7
+ this.$module = $module
8
+ this.basePath = this.$module.querySelector('input[name="base_path"]').value
9
+ this.buttonLocation = this.$module.getAttribute('data-button-location')
10
+
11
+ this.personalisationEndpoint = '/api/personalisation/check-email-subscription?base_path=' + this.basePath
12
+ // This attribute is passed through to the personalisation API to ensure the updated button has the same button_location for analytics
13
+ if (this.buttonLocation) this.personalisationEndpoint += '&button_location=' + this.buttonLocation
14
+ }
15
+
16
+ SinglePageNotificationButton.prototype.init = function () {
17
+ var xhr = new XMLHttpRequest()
18
+ xhr.open('GET', this.personalisationEndpoint, true)
19
+
20
+ xhr.onreadystatechange = function () {
21
+ if (xhr.readyState === 4 && xhr.status === 200) {
22
+ var responseText = xhr.responseText
23
+ // if response text exists and is JSON parse-able, parse the response and get the button html
24
+ if (responseText && this.responseIsJSON(responseText)) {
25
+ var newButton = JSON.parse(responseText).button_html
26
+ var html = document.createElement('div')
27
+ html.innerHTML = newButton
28
+ // test that the html returned contains the button component; if yes, swap the button for the updated version
29
+ var responseHasButton = html.querySelector('form.gem-c-single-page-notification-button .gem-c-single-page-notification-button__submit')
30
+ if (responseHasButton) {
31
+ this.$module.outerHTML = newButton
32
+ }
33
+ }
34
+ }
35
+ }.bind(this)
36
+ xhr.send()
37
+ }
38
+
39
+ SinglePageNotificationButton.prototype.responseIsJSON = function (string) {
40
+ try {
41
+ JSON.parse(string)
42
+ } catch (e) {
43
+ return false
44
+ }
45
+ return true
46
+ }
47
+ Modules.SinglePageNotificationButton = SinglePageNotificationButton
48
+ })(window.GOVUK.Modules)
@@ -1,23 +1,17 @@
1
1
  <%
2
- page ||= ''
3
- data_attributes ||= {}
4
- base_path ||= nil
5
- local_assigns[:margin_bottom] ||= 3
6
- already_subscribed ||= false
7
- text ||= already_subscribed ? t('components.single_page_notification_button.unsubscribe_text') : t('components.single_page_notification_button.subscribe_text')
8
-
2
+ component_helper = GovukPublishingComponents::Presenters::SinglePageNotificationButtonHelper.new(local_assigns)
9
3
  shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
4
+
10
5
  wrapper_classes = %w(gem-c-single-page-notification-button govuk-!-display-none-print)
11
6
  wrapper_classes << shared_helper.get_margin_bottom
12
- classes = "govuk-body-s gem-c-single-page-notification-button__submit"
13
7
  %>
14
8
  <% button_text = capture do %>
15
- <svg class="gem-c-single-page-notification-button__icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" height="18" width="18" viewBox="0 0 459.334 459.334"><path fill="currentColor" d="M177.216 404.514c-.001.12-.009.239-.009.359 0 30.078 24.383 54.461 54.461 54.461s54.461-24.383 54.461-54.461c0-.12-.008-.239-.009-.359H175.216zM403.549 336.438l-49.015-72.002v-89.83c0-60.581-43.144-111.079-100.381-122.459V24.485C254.152 10.963 243.19 0 229.667 0s-24.485 10.963-24.485 24.485v27.663c-57.237 11.381-100.381 61.879-100.381 122.459v89.83l-49.015 72.002a24.76 24.76 0 0 0 20.468 38.693H383.08a24.761 24.761 0 0 0 20.469-38.694z"/></svg><%= text %>
9
+ <svg class="gem-c-single-page-notification-button__icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" height="18" width="18" viewBox="0 0 459.334 459.334"><path fill="currentColor" d="M177.216 404.514c-.001.12-.009.239-.009.359 0 30.078 24.383 54.461 54.461 54.461s54.461-24.383 54.461-54.461c0-.12-.008-.239-.009-.359H175.216zM403.549 336.438l-49.015-72.002v-89.83c0-60.581-43.144-111.079-100.381-122.459V24.485C254.152 10.963 243.19 0 229.667 0s-24.485 10.963-24.485 24.485v27.663c-57.237 11.381-100.381 61.879-100.381 122.459v89.83l-49.015 72.002a24.76 24.76 0 0 0 20.468 38.693H383.08a24.761 24.761 0 0 0 20.469-38.694z"/></svg><%= component_helper.button_text %>
16
10
  <% end %>
17
- <%= tag.form class: wrapper_classes, action: "/email/subscriptions/single-page/new", method: "POST", data: data_attributes do %>
18
- <input type="hidden" name="base_path" value="<%= base_path %>">
11
+ <%= tag.form class: wrapper_classes, action: "/email/subscriptions/single-page/new", method: "POST", data: component_helper.data do %>
12
+ <input type="hidden" name="base_path" value="<%= component_helper.base_path %>">
19
13
  <%= content_tag(:button, button_text, {
20
- class: classes,
14
+ class: "govuk-body-s gem-c-single-page-notification-button__submit",
21
15
  type: "submit",
22
16
  }) %>
23
- <% end if base_path.presence %>
17
+ <% end if component_helper.base_path %>
@@ -1,9 +1,10 @@
1
1
  name: Single page notification button
2
2
  description: A button that subscribes the user to email notifications to a page
3
3
  body: |
4
- By default, the component displays with the "Get emails about this page" state. The component does not render without the `base_path` parameter.
4
+ By default, the component displays with the "Get emails about this page" state.
5
+ If the `js-enhancement` flag is present, the component uses JavaScript to check if the user has already subscribed to email notifications on the current page. If yes, the state of the component updates accordingly.
5
6
 
6
- The `base_path` is necessary for [checking if an email subscription is active on page load](https://github.com/alphagov/account-api/blob/main/docs/api.md#get-apipersonalisationcheck-email-subscriptiontopic_slug) and the creation/deletion of an email notification subscription.
7
+ The component does not render without the `base_path` parameter. The `base_path` is necessary for [checking if an email subscription is active on page load](https://github.com/alphagov/account-api/blob/main/docs/api.md#get-apipersonalisationcheck-email-subscriptiontopic_slug) and the creation/deletion of an email notification subscription.
7
8
 
8
9
  When the button is clicked, the `base_path` is submitted to an endpoint which proceeds to check the user's authentication status and whether they are already subscribed to the page or not. Depending on these factors, they will be routed accordingly.
9
10
  accessibility_criteria: |
@@ -23,10 +24,27 @@ examples:
23
24
  data:
24
25
  base_path: '/current-page-path'
25
26
  data_attributes:
26
- category: fancyButtons
27
+ test_attribute: "testing"
27
28
  with_margin_bottom:
28
29
  description: |
29
30
  The component accepts a number for margin bottom from 0 to 9 (0px to 60px) using the [GOV.UK Frontend spacing scale](https://design-system.service.gov.uk/styles/spacing/#the-responsive-spacing-scale). It defaults to having a margin bottom of 15px.
30
31
  data:
31
32
  base_path: '/current-page-path'
32
33
  margin_bottom: 5
34
+ with_js_enhancement:
35
+ description: |
36
+ If the `js-enhancement` flag is present, the component uses JavaScript to check if the user has already subscribed to email notifications on the current page. If yes, the state of the component updates accordingly.
37
+ data:
38
+ base_path: '/current-page-path'
39
+ js_enhancement: true
40
+ with_button_location:
41
+ description: |
42
+ When there is more than one button on a page, we should specify their location so that Analytics can differentiate between them.
43
+
44
+ The location should have one of two values: "top" or "bottom".
45
+
46
+ When this parameter is passed, its value is reflected in the `data-action` attribute (i.e "Unsubscribe-button-top"). When the flag is not present, `data-action` defaults to "Subscribe-button" or "Unsubscribe-button", depending on the state of the button.
47
+ data:
48
+ base_path: '/current-page-path'
49
+ js_enhancement: true
50
+ button_location: 'top'
@@ -0,0 +1,40 @@
1
+ module GovukPublishingComponents
2
+ module Presenters
3
+ class SinglePageNotificationButtonHelper
4
+ attr_reader :already_subscribed, :data_attributes, :base_path, :js_enhancement, :button_type, :button_location
5
+
6
+ def initialize(local_assigns)
7
+ @local_assigns = local_assigns
8
+ @data_attributes = @local_assigns[:data_attributes] || {}
9
+ @js_enhancement = @local_assigns[:js_enhancement] || false
10
+ @already_subscribed = @local_assigns[:already_subscribed] || false
11
+ @base_path = @local_assigns[:base_path] || nil
12
+ @button_location = button_location_is_valid? ? @local_assigns[:button_location] : nil
13
+ @button_type = @local_assigns[:already_subscribed] ? "Unsubscribe" : "Subscribe"
14
+ end
15
+
16
+ def data
17
+ module_names = %w[gem-track-click]
18
+ module_names << "single-page-notification-button" if js_enhancement
19
+
20
+ @data_attributes[:label] = base_path
21
+ # data-action for tracking should have the format of e.g. "Unsubscribe-button-top", or "Subscribe-button-bottom"
22
+ # when button_location is not present data-action will fall back to "Unsubscribe-button"/"Subscribe-button"
23
+ @data_attributes[:action] = [button_type, "button", button_location].compact.join("-")
24
+ @data_attributes[:module] = module_names.join(" ")
25
+ @data_attributes[:category] = "Single-page-notification-button"
26
+ # This attribute is passed through to the personalisation API to ensure when a new button is returned from the API, it has the same button_location
27
+ @data_attributes[:button_location] = button_location
28
+ @data_attributes
29
+ end
30
+
31
+ def button_location_is_valid?
32
+ %w[bottom top].include? @local_assigns[:button_location]
33
+ end
34
+
35
+ def button_text
36
+ @already_subscribed ? I18n.t("components.single_page_notification_button.unsubscribe_text") : I18n.t("components.single_page_notification_button.subscribe_text")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module GovukPublishingComponents
2
- VERSION = "27.13.0".freeze
2
+ VERSION = "27.14.0".freeze
3
3
  end
@@ -33,6 +33,7 @@ require "govuk_publishing_components/presenters/intervention_helper"
33
33
  require "govuk_publishing_components/presenters/organisation_logo_helper"
34
34
  require "govuk_publishing_components/presenters/highlight_boxes_helper"
35
35
  require "govuk_publishing_components/presenters/taxonomy_list_helper"
36
+ require "govuk_publishing_components/presenters/single_page_notification_button_helper"
36
37
 
37
38
  require "govuk_publishing_components/app_helpers/taxon_breadcrumbs"
38
39
  require "govuk_publishing_components/app_helpers/table_helper"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_publishing_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 27.13.0
4
+ version: 27.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-17 00:00:00.000000000 Z
11
+ date: 2021-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: govuk_app_config
@@ -501,6 +501,7 @@ files:
501
501
  - app/assets/javascripts/govuk_publishing_components/components/radio.js
502
502
  - app/assets/javascripts/govuk_publishing_components/components/reorderable-list.js
503
503
  - app/assets/javascripts/govuk_publishing_components/components/show-password.js
504
+ - app/assets/javascripts/govuk_publishing_components/components/single-page-notification-button.js
504
505
  - app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js
505
506
  - app/assets/javascripts/govuk_publishing_components/components/tabs.js
506
507
  - app/assets/javascripts/govuk_publishing_components/dependencies.js
@@ -968,6 +969,7 @@ files:
968
969
  - lib/govuk_publishing_components/presenters/schema_org.rb
969
970
  - lib/govuk_publishing_components/presenters/select.rb
970
971
  - lib/govuk_publishing_components/presenters/shared_helper.rb
972
+ - lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb
971
973
  - lib/govuk_publishing_components/presenters/step_by_step_nav_helper.rb
972
974
  - lib/govuk_publishing_components/presenters/subscription_links_helper.rb
973
975
  - lib/govuk_publishing_components/presenters/taxonomy_list_helper.rb