Skip to content

Commit a51424a

Browse files
authored
refactor: remove jq from simple-modals (@Miodec) (#7307)
Also fixes bugs added in #7303
1 parent 8fb18d6 commit a51424a

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

frontend/src/ts/utils/simple-modal.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
} from "../elements/input-validation";
1414
import { ElementWithUtils, qsr } from "./dom";
1515

16+
const simpleModalEl = qsr<HTMLDialogElement>("#simpleModal");
17+
1618
type CommonInput<TType, TValue> = {
1719
type: TType;
1820
initVal?: TValue;
@@ -163,50 +165,46 @@ export class SimpleModal {
163165
}
164166

165167
init(): void {
166-
const el = $(this.element);
167-
el.find("input").val("");
168168
this.reset();
169-
el.attr("data-popup-id", this.id);
170-
el.find(".title").text(this.title);
169+
this.element.setAttribute("data-popup-id", this.id);
170+
this.element.qs(".title")?.setText(this.title);
171171
if (this.textAllowHtml) {
172-
el.find(".text").html(this.text ?? "");
172+
this.element.qs(".text")?.setHtml(this.text ?? "");
173173
} else {
174-
el.find(".text").text(this.text ?? "");
174+
this.element.qs(".text")?.setText(this.text ?? "");
175175
}
176176

177177
this.initInputs();
178178

179179
if (this.buttonText === "") {
180-
el.find(".submitButton").remove();
180+
this.element.qs(".submitButton")?.remove();
181181
} else {
182-
el.find(".submitButton").text(this.buttonText);
182+
this.element.qs(".submitButton")?.setText(this.buttonText);
183183
this.updateSubmitButtonState();
184184
}
185185

186186
if ((this.text ?? "") === "") {
187-
el.find(".text").addClass("hidden");
187+
this.element.qs(".text")?.hide();
188188
} else {
189-
el.find(".text").removeClass("hidden");
189+
this.element.qs(".text")?.show();
190190
}
191191
}
192192

193193
initInputs(): void {
194-
const el = $(this.element);
195-
196194
const allInputsHidden = this.inputs.every((i) => i.hidden);
197195
if (allInputsHidden || this.inputs.length === 0) {
198-
el.find(".inputs").addClass("hidden");
196+
this.element.qs(".inputs")?.hide();
199197
return;
200198
}
201199

202-
const inputs = el.find(".inputs");
203-
if (this.showLabels) inputs.addClass("withLabel");
200+
const inputsEl = this.element.qs(".inputs");
201+
if (this.showLabels) inputsEl?.addClass("withLabel");
204202

205203
this.inputs.forEach((input, index) => {
206204
const id = `${this.id}_${index}`;
207205

208206
if (this.showLabels && !input.hidden) {
209-
inputs.append(`<label for="${id}">${input.label ?? ""}</label>`);
207+
inputsEl?.appendHtml(`<label for="${id}">${input.label ?? ""}</label>`);
210208
}
211209

212210
const tagname = input.type === "textarea" ? "textarea" : "input";
@@ -229,7 +227,7 @@ export class SimpleModal {
229227
}
230228

231229
if (input.type === "textarea") {
232-
inputs.append(
230+
inputsEl?.appendHtml(
233231
buildTag({
234232
tagname,
235233
classes,
@@ -253,9 +251,9 @@ export class SimpleModal {
253251
} else {
254252
html = `<div>${html}</div>`;
255253
}
256-
inputs.append(html);
254+
inputsEl?.appendHtml(html);
257255
} else if (input.type === "range") {
258-
inputs.append(`
256+
inputsEl?.appendHtml(`
259257
<div>
260258
${buildTag({
261259
tagname,
@@ -317,7 +315,7 @@ export class SimpleModal {
317315
break;
318316
}
319317
}
320-
inputs.append(buildTag({ tagname, classes, attributes }));
318+
inputsEl?.appendHtml(buildTag({ tagname, classes, attributes }));
321319
}
322320
const element = qsr<HTMLInputElement>("#" + attributes["id"]);
323321

@@ -358,7 +356,7 @@ export class SimpleModal {
358356
}
359357
});
360358

361-
el.find(".inputs").removeClass("hidden");
359+
this.element.qs(".inputs")?.show();
362360
}
363361

364362
exec(): void {
@@ -389,25 +387,23 @@ export class SimpleModal {
389387
});
390388
} else {
391389
this.enableInputs();
392-
$($("#simpleModal").find("input")[0] as HTMLInputElement).trigger(
393-
"focus",
394-
);
390+
simpleModalEl.qsa("input")[0]?.focus();
395391
}
396392
});
397393
}
398394

399395
disableInputs(): void {
400-
$("#simpleModal input").prop("disabled", true);
401-
$("#simpleModal button").prop("disabled", true);
402-
$("#simpleModal textarea").prop("disabled", true);
403-
$("#simpleModal .checkbox").addClass("disabled");
396+
simpleModalEl.qsa("input").disable();
397+
simpleModalEl.qsa("button").disable();
398+
simpleModalEl.qsa("textarea").disable();
399+
simpleModalEl.qsa(".checkbox").addClass("disabled");
404400
}
405401

406402
enableInputs(): void {
407-
$("#simpleModal input").prop("disabled", false);
408-
$("#simpleModal button").prop("disabled", false);
409-
$("#simpleModal textarea").prop("disabled", false);
410-
$("#simpleModal .checkbox").removeClass("disabled");
403+
simpleModalEl.qsa("input").enable();
404+
simpleModalEl.qsa("button").enable();
405+
simpleModalEl.qsa("textarea").enable();
406+
simpleModalEl.qsa(".checkbox").removeClass("disabled");
411407
}
412408

413409
show(parameters: string[] = [], showOptions: ShowOptions): void {

0 commit comments

Comments
 (0)