Skip to content

Commit 7c3478c

Browse files
committed
chore: Address TiCS codings standards warnings for base JS
1 parent 757f031 commit 7c3478c

7 files changed

Lines changed: 107 additions & 85 deletions

File tree

static/js/base/__tests__/ga.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ describe("triggerEvent", () => {
1919
test("pushes event to dataLayer", () => {
2020
window.dataLayer = [];
2121

22-
triggerEvent("category", "from", "to", "label");
22+
triggerEvent({
23+
category: "category",
24+
from: "from",
25+
to: "to",
26+
label: "label",
27+
});
2328

2429
expect(window.dataLayer[0].event).toBe("GAEvent");
2530
expect(window.dataLayer[0].eventCategory).toBe("snapcraft.io-category");

static/js/base/contactForm.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ const contactFormTriggers = document.querySelectorAll(
22
"[data-js='contact-form-trigger']",
33
) as NodeListOf<Element>;
44

5-
const modal = document.getElementById("contact-form-modal") as HTMLElement;
6-
const modalBody = document.getElementById("modal-body") as HTMLElement;
5+
const modal = document.getElementById("contact-form-modal") as
6+
| HTMLElement
7+
| undefined;
8+
const modalBody = document.getElementById("modal-body") as
9+
| HTMLElement
10+
| undefined;
711

812
if (modal) {
913
const closeModal = modal.querySelector(".p-modal__close");
1014
if (closeModal) {
11-
closeModal.addEventListener("click", () => modal.classList.add("u-hide"));
15+
closeModal.addEventListener("click", () => {
16+
modal.classList.add("u-hide");
17+
});
1218
}
1319
}
1420

@@ -24,12 +30,17 @@ function handleClick(event: Event): void {
2430

2531
let formTemplate = formEl.innerText;
2632

27-
Object.keys(args).forEach((arg) => {
33+
Object.keys(args).forEach((arg: string) => {
2834
formTemplate = formTemplate.split(`{{${arg}}}`).join(`${args[arg]}`);
2935
});
3036

31-
modalBody.innerHTML = formTemplate;
32-
modal.classList.remove("u-hide");
37+
if (modalBody) {
38+
modalBody.innerHTML = formTemplate;
39+
}
40+
41+
if (modal) {
42+
modal.classList.remove("u-hide");
43+
}
3344
}
3445

3546
function attachClickHandler(element: Element): void {

static/js/base/dropdown-menu-toggle.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
1-
(function () {
2-
function toggleDropdown(toggle: HTMLElement, open: boolean) {
1+
(function (): void {
2+
function toggleDropdown(toggle: HTMLElement, open: boolean): void {
33
const parentElement = toggle.parentNode as HTMLElement;
4-
const dropdownElId = toggle.getAttribute("aria-controls") as string;
4+
const dropdownElId = toggle.getAttribute("aria-controls") as
5+
| string
6+
| undefined;
57

6-
const dropdown = document.getElementById(dropdownElId) as HTMLElement;
8+
if (dropdownElId) {
9+
const dropdown = document.getElementById(dropdownElId) as
10+
| HTMLElement
11+
| undefined;
712

8-
const openMenu = !open;
13+
const openMenu = !open;
914

10-
dropdown.setAttribute("aria-hidden", openMenu.toString());
15+
if (dropdown) {
16+
dropdown.setAttribute("aria-hidden", openMenu.toString());
1117

12-
if (open) {
13-
parentElement.classList.add("is-active");
14-
parentElement.classList.add("is-selected");
15-
} else {
16-
parentElement.classList.remove("is-active");
17-
parentElement.classList.remove("is-selected");
18+
if (open) {
19+
parentElement.classList.add("is-active");
20+
parentElement.classList.add("is-selected");
21+
} else {
22+
parentElement.classList.remove("is-active");
23+
parentElement.classList.remove("is-selected");
24+
}
25+
}
1826
}
1927
}
2028

21-
function closeAllDropdowns(toggles: Array<HTMLElement>) {
29+
function closeAllDropdowns(toggles: HTMLElement[]): void {
2230
toggles.forEach(function (toggle) {
2331
toggleDropdown(toggle, false);
2432
});
2533
}
2634

2735
function handleClickOutside(
28-
toggles: Array<HTMLElement>,
36+
toggles: HTMLElement[],
2937
containerClass: string,
30-
) {
38+
): void {
3139
document.addEventListener("click", function (event) {
3240
const target = event.target as HTMLElement;
3341

34-
if (target.closest) {
35-
if (!target.closest(containerClass)) {
36-
closeAllDropdowns(toggles);
37-
}
42+
if (!target.closest(containerClass)) {
43+
closeAllDropdowns(toggles);
3844
}
3945
});
4046
}
4147

42-
function initNavDropdowns(containerClass: string) {
48+
function initNavDropdowns(containerClass: string): void {
4349
const toggles = [].slice.call(
4450
document.querySelectorAll(containerClass + " [aria-controls]"),
4551
);

static/js/base/ga.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
const origin = window.location.href;
1+
const urlLocation = window.location.href;
22
const categoryPrefix = "snapcraft.io-";
33

4-
type Events = {
5-
[key: string]: string;
6-
};
7-
8-
const events: Events = {
4+
const events: Record<string, string> = {
95
".global-nav a": "nav-0",
106
".p-navigation a": "nav-1",
117
"#footer a": "footer-0",
@@ -21,12 +17,14 @@ const events: Events = {
2117
".p-strip a": "content-link",
2218
};
2319

24-
function triggerEvent(
25-
category: string,
26-
from: string,
27-
to: string,
28-
label: string,
29-
): void {
20+
type Event = {
21+
category: string;
22+
from: string;
23+
to: string;
24+
label: string;
25+
};
26+
27+
function triggerEvent({ category, from, to, label }: Event): void {
3028
if (window.dataLayer) {
3129
window.dataLayer.push({
3230
event: "GAEvent",
@@ -56,14 +54,14 @@ if (typeof window.dataLayer !== "undefined") {
5654

5755
const eventTarget = e.target as HTMLElement;
5856

59-
if (!eventTarget || !eventTarget.closest) {
57+
if (!eventTarget?.closest) {
6058
return;
6159
}
6260

63-
target = eventTarget.closest("a") as HTMLAnchorElement;
61+
target = eventTarget.closest("a")!;
6462

6563
if (!target) {
66-
target = eventTarget.closest("button") as HTMLButtonElement;
64+
target = eventTarget.closest("button")!;
6765
}
6866

6967
if (!target) {
@@ -91,7 +89,12 @@ if (typeof window.dataLayer !== "undefined") {
9189
}
9290

9391
if (target instanceof HTMLAnchorElement) {
94-
triggerEvent(events[key], origin, target.href, label);
92+
triggerEvent({
93+
category: events[key],
94+
from: urlLocation,
95+
to: target.href,
96+
label,
97+
});
9598
}
9699

97100
break;

static/js/base/navigation.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Login
2-
const navAccountContainer = document.querySelector(
3-
".js-nav-account",
4-
) as HTMLElement;
2+
const navAccountContainer = document.querySelector(".js-nav-account") as
3+
| HTMLElement
4+
| undefined;
55

66
if (navAccountContainer) {
77
const notAuthenticatedMenu = navAccountContainer.querySelector(
88
".js-nav-account--notauthenticated",
9-
) as HTMLElement;
9+
)!;
1010
const authenticatedMenu = navAccountContainer.querySelector(
1111
".js-nav-account--authenticated",
12-
) as HTMLElement;
12+
)!;
1313

1414
fetch("/account.json")
15-
.then((response) => response.json())
15+
.then(async (response) => response.json())
1616
.then(
1717
(data: {
1818
publisher: {
@@ -22,31 +22,25 @@ if (navAccountContainer) {
2222
};
2323
}) => {
2424
if (data.publisher) {
25-
const displayName = navAccountContainer.querySelector(
26-
".js-account--name",
27-
) as HTMLElement;
25+
const displayName =
26+
navAccountContainer.querySelector(".js-account--name")!;
2827

2928
notAuthenticatedMenu.classList.add("u-hide");
3029
authenticatedMenu.classList.remove("u-hide");
31-
displayName.innerHTML = data.publisher["fullname"];
32-
if (window.sessionStorage) {
33-
window.sessionStorage.setItem(
34-
"displayName",
35-
data.publisher["fullname"],
36-
);
37-
}
30+
displayName.innerHTML = data.publisher.fullname;
31+
window.sessionStorage.setItem("displayName", data.publisher.fullname);
3832

3933
if (data.publisher.has_stores) {
4034
const storesMenu = authenticatedMenu.querySelector(
4135
".js-nav-account--stores",
42-
) as HTMLElement;
36+
)!;
4337
storesMenu.classList.remove("u-hide");
4438
}
4539

4640
if (data.publisher.has_validation_sets) {
4741
const validationSetsMenu = authenticatedMenu.querySelector(
4842
".js-nav-account--validation-sets",
49-
) as HTMLElement;
43+
)!;
5044
validationSetsMenu.classList.remove("u-hide");
5145
}
5246
} else {

static/js/public/ga-scroll-event.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ export default function triggerEventWhenVisible(selector: string): void {
1919

2020
if (el) {
2121
if (isInViewport(el)) {
22-
triggerEvent(
23-
"element-visible",
24-
origin,
25-
selector,
26-
`Element visible on screen: ${selector}`,
27-
);
22+
triggerEvent({
23+
category: "element-visible",
24+
from: origin,
25+
to: selector,
26+
label: `Element visible on screen: ${selector}`,
27+
});
2828
} else {
2929
let triggered = false;
3030
window.addEventListener(
3131
"scroll",
3232
debounce(() => {
3333
if (!triggered && isInViewport(el)) {
34-
triggerEvent(
35-
"element-visible",
36-
origin,
37-
selector,
38-
`Element visible on screen: ${selector}`,
39-
);
34+
triggerEvent({
35+
category: "element-visible",
36+
from: origin,
37+
to: selector,
38+
label: `Element visible on screen: ${selector}`,
39+
});
4040
triggered = true;
4141
}
4242
}, 500),

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,15 @@ class ChannelMap {
194194
} else {
195195
this.openChannelMap(target);
196196

197-
triggerEvent(
198-
this.openScreenName === "channel-map-install" ? "cta-0" : "cta-1",
199-
window.location.href,
200-
target.dataset.controls ?? "",
201-
target.innerText,
202-
);
197+
triggerEvent({
198+
category:
199+
this.openScreenName === "channel-map-install"
200+
? "cta-0"
201+
: "cta-1",
202+
from: window.location.href,
203+
to: target.dataset.controls ?? "",
204+
label: target.innerText,
205+
});
203206
}
204207
},
205208

@@ -232,12 +235,12 @@ class ChannelMap {
232235
if (isSnapElement(target)) {
233236
event.preventDefault();
234237
this.openDesktop(target);
235-
triggerEvent(
236-
"cta-1",
237-
window.location.href,
238-
`snap://${target.dataset.snap}`,
239-
target.innerText,
240-
);
238+
triggerEvent({
239+
category: "cta-1",
240+
from: window.location.href,
241+
to: `snap://${target.dataset.snap}`,
242+
label: target.innerText,
243+
});
241244
} else {
242245
console.error("Target is not a SnapElement");
243246
}

0 commit comments

Comments
 (0)