|
| 1 | +import { |
| 2 | + bemHelper, |
| 3 | + getContainerNode, |
| 4 | +} from '../../lib/utils.js'; |
| 5 | +import defaultTemplates from './defaultTemplates.js'; |
| 6 | +import cx from 'classnames'; |
| 7 | +import connectCurrent from './implementations/current.js'; |
| 8 | +import connectLegacy from './implementations/legacy.js'; |
| 9 | + |
| 10 | +const bem = bemHelper('ais-toggle'); |
| 11 | + |
| 12 | +// we cannot use helper. because the facet is not yet declared in the helper |
| 13 | +const hasFacetsRefinementsFor = (attributeName, searchParameters) => |
| 14 | + searchParameters && |
| 15 | + searchParameters.facetsRefinements && |
| 16 | + searchParameters.facetsRefinements[attributeName] !== undefined; |
| 17 | + |
| 18 | +/** |
| 19 | + * Instantiate the toggling of a boolean facet filter on and off. |
| 20 | + * @function toggle |
| 21 | + * @param {string|DOMElement} options.container CSS Selector or DOMElement to insert the widget |
| 22 | + * @param {string} options.attributeName Name of the attribute for faceting (eg. "free_shipping") |
| 23 | + * @param {string} options.label Human-readable name of the filter (eg. "Free Shipping") |
| 24 | + * @param {Object} [options.values] Lets you define the values to filter on when toggling |
| 25 | + * @param {string|number|boolean} [options.values.on=true] Value to filter on when checked |
| 26 | + * @param {string|number|boolean} [options.values.off=undefined] Value to filter on when unchecked |
| 27 | + * element (when using the default template). By default when switching to `off`, no refinement will be asked. So you |
| 28 | + * will get both `true` and `false` results. If you set the off value to `false` then you will get only objects |
| 29 | + * having `false` has a value for the selected attribute. |
| 30 | + * @param {Object} [options.templates] Templates to use for the widget |
| 31 | + * @param {string|Function} [options.templates.header] Header template |
| 32 | + * @param {string|Function} [options.templates.item] Item template, provided with `name`, `count`, `isRefined`, `url` data properties |
| 33 | + * count is always the number of hits that would be shown if you toggle the widget. We also provide |
| 34 | + * `onFacetValue` and `offFacetValue` objects with according counts. |
| 35 | + * @param {string|Function} [options.templates.footer] Footer template |
| 36 | + * @param {Function} [options.transformData.item] Function to change the object passed to the `item` template |
| 37 | + * @param {boolean} [options.autoHideContainer=true] Hide the container when there are no results |
| 38 | + * @param {Object} [options.cssClasses] CSS classes to add |
| 39 | + * @param {string|string[]} [options.cssClasses.root] CSS class to add to the root element |
| 40 | + * @param {string|string[]} [options.cssClasses.header] CSS class to add to the header element |
| 41 | + * @param {string|string[]} [options.cssClasses.body] CSS class to add to the body element |
| 42 | + * @param {string|string[]} [options.cssClasses.footer] CSS class to add to the footer element |
| 43 | + * @param {string|string[]} [options.cssClasses.list] CSS class to add to the list element |
| 44 | + * @param {string|string[]} [options.cssClasses.item] CSS class to add to each item element |
| 45 | + * @param {string|string[]} [options.cssClasses.active] CSS class to add to each active element |
| 46 | + * @param {string|string[]} [options.cssClasses.label] CSS class to add to each |
| 47 | + * label element (when using the default template) |
| 48 | + * @param {string|string[]} [options.cssClasses.checkbox] CSS class to add to each |
| 49 | + * checkbox element (when using the default template) |
| 50 | + * @param {string|string[]} [options.cssClasses.count] CSS class to add to each count |
| 51 | + * @param {object|boolean} [options.collapsible=false] Hide the widget body and footer when clicking on header |
| 52 | + * @param {boolean} [options.collapsible.collapsed] Initial collapsed state of a collapsible widget |
| 53 | + * @return {Object} |
| 54 | + */ |
| 55 | +const usage = `Usage: |
| 56 | +toggle({ |
| 57 | + container, |
| 58 | + attributeName, |
| 59 | + label, |
| 60 | + [ values={on: true, off: undefined} ], |
| 61 | + [ cssClasses.{root,header,body,footer,list,item,active,label,checkbox,count} ], |
| 62 | + [ templates.{header,item,footer} ], |
| 63 | + [ transformData.{item} ], |
| 64 | + [ autoHideContainer=true ], |
| 65 | + [ collapsible=false ] |
| 66 | +})`; |
| 67 | +function connectToggle(toggleRendering) { |
| 68 | + const legacyToggle = connectLegacy(toggleRendering); |
| 69 | + const currentToggle = connectCurrent(toggleRendering); |
| 70 | + |
| 71 | + return ({ |
| 72 | + container, |
| 73 | + attributeName, |
| 74 | + label, |
| 75 | + values: userValues = {on: true, off: undefined}, |
| 76 | + templates = defaultTemplates, |
| 77 | + collapsible = false, |
| 78 | + cssClasses: userCssClasses = {}, |
| 79 | + transformData, |
| 80 | + autoHideContainer = true, |
| 81 | + } = {}) => { |
| 82 | + const containerNode = getContainerNode(container); |
| 83 | + |
| 84 | + if (!container || !attributeName || !label) { |
| 85 | + throw new Error(usage); |
| 86 | + } |
| 87 | + |
| 88 | + const hasAnOffValue = userValues.off !== undefined; |
| 89 | + |
| 90 | + const cssClasses = { |
| 91 | + root: cx(bem(null), userCssClasses.root), |
| 92 | + header: cx(bem('header'), userCssClasses.header), |
| 93 | + body: cx(bem('body'), userCssClasses.body), |
| 94 | + footer: cx(bem('footer'), userCssClasses.footer), |
| 95 | + list: cx(bem('list'), userCssClasses.list), |
| 96 | + item: cx(bem('item'), userCssClasses.item), |
| 97 | + active: cx(bem('item', 'active'), userCssClasses.active), |
| 98 | + label: cx(bem('label'), userCssClasses.label), |
| 99 | + checkbox: cx(bem('checkbox'), userCssClasses.checkbox), |
| 100 | + count: cx(bem('count'), userCssClasses.count), |
| 101 | + }; |
| 102 | + |
| 103 | + // store the computed options for usage in the two toggle implementations |
| 104 | + const implemOptions = { |
| 105 | + attributeName, |
| 106 | + label, |
| 107 | + userValues, |
| 108 | + templates, |
| 109 | + collapsible, |
| 110 | + transformData, |
| 111 | + hasAnOffValue, |
| 112 | + containerNode, |
| 113 | + cssClasses, |
| 114 | + autoHideContainer, |
| 115 | + }; |
| 116 | + |
| 117 | + return { |
| 118 | + getConfiguration(currentSearchParameters, searchParametersFromUrl) { |
| 119 | + const useLegacyToggle = |
| 120 | + hasFacetsRefinementsFor(attributeName, currentSearchParameters) || |
| 121 | + hasFacetsRefinementsFor(attributeName, searchParametersFromUrl); |
| 122 | + |
| 123 | + const toggleImplementation = useLegacyToggle ? |
| 124 | + legacyToggle(implemOptions) : |
| 125 | + currentToggle(implemOptions); |
| 126 | + |
| 127 | + this.init = toggleImplementation.init.bind(toggleImplementation); |
| 128 | + this.render = toggleImplementation.render.bind(toggleImplementation); |
| 129 | + return toggleImplementation.getConfiguration(currentSearchParameters, searchParametersFromUrl); |
| 130 | + }, |
| 131 | + init() {}, |
| 132 | + render() {}, |
| 133 | + }; |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +export default connectToggle; |
0 commit comments