Skip to content

Commit

Permalink
fix(numerical widgets): s/facetName/attributeName
Browse files Browse the repository at this point in the history
Fix #431

BREAKING CHANGE: the priceRanges and rangeSlider widgets are now using `attributeName` instead of `facetName`.
  • Loading branch information
redox committed Nov 3, 2015
1 parent df373c3 commit f209f5d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions dev/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ search.addWidget(
search.addWidget(
instantsearch.widgets.rangeSlider({
container: '#price',
facetName: 'price',
attributeName: 'price',
cssClasses: {
header: 'facet-title'
},
Expand Down Expand Up @@ -189,7 +189,7 @@ search.once('render', function() {
search.addWidget(
instantsearch.widgets.priceRanges({
container: '#price-ranges',
facetName: 'price',
attributeName: 'price',
templates: {
header: 'Price ranges'
},
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/widget-jsdoc/priceRanges.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| Param | Description |
| --- | --- |
| <span class='attr-required'>`options.container`</span> | Valid CSS Selector as a string or DOMElement |
| <span class='attr-required'>`options.facetName`</span> | Name of the attribute for faceting |
| <span class='attr-required'>`options.attributeName`</span> | Name of the attribute for faceting |
| <span class='attr-optional'>`options.cssClasses`</span> | CSS classes to add |
| <span class='attr-optional'>`options.cssClasses.root`</span> | CSS class to add to the root element |
| <span class='attr-optional'>`options.cssClasses.header`</span> | CSS class to add to the header element |
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/widget-jsdoc/rangeSlider.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| Param | Description |
| --- | --- |
| <span class='attr-required'>`options.container`</span> | CSS Selector or DOMElement to insert the widget |
| <span class='attr-required'>`options.facetName`</span> | Name of the attribute for faceting |
| <span class='attr-required'>`options.attributeName`</span> | Name of the attribute for faceting |
| <span class='attr-optional'>`options.tooltips`</span> | Should we show tooltips or not. The default tooltip will show the formatted corresponding value without any other token. You can also provide tooltips: {format: function(formattedValue, rawValue) {return '$' + formattedValue}} So that you can format the tooltip display value as you want |
| <span class='attr-optional'>`options.templates`</span> | Templates to use for the widget |
| <span class='attr-optional'>`options.templates.header`</span> | Header template |
Expand Down
4 changes: 2 additions & 2 deletions docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ This filtering widget lets the user choose either or not to filter values to `tr
search.addWidget(
instantsearch.widgets.rangeSlider({
container: '#price',
facetName: 'price',
attributeName: 'price',
templates: {
header: 'Price'
},
Expand Down Expand Up @@ -618,7 +618,7 @@ The range slider filters values of a single numeric attribute using 2 cursors: t
search.addWidget(
instantsearch.widgets.priceRanges({
container: '#priceranges.widget-container',
facetName: 'price',
attributeName: 'price',
labels: {
currency: '$',
separator: 'to',
Expand Down
4 changes: 2 additions & 2 deletions widgets/price-ranges/__tests__/price-ranges-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('priceRanges()', () => {
priceRanges.__Rewire__('headerFooter', headerFooter);

container = document.createElement('div');
widget = priceRanges({container, facetName: 'aFacetname'});
widget = priceRanges({container, attributeName: 'aNumAttr'});
results = {
hits: [1],
nbHits: 1,
Expand All @@ -47,7 +47,7 @@ describe('priceRanges()', () => {
});

it('adds the attribute as a facet', () => {
expect(widget.getConfiguration()).toEqual({facets: ['aFacetname']});
expect(widget.getConfiguration()).toEqual({facets: ['aNumAttr']});
});

context('without refinements', function() {
Expand Down
26 changes: 13 additions & 13 deletions widgets/price-ranges/price-ranges.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let cx = require('classnames');
/**
* Instantiate a price ranges on a numerical facet
* @param {string|DOMElement} options.container Valid CSS Selector as a string or DOMElement
* @param {string} options.facetName Name of the attribute for faceting
* @param {string} options.attributeName Name of the attribute for faceting
* @param {Object} [options.cssClasses] CSS classes to add
* @param {string} [options.cssClasses.root] CSS class to add to the root element
* @param {string} [options.cssClasses.header] CSS class to add to the header element
Expand All @@ -42,7 +42,7 @@ let cx = require('classnames');
*/
function priceRanges({
container,
facetName,
attributeName,
cssClasses: userCssClasses = {},
templates = defaultTemplates,
labels = {
Expand All @@ -53,29 +53,29 @@ function priceRanges({
hideContainerWhenNoResults = true
}) {
let containerNode = utils.getContainerNode(container);
let usage = 'Usage: priceRanges({container, facetName, [cssClasses.{root,header,body,list,item,active,link,form,label,input,currency,separator,button,footer}, templates.{header,item,footer}, labels.{currency,separator,button}, hideContainerWhenNoResults]})';
let usage = 'Usage: priceRanges({container, attributeName, [cssClasses.{root,header,body,list,item,active,link,form,label,input,currency,separator,button,footer}, templates.{header,item,footer}, labels.{currency,separator,button}, hideContainerWhenNoResults]})';

let PriceRanges = headerFooter(require('../../components/PriceRanges/PriceRanges'));
if (hideContainerWhenNoResults === true) {
PriceRanges = autoHideContainer(PriceRanges);
}

if (!container || !facetName) {
if (!container || !attributeName) {
throw new Error(usage);
}

return {
getConfiguration: () => ({
facets: [facetName]
facets: [attributeName]
}),

_generateRanges: function(results) {
let stats = results.getFacetStats(facetName);
let stats = results.getFacetStats(attributeName);
return generateRanges(stats);
},

_extractRefinedRange: function(helper) {
let refinements = helper.getRefinements(facetName);
let refinements = helper.getRefinements(attributeName);
let from;
let to;

Expand All @@ -96,13 +96,13 @@ function priceRanges({
_refine: function(helper, from, to) {
let facetValues = this._extractRefinedRange(helper);

helper.clearRefinements(facetName);
helper.clearRefinements(attributeName);
if (facetValues.length === 0 || facetValues[0].from !== from || facetValues[0].to !== to) {
if (typeof from !== 'undefined') {
helper.addNumericRefinement(facetName, '>', from - 1);
helper.addNumericRefinement(attributeName, '>', from - 1);
}
if (typeof to !== 'undefined') {
helper.addNumericRefinement(facetName, '<', to + 1);
helper.addNumericRefinement(attributeName, '<', to + 1);
}
}

Expand Down Expand Up @@ -149,13 +149,13 @@ function priceRanges({
ReactDOM.render(
<PriceRanges
createURL={(from, to, isRefined) => {
let newState = state.clearRefinements(facetName);
let newState = state.clearRefinements(attributeName);
if (!isRefined) {
if (typeof from !== 'undefined') {
newState = newState.addNumericRefinement(facetName, '>', from - 1);
newState = newState.addNumericRefinement(attributeName, '>', from - 1);
}
if (typeof to !== 'undefined') {
newState = newState.addNumericRefinement(facetName, '<', to + 1);
newState = newState.addNumericRefinement(attributeName, '<', to + 1);
}
}
return createURL(newState);
Expand Down
12 changes: 6 additions & 6 deletions widgets/range-slider/__tests__/range-slider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('rangeSlider()', () => {
rangeSlider.__Rewire__('headerFooter', headerFooter);

container = document.createElement('div');
widget = rangeSlider({container, facetName: 'aFacetName'});
widget = rangeSlider({container, attributeName: 'aNumAttr'});
results = {
getFacetStats: sinon.stub().returns({
min: 1.99,
Expand All @@ -53,7 +53,7 @@ describe('rangeSlider()', () => {
});

it('configures the disjunctiveFacets', () => {
expect(widget.getConfiguration()).toEqual({disjunctiveFacets: ['aFacetName']});
expect(widget.getConfiguration()).toEqual({disjunctiveFacets: ['aNumAttr']});
});

it('calls twice ReactDOM.render(<Slider props />, container)', () => {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('rangeSlider()', () => {
widget._refine(helper, stats, [stats.min + 1, stats.max]);
expect(helper.clearRefinements.calledOnce).toBe(true, 'clearRefinements called once');
expect(helper.addNumericRefinement.calledOnce).toBe(true, 'clearRefinements called once');
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aFacetName', '>=', stats.min + 1]);
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aNumAttr', '>=', stats.min + 1]);
expect(helper.search.calledOnce).toBe(true, 'search called once');
});

Expand All @@ -106,7 +106,7 @@ describe('rangeSlider()', () => {
widget._refine(helper, stats, [stats.min, stats.max - 1]);
expect(helper.clearRefinements.calledOnce).toBe(true, 'clearRefinements called once');
expect(helper.addNumericRefinement.calledOnce).toBe(true, 'addNumericRefinement called once');
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aFacetName', '<=', stats.max - 1]);
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aNumAttr', '<=', stats.max - 1]);
expect(helper.search.calledOnce).toBe(true, 'search called once');
});

Expand All @@ -115,8 +115,8 @@ describe('rangeSlider()', () => {
widget._refine(helper, stats, [stats.min + 1, stats.max - 1]);
expect(helper.clearRefinements.calledOnce).toBe(true, 'clearRefinements called once');
expect(helper.addNumericRefinement.calledTwice).toBe(true, 'addNumericRefinement called twice');
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aFacetName', '>=', stats.min + 1]);
expect(helper.addNumericRefinement.getCall(1).args).toEqual(['aFacetName', '<=', stats.max - 1]);
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aNumAttr', '>=', stats.min + 1]);
expect(helper.addNumericRefinement.getCall(1).args).toEqual(['aNumAttr', '<=', stats.max - 1]);
expect(helper.search.calledOnce).toBe(true, 'search called once');
});

Expand Down
18 changes: 9 additions & 9 deletions widgets/range-slider/range-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let defaultTemplates = {
/**
* Instantiate a slider based on a numeric attribute
* @param {string|DOMElement} options.container CSS Selector or DOMElement to insert the widget
* @param {string} options.facetName Name of the attribute for faceting
* @param {string} options.attributeName Name of the attribute for faceting
* @param {boolean|Object} [options.tooltips=true] Should we show tooltips or not.
* The default tooltip will show the formatted corresponding value without any other token.
* You can also provide
Expand All @@ -30,7 +30,7 @@ let defaultTemplates = {
*/
function rangeSlider({
container = null,
facetName = null,
attributeName = null,
tooltips = true,
templates = defaultTemplates,
cssClasses = {
Expand All @@ -48,11 +48,11 @@ function rangeSlider({

return {
getConfiguration: () => ({
disjunctiveFacets: [facetName]
disjunctiveFacets: [attributeName]
}),
_getCurrentRefinement(helper) {
let min = helper.state.getNumericRefinement(facetName, '>=');
let max = helper.state.getNumericRefinement(facetName, '<=');
let min = helper.state.getNumericRefinement(attributeName, '>=');
let max = helper.state.getNumericRefinement(attributeName, '<=');

if (min && min.length) {
min = min[0];
Expand All @@ -72,17 +72,17 @@ function rangeSlider({
};
},
_refine(helper, stats, newValues) {
helper.clearRefinements(facetName);
helper.clearRefinements(attributeName);
if (newValues[0] > stats.min) {
helper.addNumericRefinement(facetName, '>=', newValues[0]);
helper.addNumericRefinement(attributeName, '>=', newValues[0]);
}
if (newValues[1] < stats.max) {
helper.addNumericRefinement(facetName, '<=', newValues[1]);
helper.addNumericRefinement(attributeName, '<=', newValues[1]);
}
helper.search();
},
render({results, helper, templatesConfig}) {
let stats = results.getFacetStats(facetName);
let stats = results.getFacetStats(attributeName);

let currentRefinement = this._getCurrentRefinement(helper);

Expand Down

0 comments on commit f209f5d

Please sign in to comment.