Skip to content

Commit ddf9056

Browse files
committed
refactor: remove jq from theme-picker
1 parent 10845c3 commit ddf9056

File tree

1 file changed

+70
-57
lines changed

1 file changed

+70
-57
lines changed

frontend/src/ts/elements/settings/theme-picker.ts

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -140,37 +140,37 @@ export async function fillPresetButtons(): Promise<void> {
140140

141141
export async function fillCustomButtons(): Promise<void> {
142142
// Update custom theme buttons
143-
const customThemesEl = $(
143+
const customThemesEl = qs(
144144
".pageSettings .section.themes .allCustomThemes.buttons",
145-
).empty();
146-
const addButton = $(".pageSettings .section.themes .addCustomThemeButton");
147-
const saveButton = $(
145+
)?.empty();
146+
const addButton = qs(".pageSettings .section.themes .addCustomThemeButton");
147+
const saveButton = qs(
148148
".pageSettings .section.themes .tabContent.customTheme #saveCustomThemeButton",
149149
);
150150

151151
if (!isAuthenticated()) {
152-
saveButton.text("save");
153-
addButton.addClass("hidden");
154-
customThemesEl.css("margin-bottom", "0");
152+
saveButton?.setText("save");
153+
addButton?.addClass("hidden");
154+
customThemesEl?.setStyle({ marginBottom: "0" });
155155
return;
156156
}
157157

158-
saveButton.text("save as new");
159-
addButton.removeClass("hidden");
158+
saveButton?.setText("save as new");
159+
addButton?.removeClass("hidden");
160160

161161
const customThemes = DB.getSnapshot()?.customThemes ?? [];
162162

163163
if (customThemes.length === 0) {
164-
customThemesEl.css("margin-bottom", "0");
164+
customThemesEl?.setStyle({ marginBottom: "0" });
165165
} else {
166-
customThemesEl.css("margin-bottom", "1rem");
166+
customThemesEl?.setStyle({ marginBottom: "1rem" });
167167
}
168168

169169
for (const customTheme of customThemes) {
170170
const bgColor = customTheme.colors[0];
171171
const mainColor = customTheme.colors[1];
172172

173-
customThemesEl.append(
173+
customThemesEl?.appendHtml(
174174
`<div class="customTheme button" customThemeId='${customTheme._id}'
175175
style="color:${mainColor};background:${bgColor}">
176176
<div class="editButton"><i class="fas fa-pen"></i></div>
@@ -220,12 +220,12 @@ function saveCustomThemeColors(): void {
220220
export function updateActiveTab(): void {
221221
// Set force to true only when some change for the active tab has taken place
222222
// Prevent theme buttons from being added twice by doing an update only when the state has changed
223-
$(".pageSettings .section.themes .tabs button").removeClass("active");
224-
$(
223+
qsa(".pageSettings .section.themes .tabs button")?.removeClass("active");
224+
qs(
225225
`.pageSettings .section.themes .tabs button[data-tab="${
226226
Config.customTheme ? "custom" : "preset"
227227
}"]`,
228-
).addClass("active");
228+
)?.addClass("active");
229229

230230
if (Config.customTheme) {
231231
void Misc.swapElements(
@@ -251,59 +251,72 @@ export async function updateThemeUI(): Promise<void> {
251251
// Add events to the DOM
252252

253253
// Handle click on theme: preset or custom tab
254-
$(".pageSettings .section.themes .tabs button").on("click", (e) => {
255-
$(".pageSettings .section.themes .tabs button").removeClass("active");
256-
const $target = $(e.currentTarget);
257-
$target.addClass("active");
258-
// setCustomInputs();
259-
//test
260-
if ($target.attr("data-tab") === "preset") {
254+
qsa(".pageSettings .section.themes .tabs button")?.on("click", (e) => {
255+
qsa(".pageSettings .section.themes .tabs button")?.removeClass("active");
256+
(e.currentTarget as HTMLElement).classList.add("active");
257+
if ((e.currentTarget as HTMLElement).getAttribute("data-tab") === "preset") {
261258
setConfig("customTheme", false);
262259
} else {
263260
setConfig("customTheme", true);
264261
}
265262
});
266263

267264
// Handle click on custom theme button
268-
$(".pageSettings").on("click", " .section.themes .customTheme.button", (e) => {
269-
// Do not apply if user wanted to delete it
270-
if ($(e.target).hasClass("delButton")) return;
271-
if ($(e.target).hasClass("editButton")) return;
272-
const customThemeId = $(e.currentTarget).attr("customThemeId") ?? "";
273-
const theme = DB.getSnapshot()?.customThemes?.find(
274-
(e) => e._id === customThemeId,
275-
);
276-
277-
if (theme === undefined) {
278-
//this shouldnt happen but typescript needs this check
279-
console.error(
280-
"Could not find custom theme in snapshot for id ",
281-
customThemeId,
265+
qs(".pageSettings")?.onChild(
266+
"click",
267+
".section.themes .customTheme.button",
268+
(e) => {
269+
// Do not apply if user wanted to delete it
270+
271+
const target = e.childTarget as HTMLElement;
272+
273+
if ((e.target as HTMLElement).classList.contains("delButton")) return;
274+
if ((e.target as HTMLElement).classList.contains("editButton")) return;
275+
const customThemeId = target.getAttribute("customThemeId") ?? "";
276+
const theme = DB.getSnapshot()?.customThemes?.find(
277+
(e) => e._id === customThemeId,
282278
);
283-
return;
284-
}
285279

286-
setConfig("customThemeColors", theme.colors);
287-
});
280+
if (theme === undefined) {
281+
//this shouldnt happen but typescript needs this check
282+
console.error(
283+
"Could not find custom theme in snapshot for id ",
284+
customThemeId,
285+
);
286+
return;
287+
}
288+
289+
setConfig("customThemeColors", theme.colors);
290+
},
291+
);
288292

289293
// Handle click on favorite preset theme button
290-
$(".pageSettings").on("click", ".section.themes .theme .favButton", (e) => {
291-
const theme = $(e.currentTarget)
292-
.parents(".theme.button")
293-
.attr("theme") as ThemeName;
294-
if (theme !== undefined) {
295-
toggleFavourite(theme);
296-
} else {
297-
console.error(
298-
"Could not find the theme attribute attached to the button clicked!",
299-
);
300-
}
301-
});
294+
qs(".pageSettings")?.onChild(
295+
"click",
296+
".section.themes .theme .favButton",
297+
(e) => {
298+
const theme = (e.childTarget as HTMLElement)
299+
.closest(".theme.button")
300+
?.getAttribute("theme") as ThemeName;
301+
if (theme !== undefined) {
302+
toggleFavourite(theme);
303+
} else {
304+
console.error(
305+
"Could not find the theme attribute attached to the button clicked!",
306+
);
307+
}
308+
},
309+
);
302310

303311
// Handle click on preset theme button
304-
$(".pageSettings").on("click", ".section.themes .theme.button", (e) => {
305-
const theme = $(e.currentTarget).attr("theme") as ThemeName;
306-
if (!$(e.target).hasClass("favButton") && theme !== undefined) {
312+
qs(".pageSettings")?.onChild("click", ".section.themes .theme.button", (e) => {
313+
const theme = (e.childTarget as HTMLElement).getAttribute(
314+
"theme",
315+
) as ThemeName;
316+
if (
317+
!(e.childTarget as HTMLElement).classList.contains("favButton") &&
318+
theme !== undefined
319+
) {
307320
setConfig("theme", theme);
308321
}
309322
});
@@ -364,7 +377,7 @@ qsa(".pageSettings .section.themes .tabContainer .customTheme input.input")
364377
}
365378
});
366379

367-
$(".pageSettings #loadCustomColorsFromPreset").on("click", async () => {
380+
qs(".pageSettings #loadCustomColorsFromPreset")?.on("click", async () => {
368381
ThemeController.applyPreset(Config.theme);
369382
const themeColors = getTheme();
370383

@@ -375,7 +388,7 @@ $(".pageSettings #loadCustomColorsFromPreset").on("click", async () => {
375388
);
376389
});
377390

378-
$(".pageSettings #saveCustomThemeButton").on("click", async () => {
391+
qs(".pageSettings #saveCustomThemeButton")?.on("click", async () => {
379392
saveCustomThemeColors();
380393
if (isAuthenticated()) {
381394
const newCustomTheme = {

0 commit comments

Comments
 (0)