Skip to content

Commit 5991e0c

Browse files
authored
feat: start removing jquery usages (#19850)
* feat: start removing jquery usages * fixes
1 parent f8678ce commit 5991e0c

5 files changed

Lines changed: 139 additions & 96 deletions

File tree

weblate/static/editor/browse.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
(() => {
66
function ProjectStringsBrowser() {
77
hotkeys("right", () => {
8-
const nextButton = $("#button-next");
9-
const nextLocation = nextButton.attr("href");
10-
if (nextButton.length > 0 && !nextButton.hasClass("disabled")) {
11-
if (nextLocation !== undefined) {
8+
const nextButton = document.getElementById("button-next");
9+
const nextLocation = nextButton?.getAttribute("href");
10+
if (nextButton && !nextButton.classList.contains("disabled")) {
11+
if (nextLocation !== null && nextLocation !== undefined) {
1212
window.location.href = nextLocation;
1313
}
1414
}
1515
return false;
1616
});
1717
hotkeys("left", () => {
18-
const prevButton = $("#button-prev");
19-
const prevLocation = prevButton.attr("href");
20-
if (prevButton.length > 0 && !prevButton.hasClass("disabled")) {
21-
if (prevLocation !== undefined) {
18+
const prevButton = document.getElementById("button-prev");
19+
const prevLocation = prevButton?.getAttribute("href");
20+
if (prevButton && !prevButton.classList.contains("disabled")) {
21+
if (prevLocation !== null && prevLocation !== undefined) {
2222
window.location.href = prevLocation;
2323
}
2424
}

weblate/static/js/accounts/profile/index.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

5-
$(document).ready(() => {
6-
const $profileNotificationSettings = $("#notifications");
7-
const $container = $profileNotificationSettings.find("#div_id_watched");
8-
const $selectElement = $profileNotificationSettings.find("#id_watched");
5+
document.addEventListener("DOMContentLoaded", () => {
6+
const profileNotificationSettings = document.getElementById("notifications");
7+
const container =
8+
profileNotificationSettings?.querySelector("#div_id_watched");
9+
const selectElement =
10+
profileNotificationSettings?.querySelector("#id_watched");
11+
12+
if (
13+
container === null ||
14+
container === undefined ||
15+
selectElement === null ||
16+
selectElement === undefined
17+
) {
18+
return;
19+
}
920

1021
// Make elements link-like except click behavior
11-
makeElementsLinkLike($container);
22+
makeElementsLinkLike(container);
1223
// Watch the container when elements are added and removed
1324
const watchedContainerMutationObserver = new MutationObserver(() => {
14-
makeElementsLinkLike($container);
25+
makeElementsLinkLike(container);
1526
});
1627

17-
watchedContainerMutationObserver.observe($container[0], {
28+
watchedContainerMutationObserver.observe(container, {
1829
childList: true,
1930
subtree: true,
2031
});
@@ -24,20 +35,22 @@ $(document).ready(() => {
2435
* 'data-value' attribute, change its `href` to point to project page, and
2536
* prevent default click action.
2637
*
27-
* @param {Object} parentElement - The parent element to search for 'a'
38+
* @param {Element} parentElement - The parent element to search for 'a'
2839
* elements.
2940
*/
3041
function makeElementsLinkLike(parentElement) {
31-
const slugs = JSON.parse($selectElement.attr("data-project-slugs"));
32-
parentElement.find("a").each((_index, element) => {
33-
const $element = $(element);
34-
const dataValue = $element.attr("data-value");
42+
const slugs = JSON.parse(selectElement.getAttribute("data-project-slugs"));
43+
if (slugs === undefined || slugs === null) {
44+
return;
45+
}
46+
for (const element of parentElement.querySelectorAll("a")) {
47+
const dataValue = element.getAttribute("data-value");
3548
if (dataValue) {
3649
// Encode the data value to prevent unsafe HTML injection
3750
const projectSlug = encodeURIComponent(slugs[dataValue]);
38-
$element.attr("href", `/projects/${projectSlug}/`);
39-
$element.on("click", (event) => event.preventDefault());
51+
element.setAttribute("href", `/projects/${projectSlug}/`);
52+
element.addEventListener("click", (event) => event.preventDefault());
4053
}
41-
});
54+
}
4255
}
4356
});

weblate/static/js/keyboard-shortcuts.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

55
(() => {
6+
function showShortcutsModal() {
7+
const modal = document.getElementById("shortcuts-modal");
8+
if (modal !== null) {
9+
bootstrap.Modal.getOrCreateInstance(modal).show();
10+
}
11+
}
12+
613
// Match the "?" character directly rather than binding via hotkeys-js
714
document.addEventListener("keydown", (event) => {
815
if (event.key !== "?") {
@@ -18,10 +25,10 @@
1825
return;
1926
}
2027
event.preventDefault();
21-
$("#shortcuts-modal").modal("show");
28+
showShortcutsModal();
2229
});
2330

24-
$("#shortcuts-btn").on("click", () => {
25-
$("#shortcuts-modal").modal("show");
26-
});
31+
document
32+
.getElementById("shortcuts-btn")
33+
?.addEventListener("click", showShortcutsModal);
2734
})();

weblate/static/js/screenshots/clipboard-paste.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

5-
$(document).ready(() => {
5+
document.addEventListener("DOMContentLoaded", () => {
66
// The paste trigger button
7-
const $pasteScreenshotBtn = $("#paste-screenshot-btn");
7+
const pasteScreenshotBtn = document.getElementById("paste-screenshot-btn");
8+
if (pasteScreenshotBtn === null) {
9+
return;
10+
}
811
// The file input to store the screenshot file
9-
const $screenshotFileInput = $("#screenshot-form-container input#id_image");
12+
const screenshotFileInput = document.querySelector(
13+
"#screenshot-form-container input#id_image",
14+
);
1015

1116
// Check if the browser supports the Clipboard API
1217
if (!navigator.clipboard?.read) {
13-
$pasteScreenshotBtn.remove();
18+
pasteScreenshotBtn.remove();
19+
return;
1420
}
1521

16-
$pasteScreenshotBtn.on("click", async (e) => {
22+
pasteScreenshotBtn.addEventListener("click", async (e) => {
1723
e.preventDefault();
1824
try {
1925
// Read clipboard content
@@ -27,13 +33,13 @@ $(document).ready(() => {
2733
const blob = await clipboardItem.getType(type);
2834
const reader = new FileReader();
2935
reader.onload = (_event) => {
30-
if ($screenshotFileInput.length > 0) {
36+
if (screenshotFileInput !== null) {
3137
// Load the file data into the form input
3238
const fileName = `screenshot_${Date.now()}.${type.split("/")[1]}`;
3339
const imageFile = new File([blob], fileName, { type: type });
3440
const dataTransfer = new DataTransfer();
3541
dataTransfer.items.add(imageFile);
36-
$screenshotFileInput[0].files = dataTransfer.files;
42+
screenshotFileInput.files = dataTransfer.files;
3743
// Inform paste success
3844
showInfo("success", gettext("Image Pasted!"));
3945
} else {
@@ -65,10 +71,16 @@ $(document).ready(() => {
6571
* @param {string} message - The content of the message.
6672
*/
6773
function showInfo(type, message) {
68-
const $pasteScreenshotInfo = $("#paste-screenshot-info-label");
69-
const span = `<span class="text-${type}">${message}</span>`;
70-
$pasteScreenshotInfo.html(span);
71-
$pasteScreenshotInfo
72-
.css("transform", "scale(1)")
73-
.removeClass("animate__animated animate__fadeIn");
74+
const pasteScreenshotInfo = document.getElementById(
75+
"paste-screenshot-info-label",
76+
);
77+
if (pasteScreenshotInfo === null) {
78+
return;
79+
}
80+
const span = document.createElement("span");
81+
span.classList.add(`text-${type}`);
82+
span.textContent = message;
83+
pasteScreenshotInfo.replaceChildren(span);
84+
pasteScreenshotInfo.style.transform = "scale(1)";
85+
pasteScreenshotInfo.classList.remove("animate__animated", "animate__fadeIn");
7486
}

0 commit comments

Comments
 (0)