Completely remove angular and generate parts of settings in JS

This commit is contained in:
Guillaume Gomez 2024-08-13 23:43:35 +02:00
parent 574e3dd922
commit f2193c680c
2 changed files with 102 additions and 447 deletions

View file

@ -25,7 +25,7 @@ Otherwise, have a great day =^.^=
<link id="styleAyu" rel="stylesheet" href="https://rust-lang.github.io/mdBook/ayu-highlight.css" disabled="true">
<link rel="stylesheet" href="style.css">
</head>
<body ng-app="clippy" ng-controller="lintList">
<body>
<div id="settings-dropdown">
<div class="settings-icon" tabindex="-1"></div>
<div class="settings-menu" tabindex="-1">
@ -59,121 +59,90 @@ Otherwise, have a great day =^.^=
<div class="panel panel-default">
<div class="panel-body row">
<div id="upper-filters" class="col-12 col-md-5">
<div class="btn-group" filter-dropdown>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle">
Lint levels <span class="badge">{{selectedValuesCount(levels)}}</span> <span class="caret"></span>
Lint levels <span class="badge" id="selected-levels">4</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<ul class="dropdown-menu" id="lint-levels">
<li class="checkbox">
<label ng-click="toggleLevels(true)">
<label onclick="toggleLevels(true)">
<input type="checkbox" class="invisible" />
All
</label>
</li>
<li class="checkbox">
<label ng-click="toggleLevels(false)">
<label onclick="toggleLevels(false)">
<input type="checkbox" class="invisible" />
None
</label>
</li>
<li role="separator" class="divider"></li>
<li class="checkbox" ng-repeat="(level, enabled) in levels">
<label class="text-capitalize">
<input type="checkbox" ng-model="levels[level]" />
{{level}}
</label>
</li>
</ul>
</div>
<div class="btn-group" filter-dropdown>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle">
Lint groups <span class="badge">{{selectedValuesCount(groups)}}</span> <span class="caret"></span>
Lint groups <span class="badge" id="selected-groups"></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<ul class="dropdown-menu" id="lint-groups">
<li class="checkbox">
<label ng-click="toggleGroups(true)">
<label onclick="toggleGroups(true)">
<input type="checkbox" class="invisible" />
All
</label>
</li>
<li class="checkbox">
<label ng-click="resetGroupsToDefault()">
<label onclick="resetGroupsToDefault()">
<input type="checkbox" class="invisible" />
Default
</label>
</li>
<li class="checkbox">
<label ng-click="toggleGroups(false)">
<label onclick="toggleGroups(false)">
<input type="checkbox" class="invisible" />
None
</label>
</li>
<li role="separator" class="divider"></li>
<li class="checkbox" ng-repeat="(group, enabled) in groups">
<label class="text-capitalize">
<input type="checkbox" ng-model="groups[group]" />
{{group}}
</label>
</li>
</ul>
</div>
<div id="version-filter">
<div class="btn-group" filter-dropdown>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle">
Version
<span id="version-filter-count" class="badge">
{{versionFilterCount(versionFilters)}}
</span>
<span id="version-filter-count" class="badge">0</span>
<span class="caret"></span>
</button>
<ul id="version-filter-selector" class="dropdown-menu">
<li class="checkbox">
<label ng-click="clearVersionFilters()">
<label onclick="clearVersionFilters()">
<input type="checkbox" class="invisible" />
Clear filters
</label>
</li>
<li role="separator" class="divider"></li>
<li class="checkbox" ng-repeat="(filter, vars) in versionFilters">
<label ng-attr-for="filter-{filter}">{{filter}}</label>
<span>1.</span>
<input type="number"
min="29"
ng-attr-id="filter-{filter}"
class="version-filter-input form-control filter-input"
maxlength="2"
ng-model="versionFilters[filter].minorVersion"
ng-model-options="{debounce: 50}"
ng-change="updateVersionFilters()" />
<span>.0</span>
</li>
</ul>
</div>
</div>
<div class="btn-group" filter-dropdown>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle">
Applicability <span class="badge">{{selectedValuesCount(applicabilities)}}</span> <span class="caret"></span>
Applicability
<span class="badge"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<ul class="dropdown-menu" id="lint-applicabilities">
<li class="checkbox">
<label ng-click="toggleApplicabilities(true)">
<label onclick="toggleApplicabilities(true)">
<input type="checkbox" class="invisible" />
All
</label>
</li>
<li class="checkbox">
<label ng-click="toggleApplicabilities(false)">
<label onclick="toggleApplicabilities(false)">
<input type="checkbox" class="invisible" />
None
</label>
</li>
<li role="separator" class="divider"></li>
<li class="checkbox" ng-repeat="(applicability, enabled) in applicabilities">
<label class="text-capitalize">
<input type="checkbox" ng-model="applicabilities[applicability]" />
{{applicability}}
</label>
</li>
</ul>
</div>
</div>
@ -189,10 +158,10 @@ Otherwise, have a great day =^.^=
</div>
</div>
<div class="col-12 col-md-2 btn-group expansion-group">
<button title="Collapse All" class="btn btn-default expansion-control" type="button" ng-click="toggleExpansion(data, false)">
<button title="Collapse All" class="btn btn-default expansion-control" type="button" onclick="toggleExpansion(false)">
<span class="glyphicon glyphicon-collapse-up"></span>
</button>
<button title="Expand All" class="btn btn-default expansion-control" type="button" ng-click="toggleExpansion(data, true)">
<button title="Expand All" class="btn btn-default expansion-control" type="button" onclick="toggleExpansion(true)">
<span class="glyphicon glyphicon-collapse-down"></span>
</button>
</div>
@ -315,7 +284,6 @@ Otherwise, have a great day =^.^=
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/rust.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.12/angular.min.js"></script>
<script src="script.js"></script>
</body>
</html>

