|
| 1 | +var React = require('react'); |
| 2 | + |
| 3 | +var utils = require('../lib/utils.js'); |
| 4 | +var autoHide = require('../decorators/autoHide'); |
| 5 | +var bindProps = require('../decorators/bindProps'); |
| 6 | +var headerFooter = require('../decorators/headerFooter'); |
| 7 | +var RefinementList = autoHide(headerFooter(require('../components/RefinementList'))); |
| 8 | +var Template = require('../components/Template'); |
| 9 | + |
| 10 | +var hierarchicalCounter = 0; |
| 11 | +var defaultTemplates = { |
| 12 | + header: '', |
| 13 | + item: '<a href="{{href}}">{{name}}</a> {{count}}', |
| 14 | + footer: '' |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * Create a hierarchical menu using multiple attributes |
| 19 | + * @param {String|DOMElement} options.container CSS Selector or DOMElement to insert the widget |
| 20 | + * @param {String[]} options.attributes Array of attributes to use to generate the hierarchy of the menu. |
| 21 | + * You need to follow some conventions: |
| 22 | + * @param {String[]} [options.sortBy=['count:desc']] How to sort refinements. Possible values: `count|isRefined|name:asc|desc` |
| 23 | + * @param {Number} [options.limit=100] How much facet values to get |
| 24 | + * @param {Object} [options.cssClasses] CSS classes to add to the wrapping elements: root, list, item |
| 25 | + * @param {String|String[]} [options.cssClasses.root] |
| 26 | + * @param {String|String[]} [options.cssClasses.list] |
| 27 | + * @param {String|String[]} [options.cssClasses.item] |
| 28 | + * @param {Object} [options.templates] Templates to use for the widget |
| 29 | + * @param {String|Function} [options.templates.header=''] Header template (root level only) |
| 30 | + * @param {String|Function} [options.templates.item='<a href="{{href}}">{{name}}</a> {{count}}'] Item template, provided with `name`, `count`, `isRefined`, `path` |
| 31 | + * @param {String|Function} [options.templates.footer=''] Footer template (root level only) |
| 32 | + * @param {Function} [options.transformData] Method to change the object passed to the item template |
| 33 | + * @param {boolean} [hideWhenNoResults=true] Hide the container when there's no results |
| 34 | + * @return {Object} |
| 35 | + */ |
| 36 | +function hierarchicalMenu({ |
| 37 | + container = null, |
| 38 | + attributes = [], |
| 39 | + separator, |
| 40 | + limit = 100, |
| 41 | + sortBy = ['name:asc'], |
| 42 | + cssClasses = { |
| 43 | + root: null, |
| 44 | + list: null, |
| 45 | + item: null |
| 46 | + }, |
| 47 | + hideWhenNoResults = true, |
| 48 | + templates = defaultTemplates, |
| 49 | + transformData |
| 50 | + }) { |
| 51 | + hierarchicalCounter++; |
| 52 | + |
| 53 | + var containerNode = utils.getContainerNode(container); |
| 54 | + var usage = 'Usage: hierarchicalMenu({container, attributes, [separator, sortBy, limit, cssClasses.{root, list, item}, templates.{header, item, footer}, transformData]})'; |
| 55 | + |
| 56 | + if (!container || !attributes || !attributes.length) { |
| 57 | + throw new Error(usage); |
| 58 | + } |
| 59 | + |
| 60 | + var hierarchicalFacetName = 'instantsearch.js-hierarchicalMenu' + hierarchicalCounter; |
| 61 | + |
| 62 | + return { |
| 63 | + getConfiguration: () => ({ |
| 64 | + hierarchicalFacets: [{ |
| 65 | + name: hierarchicalFacetName, |
| 66 | + attributes, |
| 67 | + separator |
| 68 | + }] |
| 69 | + }), |
| 70 | + render: function({results, helper, templatesConfig}) { |
| 71 | + var facetValues = getFacetValues(results, hierarchicalFacetName, sortBy); |
| 72 | + |
| 73 | + var templateProps = utils.prepareTemplateProps({ |
| 74 | + transformData, |
| 75 | + defaultTemplates, |
| 76 | + templatesConfig, |
| 77 | + templates |
| 78 | + }); |
| 79 | + |
| 80 | + React.render( |
| 81 | + <RefinementList |
| 82 | + cssClasses={cssClasses} |
| 83 | + facetValues={facetValues} |
| 84 | + limit={limit} |
| 85 | + Template={bindProps(Template, templateProps)} |
| 86 | + hideWhenNoResults={hideWhenNoResults} |
| 87 | + hasResults={facetValues.length > 0} |
| 88 | + facetNameKey="path" |
| 89 | + toggleRefinement={toggleRefinement.bind(null, helper, hierarchicalFacetName)} |
| 90 | + />, |
| 91 | + containerNode |
| 92 | + ); |
| 93 | + } |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +function toggleRefinement(helper, facetName, facetValue) { |
| 98 | + helper |
| 99 | + .toggleRefinement(facetName, facetValue) |
| 100 | + .search(); |
| 101 | +} |
| 102 | + |
| 103 | +function getFacetValues(results, hierarchicalFacetName, sortBy) { |
| 104 | + var values = results |
| 105 | + .getFacetValues(hierarchicalFacetName, {sortBy: sortBy}); |
| 106 | + |
| 107 | + return values.data || []; |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = hierarchicalMenu; |
0 commit comments