Skip to content

Commit 03dcc04

Browse files
committed
refactor(query): jQuery no more
1 parent 45fe0b6 commit 03dcc04

File tree

1 file changed

+87
-68
lines changed
  • src/main/xar-resources/resources/scripts

1 file changed

+87
-68
lines changed
Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,110 @@
1-
$(document).on("ready", function() {
2-
const markedOptions = { "gfm": true }
3-
const loginDialog = $("#loginDialog");
1+
document.addEventListener("DOMContentLoaded", function () {
2+
const markedOptions = { gfm: true };
3+
const loginDialog = document.getElementById("loginDialog");
44
let timeout = 0;
55

6-
loginDialog.modal({
7-
show: false
8-
});
6+
// Modal handling
7+
function showModal(element) {
8+
element.style.display = "block";
9+
}
10+
11+
function hideModal(element) {
12+
element.style.display = "none";
13+
}
14+
15+
hideModal(loginDialog);
916

1017
function search() {
11-
const data = $("#fun-query-form").serialize();
12-
$.ajax({
13-
type: "POST",
14-
url: "ajax.html",
15-
data: data + "&action=search",
16-
success: function (data) {
17-
$("#results").fadeOut(100, function() {
18-
$(this).html(data);
19-
$(this).fadeIn(100);
20-
timeout = null;
21-
});
22-
}
23-
});
18+
const formData = new FormData(document.getElementById("fun-query-form"));
19+
formData.append("action", "search");
20+
21+
fetch("ajax.html", {
22+
method: "POST",
23+
body: new URLSearchParams(formData),
24+
})
25+
.then((response) => response.text())
26+
.then((data) => {
27+
const results = document.getElementById("results");
28+
results.style.display = "none";
29+
results.innerHTML = data;
30+
results.style.display = "block";
31+
timeout = null;
32+
});
2433
}
2534

26-
function reindexIfLoggedIn(ev) {
27-
ev.preventDefault();
35+
function reindexIfLoggedIn(event) {
36+
event.preventDefault();
2837

29-
$.ajax({
30-
url: "login",
31-
dataType: "json",
32-
success: reindex,
33-
error: function () {
34-
$("#loginDialog").modal("show");
35-
}
36-
});
38+
fetch("login", { headers: { Accept: "application/json" } })
39+
.then((response) => {
40+
if (!response.ok) throw new Error();
41+
return response.json();
42+
})
43+
.then(reindex)
44+
.catch(() => showModal(loginDialog));
3745
}
3846

3947
function reindex() {
40-
$("#messages").empty();
41-
$("#f-load-indicator").show();
42-
$.ajax({
43-
type: "POST",
44-
dataType: "json",
45-
url: "modules/reindex.xql",
46-
success: function (data) {
47-
$("#f-load-indicator").hide();
48-
if (data.status == "failed") {
49-
// FIXME the server should respond with an error status code
50-
$("#messages").text(data.message);
48+
const messages = document.getElementById("messages");
49+
const loadIndicator = document.getElementById("f-load-indicator");
50+
messages.innerHTML = "";
51+
loadIndicator.style.display = "block";
52+
53+
fetch("modules/reindex.xql", {
54+
method: "POST",
55+
headers: { Accept: "application/json" },
56+
})
57+
.then((response) => response.json())
58+
.then((data) => {
59+
loadIndicator.style.display = "none";
60+
if (data.status === "failed") {
61+
messages.textContent = data.message;
5162
} else {
5263
window.location.reload();
5364
}
54-
}
55-
});
65+
});
5666
}
5767

58-
$("form", loginDialog).on("submit", function(ev) {
59-
const params = $(this).serialize();
60-
$.ajax({
61-
url: "login",
62-
data: params,
63-
dataType: "json",
64-
success: function(data) {
65-
loginDialog.modal("hide");
68+
loginDialog.querySelector("form").addEventListener("submit", function (event) {
69+
event.preventDefault();
70+
const formData = new FormData(this);
71+
72+
fetch("login", {
73+
method: "POST",
74+
body: new URLSearchParams(formData),
75+
headers: { Accept: "application/json" },
76+
})
77+
.then((response) => {
78+
if (!response.ok) throw new Error();
79+
return response.json();
80+
})
81+
.then(() => {
82+
hideModal(loginDialog);
6683
reindex();
67-
},
68-
error: function(xhr, textStatus) {
69-
$(".login-message", loginDialog).show().text("Login failed!");
70-
}
71-
});
72-
return false;
84+
})
85+
.catch(() => {
86+
loginDialog.querySelector(".login-message").style.display = "block";
87+
loginDialog.querySelector(".login-message").textContent = "Login failed!";
88+
});
7389
});
74-
$("#f-load-indicator").hide();
75-
$("#query-field").on("keyup", function() {
76-
const val = $(this).val();
77-
// fixme search request is delayed by 300ms
78-
// replace with proper debounce
90+
91+
document.getElementById("f-load-indicator").style.display = "none";
92+
93+
document.getElementById("query-field").addEventListener("keyup", function () {
94+
const val = this.value;
7995
if (val.length > 3) {
80-
if (timeout)
81-
clearTimeout(timeout);
96+
if (timeout) clearTimeout(timeout);
8297
timeout = setTimeout(search, 300);
8398
}
8499
});
85-
86-
$("#f-btn-reindex").on("click", reindexIfLoggedIn);
87-
$("#f-btn-reindex-regen").on("click", reindexIfLoggedIn);
88100

89-
$("#fun-query-form *[data-toggle='tooltip']").tooltip();
101+
document.getElementById("f-btn-reindex").addEventListener("click", reindexIfLoggedIn);
102+
document.getElementById("f-btn-reindex-regen").addEventListener("click", reindexIfLoggedIn);
90103

91-
});
104+
const tooltips = document.querySelectorAll("#fun-query-form [data-toggle='tooltip']");
105+
tooltips.forEach((tooltip) => {
106+
tooltip.addEventListener("mouseover", () => {
107+
tooltip.title = tooltip.getAttribute("data-title") || "Tooltip";
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)