Clean up JavaScript

This commit is contained in:
rubenwardy 2020-12-04 02:34:08 +00:00
parent 42f96618e2
commit b18903b59b
10 changed files with 54 additions and 52 deletions

View File

@ -22,7 +22,7 @@ $(function() {
function setField(id, value) { function setField(id, value) {
if (value && value != "") { if (value && value != "") {
var ele = $(id); const ele = $(id);
ele.val(value); ele.val(value);
ele.trigger("change"); ele.trigger("change");
} }

View File

@ -11,8 +11,8 @@ $(function() {
$("#forums").on('paste', function(e) { $("#forums").on('paste', function(e) {
try { try {
var pasteData = e.originalEvent.clipboardData.getData('text') const pasteData = e.originalEvent.clipboardData.getData('text');
var url = new URL(pasteData); const url = new URL(pasteData);
if (url.hostname == "forum.minetest.net") { if (url.hostname == "forum.minetest.net") {
$(this).val(url.searchParams.get("t")); $(this).val(url.searchParams.get("t"));
e.preventDefault(); e.preventDefault();
@ -42,7 +42,7 @@ $(function() {
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() { $("#short_desc").on("change paste keyup", function() {
var val = $(this).val().toLowerCase(); const val = $(this).val().toLowerCase();
if (val.indexOf("minetest") >= 0 || val.indexOf("mod") >= 0 || if (val.indexOf("minetest") >= 0 || val.indexOf("mod") >= 0 ||
val.indexOf("modpack") >= 0 || val.indexOf("mod pack") >= 0) { val.indexOf("modpack") >= 0 || val.indexOf("mod pack") >= 0) {
showHint($(this), hint_mtmods); showHint($(this), hint_mtmods);
@ -54,9 +54,9 @@ $(function() {
} }
}) })
var btn = $("#forums").parent().find("label").append("<a class='ml-3 btn btn-sm btn-primary'>Open</a>"); const btn = $("#forums").parent().find("label").append("<a class='ml-3 btn btn-sm btn-primary'>Open</a>");
btn.click(function() { btn.click(function() {
var id = $("#forums").val(); const id = $("#forums").val();
if (/^\d+$/.test(id)) { if (/^\d+$/.test(id)) {
window.open("https://forum.minetest.net/viewtopic.php?t=" + id, "_blank"); window.open("https://forum.minetest.net/viewtopic.php?t=" + id, "_blank");
} }

View File

@ -19,7 +19,8 @@ function getJSON(url, method) {
function pollTask(poll_url, disableTimeout) { function pollTask(poll_url, disableTimeout) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var tries = 0; let tries = 0;
function retry() { function retry() {
tries++; tries++;
if (!disableTimeout && tries > 30) { if (!disableTimeout && tries > 30) {

View File

@ -1,11 +1,11 @@
var min = $("#min_rel"); const min = $("#min_rel");
var max = $("#max_rel"); const max = $("#max_rel");
var none = $("#min_rel option:first-child").attr("value"); const none = $("#min_rel option:first-child").attr("value");
var warning = $("#minmax_warning"); const warning = $("#minmax_warning");
function ver_check() { function ver_check() {
var minv = min.val(); const minv = min.val();
var maxv = max.val(); const maxv = max.val();
if (minv != none && maxv != none && minv > maxv) { if (minv != none && maxv != none && minv > maxv) {
warning.show(); warning.show();

View File

@ -6,12 +6,12 @@
*/ */
(function($) { (function($) {
function hide_error(input) { function hide_error(input) {
var err = input.parent().parent().find(".invalid-remaining"); const err = input.parent().parent().find(".invalid-remaining");
err.hide(); err.hide();
} }
function show_error(input, msg) { function show_error(input, msg) {
var err = input.parent().parent().find(".invalid-remaining"); const err = input.parent().parent().find(".invalid-remaining");
console.log(err.length); console.log(err.length);
err.text(msg); err.text(msg);
err.show(); err.show();
@ -19,12 +19,12 @@
$.fn.selectSelector = function(source, select) { $.fn.selectSelector = function(source, select) {
return this.each(function() { return this.each(function() {
var selector = $(this), const selector = $(this),
input = $('input[type=text]', this); input = $('input[type=text]', this);
selector.click(function() { input.focus(); }) selector.click(function() { input.focus(); })
.delegate('.badge a', 'click', function() { .delegate('.badge a', 'click', function() {
var id = $(this).parent().data("id"); const id = $(this).parent().data("id");
select.find("option[value=" + id + "]").attr("selected", false) select.find("option[value=" + id + "]").attr("selected", false)
recreate(); recreate();
}); });
@ -55,8 +55,8 @@
} }
recreate(); recreate();
input.focusout(function(e) { input.focusout(function() {
var value = input.val().trim() const value = input.val().trim();
if (value != "") { if (value != "") {
show_error(input, "Please select an existing tag, it;s not possible to add custom ones."); show_error(input, "Please select an existing tag, it;s not possible to add custom ones.");
} }
@ -102,19 +102,19 @@
$.fn.csvSelector = function(source, name, result, allowSlash) { $.fn.csvSelector = function(source, name, result, allowSlash) {
return this.each(function() { return this.each(function() {
var selector = $(this), const selector = $(this),
input = $('input[type=text]', this); input = $('input[type=text]', this);
var selected = []; let selected = [];
var lookup = {}; const lookup = {};
for (var i = 0; i < source.length; i++) { for (var i = 0; i < source.length; i++) {
lookup[source[i].id] = source[i]; lookup[source[i].id] = source[i];
} }
selector.click(function() { input.focus(); }) selector.click(function() { input.focus(); })
.delegate('.badge a', 'click', function() { .delegate('.badge a', 'click', function() {
var id = $(this).parent().data("id"); const id = $(this).parent().data("id");
for (var i = 0; i < selected.length; i++) { for (let i = 0; i < selected.length; i++) {
if (selected[i] == id) { if (selected[i] == id) {
selected.splice(i, 1); selected.splice(i, 1);
} }
@ -123,7 +123,7 @@
}); });
function selectItem(id) { function selectItem(id) {
for (var i = 0; i < selected.length; i++) { for (let i = 0; i < selected.length; i++) {
if (selected[i] == id) { if (selected[i] == id) {
return false; return false;
} }
@ -133,7 +133,7 @@
} }
function addTag(id, value) { function addTag(id, value) {
var tag = $('<span class="badge badge-pill badge-primary"/>') const tag = $('<span class="badge badge-pill badge-primary"/>')
.text(value) .text(value)
.data("id", id) .data("id", id)
.append(' <a>x</a>') .append(' <a>x</a>')
@ -145,8 +145,8 @@
function recreate() { function recreate() {
selector.find("span").remove(); selector.find("span").remove();
for (var i = 0; i < selected.length; i++) { for (let i = 0; i < selected.length; i++) {
var value = lookup[selected[i]] || { value: selected[i] }; const value = lookup[selected[i]] || {value: selected[i]};
addTag(selected[i], value.value); addTag(selected[i], value.value);
} }
result.val(selected.join(",")) result.val(selected.join(","))
@ -154,9 +154,9 @@
function readFromResult() { function readFromResult() {
selected = []; selected = [];
var selected_raw = result.val().split(","); const selected_raw = result.val().split(",");
for (var i = 0; i < selected_raw.length; i++) { for (let i = 0; i < selected_raw.length; i++) {
var raw = selected_raw[i].trim(); const raw = selected_raw[i].trim();
if (lookup[raw] || raw.match(/^([a-z0-9_]+)$/)) { if (lookup[raw] || raw.match(/^([a-z0-9_]+)$/)) {
selected.push(raw); selected.push(raw);
} }
@ -169,7 +169,7 @@
result.change(readFromResult); result.change(readFromResult);
input.focusout(function() { input.focusout(function() {
var item = input.val(); const item = input.val();
if (item.length == 0) { if (item.length == 0) {
input.data("ui-autocomplete").search(""); input.data("ui-autocomplete").search("");
} else if (item.match(/^([a-z0-9_]+)$/)) { } else if (item.match(/^([a-z0-9_]+)$/)) {
@ -238,17 +238,17 @@
$(function() { $(function() {
$(".multichoice_selector").each(function() { $(".multichoice_selector").each(function() {
var ele = $(this); const ele = $(this);
var sel = ele.parent().find("select"); const sel = ele.parent().find("select");
sel.hide(); sel.hide();
var options = []; const options = [];
sel.find("option").each(function() { sel.find("option").each(function() {
var text = $(this).text(); const text = $(this).text();
options.push({ options.push({
id: $(this).attr("value"), id: $(this).attr("value"),
value: text, value: text,
selected: $(this).attr("selected") ? true : false, selected: !!$(this).attr("selected"),
toString: function() { return text; }, toString: function() { return text; },
}); });
}); });
@ -257,13 +257,13 @@
}); });
$(".metapackage_selector").each(function() { $(".metapackage_selector").each(function() {
var input = $(this).parent().children("input[type='text']"); const input = $(this).parent().children("input[type='text']");
input.hide(); input.hide();
$(this).csvSelector(meta_packages, input.attr("name"), input); $(this).csvSelector(meta_packages, input.attr("name"), input);
}); });
$(".deps_selector").each(function() { $(".deps_selector").each(function() {
var input = $(this).parent().children("input[type='text']"); const input = $(this).parent().children("input[type='text']");
input.hide(); input.hide();
$(this).csvSelector(all_packages, input.attr("name"), input); $(this).csvSelector(all_packages, input.attr("name"), input);
}); });

View File

@ -1,7 +1,7 @@
$(".topic-discard").click(function() { $(".topic-discard").click(function() {
var ele = $(this); const ele = $(this);
var tid = ele.attr("data-tid"); const tid = ele.attr("data-tid");
var discard = !ele.parent().parent().hasClass("discardtopic"); const discard = !ele.parent().parent().hasClass("discardtopic");
fetch(new Request("/api/topic_discard/?tid=" + tid + fetch(new Request("/api/topic_discard/?tid=" + tid +
"&discard=" + (discard ? "true" : "false"), { "&discard=" + (discard ? "true" : "false"), {
method: "post", method: "post",

View File

@ -45,7 +45,8 @@
<script src="/static/release_minmax.js?v=1"></script> <script src="/static/release_minmax.js?v=1"></script>
<script> <script>
function setup_toggle(type) { function setup_toggle(type) {
var toggle = $("#set_" + type); const toggle = $("#set_" + type);
function on_change() { function on_change() {
if (toggle.is(":checked")) { if (toggle.is(":checked")) {
// $("#" + type + "_rel").removeAttr("disabled"); // $("#" + type + "_rel").removeAttr("disabled");

View File

@ -72,7 +72,7 @@
<script> <script>
$(".add-btn").click(function() { $(".add-btn").click(function() {
const row = $(this).parent().parent() $(this).parent().parent();
$(".modal select option").removeAttr("selected"); $(".modal select option").removeAttr("selected");
$(".multichoice_selector").remove(); $(".multichoice_selector").remove();
@ -87,13 +87,13 @@
$(".modal").modal("show"); $(".modal").modal("show");
$(".modal input").focus(); $(".modal input").focus();
$(".multichoice_selector").each(function() { $(".multichoice_selector").each(function() {
var ele = $(this); const ele = $(this);
var sel = ele.parent().find("select"); const sel = ele.parent().find("select");
sel.hide(); sel.hide();
var options = []; const options = [];
sel.find("option").each(function() { sel.find("option").each(function() {
var text = $(this).text(); const text = $(this).text();
options.push({ options.push({
id: $(this).attr("value"), id: $(this).attr("value"),
value: text, value: text,

View File

@ -98,7 +98,7 @@ Topics to be Added
{% block scriptextra %} {% block scriptextra %}
<script> <script>
var csrf_token = "{{ csrf_token() }}"; const csrf_token = "{{ csrf_token() }}";
</script> </script>
<script src="/static/topic_discard.js"></script> <script src="/static/topic_discard.js"></script>
{% endblock %} {% endblock %}

View File

@ -242,7 +242,7 @@
{% block scriptextra %} {% block scriptextra %}
<script> <script>
var csrf_token = "{{ csrf_token() }}"; const csrf_token = "{{ csrf_token() }}";
</script> </script>
<script src="/static/topic_discard.js"></script> <script src="/static/topic_discard.js"></script>
{% endblock %} {% endblock %}