Skip to content

Commit 9c944ac

Browse files
Add default filters for the rest of filter types
1 parent d4d06b6 commit 9c944ac

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const htmlToString = (html) => {
2424
return html;
2525
};
2626

27+
const isFilterData = key => key.startsWith("f")
28+
29+
const getFilterKey = key => `f${key.charAt(0).toUpperCase()}${key.slice(1)}`
30+
2731
const attachListeners = (elementsRefs, type, callback) =>
2832
elementsRefs.map((elRef) => withEvent(elRef, type, callback));
2933

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,36 @@ class List {
132132
}
133133

134134
function areFiltersFromElementSelected() {
135-
/** @param str { string } */
136-
const haveKeywordData = key => key === Filter.KeywordsKey
137-
138135
/** @type { [key: string, value: string][] } */
139136
const dataset = Object.entries(elementData.ref.dataset)
140137

141-
const datasetWithDefaultData = dataset.filter(([key]) => !haveKeywordData(key));
142-
const datasetWithKeywordData = dataset.filter(([key]) => haveKeywordData(key));
138+
const datasetWithFilterData = dataset.filter(([key]) => isFilterData(key));
143139

144-
return datasetWithKeywordData.length > 0
145-
? hasCorrespondingFilters()
146-
: haveDefaultFilters()
140+
return hasCorrespondingFilters() || haveDefaultFilters()
147141

148142
function haveDefaultFilters() {
149143
return (
150-
filter.filters.fKeywords[Filter.defaultFilterKey] &&
151-
filter.filters.fKeywords[Filter.defaultFilterKey].selected &&
152-
datasetWithDefaultData.length >= 1
144+
Object.entries(Filter.defaultFilters).some(([key, value]) => {
145+
const filterKey = getFilterKey(key)
146+
147+
return (
148+
filter.filters[filterKey] &&
149+
filter.filters[filterKey][value].selected &&
150+
!dataset.some(([k]) => k === filterKey)
151+
)
152+
}
153+
)
153154
)
154155
}
155-
156+
157+
// check if any selected filter is on data attr
156158
function hasCorrespondingFilters() {
157-
return (
158-
datasetWithKeywordData
159-
.every(([key, value]) => {
160-
const filterGroup = filter.filters[key];
161-
return value.split(",").every(val => filterGroup && filterGroup[val].selected);
162-
})
163-
)
159+
return datasetWithFilterData
160+
.some(([filterKey, value]) =>
161+
value.split(",").some(val =>
162+
filter.filters[filterKey] && filter.filters[filterKey][val].selected
163+
)
164+
)
164165
}
165166
}
166167
}

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

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/**
2-
* @typedef { { fKeywords: Record<string, FilterItem> } } Filters
2+
* @typedef { Record<string, FilterItem> } FilterMap
3+
* @typedef { "fKeywords" | "fInherited" | "fImplicitly" | "fExtension" | "fVisibility" } FilterAttributes
4+
* @typedef { Record<FilterAttributes, FilterMap> } Filters
35
*/
46

57
class Filter {
@@ -16,15 +18,9 @@ class Filter {
1618
this._filters = this._init ? this._withNewFilters() : filters;
1719
}
1820

19-
/**
20-
* Key for filters without the `fKeywords`
21-
*/
22-
static defaultFilterKey = 'default'
23-
24-
/**
25-
* HTML data attribute that contains non-default keywords
26-
*/
27-
static KeywordsKey = 'fKeywords'
21+
static get defaultFilters() {
22+
return scala3DocData.filterDefaults
23+
}
2824

2925
get value() {
3026
return this._value;
@@ -98,12 +94,8 @@ class Filter {
9894
return filtersObject;
9995
}, this._allFiltersAreHidden());
10096

101-
const shouldAddDefaultFilter = elementsDatasets
102-
.some(d => d.length === 0 || d.some(([key]) => key !== Filter.KeywordsKey))
97+
return this._attachDefaultFilters(newFilters)
10398

104-
return shouldAddDefaultFilter
105-
? this._attachDefaultFilters(newFilters)
106-
: newFilters
10799
}
108100

109101
/**
@@ -163,24 +155,29 @@ class Filter {
163155
return filtersObject;
164156
}, {});
165157

166-
const shouldAddDefaultFilter = this._elementsRefs.some(ref => !!ref.dataset[Filter.KeywordsKey])
167-
168-
return shouldAddDefaultFilter
169-
? this._attachDefaultFilters(newFilters)
170-
: newFilters
158+
return this._attachDefaultFilters(newFilters)
171159
}
172160

173161
/**
174162
* @private
175-
* @param {Filters} filters
163+
* @param {Filters} newFilters
164+
* @returns {Filters}
176165
*/
177-
_attachDefaultFilters(filters) {
178-
return {
179-
...filters, [Filter.KeywordsKey]: {
180-
...filters.fKeywords,
181-
[Filter.defaultFilterKey]: new FilterItem()
182-
}
183-
}
166+
_attachDefaultFilters(newFilters) {
167+
return Object.entries(Filter.defaultFilters).reduce((acc, [key, defaultFilter]) => {
168+
const filterKey = getFilterKey(key)
169+
const shouldAddDefaultKeywordFilter = this._elementsRefs.some(ref => !!ref.dataset[filterKey])
170+
171+
return shouldAddDefaultKeywordFilter
172+
? {
173+
...acc,
174+
[filterKey]: {
175+
...acc[filterKey],
176+
[defaultFilter]: new FilterItem()
177+
}
178+
}
179+
: acc
180+
}, newFilters)
184181
}
185182

186183
/**
@@ -214,7 +211,7 @@ class Filter {
214211
* @returns { [key: string, value: string][] }
215212
*/
216213
_getDatasetWithKeywordData = (dataset) =>
217-
Object.entries(dataset).filter(([key]) => key === Filter.KeywordsKey);
214+
Object.entries(dataset).filter(([key]) => isFilterData(key));
218215
}
219216

220217
class FilterItem {

0 commit comments

Comments
 (0)