|
| 1 | +import { |
| 2 | + bemHelper, |
| 3 | + prepareTemplateProps, |
| 4 | + getContainerNode, |
| 5 | +} from '../../lib/utils.js'; |
| 6 | +import cx from 'classnames'; |
| 7 | +import defaultTemplates from './defaultTemplates.js'; |
| 8 | + |
| 9 | +const bem = bemHelper('ais-hierarchical-menu'); |
| 10 | +/** |
| 11 | + * Create a hierarchical menu using multiple attributes |
| 12 | + * @function hierarchicalMenu |
| 13 | + * @param {string|DOMElement} options.container CSS Selector or DOMElement to insert the widget |
| 14 | + * @param {string[]} options.attributes Array of attributes to use to generate the hierarchy of the menu. |
| 15 | + * See the example for the convention to follow. |
| 16 | + * @param {number} [options.limit=10] How much facet values to get [*] |
| 17 | + * @param {string} [options.separator=">"] Separator used in the attributes to separate level values. [*] |
| 18 | + * @param {string} [options.rootPath] Prefix path to use if the first level is not the root level. |
| 19 | + * @param {string} [options.showParentLevel=false] Show the parent level of the current refined value |
| 20 | + * @param {string[]|Function} [options.sortBy=['name:asc']] How to sort refinements. Possible values: `count|isRefined|name:asc|name:desc`. |
| 21 | + * You can also use a sort function that behaves like the standard Javascript [compareFunction](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Syntax). |
| 22 | + * @param {Object} [options.templates] Templates to use for the widget |
| 23 | + * @param {string|Function} [options.templates.header=''] Header template (root level only) |
| 24 | + * @param {string|Function} [options.templates.item] Item template, provided with `name`, `count`, `isRefined`, `url` data properties |
| 25 | + * @param {string|Function} [options.templates.footer=''] Footer template (root level only) |
| 26 | + * @param {Function} [options.transformData.item] Method to change the object passed to the `item` template |
| 27 | + * @param {boolean} [options.autoHideContainer=true] Hide the container when there are no items in the menu |
| 28 | + * @param {Object} [options.cssClasses] CSS classes to add to the wrapping elements |
| 29 | + * @param {string|string[]} [options.cssClasses.root] CSS class to add to the root element |
| 30 | + * @param {string|string[]} [options.cssClasses.header] CSS class to add to the header element |
| 31 | + * @param {string|string[]} [options.cssClasses.body] CSS class to add to the body element |
| 32 | + * @param {string|string[]} [options.cssClasses.footer] CSS class to add to the footer element |
| 33 | + * @param {string|string[]} [options.cssClasses.list] CSS class to add to the list element |
| 34 | + * @param {string|string[]} [options.cssClasses.item] CSS class to add to each item element |
| 35 | + * @param {string|string[]} [options.cssClasses.depth] CSS class to add to each item element to denote its depth. The actual level will be appended to the given class name (ie. if `depth` is given, the widget will add `depth0`, `depth1`, ... according to the level of each item). |
| 36 | + * @param {string|string[]} [options.cssClasses.active] CSS class to add to each active element |
| 37 | + * @param {string|string[]} [options.cssClasses.link] CSS class to add to each link (when using the default template) |
| 38 | + * @param {string|string[]} [options.cssClasses.count] CSS class to add to each count element (when using the default template) |
| 39 | + * @param {object|boolean} [options.collapsible=false] Hide the widget body and footer when clicking on header |
| 40 | + * @param {boolean} [options.collapsible.collapsed] Initial collapsed state of a collapsible widget |
| 41 | + * @return {Object} |
| 42 | + */ |
| 43 | +const usage = `Usage: |
| 44 | +hierarchicalMenu({ |
| 45 | + container, |
| 46 | + attributes, |
| 47 | + [ separator=' > ' ], |
| 48 | + [ rootPath ], |
| 49 | + [ showParentLevel=true ], |
| 50 | + [ limit=10 ], |
| 51 | + [ sortBy=['name:asc'] ], |
| 52 | + [ cssClasses.{root , header, body, footer, list, depth, item, active, link}={} ], |
| 53 | + [ templates.{header, item, footer} ], |
| 54 | + [ transformData.{item} ], |
| 55 | + [ autoHideContainer=true ], |
| 56 | + [ collapsible=false ] |
| 57 | +})`; |
| 58 | +const connectHierarchicalMenu = renderHierarchicalMenu => ({ |
| 59 | + container, |
| 60 | + attributes, |
| 61 | + separator = ' > ', |
| 62 | + rootPath = null, |
| 63 | + showParentLevel = true, |
| 64 | + limit = 10, |
| 65 | + sortBy = ['name:asc'], |
| 66 | + cssClasses: userCssClasses = {}, |
| 67 | + autoHideContainer = true, |
| 68 | + templates = defaultTemplates, |
| 69 | + collapsible = false, |
| 70 | + transformData, |
| 71 | + } = {}) => { |
| 72 | + if (!container || !attributes || !attributes.length) { |
| 73 | + throw new Error(usage); |
| 74 | + } |
| 75 | + |
| 76 | + const containerNode = getContainerNode(container); |
| 77 | + |
| 78 | + // we need to provide a hierarchicalFacet name for the search state |
| 79 | + // so that we can always map $hierarchicalFacetName => real attributes |
| 80 | + // we use the first attribute name |
| 81 | + const hierarchicalFacetName = attributes[0]; |
| 82 | + |
| 83 | + const cssClasses = { |
| 84 | + root: cx(bem(null), userCssClasses.root), |
| 85 | + header: cx(bem('header'), userCssClasses.header), |
| 86 | + body: cx(bem('body'), userCssClasses.body), |
| 87 | + footer: cx(bem('footer'), userCssClasses.footer), |
| 88 | + list: cx(bem('list'), userCssClasses.list), |
| 89 | + depth: bem('list', 'lvl'), |
| 90 | + item: cx(bem('item'), userCssClasses.item), |
| 91 | + active: cx(bem('item', 'active'), userCssClasses.active), |
| 92 | + link: cx(bem('link'), userCssClasses.link), |
| 93 | + count: cx(bem('count'), userCssClasses.count), |
| 94 | + }; |
| 95 | + |
| 96 | + return { |
| 97 | + getConfiguration: currentConfiguration => ({ |
| 98 | + hierarchicalFacets: [{ |
| 99 | + name: hierarchicalFacetName, |
| 100 | + attributes, |
| 101 | + separator, |
| 102 | + rootPath, |
| 103 | + showParentLevel, |
| 104 | + }], |
| 105 | + maxValuesPerFacet: currentConfiguration.maxValuesPerFacet !== undefined ? |
| 106 | + Math.max(currentConfiguration.maxValuesPerFacet, limit) : |
| 107 | + limit, |
| 108 | + }), |
| 109 | + init({helper, templatesConfig, createURL}) { |
| 110 | + this._toggleRefinement = facetValue => helper |
| 111 | + .toggleRefinement(hierarchicalFacetName, facetValue) |
| 112 | + .search(); |
| 113 | + |
| 114 | + this._templateProps = prepareTemplateProps({ |
| 115 | + transformData, |
| 116 | + defaultTemplates, |
| 117 | + templatesConfig, |
| 118 | + templates, |
| 119 | + }); |
| 120 | + |
| 121 | + // Bind createURL to this specific attribute |
| 122 | + function _createURL(facetValue) { |
| 123 | + return createURL(helper.state.toggleRefinement(hierarchicalFacetName, facetValue)); |
| 124 | + } |
| 125 | + |
| 126 | + renderHierarchicalMenu({ |
| 127 | + attributeNameKey: 'path', |
| 128 | + collapsible, |
| 129 | + createURL: _createURL, |
| 130 | + cssClasses, |
| 131 | + facetValues: undefined, |
| 132 | + shouldAutoHideContainer: autoHideContainer, |
| 133 | + templateProps: this._templateProps, |
| 134 | + toggleRefinement: this._toggleRefinement, |
| 135 | + containerNode, |
| 136 | + }, true); |
| 137 | + }, |
| 138 | + _prepareFacetValues(facetValues, state) { |
| 139 | + return facetValues |
| 140 | + .slice(0, limit) |
| 141 | + .map(subValue => { |
| 142 | + if (Array.isArray(subValue.data)) { |
| 143 | + subValue.data = this._prepareFacetValues(subValue.data, state); |
| 144 | + } |
| 145 | + |
| 146 | + return subValue; |
| 147 | + }); |
| 148 | + }, |
| 149 | + render({results, state, createURL}) { |
| 150 | + let facetValues = results.getFacetValues(hierarchicalFacetName, {sortBy}).data || []; |
| 151 | + facetValues = this._prepareFacetValues(facetValues, state); |
| 152 | + |
| 153 | + // Bind createURL to this specific attribute |
| 154 | + function _createURL(facetValue) { |
| 155 | + return createURL(state.toggleRefinement(hierarchicalFacetName, facetValue)); |
| 156 | + } |
| 157 | + |
| 158 | + renderHierarchicalMenu({ |
| 159 | + attributeNameKey: 'path', |
| 160 | + collapsible, |
| 161 | + createURL: _createURL, |
| 162 | + cssClasses, |
| 163 | + facetValues, |
| 164 | + shouldAutoHideContainer: autoHideContainer && facetValues.length === 0, |
| 165 | + templateProps: this._templateProps, |
| 166 | + toggleRefinement: this._toggleRefinement, |
| 167 | + containerNode, |
| 168 | + }, false); |
| 169 | + }, |
| 170 | + }; |
| 171 | +}; |
| 172 | + |
| 173 | +export default connectHierarchicalMenu; |
0 commit comments