Skip to content

Commit

Permalink
Make shortlist button focusable
Browse files Browse the repository at this point in the history
This fix allows users using keyboards to be able to focus the label.btn--shortlist element to shortlist a report.
  • Loading branch information
lucascumsille committed Nov 4, 2024
1 parent bd545ee commit a6ebf9a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
35 changes: 11 additions & 24 deletions templates/web/base/report/_main.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,6 @@
<input type="hidden" name="id" value="[% problem.id %]">
<input type="hidden" name="token" value="[% csrf_token %]">
<input type="hidden" name="[% IF c.user.is_planned_report(problem) %]shortlist-remove[% ELSE %]shortlist-add[% END %]" value="1">
<p><input
type="submit"
id="shortlist-report"
data-label-remove="[% loc('Remove from shortlist') %]"
data-label-add="[% loc('Add to shortlist') %]"
data-value-remove="[% loc('Shortlisted') %]"
data-value-add="[% loc('Shortlist') %]"
data-class-remove="btn--shortlisted"
data-class-add="btn--shortlist"
[%~ IF c.user.is_planned_report(problem) ~%]
value="[% loc('Shortlisted') %]"
aria-label="[% loc('Remove from shortlist') %]"
class="btn--shortlisted"
[%~ ELSE ~%]
value="[% loc('Shortlist') %]"
aria-label="[% loc('Add to shortlist') %]"
class="btn--shortlist"
[%~ END ~%]
></p>
</form>
[% END %]

Expand Down Expand Up @@ -130,11 +111,17 @@ <h1>[% problem.title | html %]</h1>
aria-label="[% loc('Moderate this report') %]">[% loc('Moderate') %]</a>
[% END %]
[% IF permissions.planned_reports %]
[%~ IF c.user.is_planned_report(problem) ~%]
<label class="btn btn--shortlisted" for="shortlist-report" role="menuitem" aria-label="[% loc('Remove from shortlist') %]">[% loc('Shortlisted') %]</label>
[%~ ELSE ~%]
<label class="btn btn--shortlist" for="shortlist-report" role="menuitem" aria-label="[% loc('Add to shortlist') %]">[% loc('Shortlist') %]</label>
[%~ END ~%]
<button class="btn [% IF c.user.is_planned_report(problem) %]btn--shortlisted[% ELSE %]btn--shortlist[% END %]"
form="planned_form"
aria-label="[% IF c.user.is_planned_report(problem) %][% loc('Remove from shortlist') %][% ELSE %][% loc('Add to shortlist') %][% END %]"
data-label-remove="[% loc('Remove from shortlist') %]"
data-value-remove="[% loc('Shortlisted') %]"
data-class-remove="btn--shortlisted"
data-label-add="[% loc('Add to shortlist') %]"
data-value-add="[% loc('Shortlist') %]"
data-class-add="btn--shortlist">
[% IF c.user.is_planned_report(problem) %][% loc('Shortlisted') %][% ELSE %][% loc('Shortlist') %][% END %]
</button>
[% END %]
</div>
[% END %]
Expand Down
39 changes: 21 additions & 18 deletions web/cobrands/fixmystreet/fixmystreet.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,38 +509,41 @@ $.extend(fixmystreet.set_up, {
$('#planned_form').on('submit', function(e) {
if (e.metaKey || e.ctrlKey) {
return;
}
}

e.preventDefault();
var $form = $(this),
$submit = $form.find("input[type='submit']" ),
$labels = $('label[for="' + $submit.attr('id') + '"]'),
problemId = $form.find("input[name='id']").val(),
data = $form.serialize() + '&ajax=1',
changeValue,
buttonLabel,
buttonValue,
classToAdd,
classToRemove;
var $form = $(this);
var $submit = $('button[form="planned_form"]');
var problemId = $form.find("input[name='id']").val();
var data = $form.serialize() + '&ajax=1';

Check warning on line 518 in web/cobrands/fixmystreet/fixmystreet.js

View check run for this annotation

Codecov / codecov/patch

web/cobrands/fixmystreet/fixmystreet.js#L515-L518

Added lines #L515 - L518 were not covered by tests

$.post(this.action, data, function(data) {
if (data.outcome == 'add') {
$form.find("input[name='shortlist-add']" ).attr('name', 'shortlist-remove');
var inputName, newInputName, buttonLabel, buttonValue, classToAdd, classToRemove;

if (data.outcome === 'add') {
inputName = 'shortlist-add';
newInputName = 'shortlist-remove';

Check warning on line 525 in web/cobrands/fixmystreet/fixmystreet.js

View check run for this annotation

Codecov / codecov/patch

web/cobrands/fixmystreet/fixmystreet.js#L523-L525

Added lines #L523 - L525 were not covered by tests
buttonLabel = $submit.data('label-remove');
buttonValue = $submit.data('value-remove');
classToAdd = $submit.data('class-remove');
classToRemove = $submit.data('class-add');
$('.shortlisted-status').remove();
$(document).trigger('shortlist-add', problemId);
} else if (data.outcome == 'remove') {
$form.find("input[name='shortlist-remove']" ).attr('name', 'shortlist-add');
} else if (data.outcome === 'remove') {
inputName = 'shortlist-remove';
newInputName = 'shortlist-add';

Check warning on line 534 in web/cobrands/fixmystreet/fixmystreet.js

View check run for this annotation

Codecov / codecov/patch

web/cobrands/fixmystreet/fixmystreet.js#L532-L534

Added lines #L532 - L534 were not covered by tests
buttonLabel = $submit.data('label-add');
buttonValue = $submit.data('value-add');
$(document).trigger('shortlist-remove', problemId);
classToAdd = $submit.data('class-add');
classToRemove = $submit.data('class-remove');
$(document).trigger('shortlist-remove', problemId);

Check warning on line 539 in web/cobrands/fixmystreet/fixmystreet.js

View check run for this annotation

Codecov / codecov/patch

web/cobrands/fixmystreet/fixmystreet.js#L539

Added line #L539 was not covered by tests
}
$submit.val(buttonValue).attr('aria-label', buttonLabel).removeClass(classToRemove).addClass(classToAdd);
$labels.text(buttonValue).attr('aria-label', buttonLabel).removeClass(classToRemove).addClass(classToAdd);

$form.find("input[name='" + inputName + "']").attr('name', newInputName);
$submit.text(buttonValue)

Check warning on line 543 in web/cobrands/fixmystreet/fixmystreet.js

View check run for this annotation

Codecov / codecov/patch

web/cobrands/fixmystreet/fixmystreet.js#L542-L543

Added lines #L542 - L543 were not covered by tests
.attr('aria-label', buttonLabel)
.removeClass(classToRemove)
.addClass(classToAdd);
});
});
},
Expand Down

0 comments on commit a6ebf9a

Please sign in to comment.