View file

@ -1,394 +1,3 @@
(function () {
function selectGroup($scope, selectedGroup) {
const groups = $scope.groups;
for (const group in groups) {
if (groups.hasOwnProperty(group)) {
groups[group] = group === selectedGroup;
}
}
}
angular.module("clippy", [])
.directive('filterDropdown', function ($document) {
return {
restrict: 'A',
link: function ($scope, $element, $attr) {
$element.bind('click', function (event) {
if (event.target.closest('button')) {
$element.toggleClass('open');
} else {
$element.addClass('open');
}
$element.addClass('open-recent');
});
$document.bind('click', function () {
if (!$element.hasClass('open-recent')) {
$element.removeClass('open');
}
$element.removeClass('open-recent');
})
}
}
})
.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit(attr.onFinishRender);
});
}
}
};
})
.controller("lintList", function ($scope, $http, $location, $timeout) {
// Level filter
const LEVEL_FILTERS_DEFAULT = {allow: true, warn: true, deny: true, none: true};
$scope.levels = { ...LEVEL_FILTERS_DEFAULT };
$scope.byLevels = function (lint) {
return $scope.levels[lint.level];
};
const GROUPS_FILTER_DEFAULT = {
cargo: true,
complexity: true,
correctness: true,
nursery: true,
pedantic: true,
perf: true,
restriction: true,
style: true,
suspicious: true,
deprecated: false,
}
$scope.groups = {
...GROUPS_FILTER_DEFAULT
};
$scope.versionFilters = {
"≥": {enabled: false, minorVersion: null },
"≤": {enabled: false, minorVersion: null },
"=": {enabled: false, minorVersion: null },
};
// Map the versionFilters to the query parameters in a way that is easier to work with in a URL
const versionFilterKeyMap = {
"≥": "gte",
"≤": "lte",
"=": "eq"
};
const reverseVersionFilterKeyMap = Object.fromEntries(
Object.entries(versionFilterKeyMap).map(([key, value]) => [value, key])
);
const APPLICABILITIES_FILTER_DEFAULT = {
MachineApplicable: true,
MaybeIncorrect: true,
HasPlaceholders: true,
Unspecified: true,
};
$scope.applicabilities = {
...APPLICABILITIES_FILTER_DEFAULT
}
// loadFromURLParameters retrieves filter settings from the URL parameters and assigns them
// to corresponding $scope variables.
function loadFromURLParameters() {
// Extract parameters from URL
const urlParameters = $location.search();
// Define a helper function that assigns URL parameters to a provided scope variable
const handleParameter = (parameter, scopeVariable, defaultValues) => {
if (urlParameters[parameter]) {
const items = urlParameters[parameter].split(',');
for (const key in scopeVariable) {
if (scopeVariable.hasOwnProperty(key)) {
scopeVariable[key] = items.includes(key);
}
}
} else if (defaultValues) {
for (const key in defaultValues) {
if (scopeVariable.hasOwnProperty(key)) {
scopeVariable[key] = defaultValues[key];
}
}
}
};
handleParameter('levels', $scope.levels, LEVEL_FILTERS_DEFAULT);
handleParameter('groups', $scope.groups, GROUPS_FILTER_DEFAULT);
handleParameter('applicabilities', $scope.applicabilities, APPLICABILITIES_FILTER_DEFAULT);
// Handle 'versions' parameter separately because it needs additional processing
if (urlParameters.versions) {
const versionFilters = urlParameters.versions.split(',');
for (const versionFilter of versionFilters) {
const [key, minorVersion] = versionFilter.split(':');
const parsedMinorVersion = parseInt(minorVersion);
// Map the key from the URL parameter to its original form
const originalKey = reverseVersionFilterKeyMap[key];
if (originalKey in $scope.versionFilters && !isNaN(parsedMinorVersion)) {
$scope.versionFilters[originalKey].enabled = true;
$scope.versionFilters[originalKey].minorVersion = parsedMinorVersion;
}
}
}
// Load the search parameter from the URL path
const searchParameter = $location.path().substring(1); // Remove the leading slash
if (searchParameter) {
$scope.search = searchParameter;
$scope.open[searchParameter] = true;
scrollToLintByURL($scope, $location);
}
}
// updateURLParameter updates the URL parameter with the given key to the given value
function updateURLParameter(filterObj, urlKey, defaultValue = {}, processFilter = filter => filter) {
const parameter = Object.keys(filterObj)
.filter(filter => filterObj[filter])
.sort()
.map(processFilter)
.filter(Boolean) // Filters out any falsy values, including null
.join(',');
const defaultParameter = Object.keys(defaultValue)
.filter(filter => defaultValue[filter])
.sort()
.map(processFilter)
.filter(Boolean) // Filters out any falsy values, including null
.join(',');
// if we ended up back at the defaults, just remove it from the URL
if (parameter === defaultParameter) {
$location.search(urlKey, null);
} else {
$location.search(urlKey, parameter || null);
}
}
// updateVersionURLParameter updates the version URL parameter with the given version filters
function updateVersionURLParameter(versionFilters) {
updateURLParameter(
versionFilters,
'versions', {},
versionFilter => versionFilters[versionFilter].enabled && versionFilters[versionFilter].minorVersion != null
? `${versionFilterKeyMap[versionFilter]}:${versionFilters[versionFilter].minorVersion}`
: null
);
}
// updateAllURLParameters updates all the URL parameters with the current filter settings
function updateAllURLParameters() {
updateURLParameter($scope.levels, 'levels', LEVEL_FILTERS_DEFAULT);
updateURLParameter($scope.groups, 'groups', GROUPS_FILTER_DEFAULT);
updateVersionURLParameter($scope.versionFilters);
updateURLParameter($scope.applicabilities, 'applicabilities', APPLICABILITIES_FILTER_DEFAULT);
}
// Add $watches to automatically update URL parameters when the data changes
$scope.$watch('levels', function (newVal, oldVal) {
if (newVal !== oldVal) {
updateURLParameter(newVal, 'levels', LEVEL_FILTERS_DEFAULT);
}
}, true);
$scope.$watch('groups', function (newVal, oldVal) {
if (newVal !== oldVal) {
updateURLParameter(newVal, 'groups', GROUPS_FILTER_DEFAULT);
}
}, true);
$scope.$watch('versionFilters', function (newVal, oldVal) {
if (newVal !== oldVal) {
updateVersionURLParameter(newVal);
}
}, true);
$scope.$watch('applicabilities', function (newVal, oldVal) {
if (newVal !== oldVal) {
updateURLParameter(newVal, 'applicabilities', APPLICABILITIES_FILTER_DEFAULT)
}
}, true);
// Watch for changes in the URL path and update the search and lint display
$scope.$watch(function () { return $location.path(); }, function (newPath) {
const searchParameter = newPath.substring(1);
if ($scope.search !== searchParameter) {
$scope.search = searchParameter;
$scope.open[searchParameter] = true;
scrollToLintByURL($scope, $location);
}
});
let debounceTimeout;
$scope.$watch('search', function (newVal, oldVal) {
if (newVal !== oldVal) {
if (debounceTimeout) {
$timeout.cancel(debounceTimeout);
}
debounceTimeout = $timeout(function () {
$location.path(newVal);
}, 1000);
}
});
$scope.$watch(function () { return $location.search(); }, function (newParameters) {
loadFromURLParameters();
}, true);
$scope.updatePath = function () {
if (debounceTimeout) {
$timeout.cancel(debounceTimeout);
}
$location.path($scope.search);
}
$scope.toggleLevels = function (value) {
const levels = $scope.levels;
for (const key in levels) {
if (levels.hasOwnProperty(key)) {
levels[key] = value;
}
}
};
$scope.toggleGroups = function (value) {
const groups = $scope.groups;
for (const key in groups) {
if (groups.hasOwnProperty(key)) {
groups[key] = value;
}
}
};
$scope.toggleApplicabilities = function (value) {
const applicabilities = $scope.applicabilities;
for (const key in applicabilities) {
if (applicabilities.hasOwnProperty(key)) {
applicabilities[key] = value;
}
}
}
$scope.resetGroupsToDefault = function () {
$scope.groups = {
...GROUPS_FILTER_DEFAULT
};
};
$scope.selectedValuesCount = function (obj) {
return Object.values(obj).filter(x => x).length;
}
$scope.clearVersionFilters = function () {
for (const filter in $scope.versionFilters) {
$scope.versionFilters[filter] = { enabled: false, minorVersion: null };
}
}
$scope.versionFilterCount = function(obj) {
return Object.values(obj).filter(x => x.enabled).length;
}
$scope.updateVersionFilters = function() {
for (const filter in $scope.versionFilters) {
const minorVersion = $scope.versionFilters[filter].minorVersion;
// 1.29.0 and greater
if (minorVersion && minorVersion > 28) {
$scope.versionFilters[filter].enabled = true;
continue;
}
$scope.versionFilters[filter].enabled = false;
}
}
$scope.byVersion = function(lint) {
const filters = $scope.versionFilters;
for (const filter in filters) {
if (filters[filter].enabled) {
const minorVersion = filters[filter].minorVersion;
// Strip the "pre " prefix for pre 1.29.0 lints
const lintVersion = lint.version.startsWith("pre ") ? lint.version.substring(4, lint.version.length) : lint.version;
const lintMinorVersion = lintVersion.substring(2, 4);
switch (filter) {
// "=" gets the highest priority, since all filters are inclusive
case "=":
return (lintMinorVersion == minorVersion);
case "≥":
if (lintMinorVersion < minorVersion) { return false; }
break;
case "≤":
if (lintMinorVersion > minorVersion) { return false; }
break;
default:
return true
}
}
}
return true;
}
$scope.byGroups = function (lint) {
return $scope.groups[lint.group];
};
$scope.byApplicabilities = function (lint) {
return $scope.applicabilities[lint.applicability];
};
$scope.toggleExpansion = function(lints, isExpanded) {
lints.forEach(lint => {
$scope.open[lint.id] = isExpanded;
});
}
// Get data
$scope.open = {};
$scope.loading = true;
// This will be used to jump into the source code of the version that this documentation is for.
$scope.docVersion = window.location.pathname.split('/')[2] || "master";
// Set up the filters from the URL parameters before we start loading the data
loadFromURLParameters();
const selectedGroup = getQueryVariable("sel");
if (selectedGroup) {
selectGroup($scope, selectedGroup.toLowerCase());
}
setTimeout(function () {
const el = document.getElementById('filter-input');
if (el) { el.focus() }
}, 0);
});
})();
function getQueryVariable(variable) {
const query = window.location.search.substring(1);
const vars = query.split('&');
for (const entry of vars) {
const pair = entry.split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
}
function storeValue(settingName, value) {
try {
localStorage.setItem(`clippy-lint-list-${settingName}`, value);
@ -611,6 +220,34 @@ function handleBlur(event) {
}
}
function toggleExpansion(expand) {
onEachLazy(
document.querySelectorAll("article"),
expand ? el => el.classList.remove("collapsed") : el => el.classList.add("collapsed"),
);
}
function generateListOfOptions(list, elementId) {
let html = '';
let nbEnabled = 0;
for (const [key, value] of Object.entries(list)) {
const attr = value ? " checked" : "";
html += `\
<li class="checkbox">\
<label class="text-capitalize">\
<input type="checkbox"${attr}/>${key}\
</label>\
</li>`;
if (value) {
nbEnabled += 1;
}
}
const elem = document.getElementById(elementId);
elem.previousElementSibling.querySelector(".badge").innerText = `${nbEnabled}`;
elem.innerHTML += html;
}
function generateSettings() {
const settings = document.getElementById("settings-dropdown");
const settingsButton = settings.querySelector(".settings-icon")
@ -622,6 +259,56 @@ function generateSettings() {
settingsMenu.querySelectorAll("input"),
el => el.onblur = handleBlur,
);
const LEVEL_FILTERS_DEFAULT = {allow: true, warn: true, deny: true, none: true};
generateListOfOptions(LEVEL_FILTERS_DEFAULT, "lint-levels");
// Generate lint groups.
const GROUPS_FILTER_DEFAULT = {
cargo: true,
complexity: true,
correctness: true,
deprecated: false,
nursery: true,
pedantic: true,
perf: true,
restriction: true,
style: true,
suspicious: true,
};
generateListOfOptions(GROUPS_FILTER_DEFAULT, "lint-groups");
const APPLICABILITIES_FILTER_DEFAULT = {
Unspecified: true,
Unresolved: true,
MachineApplicable: true,
MaybeIncorrect: true,
HasPlaceholders: true
};
generateListOfOptions(APPLICABILITIES_FILTER_DEFAULT, "lint-applicabilities");
const VERSIONS_FILTERS = {
"≥": {enabled: false, minorVersion: null },
"≤": {enabled: false, minorVersion: null },
"=": {enabled: false, minorVersion: null },
};
let html = '';
for (const kind of ["≥", "≤", "="]) {
html += `\
<li class="checkbox">\
<label>${kind}</label>\
<span>1.</span> \
<input type="number" \
min="29" \
id="filter-${kind}" \
class="version-filter-input form-control filter-input" \
maxlength="2" \
onchange="updateVersionFilters()" />\
<span>.0</span>\
</li>`;
}
document.getElementById("version-filter-selector").innerHTML += html;
}
function generateSearch() {