Skip to content

Commit 3f04fd2

Browse files
committed
chore: Address TiCS coding standards warnings for public JS
1 parent 5573f80 commit 3f04fd2

4 files changed

Lines changed: 65 additions & 54 deletions

File tree

static/js/public/accordion.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
export const toggleAccordion = (element: HTMLElement, show: boolean): void => {
55
element.setAttribute("aria-expanded", show.toString());
6-
const controlElId: string = `#${element.getAttribute("aria-controls")}`;
7-
const controlEl = document.querySelector(controlElId) as HTMLElement;
6+
7+
const controlElId = `#${element.getAttribute("aria-controls")}`;
8+
const controlEl = document.querySelector(controlElId)!;
9+
810
controlEl.setAttribute("aria-hidden", `${show ? "false" : "true"}`);
911
};
1012

@@ -15,21 +17,25 @@ export default function initAccordion(
1517
// and removed and events do not need to be managed separately.
1618
const accordionContainer = document.querySelector(
1719
accordionContainerSelector,
18-
) as HTMLElement;
20+
)!;
21+
1922
accordionContainer.addEventListener("click", (e) => {
2023
const targetEl = e.target as HTMLElement;
2124
const target = targetEl.closest(
2225
"[class*='p-accordion__tab']",
2326
) as HTMLButtonElement;
27+
2428
if (target && !target.disabled) {
2529
// Find any open panels within the container and close them.
2630
const currentTarget = e.currentTarget as HTMLElement;
2731
const expandedElements = currentTarget.querySelectorAll(
2832
"[aria-expanded=true]",
2933
) as NodeListOf<HTMLElement>;
30-
Array.from(expandedElements).forEach((element: HTMLElement) =>
31-
toggleAccordion(element, false),
32-
);
34+
35+
Array.from(expandedElements).forEach((element: HTMLElement) => {
36+
toggleAccordion(element, false);
37+
});
38+
3339
// Open the target.
3440
toggleAccordion(target, true);
3541
}
@@ -71,22 +77,22 @@ export function initAccordionButtons(continueButton: HTMLButtonElement): void {
7177
continueButton.addEventListener("click", (event) => {
7278
event.preventDefault();
7379

74-
const currentPanel = continueButton.closest(
75-
".p-accordion__group",
76-
) as HTMLElement;
80+
const currentPanel = continueButton.closest(".p-accordion__group")!;
7781
const currentToggle = currentPanel.querySelector(
7882
"[class*='p-accordion__tab']",
7983
) as HTMLElement;
8084
const currentSuccess = currentPanel.querySelector(".p-icon--success");
8185
const nextPanel = currentPanel.nextElementSibling as HTMLElement;
8286
const nextToggle = nextPanel.querySelector(
8387
"[class*='p-accordion__tab']",
84-
) as HTMLElement;
88+
)! as HTMLElement;
8589

8690
toggleAccordion(currentToggle, false);
91+
8792
if (currentSuccess) {
8893
currentSuccess.classList.remove("u-hide");
8994
}
95+
9096
toggleAccordion(nextToggle, true);
9197
});
9298
}

static/js/public/scroll-to.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
export function animateScrollTo(to: string | number | HTMLElement, offset = 0) {
2-
const element = (document.scrollingElement as HTMLElement) || window;
1+
export function animateScrollTo(
2+
to: HTMLElement | number | string,
3+
offset = 0,
4+
): void {
5+
const element = document.scrollingElement!;
36

47
if (typeof to === "string") {
58
to = document.querySelector(to) as HTMLElement;
6-
if (!to) {
7-
throw Error(`Can't find any element for "${to}" in animateScrollTo.`);
8-
}
99
}
10+
1011
if (typeof to !== "number") {
1112
to = to.getBoundingClientRect().top + element.scrollTop;
1213
}
14+
1315
to = to - offset;
1416

15-
if (element.scrollTo) {
16-
element.scrollTo({ top: to, left: 0, behavior: "smooth" });
17-
} else {
18-
element.scrollTop = to;
19-
}
17+
element.scrollTo({ top: to, left: 0, behavior: "smooth" });
2018
}
2119

22-
export function initLinkScroll(link: HTMLLinkElement, { offset = 0 }) {
23-
if (link && (link.dataset.scrollTo || link.href)) {
24-
const href =
25-
(link.dataset.scrollTo as string) ||
26-
(link.getAttribute("href") as string);
20+
export function initLinkScroll(
21+
link: HTMLLinkElement,
22+
{ offset = 0 }: { offset: number },
23+
): void {
24+
if (link.dataset.scrollTo || link.href) {
25+
const href = link.dataset.scrollTo! || link.getAttribute("href")!;
2726
const target = document.querySelector(href) as HTMLElement;
27+
2828
if (target) {
2929
link.addEventListener("click", (event) => {
3030
event.preventDefault();
31+
3132
animateScrollTo(target, offset);
32-
setTimeout(() => window.history.pushState({}, "", href), 100);
33+
34+
setTimeout(() => {
35+
window.history.pushState({}, "", href);
36+
}, 100);
3337
});
3438
}
3539
}

static/js/public/snap-details/reportSnap.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import "whatwg-fetch";
22
import { buttonEnabled, buttonLoading } from "../../libs/formHelpers";
33

4-
const showEl = (el: HTMLElement) => el.classList.remove("u-hide");
5-
const hideEl = (el: HTMLElement) => el.classList.add("u-hide");
4+
const showEl = (el: HTMLElement): void => {
5+
el.classList.remove("u-hide");
6+
};
7+
8+
const hideEl = (el: HTMLElement): void => {
9+
el.classList.add("u-hide");
10+
};
11+
12+
function initForm(modal: HTMLElement): void {
13+
buttonEnabled(modal.querySelector("button[type=submit]")!, "Submit report");
14+
15+
showEl(modal.querySelector(".js-report-snap-form")!);
16+
hideEl(modal.querySelector(".js-report-snap-success")!);
17+
hideEl(modal.querySelector(".js-report-snap-error")!);
18+
}
619

720
function toggleModal(modal: HTMLElement, show?: boolean): void {
821
if (typeof show === "undefined") {
@@ -17,27 +30,16 @@ function toggleModal(modal: HTMLElement, show?: boolean): void {
1730
}
1831
}
1932

20-
function initForm(modal: HTMLElement): void {
21-
buttonEnabled(
22-
modal.querySelector("button[type=submit]") as HTMLButtonElement,
23-
"Submit report",
24-
);
25-
26-
showEl(modal.querySelector(".js-report-snap-form") as HTMLElement);
27-
hideEl(modal.querySelector(".js-report-snap-success") as HTMLElement);
28-
hideEl(modal.querySelector(".js-report-snap-error") as HTMLElement);
29-
}
30-
3133
function showSuccess(modal: HTMLElement): void {
32-
hideEl(modal.querySelector(".js-report-snap-form") as HTMLElement);
33-
showEl(modal.querySelector(".js-report-snap-success") as HTMLElement);
34-
hideEl(modal.querySelector(".js-report-snap-error") as HTMLElement);
34+
hideEl(modal.querySelector(".js-report-snap-form")!);
35+
showEl(modal.querySelector(".js-report-snap-success")!);
36+
hideEl(modal.querySelector(".js-report-snap-error")!);
3537
}
3638

3739
function showError(modal: HTMLElement): void {
38-
hideEl(modal.querySelector(".js-report-snap-form") as HTMLElement);
39-
hideEl(modal.querySelector(".js-report-snap-success") as HTMLElement);
40-
showEl(modal.querySelector(".js-report-snap-error") as HTMLElement);
40+
hideEl(modal.querySelector(".js-report-snap-form")!);
41+
hideEl(modal.querySelector(".js-report-snap-success")!);
42+
showEl(modal.querySelector(".js-report-snap-error")!);
4143
}
4244

4345
export default function initReportSnap(
@@ -72,14 +74,11 @@ export default function initReportSnap(
7274
reportForm.addEventListener("submit", async (e) => {
7375
e.preventDefault();
7476
buttonLoading(
75-
reportForm.querySelector("button[type=submit]") as HTMLButtonElement,
77+
reportForm.querySelector("button[type=submit]")!,
7678
"Submitting…",
7779
);
7880

79-
if (
80-
honeypotField.checked ||
81-
(commentField.value && commentField.value.includes("http"))
82-
) {
81+
if (honeypotField.checked || commentField?.value?.includes("http")) {
8382
showSuccess(modal);
8483
return;
8584
}
@@ -111,7 +110,7 @@ export default function initReportSnap(
111110

112111
// close modal on ESC
113112
window.addEventListener("keyup", (event) => {
114-
if (event.keyCode === 27) {
113+
if (event.key === "Escape") {
115114
toggleModal(modal, false);
116115
}
117116
});

static/js/public/snap-details/videos.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function vimeo(): void {
1111
"vimeoplayer",
1212
) as HTMLIFrameElement | null;
1313

14-
const vimeoReady = () => {
14+
const vimeoReady = (): void => {
1515
if (frame && window.Vimeo && window.Vimeo.Player) {
1616
const player = new window.Vimeo.Player(frame);
1717
player.on("play", function () {
@@ -21,7 +21,7 @@ function vimeo(): void {
2121
}
2222
};
2323

24-
const checkVimeo = () => {
24+
const checkVimeo = (): void => {
2525
if (window.Vimeo && window.Vimeo.Player) {
2626
vimeoReady();
2727
} else {
@@ -36,7 +36,9 @@ function asciinema(holderEl: HTMLElement): void {
3636
const asciinemaPlayer = holderEl.querySelector("iframe");
3737

3838
if (!asciinemaPlayer) {
39-
setTimeout(() => asciinema(holderEl), 200);
39+
setTimeout(() => {
40+
asciinema(holderEl);
41+
}, 200);
4042
return;
4143
}
4244
}

0 commit comments

Comments
 (0)