/
utils.js
221 lines (195 loc) · 6.25 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import reduce from 'lodash/collection/reduce';
import forEach from 'lodash/collection/forEach';
import find from 'lodash/collection/find';
import get from 'lodash/object/get';
import isEmpty from 'lodash/lang/isEmpty';
import keys from 'lodash/object/keys';
import uniq from 'lodash/array/uniq';
let utils = {
getContainerNode,
bemHelper,
prepareTemplateProps,
isSpecialClick,
isDomElement,
getRefinements,
clearRefinementsFromState,
clearRefinementsAndSearch
};
/**
* Return the container. If it's a string, it is considered a
* css selector and retrieves the first matching element. Otherwise
* test if it validates that it's a correct DOMElement.
* @param {string|DOMElement} selectorOrHTMLElement a selector or a node
* @return {DOMElement}
* @throws Error when the type is not correct
*/
function getContainerNode(selectorOrHTMLElement) {
let isFromString = (typeof selectorOrHTMLElement === 'string');
let domElement;
if (isFromString) {
domElement = document.querySelector(selectorOrHTMLElement);
} else {
domElement = selectorOrHTMLElement;
}
if (!isDomElement(domElement)) {
let errorMessage = 'Container must be `string` or `HTMLElement`.';
if (isFromString) {
errorMessage += ' Unable to find ' + selectorOrHTMLElement;
}
throw new Error(errorMessage);
}
return domElement;
}
/**
* Returns true if the parameter is a DOMElement.
* @param {any} o the value to test
* @return {boolean} true if o is a DOMElement
*/
function isDomElement(o) {
return o instanceof HTMLElement || !!o && o.nodeType > 0;
}
function isSpecialClick(event) {
let isMiddleClick = (event.button === 1);
return isMiddleClick || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
}
/**
* Creates BEM class name according the vanilla BEM style.
* @param {string} block the main block
* @return {function} function that takes up to 2 parameters
* that determine the element and the modifier of the BEM class.
*/
function bemHelper(block) {
return function(element, modifier) {
// block
if (!element && !modifier) {
return block;
}
// block--element
if (element && !modifier) {
return `${block}--${element}`;
}
// block--element__modifier
if (element && modifier) {
return `${block}--${element}__${modifier}`;
}
// block__modifier
if (!element && modifier) {
return `${block}__${modifier}`;
}
};
}
/**
* Prepares an object to be passed to the Template widget
* @param {object} unknownBecauseES6 an object with the following attributes:
* - transformData
* - defaultTemplate
* - templates
* - templatesConfig
* @return {object} the configuration with the attributes:
* - transformData
* - defaultTemplate
* - templates
* - useCustomCompileOptions
*/
function prepareTemplateProps({
transformData,
defaultTemplates,
templates,
templatesConfig
}) {
let preparedTemplates = prepareTemplates(defaultTemplates, templates);
return {
transformData,
templatesConfig,
...preparedTemplates
};
}
function prepareTemplates(defaultTemplates = {}, templates = {}) {
const allKeys = uniq([...(keys(defaultTemplates)), ...(keys(templates))]);
return reduce(allKeys, (config, key) => {
const defaultTemplate = defaultTemplates[key];
const customTemplate = templates[key];
const isCustomTemplate = customTemplate !== undefined && (customTemplate !== defaultTemplate);
config.templates[key] = isCustomTemplate ? customTemplate : defaultTemplate;
config.useCustomCompileOptions[key] = isCustomTemplate;
return config;
}, {templates: {}, useCustomCompileOptions: {}});
}
function getRefinement(state, type, attributeName, name, resultsFacets) {
let res = {type, attributeName, name};
let facet = find(resultsFacets, {name: attributeName});
let count;
if (type === 'hierarchical') {
let facetDeclaration = state.getHierarchicalFacetByName(attributeName);
let splitted = name.split(facetDeclaration.separator);
res.name = splitted[splitted.length - 1];
for (let i = 0; facet !== undefined && i < splitted.length; ++i) {
facet = find(facet.data, {name: splitted[i]});
}
count = get(facet, 'count');
} else {
count = get(facet, 'data["' + res.name + '"]');
}
const exhaustive = get(facet, 'exhaustive');
if (count !== undefined) {
res.count = count;
}
if (exhaustive !== undefined) {
res.exhaustive = exhaustive;
}
return res;
}
function getRefinements(results, state) {
let res = [];
forEach(state.facetsRefinements, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push(getRefinement(state, 'facet', attributeName, name, results.facets));
});
});
forEach(state.facetsExcludes, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push({type: 'exclude', attributeName, name, exclude: true});
});
});
forEach(state.disjunctiveFacetsRefinements, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push(getRefinement(state, 'disjunctive', attributeName, name, results.disjunctiveFacets));
});
});
forEach(state.hierarchicalFacetsRefinements, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push(getRefinement(state, 'hierarchical', attributeName, name, results.hierarchicalFacets));
});
});
forEach(state.numericRefinements, (operators, attributeName) => {
forEach(operators, (values, operator) => {
forEach(values, (name) => {
name = name + '';
res.push({type: 'numeric', attributeName, name, operator});
});
});
});
forEach(state.tagRefinements, (name) => {
res.push({type: 'tag', attributeName: '_tags', name});
});
return res;
}
function clearRefinementsFromState(state, attributeNames) {
if (isEmpty(attributeNames)) {
state = state.clearTags();
state = state.clearRefinements();
return state;
}
forEach(attributeNames, (attributeName) => {
if (attributeName === '_tags') {
state = state.clearTags();
} else {
state = state.clearRefinements(attributeName);
}
});
return state;
}
function clearRefinementsAndSearch(helper, attributeNames) {
helper.setState(clearRefinementsFromState(helper.state, attributeNames)).search();
}
export default utils;