Skip to content

Commit 9e0a55e

Browse files
committed
refactor: Replace old event handler bindings with new initialize callback rebinding
1 parent 715ed05 commit 9e0a55e

9 files changed

+59
-45
lines changed

dist/utilities/elements.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export declare function isHTMLLinkElement(element: Element): element is HTMLLinkElement;
2-
export declare function isHTMLFormElement(element: Element): element is HTMLFormElement;
3-
export declare function isHTMLInputElement(element: Element): element is HTMLInputElement;
4-
export declare function isHTMLImageElement(element: Element): element is HTMLImageElement;
5-
export declare function isElementCheckable(element: Element): element is HTMLInputElement & {
6-
checked: boolean;
7-
};
1+
export declare function isHTMLLinkElement(element: Element): element is HTMLLinkElement;
2+
export declare function isHTMLFormElement(element: Element): element is HTMLFormElement;
3+
export declare function isHTMLInputElement(element: Element): element is HTMLInputElement;
4+
export declare function isHTMLImageElement(element: Element): element is HTMLImageElement;
5+
export declare function isElementCheckable(element: Element): element is HTMLInputElement & {
6+
checked: boolean;
7+
};
88
//# sourceMappingURL=elements.d.ts.map

src/auto_submit_form_controller.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import {BaseController} from "./base_controller";
22

