Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate filterbar labels at >= 40 characters #1504

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion js/FilterBarComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ FilterBarComponent.prototype.init = function () {
showFilterBar(component, $filterbar);
populateFilterList($filterbar);
showAddFilterPopover($filterbar);
showHideTruncation(component);
addFilter($filterbar);
removeFilter($filterbar);
clearAllFilters($filterbar);
Expand Down Expand Up @@ -499,7 +500,17 @@ function populateFilterList ($filterbar) {
}

if (filterValue !== '' && filterValue !== null && filterValue.length !== 0) {
$labelContainer.prepend('<span class="label label--large label--inverse label--removable" data-filter-id="' + _.escape(filterId) + '"><span class="label__text">' + _.escape(filterLabel) + _.escape(filterValue) + '</span><button type="button" data-ui="filter-cancel" class="btn remove-button" data-filter-id="'+ _.escape(filterId) +'"><i class="icon-remove-sign"><span class="hide">Remove '+ _.escape(initalLabelText) +' filter</span></i></button></span>');

let value;

if (filterValue.length >= 40) {
value = '<span class="label__truncate--visible">' + _.escape(filterValue).substr(0, 40).trimEnd() + ' <button class="btn btn--small btn--white btn--outline" data-ui="filter-expand" aria-label="Show more: '+_.escape(initalLabelText)+'">more</button></span><span class="label__truncate--invisible u-display-none">' + _.escape(filterValue).substr(40).trimStart() + ' <button class="btn btn--small btn--white btn--outline" data-ui="filter-collapse" class="u-display-none" aria-label="Show less: '+_.escape(initalLabelText)+'">less</button></span>';
}
else {
value = _.escape(filterValue);
}

$labelContainer.prepend('<span class="label label--large label--inverse label--removable" data-filter-id="'+ _.escape(filterId) +'"><span class="label__text">'+ _.escape(filterLabel) + value +'</span><button type="button" data-ui="filter-cancel" class="btn remove-button" data-filter-id="'+ _.escape(filterId) +'"><i class="icon-remove-sign"><span class="hide">Remove '+ _.escape(initalLabelText) +' filter</span></i></button></span>');

// Keep track of how many filters have already been applied
hiddenFormGroups++;
Expand All @@ -525,4 +536,26 @@ function populateFilterList ($filterbar) {
});
}

function showHideTruncation (component) {
component.$html.on('click', '[data-ui="filter-expand"]', function(e) {
e.preventDefault();

let $target = $(this).closest('.label__text');

$target.find('.label__truncate--invisible').removeClass('u-display-none');
$target.find('[data-ui="filter-expand"]').addClass('u-display-none');
$target.find('[data-ui="filter-collapse"]').removeClass('u-display-none').trigger('focus');
});

component.$html.on('click', '[data-ui="filter-collapse"]', function(e) {
e.preventDefault();

let $target = $(this).closest('.label__text');

$target.find('.label__truncate--invisible').addClass('u-display-none');
$target.find('[data-ui="filter-collapse"]').addClass('u-display-none');
$target.find('[data-ui="filter-expand"]').removeClass('u-display-none').trigger('focus');
});
}

module.exports = FilterBarComponent;
19 changes: 18 additions & 1 deletion stylesheets/_component.filterbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
float: right;

@include respond-min($screen-tablet) {
float: none;
// float: none;
}
}
}
Expand Down Expand Up @@ -125,6 +125,23 @@
padding-right: 9em;
vertical-align: top;
}

.label {
padding-right: 28px;
position: relative;
word-break: normal;
word-wrap: break-word;
}

.label__truncate--invisible {
white-space: normal;
}

.remove-button {
position: absolute;
right: 0;
top: 4px;
}
}

