-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from alphagov/iterate-subscription-links-comp…
…onent Modify subscription links component
- Loading branch information
Showing
8 changed files
with
249 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/assets/javascripts/govuk_publishing_components/lib/toggle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Toggle the display of elements | ||
This is a straight copy of the same file from static. | ||
Use `data-controls` and `data-expanded` to indicate the | ||
starting state and the IDs of the elements that the toggle | ||
controls. This is synonymous with ARIA attributes, which | ||
get added when starting. | ||
*/ | ||
|
||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
'use strict' | ||
|
||
Modules.GemToggle = function () { | ||
this.start = function ($el) { | ||
var toggleSelector = '[data-controls][data-expanded]' | ||
|
||
$el.on('click', toggleSelector, toggle) | ||
$el.find(toggleSelector).each(addAriaAttrs) | ||
|
||
// Add the ARIA attributes with JavaScript | ||
// If the JS fails and there's no interactive elements, having | ||
// no aria attributes is an accurate representation. | ||
function addAriaAttrs () { | ||
var $toggle = $(this) | ||
$toggle.attr('role', 'button') | ||
$toggle.attr('aria-controls', $toggle.data('controls')) | ||
$toggle.attr('aria-expanded', $toggle.data('expanded')) | ||
|
||
var $targets = getTargetElements($toggle) | ||
$targets.attr('aria-live', 'polite') | ||
$targets.attr('role', 'region') | ||
$toggle.data('$targets', $targets) | ||
} | ||
|
||
function toggle (event) { | ||
var $toggle = $(event.target), | ||
expanded = $toggle.attr('aria-expanded') === 'true', | ||
$targets = $toggle.data('$targets') | ||
|
||
if (expanded) { | ||
$toggle.attr('aria-expanded', false) | ||
$targets.addClass('js-hidden') | ||
} else { | ||
$toggle.attr('aria-expanded', true) | ||
$targets.removeClass('js-hidden') | ||
} | ||
|
||
var toggledText = $toggle.data('toggled-text') | ||
if (typeof toggledText === 'string') { | ||
$toggle.data('toggled-text', $toggle.text()) | ||
$toggle.text(toggledText) | ||
} | ||
|
||
event.preventDefault() | ||
} | ||
|
||
function getTargetElements ($toggle) { | ||
var ids = $toggle.attr('aria-controls').split(' '), | ||
selector = '#' + ids.join(', #') | ||
|
||
return $el.find(selector) | ||
} | ||
} | ||
} | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 35 additions & 6 deletions
41
app/views/govuk_publishing_components/components/_subscription-links.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,50 @@ | ||
<% | ||
email_signup_link ||= false | ||
email_signup_link_text ||= t("govuk_component.subscription_links.email_signup_link_text", default: "Get email alerts") | ||
feed_link ||= false | ||
feed_link_text ||= t("govuk_component.subscription_links.feed_link_text", default: "Subscribe to feed") | ||
feed_link_box_value ||= false | ||
feed_link_data = false | ||
|
||
if feed_link_box_value | ||
feed_link = "#" | ||
feed_link_data = { | ||
controls: "feed-reader", | ||
expanded: "false" | ||
} | ||
end | ||
%> | ||
<% if email_signup_link || feed_link %> | ||
<section class="gem-c-subscription-links"> | ||
<h2 class="visuallyhidden">Subscriptions</h2> | ||
<% if email_signup_link || feed_link || feed_link_box_value %> | ||
<section class="gem-c-subscription-links" data-module="gem-toggle"> | ||
<h2 class="visuallyhidden"><%= t("govuk_component.subscription_links.subscriptions", default: "Subscriptions") %></h2> | ||
<ul class="gem-c-subscription-links__list"> | ||
<% if email_signup_link.present? %> | ||
<li class="gem-c-subscription-links__list-item"> | ||
<%= link_to "Get email alerts", email_signup_link, class: "gem-c-subscription-links__link gem-c-subscription-links__link--email-alerts" %> | ||
<%= link_to email_signup_link_text, email_signup_link, class: "gem-c-subscription-links__link gem-c-subscription-links__link--email-alerts" %> | ||
</li> | ||
<% end %> | ||
<% if feed_link.present? %> | ||
|
||
<% if feed_link_box_value || feed_link.present? %> | ||
<li class="gem-c-subscription-links__list-item"> | ||
<%= link_to "Subscribe to feed", feed_link, class: "gem-c-subscription-links__link gem-c-subscription-links__link--feed" %> | ||
<%= link_to feed_link_text, feed_link, | ||
class: "gem-c-subscription-links__link gem-c-subscription-links__link--feed", | ||
data: feed_link_data | ||
%> | ||
</li> | ||
<% end %> | ||
</ul> | ||
|
||
<% if feed_link_box_value %> | ||
<div class="gem-c-subscription-links__feed-box js-hidden" id="feed-reader"> | ||
<p class="gem-c-subscription-links__feed-description js-hidden"><%= feed_link_text %></p> | ||
<%= render "govuk_publishing_components/components/input", { | ||
label: { | ||
text: "Copy and paste this URL into your feed reader" | ||
}, | ||
name: "feed-reader-box", | ||
value: feed_link_box_value | ||
} %> | ||
</div> | ||
<% end %> | ||
</section> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
describe('A toggle module', function () { | ||
'use strict' | ||
|
||
var toggle, | ||
element | ||
|
||
beforeEach(function () { | ||
toggle = new GOVUK.Modules.GemToggle() | ||
}) | ||
|
||
describe('when starting', function () { | ||
var element = $('\ | ||
<div>\ | ||
<a href="#" class="my-toggle" data-expanded="false" data-controls="target">Toggle</a>\ | ||
<div id="target">Target</div>\ | ||
</div>') | ||
|
||
it('adds aria attributes to toggles', function () { | ||
toggle.start(element) | ||
|
||
var $toggle = element.find('.my-toggle') | ||
expect($toggle.attr('role')).toBe('button') | ||
expect($toggle.attr('aria-expanded')).toBe('false') | ||
expect($toggle.attr('aria-controls')).toBe('target') | ||
}) | ||
}) | ||
|
||
describe('when clicking a toggle', function () { | ||
var element | ||
|
||
beforeEach(function () { | ||
element = $('\ | ||
<div>\ | ||
<a href="#" class="my-toggle" data-expanded="false" data-controls="target" data-toggled-text="Show fewer">Toggle</a>\ | ||
<div id="target" class="js-hidden">Target</div>\ | ||
</div>') | ||
|
||
toggle.start(element) | ||
element.find('.my-toggle').trigger('click') | ||
}) | ||
|
||
it('toggles the display of a target', function () { | ||
expect(element.find('#target').is('.js-hidden')).toBe(false) | ||
element.find('.my-toggle').trigger('click') | ||
expect(element.find('#target').is('.js-hidden')).toBe(true) | ||
}) | ||
|
||
it('updates the aria-expanded attribute on the toggle', function () { | ||
expect(element.find('.my-toggle').attr('aria-expanded')).toBe('true') | ||
|
||
element.find('.my-toggle').trigger('click') | ||
expect(element.find('.my-toggle').attr('aria-expanded')).toBe('false') | ||
}) | ||
|
||
it('updates the text shown in the toggle link when expanded if such text is supplied', function () { | ||
expect(element.find('.my-toggle').data('toggled-text')).toBe('Toggle') | ||
expect(element.find('.my-toggle').text()).toBe('Show fewer') | ||
element.find('.my-toggle').trigger('click') | ||
expect(element.find('.my-toggle').data('toggled-text')).toBe('Show fewer') | ||
expect(element.find('.my-toggle').text()).toBe('Toggle') | ||
}) | ||
}) | ||
|
||
describe('when clicking a toggle that controls multiple targets', function () { | ||
it('toggles the display of each target', function () { | ||
var element = $('\ | ||
<div>\ | ||
<a href="#" class="my-toggle" data-expanded="false" data-controls="target another-target">Toggle</a>\ | ||
<div id="target" class="js-hidden">Target</div>\ | ||
<div id="another-target" class="js-hidden">Another target</div>\ | ||
</div>') | ||
|
||
toggle.start(element) | ||
expect(element.find('#target').is('.js-hidden')).toBe(true) | ||
expect(element.find('#another-target').is('.js-hidden')).toBe(true) | ||
|
||
element.find('.my-toggle').trigger('click') | ||
expect(element.find('#target').is('.js-hidden')).toBe(false) | ||
expect(element.find('#another-target').is('.js-hidden')).toBe(false) | ||
|
||
element.find('.my-toggle').trigger('click') | ||
expect(element.find('#target').is('.js-hidden')).toBe(true) | ||
expect(element.find('#another-target').is('.js-hidden')).toBe(true) | ||
}) | ||
}) | ||
}) |