|
| 1 | +import { |
| 2 | + bemHelper, |
| 3 | + prepareTemplateProps, |
| 4 | + getContainerNode, |
| 5 | +} from '../../lib/utils.js'; |
| 6 | +import find from 'lodash/find'; |
| 7 | +import cx from 'classnames'; |
| 8 | + |
| 9 | +const bem = bemHelper('ais-range-slider'); |
| 10 | +const defaultTemplates = { |
| 11 | + header: '', |
| 12 | + footer: '', |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Instantiate a slider based on a numeric attribute. |
| 17 | + * This is a wrapper around [noUiSlider](http://refreshless.com/nouislider/) |
| 18 | + * @function rangeSlider |
| 19 | + * @param {string|DOMElement} options.container CSS Selector or DOMElement to insert the widget |
| 20 | + * @param {string} options.attributeName Name of the attribute for faceting |
| 21 | + * @param {boolean|Object} [options.tooltips=true] Should we show tooltips or not. |
| 22 | + * The default tooltip will show the raw value. |
| 23 | + * You can also provide |
| 24 | + * `tooltips: {format: function(rawValue) {return '$' + Math.round(rawValue).toLocaleString()}}` |
| 25 | + * So that you can format the tooltip display value as you want |
| 26 | + * @param {Object} [options.templates] Templates to use for the widget |
| 27 | + * @param {string|Function} [options.templates.header=''] Header template |
| 28 | + * @param {string|Function} [options.templates.footer=''] Footer template |
| 29 | + * @param {boolean} [options.autoHideContainer=true] Hide the container when no refinements available |
| 30 | + * @param {Object} [options.cssClasses] CSS classes to add to the wrapping elements |
| 31 | + * @param {string|string[]} [options.cssClasses.root] CSS class to add to the root element |
| 32 | + * @param {string|string[]} [options.cssClasses.header] CSS class to add to the header element |
| 33 | + * @param {string|string[]} [options.cssClasses.body] CSS class to add to the body element |
| 34 | + * @param {string|string[]} [options.cssClasses.footer] CSS class to add to the footer element |
| 35 | + * @param {boolean|object} [options.pips=true] Show slider pips. |
| 36 | + * @param {boolean|object} [options.step=1] Every handle move will jump that number of steps. |
| 37 | + * @param {object|boolean} [options.collapsible=false] Hide the widget body and footer when clicking on header |
| 38 | + * @param {boolean} [options.collapsible.collapsed] Initial collapsed state of a collapsible widget |
| 39 | + * @param {number} [options.min] Minimal slider value, default to automatically computed from the result set |
| 40 | + * @param {number} [options.max] Maximal slider value, defaults to automatically computed from the result set |
| 41 | + * @return {Object} |
| 42 | + */ |
| 43 | +const usage = `Usage: |
| 44 | +rangeSlider({ |
| 45 | + container, |
| 46 | + attributeName, |
| 47 | + [ tooltips=true ], |
| 48 | + [ templates.{header, footer} ], |
| 49 | + [ cssClasses.{root, header, body, footer} ], |
| 50 | + [ step=1 ], |
| 51 | + [ pips=true ], |
| 52 | + [ autoHideContainer=true ], |
| 53 | + [ collapsible=false ], |
| 54 | + [ min ], |
| 55 | + [ max ] |
| 56 | +}); |
| 57 | +`; |
| 58 | +const connectRangeSlider = rangeSliderRendering => ({ |
| 59 | + container, |
| 60 | + attributeName, |
| 61 | + tooltips = true, |
| 62 | + templates = defaultTemplates, |
| 63 | + collapsible = false, |
| 64 | + cssClasses: userCssClasses = {}, |
| 65 | + step = 1, |
| 66 | + pips = true, |
| 67 | + autoHideContainer = true, |
| 68 | + min: userMin, |
| 69 | + max: userMax, |
| 70 | + precision = 2, |
| 71 | + } = {}) => { |
| 72 | + if (!container || !attributeName) { |
| 73 | + throw new Error(usage); |
| 74 | + } |
| 75 | + |
| 76 | + const formatToNumber = v => Number(Number(v).toFixed(precision)); |
| 77 | + |
| 78 | + const sliderFormatter = { |
| 79 | + from: v => v, |
| 80 | + to: v => formatToNumber(v).toLocaleString(), |
| 81 | + }; |
| 82 | + |
| 83 | + const containerNode = getContainerNode(container); |
| 84 | + |
| 85 | + const cssClasses = { |
| 86 | + root: cx(bem(null), userCssClasses.root), |
| 87 | + header: cx(bem('header'), userCssClasses.header), |
| 88 | + body: cx(bem('body'), userCssClasses.body), |
| 89 | + footer: cx(bem('footer'), userCssClasses.footer), |
| 90 | + }; |
| 91 | + |
| 92 | + return { |
| 93 | + getConfiguration: originalConf => { |
| 94 | + const conf = { |
| 95 | + disjunctiveFacets: [attributeName], |
| 96 | + }; |
| 97 | + |
| 98 | + if ( |
| 99 | + (userMin !== undefined || userMax !== undefined) |
| 100 | + && |
| 101 | + (!originalConf || |
| 102 | + originalConf.numericRefinements && |
| 103 | + originalConf.numericRefinements[attributeName] === undefined) |
| 104 | + ) { |
| 105 | + conf.numericRefinements = {[attributeName]: {}}; |
| 106 | + |
| 107 | + if (userMin !== undefined) { |
| 108 | + conf.numericRefinements[attributeName]['>='] = [userMin]; |
| 109 | + } |
| 110 | + |
| 111 | + if (userMax !== undefined) { |
| 112 | + conf.numericRefinements[attributeName]['<='] = [userMax]; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return conf; |
| 117 | + }, |
| 118 | + _getCurrentRefinement(helper) { |
| 119 | + let min = helper.state.getNumericRefinement(attributeName, '>='); |
| 120 | + let max = helper.state.getNumericRefinement(attributeName, '<='); |
| 121 | + |
| 122 | + if (min && min.length) { |
| 123 | + min = min[0]; |
| 124 | + } else { |
| 125 | + min = -Infinity; |
| 126 | + } |
| 127 | + |
| 128 | + if (max && max.length) { |
| 129 | + max = max[0]; |
| 130 | + } else { |
| 131 | + max = Infinity; |
| 132 | + } |
| 133 | + |
| 134 | + return { |
| 135 | + min, |
| 136 | + max, |
| 137 | + }; |
| 138 | + }, |
| 139 | + init({helper, templatesConfig}) { |
| 140 | + this._templateProps = prepareTemplateProps({ |
| 141 | + defaultTemplates, |
| 142 | + templatesConfig, |
| 143 | + templates, |
| 144 | + }); |
| 145 | + this._refine = oldValues => newValues => { |
| 146 | + helper.clearRefinements(attributeName); |
| 147 | + if (newValues[0] > oldValues.min) { |
| 148 | + helper.addNumericRefinement(attributeName, '>=', formatToNumber(newValues[0])); |
| 149 | + } |
| 150 | + if (newValues[1] < oldValues.max) { |
| 151 | + helper.addNumericRefinement(attributeName, '<=', formatToNumber(newValues[1])); |
| 152 | + } |
| 153 | + helper.search(); |
| 154 | + }; |
| 155 | + |
| 156 | + const stats = { |
| 157 | + min: userMin || null, |
| 158 | + max: userMax || null, |
| 159 | + }; |
| 160 | + const currentRefinement = this._getCurrentRefinement(helper); |
| 161 | + |
| 162 | + rangeSliderRendering({ |
| 163 | + collapsible, |
| 164 | + cssClasses, |
| 165 | + onChange: this._refine(stats), |
| 166 | + pips, |
| 167 | + range: {min: Math.floor(stats.min), max: Math.ceil(stats.max)}, |
| 168 | + shouldAutoHideContainer: autoHideContainer && stats.min === stats.max, |
| 169 | + start: [currentRefinement.min, currentRefinement.max], |
| 170 | + step, |
| 171 | + templateProps: this._templateProps, |
| 172 | + tooltips, |
| 173 | + format: sliderFormatter, |
| 174 | + containerNode, |
| 175 | + }, true); |
| 176 | + }, |
| 177 | + render({results, helper}) { |
| 178 | + const facet = find(results.disjunctiveFacets, {name: attributeName}); |
| 179 | + const stats = facet !== undefined && facet.stats !== undefined ? facet.stats : { |
| 180 | + min: null, |
| 181 | + max: null, |
| 182 | + }; |
| 183 | + |
| 184 | + if (userMin !== undefined) stats.min = userMin; |
| 185 | + if (userMax !== undefined) stats.max = userMax; |
| 186 | + |
| 187 | + const currentRefinement = this._getCurrentRefinement(helper); |
| 188 | + |
| 189 | + if (tooltips.format !== undefined) { |
| 190 | + tooltips = [{to: tooltips.format}, {to: tooltips.format}]; |
| 191 | + } |
| 192 | + |
| 193 | + rangeSliderRendering({ |
| 194 | + collapsible, |
| 195 | + cssClasses, |
| 196 | + onChange: this._refine(stats), |
| 197 | + pips, |
| 198 | + range: {min: Math.floor(stats.min), max: Math.ceil(stats.max)}, |
| 199 | + shouldAutoHideContainer: autoHideContainer && stats.min === stats.max, |
| 200 | + start: [currentRefinement.min, currentRefinement.max], |
| 201 | + step, |
| 202 | + templateProps: this._templateProps, |
| 203 | + tooltips, |
| 204 | + format: sliderFormatter, |
| 205 | + containerNode, |
| 206 | + }, false); |
| 207 | + }, |
| 208 | + }; |
| 209 | +}; |
| 210 | + |
| 211 | +export default connectRangeSlider; |
0 commit comments