.filter-bar__list {
Expand Down
57 changes: 35 additions & 22 deletions tests/js/web/FilterBarComponentTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('FilterBarComponent', function () {
' <option value="small">Small</option>' +
' <option value="medium">Medium</option>' +
' <option value="large">Large</option>' +
' <option value="long">Really long option which should trigger the expanding behaviour</option>' +
' </select>' +
' </div>' +
' </div>' +
Expand All @@ -45,12 +46,12 @@ describe('FilterBarComponent', function () {
' <input id="inStock" type="checkbox" class="form__control checkbox">' +
' </div>' +
' </div>' +
' </fieldset>' +
' <div class="form__actions">' +
' <button type="submit" class="btn btn--primary js-submit-disable">Save</button>' +
' <a href="#" class="btn btn--naked" data-ui="clear-all-filters">Clear</a>' +
' </div>' +
' </form>' +
' </fieldset>' +
' <div class="form__actions">' +
' <button type="submit" class="btn btn--primary js-submit-disable">Save</button>' +
' <a href="#" class="btn btn--naked" data-ui="clear-all-filters">Clear</a>' +
' </div>' +
' </form>' +
'</div>'
).appendTo(this.$body);

Expand All @@ -62,9 +63,9 @@ describe('FilterBarComponent', function () {

afterEach(function () {
this.$body.empty();
});
});

describe('On init', function() {
describe('On init', function() {

beforeEach(function() {
this.filterBar.init();
Expand Down Expand Up @@ -203,19 +204,19 @@ describe('FilterBarComponent', function () {
this.filterBar.init();

this.$container.append(
'<div class="popover">' +
' <ul class="filter-bar__list">' +
' <li>' +
' <a href="#" class="filter-bar__list-item" data-ui="filter-item" data-filter-id="inStock" data-filter-title="In Stock">In Stock</a>' +
' </li>' +
' <li>' +
' <a href="#" class="filter-bar__list-item" data-ui="filter-item" data-filter-id="colour" data-filter-title="Colour">Colour</a>' +
' </li>' +
' </ul>' +
'</div>'
);
'<div class="popover">' +
' <ul class="filter-bar__list">' +
' <li>' +
' <a href="#" class="filter-bar__list-item" data-ui="filter-item" data-filter-id="inStock" data-filter-title="In Stock">In Stock</a>' +
' </li>' +
' <li>' +
' <a href="#" class="filter-bar__list-item" data-ui="filter-item" data-filter-id="colour" data-filter-title="Colour">Colour</a>' +
' </li>' +
' </ul>' +
'</div>'
);

this.$showFilterListButton = this.$container.find('[data-ui="show-filter-list"]');
this.$showFilterListButton = this.$container.find('[data-ui="show-filter-list"]');
});

it('should prevent the default behavior', function () {
Expand All @@ -240,7 +241,7 @@ describe('FilterBarComponent', function () {
describe('If the filter field type is a checkbox', function() {

beforeEach(function() {
this.$popoverFilterLink = this.$container.find('.filter-bar__list [data-filter-id="inStock"]');
this.$popoverFilterLink = this.$container.find('.filter-bar__list [data-filter-id="inStock"]');
});

it('should add a label to the filter bar for the clicked filter', function () {
Expand Down Expand Up @@ -509,7 +510,7 @@ describe('FilterBarComponent', function () {
this.clickEvent2 = $.Event('click');
this.resetForm = sinon.spy();
this.$form = this.$container.find('form');
this.$form[0].reset = this.resetForm;
this.$form[0].reset = this.resetForm;

this.filterBar.init();

Expand Down Expand Up @@ -547,6 +548,18 @@ describe('FilterBarComponent', function () {
});
});

describe('when a filter form has a long value on page load', function() {
beforeEach(function() {
this.$container.find('#size').val('long');
});

it('truncate the label at 40 characters', function () {
this.filterBar.init();

expect(this.$container.find('span[data-filter-id="size"] .label__text').html()).to.be.equal('Size: <span class="label__truncate--visible">Really long option which should trigger <button class="btn btn--small btn--white btn--outline" data-ui="filter-expand" aria-label="Show more: Size">more</button></span><span class="label__truncate--invisible u-display-none">the expanding behaviour <button class="btn btn--small btn--white btn--outline" data-ui="filter-collapse" aria-label="Show less: Size">less</button></span>');
});
});

describe('when a filter form has a value on page load', function() {
beforeEach(function() {
this.$container.find('#foo').val('some value');
Expand Down