33
export class AutoSubmitFormController extends BaseController {
44

5-
private boundHandler = this.handler.bind(this);
5+
initialize() {
6+
this.handler = this.handler.bind(this);
7+
}
68

79
connect() {
8-
(this.element as HTMLElement).querySelectorAll("input, select, textarea").forEach(el => el.addEventListener("change", this.boundHandler));
10+
(this.element as HTMLElement).querySelectorAll("input, select, textarea").forEach(el => el.addEventListener("change", this.handler));
911
}
1012

1113
disconnect() {
12-
(this.element as HTMLElement).querySelectorAll("input, select, textarea").forEach(el => el.removeEventListener("change", this.boundHandler));
14+
(this.element as HTMLElement).querySelectorAll("input, select, textarea").forEach(el => el.removeEventListener("change", this.handler));
1315
}
1416

1517
private handler(e: Event) {
1618
// this.element.submit()
1719
// Moved to this to support remote forms and CSRF properly
18-
this.element.dispatchEvent(
20+
(this.element as HTMLElement).dispatchEvent(
1921
new CustomEvent("submit", {
2022
bubbles: true,
2123
cancelable: true,

src/autosize_controller.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ import {BaseController} from "./base_controller";
33

44
export class AutosizeController extends BaseController {
55

6-
private boundHandler = this.handler.bind(this);
6+
initialize() {
7+
this._handler = this._handler.bind(this);
8+
}
79

810
connect() {
911
let target = this.element as HTMLTextAreaElement;
1012
target.style.resize = "none";
1113
target.style.boxSizing = "border-box";
12-
target.addEventListener("input", this.boundHandler);
13-
target.addEventListener("focus", this.boundHandler);
14+
target.addEventListener("input", this._handler);
15+
target.addEventListener("focus", this._handler);
1416
useWindowResize(this);
15-
requestAnimationFrame(this.boundHandler);
17+
requestAnimationFrame(this._handler);
1618
}
1719

1820
windowResize() {
19-
this.handler();
21+
this._handler();
2022
}
2123

22-
private handler() {
24+
private _handler() {
2325
this.autosize(this.element as HTMLTextAreaElement);
2426
};
2527

src/char_count_controller.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,32 @@ export class CharCountController extends BaseController {
1515
declare errorClass: string;
1616
declare hasErrorClass: boolean;
1717

18-
boundHandler = this.updateCharCount.bind(this);
18+
initialize() {
19+
this._updateCharCount = this._updateCharCount.bind(this);
20+
}
1921

2022
connect() {
21-
this.updateCharCount();
22-
this.inputTarget.addEventListener("input", this.boundHandler);
23+
this._updateCharCount();
24+
this.inputTarget.addEventListener("input", this._updateCharCount);
2325
}
2426

2527
disconnect() {
26-
this.inputTarget.removeEventListener("input", this.boundHandler);
28+
this.inputTarget.removeEventListener("input", this._updateCharCount);
2729
}
2830

29-
updateCharCount() {
31+
private _updateCharCount() {
3032
let charCount = this.inputTarget.value.length;
3133
this.outputTarget.innerText = charCount.toString();
3234
if (this.hasErrorClass) {
33-
if (this.isValidCount(charCount)) {
35+
if (this._isValidCount(charCount)) {
3436
this.outputTarget.classList.remove(this.errorClass);
3537
} else {
3638
this.outputTarget.classList.add(this.errorClass);
3739
}
3840
}
3941
}
4042

41-
isValidCount(count: number) {
43+
private _isValidCount(count: number) {
4244
let min = 0;
4345
let max = 99999;
4446

src/detect_dirty_controller.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ export class DetectDirtyController extends BaseController {
55

66
initialValue: string | boolean | null = null;
77

8-
boundHandler = this.handler.bind(this);
8+
initialize() {
9+
this.checkDirty = this.checkDirty.bind(this);
10+
}
911

1012
connect() {
1113
let element = this.element as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
@@ -14,14 +16,14 @@ export class DetectDirtyController extends BaseController {
1416
} else {
1517
this.initialValue = element.value;
1618
}
17-
element.addEventListener("input", this.boundHandler);
18-
element.addEventListener("change", this.boundHandler);
19+
element.addEventListener("input", this.checkDirty);
20+
element.addEventListener("change", this.checkDirty);
1921
}
2022

2123
disconnect() {
2224
let element = this.element as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
23-
element.removeEventListener("input", this.boundHandler);
24-
element.removeEventListener("change", this.boundHandler);
25+
element.removeEventListener("input", this.checkDirty);
26+
element.removeEventListener("change", this.checkDirty);
2527
}
2628

2729
restore() {
@@ -33,7 +35,7 @@ export class DetectDirtyController extends BaseController {
3335
}
3436
}
3537

36-
private handler(event?: Event) {
38+
private checkDirty(event?: Event) {
3739
let element = this.element as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
3840

3941
if (this.initialValue !== element.value) {

src/limited_selection_checkboxes_controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ export class LimitedSelectionCheckboxesController extends BaseController {
1111
declare readonly maxValue: number;
1212
declare readonly messageValue: string;
1313

14-
maxSelections = 0;
15-
16-
boundHandleInputs = this.handleInputs.bind(this);
14+
initialize() {
15+
this.handleInputs = this.handleInputs.bind(this);
16+
}
1717

1818
connect() {
19-
this.inputTargets.forEach((el) => el.addEventListener("change", this.boundHandleInputs));
19+
this.inputTargets.forEach((el) => el.addEventListener("change", this.handleInputs));
2020
}
2121

2222
disconnect() {
23-
this.inputTargets.forEach((el) => el.removeEventListener("change", this.boundHandleInputs));
23+
this.inputTargets.forEach((el) => el.removeEventListener("change", this.handleInputs));
2424
}
2525

2626
handleInputs(event: Event) {

src/password_confirm_controller.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ export class PasswordConfirmController extends BaseController {
1010
declare readonly errorClass: string;
1111
declare readonly hasErrorClass: boolean;
1212

13-
private boundCheckPasswordsMatch = this.checkPasswordsMatch.bind(this);
13+
initialize() {
14+
this.checkPasswordsMatch = this.checkPasswordsMatch.bind(this);
15+
}
1416

1517
connect() {
16-
this.passwordTargets.forEach((el) => el.addEventListener("change", this.boundCheckPasswordsMatch));
18+
this.passwordTargets.forEach((el) => el.addEventListener("change", this.checkPasswordsMatch));
1719
}
1820

1921
disconnect() {
20-
this.passwordTargets.forEach((el) => el.removeEventListener("change", this.boundCheckPasswordsMatch));
22+
this.passwordTargets.forEach((el) => el.removeEventListener("change", this.checkPasswordsMatch));
2123
}
2224

23-
private allPasswordsMatch() {
25+
private allPasswordsMatch(): boolean {
2426
let values = new Set(this.passwordTargets.map(el => el.value)); // Create a unique set of the password values
2527
return values.has("") || values.size == 1; // If any of the passwords are still blank, or there is only one distinct password value (i.e. they all are the same)
2628
}

src/responsive_iframe_controller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ interface ResponsiveIframeMessage {
99

1010
export class ResponsiveIframeWrapperController extends BaseController {
1111

12-
boundMessageReceived = this.messageReceived.bind(this);
12+
initialize() {
13+
this.messageReceived = this.messageReceived.bind(this);
14+
}
1315

1416
connect() {
15-
window.addEventListener("message", this.boundMessageReceived);
17+
window.addEventListener("message", this.messageReceived);
1618
}
1719

1820
disconnect() {
19-
window.removeEventListener("message", this.boundMessageReceived);
21+
window.removeEventListener("message", this.messageReceived);
2022
}
2123

2224
messageReceived(message: MessageEvent<ResponsiveIframeMessage>) {

src/word_count_controller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ export class WordCountController extends BaseController {
1515
declare errorClass: string;
1616
declare hasErrorClass: boolean;
1717

18-
boundHandler = this.updateWordCount.bind(this);
18+
initialize() {
19+
this.updateWordCount = this.updateWordCount.bind(this);
20+
}
1921

2022
connect() {
2123
this.updateWordCount();
22-
this.inputTarget.addEventListener("input", this.boundHandler);
24+
this.inputTarget.addEventListener("input", this.updateWordCount);
2325
}
2426

2527
disconnect() {
26-
this.inputTarget.removeEventListener("input", this.boundHandler);
28+
this.inputTarget.removeEventListener("input", this.updateWordCount);
2729
}
2830

2931
updateWordCount() {

0 commit comments

Comments
 (0)