Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #12 from unchartedsoftware/remove-group-get-group
Browse files Browse the repository at this point in the history
Add 'removeGroup' and 'getGroup' methods to Facet object.
  • Loading branch information
kbirk authored Jul 4, 2017
2 parents b851708 + 8e5e705 commit 0d08f1a
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Facets Component developed by Uncharted Software",
"repository": "https://github.com/unchartedsoftware/stories-facets",
"license": "Apache-2.0",
"version": "2.6.1",
"version": "2.7.0",
"devDependencies": {
"body-parser": "~1.13.2",
"browserify": "^12.0.1",
Expand Down
46 changes: 31 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Facets.prototype.select = function(subgroups, isQuery) {
var queriesInitialized = false;

subgroups.forEach(function(groupSpec) {
var group = this._getGroup(groupSpec.key);
var group = this.getGroup(groupSpec.key);
if (!isQuery && group) {
if (!groupsInitialized) {
// Initialize selection state
Expand Down Expand Up @@ -107,7 +107,7 @@ Facets.prototype.deselect = function(simpleGroups) {
});
} else {
simpleGroups.forEach(function(simpleGroup) {
var group = this._getGroup(simpleGroup.key);
var group = this.getGroup(simpleGroup.key);
if (group) {
if ('value' in simpleGroup) {
var facet = group._getFacet(simpleGroup.value);
Expand Down Expand Up @@ -143,7 +143,7 @@ Facets.prototype.replace = function(groups, queries, noTransition) {
* @param {Object} group - An object describing the information of the new group.
*/
Facets.prototype.replaceGroup = function(group) {
var existingGroup = this._getGroup(group.key);
var existingGroup = this.getGroup(group.key);
if (existingGroup) {
existingGroup.replace(group);
this._bindClientEvents();
Expand All @@ -159,7 +159,7 @@ Facets.prototype.replaceGroup = function(group) {
*/
Facets.prototype.highlight = function(simpleGroups, isQuery) {
simpleGroups.forEach(function(simpleGroup) {
var group = this._getGroup(simpleGroup.key);
var group = this.getGroup(simpleGroup.key);
if (!isQuery && group) {
group.highlight(simpleGroup.value);
} else {
Expand All @@ -181,7 +181,7 @@ Facets.prototype.highlight = function(simpleGroups, isQuery) {
Facets.prototype.createBadges = function(simpleGroups, isQuery) {

simpleGroups.forEach(function(simpleGroup) {
var group = this._getGroup(simpleGroup.key);
var group = this.getGroup(simpleGroup.key);
if (!isQuery && group) {
this._badgeGroup._createBadge(simpleGroup);
} else {
Expand All @@ -208,7 +208,7 @@ Facets.prototype.removeBadges = function(simpleGroups, isQuery) {
this._badgeGroup._removeAllBadges();
} else {
simpleGroups.forEach(function(simpleGroup) {
var group = this._getGroup(simpleGroup.key);
var group = this.getGroup(simpleGroup.key);
if (!isQuery && group) {
this._badgeGroup._removeBadge(simpleGroup.key, simpleGroup.value);
} else {
Expand All @@ -231,7 +231,7 @@ Facets.prototype.removeBadges = function(simpleGroups, isQuery) {
Facets.prototype.unhighlight = function(simpleGroups, isQuery) {
if (arguments.length > 0) {
simpleGroups.forEach(function(simpleGroup) {
var group = this._getGroup(simpleGroup.key);
var group = this.getGroup(simpleGroup.key);
if (!isQuery && group) {
group.unhighlight(simpleGroup.value);
} else {
Expand All @@ -255,7 +255,7 @@ Facets.prototype.unhighlight = function(simpleGroups, isQuery) {
* @returns {boolean}
*/
Facets.prototype.isHighlighted = function(simpleGroup, isQuery) {
var group = this._getGroup(simpleGroup.key);
var group = this.getGroup(simpleGroup.key);
if (!isQuery && group) {
return group.isHighlighted(simpleGroup.value);
} else {
Expand All @@ -275,7 +275,7 @@ Facets.prototype.isHighlighted = function(simpleGroup, isQuery) {
* @returns {boolean}
*/
Facets.prototype.isCollapsed = function(key) {
var group = this._getGroup(key);
var group = this.getGroup(key);
if (group) {
return group.collapsed;
}
Expand All @@ -291,7 +291,7 @@ Facets.prototype.isCollapsed = function(key) {
* @returns {Object|null}
*/
Facets.prototype.getFilterRange = function(key, value) {
var group = this._getGroup(key);
var group = this.getGroup(key);
if (group) {
return group.getFilterRange(value);
}
Expand All @@ -305,7 +305,7 @@ Facets.prototype.getFilterRange = function(key, value) {
* @param spec the spec values to replace
*/
Facets.prototype.updateSpec = function(groupKey,facetKey,spec) {
var group = this._getGroup(groupKey);
var group = this.getGroup(groupKey);
if (group) {
var facet = group._getFacet(facetKey);
if (facet) {
Expand All @@ -328,7 +328,7 @@ Facets.prototype.append = function(groups, queries) {
// Append groups
if (groups) {
groups.forEach(function(groupSpec) {
existingGroup = this._getGroup(groupSpec.key);
existingGroup = this.getGroup(groupSpec.key);
if (existingGroup) {
existingGroup.append(groupSpec);
} else {
Expand Down Expand Up @@ -356,12 +356,26 @@ Facets.prototype.append = function(groups, queries) {
* @param {*} value - The value of the facet to remove.
*/
Facets.prototype.removeFacet = function(key, value) {
var group = this._getGroup(key);
var group = this.getGroup(key);
if (group) {
group.removeFacet(value);
}
};

/**
* Removes the group with the specified key.
*
* @method removeGroup
* @param {*} key - The key of the group containing the facet to remove.
*/
Facets.prototype.removeGroup = function(key) {
var group = this.getGroup(key);
if (group) {
group.destroy();
this._groups.splice(this._groups.indexOf(group), 1);
}
};

/**
* Adds a query to the query group in this widget.
*
Expand Down Expand Up @@ -486,12 +500,12 @@ Facets.prototype._getQuery = function(key, value) {
/**
* Gets the group with the specified key.
*
* @method _getGroup
* @method getGroup
* @param {string} key - The key of the group to find.
* @returns {Group|null}
* @private
*/
Facets.prototype._getGroup = function(key) {
Facets.prototype.getGroup = function(key) {
var groupObj = this._groups.filter(function(g) {
return g.key === key;
});
Expand All @@ -501,6 +515,8 @@ Facets.prototype._getGroup = function(key) {
return null;
}
};
// alias for backwards compatibility
Facets.prototype._getGroup = Facets.prototype.getGroup;

/**
* Internal method to destroy the groups, facets and queries contained in this widget.
Expand Down
10 changes: 5 additions & 5 deletions tests/facetAppendTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ describe('Append', function() {
testSupport.verifyQuery(secondQueryQuery, secondQueryQuery._element, '*', 'second query', 8, 10, '80%');

// Verify baseline groups/facets
var phoneGroup = facetsComponent._getGroup('phones');
var phoneGroup = facetsComponent.getGroup('phones');
var firstPhoneFacet = phoneGroup._getFacet('111 111 1111');
var secondPhoneFacet = phoneGroup._getFacet('222 222 2222');
testSupport.verifyFacet(firstPhoneFacet, firstPhoneFacet._element, 'phones', '111 111 1111', 10, 40, 'orange', '25%');
testSupport.verifyFacet(secondPhoneFacet, secondPhoneFacet._element, 'phones', '222 222 2222', 30, 40, 'blue', '75%');

var nameGroup = facetsComponent._getGroup('names');
var nameGroup = facetsComponent.getGroup('names');
var firstNameFacet = nameGroup._getFacet('Maya');
testSupport.verifyFacet(firstNameFacet, firstNameFacet._element, 'names', 'Maya', 20, 20, 'grey', '100%');
});
Expand All @@ -84,7 +84,7 @@ describe('Append', function() {
facetsComponent.append(groupsToAppend);

// Then expect names group to be updated
var nameGroup = facetsComponent._getGroup('names');
var nameGroup = facetsComponent.getGroup('names');
var firstNameFacet = nameGroup._getFacet('Maya');
var secondNameFacet = nameGroup._getFacet('John');
testSupport.verifyFacet(firstNameFacet, firstNameFacet._element, 'names', 'Maya', 20, 80, 'grey', '25%');
Expand All @@ -101,7 +101,7 @@ describe('Append', function() {
facetsComponent.append(groupsToAppend);

// Then expect new group added
var fooGroup = facetsComponent._getGroup('foo');
var fooGroup = facetsComponent.getGroup('foo');
var fooFacet = fooGroup._getFacet('bar');
testSupport.verifyFacet(fooFacet, fooFacet._element, 'foo', 'bar', 60, 60, 'grey', '100%');
});
Expand All @@ -116,7 +116,7 @@ describe('Append', function() {
facetsComponent.append(groupsToAppend);

// Then expect phone group modified
var phoneGroup = facetsComponent._getGroup('phones');
var phoneGroup = facetsComponent.getGroup('phones');
var firstPhoneFacet = phoneGroup._getFacet('111 111 1111');
var secondPhoneFacet = phoneGroup._getFacet('222 222 2222');
testSupport.verifyFacet(firstPhoneFacet, firstPhoneFacet._element, 'phones', '111 111 1111', 10, 50, 'orange', '20%');
Expand Down
8 changes: 4 additions & 4 deletions tests/facetClickTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Click', function() {
facetsComponent.on('facet:click', onFacetClick);

// When a facet is clicked
var phoneGroup = facetsComponent._getGroup('phone');
var phoneGroup = facetsComponent.getGroup('phone');
var secondPhoneFacet = phoneGroup._getFacet('222 222 2222');
$(secondPhoneFacet._element).trigger('click');

Expand All @@ -79,7 +79,7 @@ describe('Click', function() {
]);

// ... and the modified name is clicked
var nameGroup = facetsComponent._getGroup('name');
var nameGroup = facetsComponent.getGroup('name');
var secondNameFacet = nameGroup._getFacet('John');
$(secondNameFacet._element).trigger('click');

Expand All @@ -100,7 +100,7 @@ describe('Click', function() {
]);

// ... and the newly added name is clicked
var nameGroup = facetsComponent._getGroup('name');
var nameGroup = facetsComponent.getGroup('name');
var secondNameFacet = nameGroup._getFacet('Maya');
$(secondNameFacet._element).trigger('click');

Expand All @@ -120,7 +120,7 @@ describe('Click', function() {
facetsComponent.off('facet:click');

// ...and a facet is clicked
var phoneGroup = facetsComponent._getGroup('phone');
var phoneGroup = facetsComponent.getGroup('phone');
var secondPhoneFacet = phoneGroup._getFacet('222 222 2222');
$(secondPhoneFacet._element).trigger('click');

Expand Down
10 changes: 5 additions & 5 deletions tests/facetCollapseExpandTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('Expand and Collapse', function() {
});

it('Groups are expanded by default', function() {
var phoneGroup = facetsComponent._getGroup('phone');
var nameGroup = facetsComponent._getGroup('name');
var phoneGroup = facetsComponent.getGroup('phone');
var nameGroup = facetsComponent.getGroup('name');

expect(phoneGroup.collapsed).to.be.false;
expect(phoneGroup._element.hasClass('facets-group-collapsed')).to.be.false;
Expand All @@ -63,7 +63,7 @@ describe('Expand and Collapse', function() {

it('Toggles group collapse on click', function() {
// Given
var phoneGroup = facetsComponent._getGroup('phone');
var phoneGroup = facetsComponent.getGroup('phone');
var phoneGroupExpander = phoneGroup._element.find('.group-expander');

// When
Expand All @@ -83,7 +83,7 @@ describe('Expand and Collapse', function() {

it('Emits collapse and expand events with group key', function() {
// Given
var phoneGroup = facetsComponent._getGroup('phone'),
var phoneGroup = facetsComponent.getGroup('phone'),
phoneGroupIcon = phoneGroup._element.find('.group-expander'),
onGroupCollapse = sinon.spy(),
onGroupExpand = sinon.spy();
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('Expand and Collapse', function() {
]);

// ... and new group is collapsed and expanded
var fooGroup = facetsComponent._getGroup('foo'),
var fooGroup = facetsComponent.getGroup('foo'),
fooGroupIcon = fooGroup._element.find('.group-expander');
fooGroupIcon.trigger('click');
fooGroupIcon.trigger('click');
Expand Down
4 changes: 2 additions & 2 deletions tests/facetDestroyTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ describe('Destroy', function() {
facetsComponent.on('facet-group:more', onGroupMore);

// Then event handlers are persisted in component
var phoneGroup = facetsComponent._getGroup('phones');
var nameGroup = facetsComponent._getGroup('names');
var phoneGroup = facetsComponent.getGroup('phones');
var nameGroup = facetsComponent.getGroup('names');
var phoneFacet = phoneGroup._getFacet('111 111 1111');
var nameFacet = nameGroup._getFacet('Maya');

Expand Down
6 changes: 3 additions & 3 deletions tests/facetDisplayTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Display', function() {
var secondQueryElement = secondQuery._element;
testSupport.verifyQuery(secondQuery, secondQueryElement, '*', 'Toronto', 6, 10, '60%');

var phoneGroup = facetsComponent._getGroup('phones');
var phoneGroup = facetsComponent.getGroup('phones');
var firstFacet = phoneGroup._getFacet('111 111 1111');
var secondFacet = phoneGroup._getFacet('222 222 2222');
testSupport.verifyFacet(firstFacet, firstFacet._element, 'phones', '111 111 1111', 10, 40, 'orange', '25%'); // total is sum of facets in group
Expand All @@ -80,7 +80,7 @@ describe('Display', function() {
expect(facetsComponent._queryGroup.visible).to.equal(false);

// And facet groups rendered same as always
var phoneGroup = facetsComponent._getGroup('phones');
var phoneGroup = facetsComponent.getGroup('phones');
var firstFacet = phoneGroup._getFacet('111 111 1111');
var secondFacet = phoneGroup._getFacet('222 222 2222');
testSupport.verifyFacet(firstFacet, firstFacet._element, 'phones', '111 111 1111', 10, 40, 'orange', '25%'); // total is sum of facets in group
Expand All @@ -100,7 +100,7 @@ describe('Display', function() {
var facetsComponent = new Facets(container[0], groups);

// Then
var personaGroup = facetsComponent._getGroup('persona');
var personaGroup = facetsComponent.getGroup('persona');
var personaFacet = personaGroup._getFacet('333 333 3333');
var personaLink = personaFacet._element.find('.facet-links');
expect(personaLink.text().trim()).to.equal('333');
Expand Down
8 changes: 4 additions & 4 deletions tests/facetMoreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ describe('More', function() {

it('Displays a more link if provided', function() {
// Phone group has more link
var phoneGroup = facetsComponent._getGroup('phone');
var phoneGroup = facetsComponent.getGroup('phone');
var phoneMoreCount = phoneGroup._element.find('.group-other-label-count');
expect(phoneMoreCount.text()).to.equal('15+');

// Name group does not have more link
var nameGroup = facetsComponent._getGroup('name');
var nameGroup = facetsComponent.getGroup('name');
var nameMoreCount = nameGroup._element.find('.group-other-label-count');
expect(nameMoreCount.text()).to.equal('');
});

it('Emits more event with group key', function() {
// Given
var phoneGroup = facetsComponent._getGroup('phone'),
var phoneGroup = facetsComponent.getGroup('phone'),
phoneMore = phoneGroup._element.find('.group-more-target'),
onGroupMore = sinon.spy();

Expand All @@ -91,7 +91,7 @@ describe('More', function() {
]);

// ...and the newly added more link is clicked
var nameGroup = facetsComponent._getGroup('name'),
var nameGroup = facetsComponent.getGroup('name'),
nameMore = nameGroup._element.find('.group-more-target');
nameMore.trigger('click');

Expand Down
6 changes: 3 additions & 3 deletions tests/facetMouseTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Mouse Events', function() {
facetsComponent.on('facet:mouseleave', onMouseLeave);

// When first phone icon is hovered over
var phoneGroup = facetsComponent._getGroup('phone'),
var phoneGroup = facetsComponent.getGroup('phone'),
firstPhoneFacet = phoneGroup._getFacet('111 111 1111'),
firstPhoneIcon = firstPhoneFacet._element.find('.facet-icon');
firstPhoneIcon.trigger('mouseenter');
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('Mouse Events', function() {
]);

// ...and the newly added name icon is hovered over
var nameGroup = facetsComponent._getGroup('name'),
var nameGroup = facetsComponent.getGroup('name'),
johnFacet = nameGroup._getFacet('John'),
johnIcon = johnFacet._element.find('.facet-icon');
johnIcon.trigger('mouseenter');
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('Mouse Events', function() {
facetsComponent.off('facet:mouseleave');

// ... and a hover is triggerred
var phoneGroup = facetsComponent._getGroup('phone'),
var phoneGroup = facetsComponent.getGroup('phone'),
firstPhoneFacet = phoneGroup._getFacet('111 111 1111'),
firstPhoneIcon = firstPhoneFacet._element.find('.facet-icon');
firstPhoneIcon.trigger('mouseenter');
Expand Down
Loading

0 comments on commit 0d08f1a

Please sign in to comment.