Remove direct use of jQuery

jQuery is still required by jQuery UI
This commit is contained in:
rubenwardy 2023-08-26 12:34:55 +01:00
parent 9eb03c6a57
commit 2f458ba40e
22 changed files with 233 additions and 191 deletions

@ -6,17 +6,17 @@ toc: False
Please reconsider the choice of WTFPL as a license.
<script src="/static/libs/jquery.min.js"></script>
<script>
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
var params = new URLSearchParams(location.search);
var r = params.get("r");
if (r)
var r = params.get("r");
if (r) {
document.write("<a class='alert_right button' href='" + r + "'>Okay</a>");
else
$("#warning").hide();
} else {
document.getElementById("warning").style.display = "none";
}
</script>
</div>

@ -1,6 +1,8 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
function updateOrder() {
const elements = [...document.querySelector(".sortable").children];
@ -33,7 +35,9 @@ function restorePackage(id) {
return false;
}
const card = idElement.parentNode.parentNode;
const card = idElement.parentNode.parentNode.parentNode.parentNode;
console.assert(card.classList.contains("card"));
card.classList.remove("d-none");
card.querySelector("input[name^=package_removed]").value = "0";
card.scrollIntoView();
@ -176,7 +180,9 @@ function onPackageQueryUpdate() {
window.addEventListener("load", () => {
document.querySelectorAll(".remove-package").forEach(button => {
const card = button.parentNode.parentNode;
const card = button.parentNode.parentNode.parentNode.parentNode;
console.assert(card.classList.contains("card"));
const field = card.querySelector("input[name^=package_removed]");
// Reloading/validation errors will cause this to be 1 at load
@ -191,6 +197,12 @@ window.addEventListener("load", () => {
addPackageQuery.value = "";
addPackageQuery.classList.remove("d-none");
addPackageQuery.addEventListener("input", onPackageQueryUpdate);
addPackageQuery.addEventListener('keydown',(e)=>{
if (e.key === "Enter") {
onPackageQueryUpdate();
e.preventDefault();
}
})
updateOrder();
$(".sortable").sortable({

@ -1,7 +1,9 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
$("textarea.markdown").each(function() {
"use strict";
document.querySelectorAll("textarea.markdown").forEach((element) => {
async function render(plainText, preview) {
const response = await fetch(new Request("/api/markdown/", {
method: "POST",
@ -16,18 +18,8 @@ $("textarea.markdown").each(function() {
}
let timeout_id = null;
function urlInserter(url) {
return (editor) => {
var cm = editor.codemirror;
var stat = getState(cm);
var options = editor.options;
_replaceSelection(cm, stat.table, `[](${url})`);
};
}
this.easy_mde = new EasyMDE({
element: this,
element.easy_mde = new EasyMDE({
element: element,
hideIcons: ["image"],
showIcons: ["code", "table"],
forceSync: true,
@ -49,20 +41,6 @@ $("textarea.markdown").each(function() {
"fullscreen",
"|",
"guide",
// {
// name: "rules",
// className: "fa fa-book",
// title: "others buttons",
// children: [
// {
// name: "rules",
// action: urlInserter("/policy_and_guidance/#2-accepted-content"),
// className: "fa fa-star",
// title: "2. Accepted content",
// text: "2. Accepted content",
// },
// ]
// },
],
previewRender: (plainText, preview) => {
if (timeout_id) {
@ -70,7 +48,7 @@ $("textarea.markdown").each(function() {
}
timeout_id = setTimeout(() => {
render(plainText, preview);
render(plainText, preview).catch(console.error);
timeout_id = null;
}, 500);

@ -3,7 +3,6 @@
"use strict";
const labelColor = "#bbb";
const annotationColor = "#bbb";
const annotationLabelBgColor = "#444";
@ -304,4 +303,4 @@ function setup_chart(ctx, data, annotations) {
}
$(load_data);
window.addEventListener("load", load_data);

@ -1,36 +1,47 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
$(function() {
"use strict";
function hide(sel) {
document.querySelectorAll(sel).forEach(x => x.classList.add("d-none"));
}
function show(sel) {
document.querySelectorAll(sel).forEach(x => x.classList.remove("d-none"));
}
window.addEventListener("load", () => {
function finish() {
$(".pkg_wiz_1").hide()
$(".pkg_wiz_2").hide()
$(".pkg_repo").show()
$(".pkg_meta").show()
hide(".pkg_wiz_1");
hide(".pkg_wiz_2");
show(".pkg_repo");
show(".pkg_meta");
}
$(".pkg_meta").hide()
$(".pkg_wiz_1").show()
hide(".pkg_meta");
show(".pkg_wiz_1");
$("#pkg_wiz_1_skip").click(finish)
$("#pkg_wiz_1_next").click(function() {
const repoURL = $("#repo").val();
if (repoURL.trim() != "") {
$(".pkg_wiz_1").hide()
$(".pkg_wiz_2").show()
$(".pkg_repo").hide()
document.getElementById("pkg_wiz_1_skip").addEventListener("click", finish);
document.getElementById("pkg_wiz_1_next").addEventListener("click", () => {
const repoURL = document.getElementById("repo").value;
if (repoURL.trim() !== "") {
hide(".pkg_wiz_1");
show(".pkg_wiz_2");
hide(".pkg_repo");
function setField(id, value) {
if (value && value != "") {
const ele = $(id);
ele.val(value);
ele.trigger("change");
function setField(sel, value) {
if (value && value !== "") {
const ele = document.querySelector(sel);
ele.value = value;
ele.dispatchEvent(new Event("change"));
// EasyMDE doesn't always refresh the codemirror correctly
if (ele[0].easy_mde) {
if (ele.easy_mde) {
setTimeout(() => {
ele[0].easy_mde.value(value);
ele[0].easy_mde.codemirror.refresh()
ele.easy_mde.value(value);
ele.easy_mde.codemirror.refresh()
}, 100);
}
}
@ -45,19 +56,19 @@ $(function() {
setField("#short_desc", result.short_desc);
setField("#forums", result.forums);
if (result.type && result.type.length > 2) {
$("#type").val(result.type);
setField("[name='type']", result.type);
}
finish();
}).catch(function(e) {
alert(e);
$(".pkg_wiz_1").show();
$(".pkg_wiz_2").hide();
$(".pkg_repo").show();
show(".pkg_wiz_1");
hide(".pkg_wiz_2");
show(".pkg_repo");
// finish()
});
} else {
finish()
finish();
}
})
})

@ -1,20 +1,33 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
$(function() {
$("#type").change(function() {
$(".not_mod, .not_game, .not_txp").show()
$(".not_" + this.value.toLowerCase()).hide()
})
$(".not_mod, .not_game, .not_txp").show()
$(".not_" + $("#type").val().toLowerCase()).hide()
"use strict";
$("#forums").on('paste', function(e) {
function hide(sel) {
document.querySelectorAll(sel).forEach(x => x.classList.add("d-none"));
}
function show(sel) {
document.querySelectorAll(sel).forEach(x => x.classList.remove("d-none"));
}
window.addEventListener("load", () => {
const typeEle = document.getElementById("type");
typeEle.addEventListener("change", () => {
show(".not_mod, .not_game, .not_txp");
hide(".not_" + typeEle.value.toLowerCase());
})
show(".not_mod, .not_game, .not_txp");
hide(".not_" + typeEle.value.toLowerCase());
const forumsField = document.getElementById("forums");
forumsField.addEventListener("paste", function(e) {
try {
const pasteData = e.originalEvent.clipboardData.getData('text');
const pasteData = e.clipboardData.getData('text');
const url = new URL(pasteData);
if (url.hostname == "forum.minetest.net") {
$(this).val(url.searchParams.get("t"));
if (url.hostname === "forum.minetest.net") {
forumsField.value = url.searchParams.get("t");
e.preventDefault();
}
} catch (e) {
@ -22,8 +35,9 @@ $(function() {
}
});
$("#forums-button").click(function(e) {
window.open("https://forum.minetest.net/viewtopic.php?t=" + $("#forums").val(), "_blank");
const openForums = document.getElementById("forums-button");
openForums.addEventListener("click", () => {
window.open("https://forum.minetest.net/viewtopic.php?t=" + forumsField.value, "_blank");
});
let hint = null;
@ -32,9 +46,13 @@ $(function() {
hint.remove();
}
hint = ele.parent()
.append(`<div class="alert alert-warning my-3">${text}</div>`)
.find(".alert");
hint = document.createElement("div");
hint.classList.add("alert");
hint.classList.add("alert-warning");
hint.classList.add("my-1");
hint.innerHTML = text;
ele.parentNode.appendChild(hint);
}
let hint_mtmods = `Tip:
@ -43,26 +61,24 @@ $(function() {
let hint_thegame = `Tip:
It's obvious that this adds something to Minetest,
there's no need to use phrases such as \"adds X to the game\".`
there's no need to use phrases such as \"adds X to the game\".`;
$("#short_desc").on("change paste keyup", function() {
const val = $(this).val().toLowerCase();
const shortDescField = document.getElementById("short_desc");
function handleShortDescChange() {
const val = shortDescField.value.toLowerCase();
if (val.indexOf("minetest") >= 0 || val.indexOf("mod") >= 0 ||
val.indexOf("modpack") >= 0 || val.indexOf("mod pack") >= 0) {
showHint($(this), hint_mtmods);
showHint(shortDescField, hint_mtmods);
} else if (val.indexOf("the game") >= 0) {
showHint($(this), hint_thegame);
showHint(shortDescField, hint_thegame);
} else if (hint) {
hint.remove();
hint = null;
}
})
}
const btn = $("#forums").parent().find("label").append("<a class='ms-3 btn btn-sm btn-primary'>Open</a>");
btn.click(function() {
const id = $("#forums").val();
if (/^\d+$/.test(id)) {
window.open("https://forum.minetest.net/viewtopic.php?t=" + id, "_blank");
}
});
shortDescField.addEventListener("change", handleShortDescChange);
shortDescField.addEventListener("paste", handleShortDescChange);
shortDescField.addEventListener("keyup", handleShortDescChange);
})

@ -1,16 +1,18 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
function getJSON(url, method) {
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
fetch(new Request(url, {
method: method || "get",
credentials: "same-origin",
headers: {
"Accept": "application/json",
},
})).then(function(response) {
response.text().then(function(txt) {
})).then((response) => {
response.text().then((txt) => {
resolve(JSON.parse(txt))
}).catch(reject)
}).catch(reject)
@ -18,7 +20,7 @@ function getJSON(url, method) {
}
function pollTask(poll_url, disableTimeout) {
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
let tries = 0;
function retry() {
@ -32,11 +34,11 @@ function pollTask(poll_url, disableTimeout) {
}
}
function step() {
getJSON(poll_url).then(function(res) {
if (res.status == "SUCCESS") {
getJSON(poll_url).then((res) => {
if (res.status === "SUCCESS") {
console.log("Got result")
resolve(res.result)
} else if (res.status == "FAILURE" || res.status == "REVOKED") {
} else if (res.status === "FAILURE" || res.status === "REVOKED") {
reject(res.error || "Unknown server error")
} else {
retry()
@ -49,8 +51,8 @@ function pollTask(poll_url, disableTimeout) {
function performTask(url) {
return new Promise(function(resolve, reject) {
getJSON(url, "post").then(function(startResult) {
return new Promise((resolve, reject) => {
getJSON(url, "post").then((startResult) => {
console.log(startResult)
if (typeof startResult.poll_url == "string") {
pollTask(startResult.poll_url).then(resolve).catch(reject)

@ -0,0 +1,28 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
window.addEventListener("load", () => {
function setup_toggle(type) {
const toggle = document.getElementById("set_" + type);
function on_change() {
const rel = document.getElementById(type + "_rel");
if (toggle.checked) {
rel.parentElement.style.opacity = "1";
} else {
// $("#" + type + "_rel").attr("disabled", "disabled");
rel.parentElement.style.opacity = "0.4";
rel.value = document.querySelector(`#${type}_rel option:first-child`).value;
rel.dispatchEvent(new Event("change"));
}
}
toggle.addEventListener("change", on_change);
on_change();
}
setup_toggle("min");
setup_toggle("max");
});

@ -1,21 +1,25 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
const min = $("#min_rel");
const max = $("#max_rel");
const none = parseInt($("#min_rel option:first-child").attr("value"));
const warning = $("#minmax_warning");
"use strict";
function ver_check() {
const minv = parseInt(min.val());
const maxv = parseInt(max.val());
window.addEventListener("load", () => {
const min = document.getElementById("min_rel");
const max = document.getElementById("max_rel");
const none = parseInt(document.querySelector("#min_rel option:first-child").value);
const warning = document.getElementById("minmax_warning");
if (minv != none && maxv != none && minv > maxv) {
warning.show();
} else {
warning.hide();
function ver_check() {
const minv = parseInt(min.value);
const maxv = parseInt(max.value);
if (minv != none && maxv != none && minv > maxv) {
warning.style.display = "block";
} else {
warning.style.display = "none";
}
}
}
min.change(ver_check);
max.change(ver_check);
min.addEventListener("change", ver_check);
max.addEventListener("change", ver_check);
ver_check();
});

@ -0,0 +1,19 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
window.addEventListener("load", () => {
function check_opt() {
if (document.querySelector("input[name='uploadOpt']:checked").value === "vcs") {
document.getElementById("file_upload").parentElement.classList.add("d-none");
document.getElementById("vcsLabel").parentElement.classList.remove("d-none");
} else {
document.getElementById("file_upload").parentElement.classList.remove("d-none");
document.getElementById("vcsLabel").parentElement.classList.add("d-none");
}
}
document.querySelectorAll("input[name='uploadOpt']").forEach(x => x.addEventListener("change", check_opt));
check_opt();
});

@ -0,0 +1,17 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
window.addEventListener("load", () => {
function update() {
const elements = [...document.querySelector(".sortable").children];
const ids = elements.map(x => x.dataset.id).filter(x => x);
document.querySelector("input[name='order']").value = ids.join(",");
}
update();
$(".sortable").sortable({
update: update
});
})

@ -3,6 +3,9 @@
* License: MIT
* https://petprojects.googlecode.com/svn/trunk/MIT-LICENSE.txt
*/
"use strict";
(function($) {
function make_bold(text) {
const idx = text.indexOf(":");

@ -1,10 +1,12 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
$(".topic-discard").click(function() {
const ele = $(this);
const tid = ele.attr("data-tid");
const discard = !ele.parent().parent().hasClass("discardtopic");
"use strict";
document.querySelectorAll(".topic-discard").forEach(ele => ele.addEventListener("click", (e) => {
const row = ele.parentNode.parentNode;
const tid = ele.getAttribute("data-tid");
const discard = !row.classList.contains("discardtopic");
fetch(new Request("/api/topic_discard/?tid=" + tid +
"&discard=" + (discard ? "true" : "false"), {
method: "post",
@ -15,18 +17,17 @@ $(".topic-discard").click(function() {
},
})).then(function(response) {
response.text().then(function(txt) {
console.log(JSON.parse(txt));
if (JSON.parse(txt).discarded) {
ele.parent().parent().addClass("discardtopic");
ele.removeClass("btn-danger");
ele.addClass("btn-success");
ele.text("Show");
row.classList.add("discardtopic");
ele.classList.remove("btn-danger");
ele.classList.add("btn-success");
ele.innerText = "Show";
} else {
ele.parent().parent().removeClass("discardtopic");
ele.removeClass("btn-success");
ele.addClass("btn-danger");
ele.text("Discard");
row.classList.remove("discardtopic");
ele.classList.remove("btn-success");
ele.classList.add("btn-danger");
ele.innerText = "Discard";
}
}).catch(console.log)
}).catch(console.log)
});
}).catch(console.error);
}).catch(console.error);
}));

@ -1,6 +1,8 @@
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
document.querySelectorAll(".video-embed").forEach(ele => {
try {
const href = ele.getAttribute("href");

@ -65,10 +65,6 @@
font-size: 120%;
}
.pkg_wiz_1, .pkg_wiz_2 {
display: none;
}
.plsearchform {
padding: 0.5em;
}

@ -276,7 +276,6 @@
{% endif %}
</footer>
<script src="/static/libs/jquery.min.js?v=2"></script>
<script src="/static/libs/bootstrap.min.js?v=2"></script>
<script src="/static/libs/easymde.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/libs/easymde.min.css">

@ -9,6 +9,7 @@
{% endblock %}
{% block scriptextra %}
<script src="/static/libs/jquery.min.js?v=2"></script>
<script src="/static/libs/jquery-ui.min.js?v=2"></script>
<script src="/static/collection_editor.js?v=8"></script>
{% endblock %}

@ -70,6 +70,7 @@
{% macro form_scripts() -%}
<link href="/static/libs/jquery-ui.min.css?v=2" rel="stylesheet" type="text/css">
<script src="/static/libs/jquery.min.js?v=2"></script>
<script src="/static/libs/jquery-ui.min.js?v=2"></script>
<script src="/static/tagselector.js?v=5"></script>
{% endmacro %}

@ -88,7 +88,7 @@
<fieldset class="mt-4">
<legend class="pkg_meta">{{ _("Repository and Links") }}</legend>
<div class="pkg_wiz_1">
<div class="pkg_wiz_1 d-none">
<p>{{ _("Enter the repo URL for the package.
If the repo uses git then the metadata will be automatically imported.") }}</p>
@ -98,12 +98,12 @@
{{ render_field(form.website, class_="pkg_meta") }}
{{ render_field(form.repo, class_="pkg_repo", hint=_("HTTP URL to a Git, Mercurial, or other repository")) }}
<div class="pkg_wiz_1">
<div class="pkg_wiz_1 d-none">
<a id="pkg_wiz_1_next" class="btn btn-primary">{{ _("Next (Autoimport)") }}</a>
<a id="pkg_wiz_1_skip" class="btn btn-default">{{ _("Skip Autoimport") }}</a>
</div>
<div class="pkg_wiz_2">
<div class="pkg_wiz_2 d-none">
{{ _("Importing... (This may take a while)") }}
</div>

@ -42,26 +42,5 @@
{% block scriptextra %}
<script src="/static/release_minmax.js?v=2"></script>
<script>
function setup_toggle(type) {
const toggle = $("#set_" + type);
function on_change() {
if (toggle.is(":checked")) {
// $("#" + type + "_rel").removeAttr("disabled");
$("#" + type + "_rel").parent().css("opacity", "1");
} else {
// $("#" + type + "_rel").attr("disabled", "disabled");
$("#" + type + "_rel").parent().css("opacity", "0.4");
$("#" + type + "_rel").val($("#" + type + "_rel option:first-child").attr("value"));
$("#" + type + "_rel").change();
}
}
toggle.change(on_change);
on_change();
}
setup_toggle("min");
setup_toggle("max");
</script>
<script src="/static/release_bulk_change.js"></script>
{% endblock %}

@ -86,22 +86,9 @@
{{ render_submit_field(form.submit) }}
</p>
</form>
{% endblock %}
{% block scriptextra %}
<script src="/static/release_minmax.js?v=2"></script>
<script>
function check_opt() {
if ($("input[name=uploadOpt]:checked").val() == "vcs") {
$("#file_upload").parent().hide();
$("#vcsLabel").parent().show();
} else {
$("#file_upload").parent().show();
$("#vcsLabel").parent().hide();
}
}
$("input[name=uploadOpt]").change(check_opt);
check_opt();
</script>
<script src="/static/release_new.js"></script>
{% endblock %}

@ -131,20 +131,7 @@
{% endblock %}
{% block scriptextra %}
<script src="/static/libs/jquery.min.js?v=2"></script>
<script src="/static/libs/jquery-ui.min.js?v=2"></script>
<script>
function update() {
const elements = Array.from(document.getElementsByClassName("sortable")[0].children);
const ids = elements.map(x => x.dataset.id).filter(x => x);
$("input[name='order']").val(ids.join(","))
}
update();
$(function() {
$(".sortable").sortable({
update: update
});
});
</script>
<script src="/static/screenshots_editor.js"></script>
{% endblock %}