Skip to content

Commit ac3abc4

Browse files
committed
MAGE-975: Centralize rangeSlider widget logic
1 parent ef78cdd commit ac3abc4

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

view/frontend/web/js/instantsearch.js

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ define([
10131013
case 'disjunctive':
10141014
return this.getDisjunctiveFacetConfig(facet, panelOptions);
10151015
case 'slider':
1016-
return this.getRangeSliderFacetConfig(facet, panelOptions);
1016+
return this.getRangeSliderFacetConfig(facet);
10171017
}
10181018

10191019
throw new Error(`[Algolia] Invalid facet widget type: ${facet.type}`);
@@ -1050,6 +1050,39 @@ define([
10501050
];
10511051
},
10521052

1053+
/**
1054+
* Docs: https://www.algolia.com/doc/api-reference/widgets/range-slider/js/
1055+
*/
1056+
getRangeSliderFacetConfig(facet) {
1057+
const panelOptions = {
1058+
templates: this.getDefaultFacetPanelTemplates(facet),
1059+
hidden(options) {
1060+
return options.range.min === 0 && options.range.max === 0;
1061+
},
1062+
};
1063+
return [
1064+
'rangeSlider',
1065+
{
1066+
container : facet.wrapper.appendChild(
1067+
algoliaCommon.createISWidgetContainer(facet.attribute)
1068+
),
1069+
attribute : facet.attribute,
1070+
pips : false,
1071+
panelOptions,
1072+
tooltips : {
1073+
format(value) {
1074+
return facet.attribute.match(/price/) === null
1075+
? parseInt(value)
1076+
: priceUtils.formatPrice(
1077+
value,
1078+
algoliaConfig.priceFormat
1079+
);
1080+
},
1081+
},
1082+
},
1083+
];
1084+
},
1085+
10531086
getRefinementListOptions(facet, panelOptions) {
10541087
return {
10551088
container : facet.wrapper.appendChild(
@@ -1059,7 +1092,7 @@ define([
10591092
limit : algoliaConfig.maxValuesPerFacet,
10601093
templates : this.getRefinementsListTemplate(),
10611094
sortBy : ['count:desc', 'name:asc'],
1062-
panelOptions: panelOptions,
1095+
panelOptions
10631096
};
10641097
},
10651098

@@ -1097,38 +1130,15 @@ define([
10971130
return ['refinementList', this.addSearchForFacetValues(facet, refinementListOptions)];
10981131
},
10991132

1100-
/**
1101-
* Docs: https://www.algolia.com/doc/api-reference/widgets/range-slider/js/
1102-
*/
1103-
getRangeSliderFacetConfig(facet, panelOptions) {
1104-
return [
1105-
'rangeSlider',
1106-
{
1107-
container : facet.wrapper.appendChild(
1108-
algoliaCommon.createISWidgetContainer(facet.attribute)
1109-
),
1110-
attribute : facet.attribute,
1111-
pips : false,
1112-
panelOptions,
1113-
tooltips : {
1114-
format(value) {
1115-
return facet.attribute.match(/price/) === null
1116-
? parseInt(value)
1117-
: priceUtils.formatPrice(
1118-
value,
1119-
algoliaConfig.priceFormat
1120-
);
1121-
},
1122-
},
1123-
},
1124-
];
1133+
getDefaultFacetPanelTemplates(facet) {
1134+
return {
1135+
header: `<div class="name">${facet.label || facet.attribute}</div>`,
1136+
};
11251137
},
11261138

11271139
getFacetPanelOptions(facet) {
11281140
return {
1129-
templates: {
1130-
header: `<div class="name">${facet.label || facet.attribute}</div>`,
1131-
},
1141+
templates: this.getDefaultFacetPanelTemplates(facet),
11321142
hidden: (options) => {
11331143
if (!options.results) return true;
11341144

@@ -1165,16 +1175,8 @@ define([
11651175
);
11661176
delete config.panelOptions;
11671177
}
1168-
if (type === 'rangeSlider' && config.attribute.indexOf('price.') < 0) {
1169-
config.panelOptions = {
1170-
hidden(options) {
1171-
return options.range.min === 0 && options.range.max === 0;
1172-
},
1173-
};
1174-
widget = instantsearch.widgets.panel(config.panelOptions)(widget);
1175-
delete config.panelOptions;
1176-
}
11771178

1179+
// TODO: Assumes panel widget - problematic
11781180
search.addWidgets([widget(config)]);
11791181
},
11801182

0 commit comments

Comments
 (0)