Skip to content

Commit

Permalink
Merge pull request #49 from davehunt/url-params
Browse files Browse the repository at this point in the history
Support loading values from URL
  • Loading branch information
davehunt authored Mar 6, 2024
2 parents 91d03e1 + d94f558 commit 78643d3
Showing 1 changed file with 104 additions and 3 deletions.
107 changes: 104 additions & 3 deletions docs/calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,103 @@ <h1><span class="badge bg-dark text-bg-dark" id="impact">low</span></h1>
.querySelector("form")
.addEventListener("reset", () => setTimeout(refreshScore, 0));

function populateFormFromURL() {
var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);

var platforms = urlParams.get('platform');
if (platforms) {
platforms = platforms.split(",");
platforms.forEach(function(platform) {
document.getElementById("platform-" + platform.trim()).checked = true;
});
}

var impactOnBrowser = urlParams.get('browser');
if (impactOnBrowser) {
document.getElementById("browser-" + impactOnBrowser).checked = true;
}

var impactOnSite = urlParams.get('site');
if (impactOnSite) {
document.getElementById("site-" + impactOnSite).checked = true;
}

var configuration = urlParams.get('config');
if (configuration) {
document.getElementById("scenario-" + configuration).checked = true;
}

var pageLoadImpact = urlParams.get('pageload');
if (pageLoadImpact) {
document.getElementById("pageload-" + pageLoadImpact).checked = true;
}

var pagesAffected = urlParams.get('pages');
if (pagesAffected) {
document.getElementById("pages-" + pagesAffected).checked = true;
}

var resourceImpact = urlParams.get('resource');
if (resourceImpact) {
document.getElementById("resource-" + resourceImpact).checked = true;
}

document.getElementById("animation").checked = (urlParams.get('animation') === 'true');
document.getElementById("reproducible").checked = (urlParams.get('reproducible') === 'true');
document.getElementById("multiple-sites").checked = (urlParams.get('multiple-sites') === 'true');
document.getElementById("multiple-reporters").checked = (urlParams.get('multiple-reporters') === 'true');
document.getElementById("chrome").checked = (urlParams.get('chrome') === 'true');

// Refresh the score after populating the form
refreshScore();
}

function generateURLFromForm() {
var params = [];

// Get the values of the platform checkboxes that are checked
var platforms = document.querySelectorAll('input[type=checkbox][id^=platform]:checked');
if (platforms.length > 0) {
var platformValues = [];
platforms.forEach(function(platform) {
platformValues.push(platform.id.replace('platform-', ''));
});
params.push('platform=' + platformValues.join(','));
}

var impactOnBrowser = document.querySelector('input[type=radio][name=affects-browser]:checked');
params.push('browser=' + impactOnBrowser.id.replace('browser-', ''));

var impactOnSite = document.querySelector('input[type=radio][name=affects-site]:checked');
params.push('site=' + impactOnSite.id.replace('site-', ''));

var configuration = document.querySelector('input[type=radio][name=affects-scenario]:checked');
params.push('config=' + configuration.id.replace('scenario-', ''));

var pageLoadImpact = document.querySelector('input[type=radio][name=pageload-impact]:checked');
params.push('pageload=' + pageLoadImpact.id.replace('pageload-', ''));

var pagesAffected = document.querySelector('input[type=radio][name=pages-affected]:checked');
params.push('pages=' + pagesAffected.id.replace('pages-', ''));

var resourceImpact = document.querySelector('input[type=radio][name=resource-impact]:checked');
params.push('resource=' + resourceImpact.id.replace('resource-', ''));

var additionalCheckboxes = document.querySelectorAll('input[type=checkbox]:checked');
additionalCheckboxes.forEach(function(checkbox) {
params.push(checkbox.id + '=true');
});

// Combine all parameters into a single URL string
var url = window.location.origin + window.location.pathname;
if (params.length > 0) {
url += '?' + params.join('&');
}

return url;
}

function generateDebug() {
for (const input of document.querySelectorAll(
"[data-basescore], [data-multiplier]"
Expand Down Expand Up @@ -407,6 +504,8 @@ <h1><span class="badge bg-dark text-bg-dark" id="impact">low</span></h1>
document.querySelector("#impact").textContent = impact;
document.querySelector("#keywords").value =
keywords.size == 0 ? "(none)" : [...keywords].join(", ");
var newURL = generateURLFromForm();
window.history.replaceState(null, null, newURL);
}

function getGroups() {
Expand Down Expand Up @@ -457,7 +556,7 @@ <h1><span class="badge bg-dark text-bg-dark" id="impact">low</span></h1>
function computeTextSummary() {
const { impact, score, scoreInterval } = computeScore();
const summaryLines = [
`The [Performance Impact Calculator](https://mozilla.github.io/perf-triage/calculator.html) has determined this bug's performance impact to be **${impact}**. If you'd like to request re-triage, you can reset the Performance Impact flag to "?" or needinfo the triage sheriff.`,
`The [Performance Impact Calculator](${generateURLFromForm()}) has determined this bug's performance impact to be **${impact}**. If you'd like to request re-triage, you can reset the Performance Impact flag to "?" or needinfo the triage sheriff.`,
""
];
const groups = getGroups();
Expand Down Expand Up @@ -510,8 +609,10 @@ <h1><span class="badge bg-dark text-bg-dark" id="impact">low</span></h1>
navigator.clipboard.writeText(document.querySelector("#keywords").value);
}

generateDebug();
refreshScore();
window.onload = function() {
generateDebug();
populateFormFromURL();
};
</script>
</body>

Expand Down

0 comments on commit 78643d3

Please sign in to comment.