Skip to content

Commit ac279ef

Browse files
committed
Clippy Lints page - Do not show filters in URL if configured as default values
1 parent 2e4ef8e commit ac279ef

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

util/gh-pages/script.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@
156156
Object.entries(versionFilterKeyMap).map(([key, value]) => [value, key])
157157
);
158158

159-
// An internal URL change occurs when we are modifying the URL parameters in a way
160-
// that should not reload parameters from the URL
161-
let internalURLChange = false;
162-
163159
// loadFromURLParameters retrieves filter settings from the URL parameters and assigns them
164160
// to corresponding $scope variables.
165161
function loadFromURLParameters() {
@@ -211,29 +207,37 @@
211207
$scope.open[searchParameter] = true;
212208
scrollToLintByURL($scope, $location);
213209
}
214-
215-
// If there are any filters in the URL, mark that the filters have been changed
216-
if (urlParameters.levels || urlParameters.groups || urlParameters.versions) {
217-
$scope.filtersChanged = true;
218-
}
219210
}
220211

221212
// updateURLParameter updates the URL parameter with the given key to the given value
222-
function updateURLParameter(filterObj, urlKey, processFilter = filter => filter) {
213+
function updateURLParameter(filterObj, urlKey, defaultValue = {}, processFilter = filter => filter) {
223214
const parameter = Object.keys(filterObj)
224215
.filter(filter => filterObj[filter])
216+
.sort()
217+
.map(processFilter)
218+
.filter(Boolean) // Filters out any falsy values, including null
219+
.join(',');
220+
221+
const defaultParameter = Object.keys(defaultValue)
222+
.filter(filter => defaultValue[filter])
223+
.sort()
225224
.map(processFilter)
226225
.filter(Boolean) // Filters out any falsy values, including null
227226
.join(',');
228227

229-
$location.search(urlKey, parameter || null);
228+
// if we ended up back at the defaults, just remove it from the URL
229+
if (parameter === defaultParameter) {
230+
$location.search(urlKey, null);
231+
} else {
232+
$location.search(urlKey, parameter || null);
233+
}
230234
}
231235

232236
// updateVersionURLParameter updates the version URL parameter with the given version filters
233237
function updateVersionURLParameter(versionFilters) {
234238
updateURLParameter(
235239
versionFilters,
236-
'versions',
240+
'versions', {},
237241
versionFilter => versionFilters[versionFilter].enabled && versionFilters[versionFilter].minorVersion != null
238242
? `${versionFilterKeyMap[versionFilter]}:${versionFilters[versionFilter].minorVersion}`
239243
: null
@@ -242,29 +246,26 @@
242246

243247
// updateAllURLParameters updates all the URL parameters with the current filter settings
244248
function updateAllURLParameters() {
245-
updateURLParameter($scope.levels, 'levels');
246-
updateURLParameter($scope.groups, 'groups');
249+
updateURLParameter($scope.levels, 'levels', LEVEL_FILTERS_DEFAULT);
250+
updateURLParameter($scope.groups, 'groups', GROUPS_FILTER_DEFAULT);
247251
updateVersionURLParameter($scope.versionFilters);
248252
}
249253

250254
// Add $watches to automatically update URL parameters when the data changes
251255
$scope.$watch('levels', function (newVal, oldVal) {
252256
if (newVal !== oldVal) {
253-
$scope.filtersChanged = true;
254-
updateURLParameter(newVal, 'levels');
257+
updateURLParameter(newVal, 'levels', LEVEL_FILTERS_DEFAULT);
255258
}
256259
}, true);
257260

258261
$scope.$watch('groups', function (newVal, oldVal) {
259262
if (newVal !== oldVal) {
260-
$scope.filtersChanged = true;
261-
updateURLParameter(newVal, 'groups');
263+
updateURLParameter(newVal, 'groups', GROUPS_FILTER_DEFAULT);
262264
}
263265
}, true);
264266

265267
$scope.$watch('versionFilters', function (newVal, oldVal) {
266268
if (newVal !== oldVal) {
267-
$scope.filtersChanged = true;
268269
updateVersionURLParameter(newVal);
269270
}
270271
}, true);
@@ -293,10 +294,7 @@
293294
});
294295

295296
$scope.$watch(function () { return $location.search(); }, function (newParameters) {
296-
if (!internalURLChange) {
297-
loadFromURLParameters();
298-
}
299-
internalURLChange = false;
297+
loadFromURLParameters();
300298
}, true);
301299

302300
$scope.updatePath = function () {
@@ -330,12 +328,9 @@
330328
};
331329

332330
$scope.resetGroupsToDefault = function () {
333-
const groups = $scope.groups;
334-
for (const [key, value] of Object.entries(GROUPS_FILTER_DEFAULT)) {
335-
groups[key] = value;
336-
}
337-
internalURLChange = true;
338-
$location.search('groups', null);
331+
$scope.groups = {
332+
...GROUPS_FILTER_DEFAULT
333+
};
339334
};
340335

341336
$scope.selectedValuesCount = function (obj) {
@@ -439,10 +434,6 @@
439434
$scope.openLint = function (lint) {
440435
$scope.open[lint.id] = true;
441436
$location.path(lint.id);
442-
if ($scope.filtersChanged) {
443-
updateAllURLParameters();
444-
$scope.filtersChanged = false;
445-
}
446437
};
447438

448439
$scope.copyToClipboard = function (lint) {
@@ -469,7 +460,6 @@
469460
// Get data
470461
$scope.open = {};
471462
$scope.loading = true;
472-
$scope.filtersChanged = false;
473463

474464
// This will be used to jump into the source code of the version that this documentation is for.
475465
$scope.docVersion = window.location.pathname.split('/')[2] || "master";

0 commit comments

Comments
 (0)