-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor reviewer unsaved changes notification
Based on feedback, refactor the reviewer unsaved changes notification to avoid deprecated jQuery, use a precise selector for the form element, and remove unrelated code. Credit to dolphin-mixtral:8x7b cfada4ba31c7 on ollama 0.3.7-rc5, to Saurabh Kumar for the selector suggestion, and to Fredrik Jonsson for finding a miss. Issue #3977
- Loading branch information
Showing
3 changed files
with
21 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,24 @@ | ||
(function ($) { | ||
function formContents(f) { | ||
"use strict"; | ||
$(document).ready(function () { | ||
var form = $("form"); | ||
var original = form.serialize(); | ||
// Thanks to https://stackoverflow.com/a/44033425 | ||
return Array.from(new FormData(f), function (e) { | ||
return e.map(encodeURIComponent).join("="); | ||
}).join("&"); | ||
} | ||
|
||
window.onbeforeunload = function () { | ||
if (form.serialize() !== original) { | ||
return "Are you sure you want to leave?"; | ||
} | ||
}; | ||
|
||
form.submit(function () { | ||
window.onbeforeunload = null; | ||
}); | ||
document.addEventListener("DOMContentLoaded", function () { | ||
"use strict"; | ||
const form = document.getElementById("review-form-edit"); | ||
const original = formContents(form); | ||
|
||
// grab all the selectors | ||
let filtered_selectors; | ||
const selectors = Array.prototype.slice.call( | ||
document.querySelectorAll("select") | ||
); | ||
if (selectors.length > 1) { | ||
document.querySelector(".form--score-box").style.display = "block"; | ||
// remove recommendation select box from array | ||
filtered_selectors = selectors.filter( | ||
(selector) => selector[0].text !== "Need More Info" | ||
); | ||
filtered_selectors.forEach(function (selector) { | ||
selector.onchange = calculate_score; | ||
}); | ||
calculate_score(); | ||
} | ||
function calculate_score() { | ||
let score = 0; | ||
filtered_selectors.forEach(function (selector) { | ||
const value = parseInt(selector.value); | ||
if (!isNaN(value) && value !== 99) { | ||
score += value; | ||
} | ||
}); | ||
$(".form--score-box").text("Score: " + score); | ||
window.onbeforeunload = function () { | ||
const formNow = formContents(form); | ||
if (formNow !== original) { | ||
return "Are you sure you want to leave?"; | ||
} | ||
}; | ||
|
||
form.addEventListener("submit", function () { | ||
window.onbeforeunload = null; | ||
}); | ||
})(jQuery); | ||
}); |