|
156 | 156 | Object.entries(versionFilterKeyMap).map(([key, value]) => [value, key]) |
157 | 157 | ); |
158 | 158 |
|
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 | | - |
163 | 159 | // loadFromURLParameters retrieves filter settings from the URL parameters and assigns them |
164 | 160 | // to corresponding $scope variables. |
165 | 161 | function loadFromURLParameters() { |
|
211 | 207 | $scope.open[searchParameter] = true; |
212 | 208 | scrollToLintByURL($scope, $location); |
213 | 209 | } |
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 | | - } |
219 | 210 | } |
220 | 211 |
|
221 | 212 | // 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) { |
223 | 214 | const parameter = Object.keys(filterObj) |
224 | 215 | .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() |
225 | 224 | .map(processFilter) |
226 | 225 | .filter(Boolean) // Filters out any falsy values, including null |
227 | 226 | .join(','); |
228 | 227 |
|
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 | + } |
230 | 234 | } |
231 | 235 |
|
232 | 236 | // updateVersionURLParameter updates the version URL parameter with the given version filters |
233 | 237 | function updateVersionURLParameter(versionFilters) { |
234 | 238 | updateURLParameter( |
235 | 239 | versionFilters, |
236 | | - 'versions', |
| 240 | + 'versions', {}, |
237 | 241 | versionFilter => versionFilters[versionFilter].enabled && versionFilters[versionFilter].minorVersion != null |
238 | 242 | ? `${versionFilterKeyMap[versionFilter]}:${versionFilters[versionFilter].minorVersion}` |
239 | 243 | : null |
|
242 | 246 |
|
243 | 247 | // updateAllURLParameters updates all the URL parameters with the current filter settings |
244 | 248 | 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); |
247 | 251 | updateVersionURLParameter($scope.versionFilters); |
248 | 252 | } |
249 | 253 |
|
250 | 254 | // Add $watches to automatically update URL parameters when the data changes |
251 | 255 | $scope.$watch('levels', function (newVal, oldVal) { |
252 | 256 | if (newVal !== oldVal) { |
253 | | - $scope.filtersChanged = true; |
254 | | - updateURLParameter(newVal, 'levels'); |
| 257 | + updateURLParameter(newVal, 'levels', LEVEL_FILTERS_DEFAULT); |
255 | 258 | } |
256 | 259 | }, true); |
257 | 260 |
|
258 | 261 | $scope.$watch('groups', function (newVal, oldVal) { |
259 | 262 | if (newVal !== oldVal) { |
260 | | - $scope.filtersChanged = true; |
261 | | - updateURLParameter(newVal, 'groups'); |
| 263 | + updateURLParameter(newVal, 'groups', GROUPS_FILTER_DEFAULT); |
262 | 264 | } |
263 | 265 | }, true); |
264 | 266 |
|
265 | 267 | $scope.$watch('versionFilters', function (newVal, oldVal) { |
266 | 268 | if (newVal !== oldVal) { |
267 | | - $scope.filtersChanged = true; |
268 | 269 | updateVersionURLParameter(newVal); |
269 | 270 | } |
270 | 271 | }, true); |
|
293 | 294 | }); |
294 | 295 |
|
295 | 296 | $scope.$watch(function () { return $location.search(); }, function (newParameters) { |
296 | | - if (!internalURLChange) { |
297 | | - loadFromURLParameters(); |
298 | | - } |
299 | | - internalURLChange = false; |
| 297 | + loadFromURLParameters(); |
300 | 298 | }, true); |
301 | 299 |
|
302 | 300 | $scope.updatePath = function () { |
|
330 | 328 | }; |
331 | 329 |
|
332 | 330 | $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 | + }; |
339 | 334 | }; |
340 | 335 |
|
341 | 336 | $scope.selectedValuesCount = function (obj) { |
|
439 | 434 | $scope.openLint = function (lint) { |
440 | 435 | $scope.open[lint.id] = true; |
441 | 436 | $location.path(lint.id); |
442 | | - if ($scope.filtersChanged) { |
443 | | - updateAllURLParameters(); |
444 | | - $scope.filtersChanged = false; |
445 | | - } |
446 | 437 | }; |
447 | 438 |
|
448 | 439 | $scope.copyToClipboard = function (lint) { |
|
469 | 460 | // Get data |
470 | 461 | $scope.open = {}; |
471 | 462 | $scope.loading = true; |
472 | | - $scope.filtersChanged = false; |
473 | 463 |
|
474 | 464 | // This will be used to jump into the source code of the version that this documentation is for. |
475 | 465 | $scope.docVersion = window.location.pathname.split('/')[2] || "master"; |
|
0 commit comments