Skip to content

Commit 5ac7383

Browse files
grzegorz-bielskiromanowski
authored andcommitted
Attach default filters on input
1 parent 6495aea commit 5ac7383

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

scala3doc/resources/dotty_res/scripts/common/utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const attachDOM = (element, html) => {
1717
}
1818
};
1919

20-
const startsWith = (str, character) => str.charAt(0) === character;
21-
2220
const htmlToString = (html) => {
2321
if (Array.isArray(html)) {
2422
return html.join("");

scala3doc/resources/dotty_res/scripts/components/DocumentableList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class List {
133133

134134
function areFiltersFromElementSelected() {
135135
/** @param str { string } */
136-
const haveKeywordData = str => str.startsWith("f")
136+
const haveKeywordData = key => key === Filter.KeywordsKey
137+
137138
/** @type { [key: string, value: string][] } */
138139
const dataset = Object.entries(elementData.ref.dataset)
139140

scala3doc/resources/dotty_res/scripts/components/Filter.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @typedef { { selected: boolean; visible: boolean } } FilterItem
32
* @typedef { { fKeywords: Record<string, FilterItem> } } Filters
43
*/
54

@@ -22,6 +21,11 @@ class Filter {
2221
*/
2322
static defaultFilterKey = 'default'
2423

24+
/**
25+
* HTML data attribute that contains non-default keywords
26+
*/
27+
static KeywordsKey = 'fKeywords'
28+
2529
get value() {
2630
return this._value;
2731
}
@@ -60,7 +64,6 @@ class Filter {
6064

6165
/**
6266
* @param value { string }
63-
* @returns { FilterItem }
6467
*/
6568
onInputValueChange(value) {
6669
return new Filter(
@@ -76,24 +79,31 @@ class Filter {
7679
* @returns { Filters }
7780
*/
7881
_generateFiltersOnTyping(value) {
79-
const datasets = this.elementsRefs
80-
.filter((elRef) => {
81-
const name = getElementTextContent(getElementNameRef(elRef));
82-
const description = getElementTextContent(getElementDescription(elRef));
82+
const elementsDatasets = this.elementsRefs
83+
.filter(element => {
84+
const name = getElementTextContent(getElementNameRef(element));
85+
const description = getElementTextContent(getElementDescription(element));
8386

8487
return name.includes(value) || description.includes(value);
8588
})
86-
.map((elRef) => this._getDatasetWithKeywordData(elRef.dataset))
87-
89+
.map(element => this._getDatasetWithKeywordData(element.dataset))
8890

89-
return dataset.reduce((filtersObject, datasets) => {
91+
const newFilters = elementsDatasets.reduce((filtersObject, datasets) => {
9092
datasets.forEach(([key, value]) => {
9193
this._splitByComma(value).forEach((val) => {
9294
filtersObject[key] = { ...filtersObject[key], [val]: { ...filtersObject[key][val], visible: true} };
9395
});
9496
});
97+
9598
return filtersObject;
9699
}, this._allFiltersAreHidden());
100+
101+
const shouldAddDefaultFilter = elementsDatasets
102+
.some(d => d.length === 0 || d.some(([key]) => key !== Filter.KeywordsKey))
103+
104+
return shouldAddDefaultFilter
105+
? this._attachDefaultFilters(newFilters)
106+
: newFilters
97107
}
98108

99109
/**
@@ -142,26 +152,37 @@ class Filter {
142152
* @returns { Filters }
143153
*/
144154
_withNewFilters() {
145-
const newFilter = { selected: true, visible: true }
146-
147155
const newFilters = this._elementsRefs.reduce((filtersObject, elementRef) => {
148156
this._getDatasetWithKeywordData(elementRef.dataset).forEach(([key, value]) =>
149157
this._splitByComma(value).forEach((val) => {
150158
filtersObject[key] = filtersObject[key]
151-
? { ...filtersObject[key], [val]: filtersObject[key][val] ?? newFilter}
152-
: { [val]: newFilter }
159+
? { ...filtersObject[key], [val]: filtersObject[key][val] ?? new FilterItem() }
160+
: { [val]: new FilterItem() }
153161
})
154162
);
155163
return filtersObject;
156164
}, {});
157165

158-
const shouldAddDefaultFilter = this._elementsRefs.some(ref => !!ref.dataset['fKeywords'])
166+
const shouldAddDefaultFilter = this._elementsRefs.some(ref => !!ref.dataset[Filter.KeywordsKey])
159167

160168
return shouldAddDefaultFilter
161-
? { ...newFilters, fKeywords: { ...newFilters.fKeywords, [Filter.defaultFilterKey]: newFilter } }
169+
? this._attachDefaultFilters(newFilters)
162170
: newFilters
163171
}
164172

173+
/**
174+
* @private
175+
* @param {Filters} filters
176+
*/
177+
_attachDefaultFilters(filters) {
178+
return {
179+
...filters, [Filter.KeywordsKey]: {
180+
...filters.fKeywords,
181+
[Filter.defaultFilterKey]: new FilterItem()
182+
}
183+
}
184+
}
185+
165186
/**
166187
* @private
167188
* @param key { string }
@@ -193,5 +214,12 @@ class Filter {
193214
* @returns { [key: string, value: string][] }
194215
*/
195216
_getDatasetWithKeywordData = (dataset) =>
196-
Object.entries(dataset).filter(([key]) => startsWith(key, "f"));
217+
Object.entries(dataset).filter(([key]) => key === Filter.KeywordsKey);
197218
}
219+
220+
class FilterItem {
221+
constructor(selected = true, visible = true) {
222+
this.selected = selected
223+
this.visible = visible
224+
}
225+
}

scala3doc/resources/dotty_res/scripts/components/FilterGroup.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class FilterGroup extends Component {
9999
}
100100

101101
render({ filter }) {
102-
console.log(filter.filters)
103102
attachDOM(
104103
this.filterLowerContainerRef,
105104
Object.entries(filter.filters)

0 commit comments

Comments
 (0)