-
Notifications
You must be signed in to change notification settings - Fork 14
/
searchFacet.js
188 lines (172 loc) · 4.47 KB
/
searchFacet.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
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* For full license text, see the LICENSE file in the repo
* root or https://opensource.org/licenses/apache-2-0/
*/ import { LightningElement, api } from 'lwc';
import generateLabel from './facetLabelGenerator';
/**
* @typedef {import('../searchResults/searchResults').SearchFacet} SearchFacet
*/
/**
* @typedef {import('../searchResults/searchResults').DistinctFacetValue} DistinctFacetValue
*/
/**
* An event fired when a facet has been selected
* @event SearchFacet#facetvaluetoggle
* @type {CustomEvent}
* @property {object} detail CustomEvent details
* @property {string} detail.id
* ID or internal name of the facet value
* @property {boolean} detail.checked
* Whether the value is selected.
* @property {string} detail.facetId
* The selected facet id
*/
/**
* General facet display component
* @fires SearchFacet#facetvaluetoggle
*/
export default class SearchFacet extends LightningElement {
static renderMode = 'light';
/**
* Determines whether we expand to show the facet's values or not
* @type {boolean}
*/
_expanded = true;
/**
* Gets or sets the facet display-data.
* @type {?SearchFacet}
*/
@api
displayData;
get normalizedDisplayData() {
return {
...(this.displayData ?? {}),
nameOrId: this.displayData?.nameOrId ?? '',
displayType: this.displayData?.displayType ?? 'checkbox',
values: this.displayData?.values ?? [],
};
}
/**
* Gets the type of facet being displayed.
* Types supported: 'checkbox', 'radio', 'range', 'datetime'
* @type {?string}
* @private
* @readonly
*/
get type() {
return this.normalizedDisplayData.displayType;
}
/**
* Gets the display name of facet to be displayed.
* @type {?string}
* @private
* @readonly
*/
get name() {
return this.normalizedDisplayData.displayName;
}
/**
* Gets the values of the facet
* @type {DistinctFacetValue[]}
* @private
* @readonly
*/
get values() {
return this.normalizedDisplayData.values;
}
/**
* Determines whether the facet is a slider
* @type {boolean}
*/
get isSlider() {
return this.type === 'range';
}
/**
* Determines whether the facet is a date-time
* @type {boolean}
* @private
* @readonly
*/
get isDateTime() {
return this.type === 'datetime';
}
/**
* Determines whether the facet is a date-time or slider
* @type {boolean}
* @private
* @readonly
*/
get isDateTimeOrSlider() {
return this.isDateTime || this.isSlider;
}
/**
* The minimum value count for the facet, if it is a slider
* @type {?number}
* @private
* @readonly
*/
get minValue() {
return this.values[0].productCount;
}
/**
* The maximum value count for the facet, if it is a slider
* @type {?number}
* @private
* @readonly
*/
get maxValue() {
return this.values[1].productCount;
}
/**
* The icon for the facet toggle button
* @type {string}
* @private
* @readonly
*/
get iconName() {
return this._expanded ? 'utility:chevrondown' : 'utility:chevronup';
}
/**
* The CSS classes for the facet toggle button
* @type {string}
* @private
* @readonly
*/
get facetDisplayClasses() {
return this._expanded ? '' : 'slds-hide';
}
/**
* Gets the label for the facet toggle.
* @type {string} the generated label
* @private
* @readonly
*/
get facetToggleLabel() {
return generateLabel(this._expanded);
}
handleFacetHeaderToggle() {
this._expanded = !this._expanded;
}
/**
* Handle the 'click' event fired from the facet body
* @param {CustomEvent} event The event object
* @fires SearchFacet#facetvaluetoggle
*/
handleFacetToggle(event) {
event.stopImmediatePropagation();
this.dispatchEvent(
new CustomEvent('facetvaluetoggle', {
bubbles: true,
composed: true,
cancelable: true,
detail: {
...event.detail,
facetId: this.normalizedDisplayData.id,
},
})
);
}
}