Skip to content

Commit 98b93d8

Browse files
refactor(ui): Consistently accept options for construction
1 parent 227172b commit 98b93d8

File tree

11 files changed

+64
-44
lines changed

11 files changed

+64
-44
lines changed

packages/userscript/source/ui/components/Container.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { UserScript } from "../../UserScript";
2-
import { UiComponent } from "./UiComponent";
2+
import { UiComponent, UiComponentOptions } from "./UiComponent";
33

44
export class Container extends UiComponent {
55
readonly element: JQuery<HTMLElement>;
@@ -8,9 +8,10 @@ export class Container extends UiComponent {
88
* Constructs a simple container element without any special properties.
99
*
1010
* @param host A reference to the host.
11+
* @param options Options for the container.
1112
*/
12-
constructor(host: UserScript) {
13-
super(host);
13+
constructor(host: UserScript, options?: Partial<UiComponentOptions>) {
14+
super(host, options);
1415

1516
const element = $("<div/>");
1617

packages/userscript/source/ui/components/CyclesList.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Cycle } from "../../types";
33
import { UserScript } from "../../UserScript";
44
import { SettingListItem } from "./SettingListItem";
55
import { SettingsList } from "./SettingsList";
6+
import { UiComponentOptions } from "./UiComponent";
67

78
export type SettingWithCycles = Record<Cycle, Setting>;
89

@@ -28,9 +29,10 @@ export class CyclesList extends SettingsList {
2829
*
2930
* @param host A reference to the host.
3031
* @param setting The settings that correlate to this list.
32+
* @param options Options for this list.
3133
*/
32-
constructor(host: UserScript, setting: SettingWithCycles) {
33-
super(host);
34+
constructor(host: UserScript, setting: SettingWithCycles, options?: Partial<UiComponentOptions>) {
35+
super(host, options);
3436
this.setting = setting;
3537

3638
this.addEventListener("enableAll", () => {

packages/userscript/source/ui/components/ExpandoButton.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { UserScript } from "../../UserScript";
2-
import { UiComponent } from "./UiComponent";
2+
import { UiComponent, UiComponentOptions } from "./UiComponent";
33

44
export class ExpandoButton extends UiComponent {
55
readonly element: JQuery<HTMLElement>;
@@ -9,9 +9,10 @@ export class ExpandoButton extends UiComponent {
99
* collapses a section of the UI.
1010
*
1111
* @param host A reference to the host.
12+
* @param options Options for this expando.
1213
*/
13-
constructor(host: UserScript) {
14-
super(host);
14+
constructor(host: UserScript, options?: Partial<UiComponentOptions>) {
15+
super(host, options);
1516

1617
const element = $("<div/>", {
1718
title: host.engine.i18n("ui.itemsShow"),

packages/userscript/source/ui/components/ExplainerListItem.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { UserScript } from "../../UserScript";
2-
import { UiComponent } from "./UiComponent";
2+
import { UiComponent, UiComponentOptions } from "./UiComponent";
33

44
export class ExplainerListItem extends UiComponent {
55
readonly element: JQuery<HTMLElement>;
@@ -10,8 +10,9 @@ export class ExplainerListItem extends UiComponent {
1010
*
1111
* @param host A reference to the host.
1212
* @param text The text to appear on the element.
13+
* @param options Options for this explainer.
1314
*/
14-
constructor(host: UserScript, text: string) {
15+
constructor(host: UserScript, text: string, options?: Partial<UiComponentOptions>) {
1516
super(host);
1617

1718
const element = $("<li/>", { text }).addClass("ks-explainer");

packages/userscript/source/ui/components/Fieldset.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { UserScript } from "../../UserScript";
2-
import { UiComponent } from "./UiComponent";
2+
import { UiComponent, UiComponentOptions } from "./UiComponent";
3+
4+
export type FieldsetOptions = UiComponentOptions & {
5+
readonly delimiter: boolean;
6+
};
37

48
export class Fieldset extends UiComponent {
59
readonly element: JQuery<HTMLElement>;
@@ -9,13 +13,13 @@ export class Fieldset extends UiComponent {
913
*
1014
* @param host A reference to the host.
1115
* @param label The label on the fieldset.
12-
* @param delimiter Should this fieldset have additional padding on the bottom?
16+
* @param options Options for the fieldset.
1317
*/
14-
constructor(host: UserScript, label: string, delimiter = false) {
15-
super(host);
18+
constructor(host: UserScript, label: string, options?: Partial<FieldsetOptions>) {
19+
super(host, options);
1620

1721
const element = $("<fieldset/>").addClass("ks-fieldset");
18-
if (delimiter) {
22+
if (options?.delimiter) {
1923
element.addClass("ks-delimiter");
2024
}
2125
const legend = $("<legend/>").text(label).addClass("ks-label");

packages/userscript/source/ui/components/HeaderListItem.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UserScript } from "../../UserScript";
22
import { ListItem } from "./ListItem";
3-
import { UiComponent } from "./UiComponent";
3+
import { UiComponent, UiComponentOptions } from "./UiComponent";
44

55
export class HeaderListItem extends UiComponent implements ListItem {
66
readonly element: JQuery<HTMLElement>;
@@ -14,9 +14,10 @@ export class HeaderListItem extends UiComponent implements ListItem {
1414
*
1515
* @param host A reference to the host.
1616
* @param text The text to appear on the header element.
17+
* @param options Options for the header.
1718
*/
18-
constructor(host: UserScript, text: string) {
19-
super(host);
19+
constructor(host: UserScript, text: string, options?: Partial<UiComponentOptions>) {
20+
super(host, options);
2021

2122
const element = $("<li/>", { text }).addClass("ks-header");
2223

packages/userscript/source/ui/components/IconButton.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { UserScript } from "../../UserScript";
2-
import { UiComponent } from "./UiComponent";
2+
import { UiComponent, UiComponentOptions } from "./UiComponent";
33

44
/**
55
* A button that is visually represented through an SVG element.
@@ -13,9 +13,15 @@ export class IconButton extends UiComponent {
1313
* @param host A reference to the host.
1414
* @param pathData The SVG path data of the icon.
1515
* @param title The `title` of the element.
16+
* @param options Options for the icon button.
1617
*/
17-
constructor(host: UserScript, pathData: string, title: string) {
18-
super(host);
18+
constructor(
19+
host: UserScript,
20+
pathData: string,
21+
title: string,
22+
options?: Partial<UiComponentOptions>
23+
) {
24+
super(host, options);
1925

2026
const element = $("<div/>", {
2127
html: `<svg style="width: 18px; height: 18px;" viewBox="0 0 48 48"><path fill="currentColor" d="${pathData}"/></svg>`,

packages/userscript/source/ui/components/LimitedButton.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { SettingLimited } from "../../settings/Settings";
22
import { UserScript } from "../../UserScript";
3-
import { UiComponent } from "./UiComponent";
3+
import { UiComponent, UiComponentOptions } from "./UiComponent";
4+
5+
export type LimitedButtonOptions = UiComponentOptions & {
6+
readonly onLimitedCheck: () => void;
7+
readonly onLimitedUnCheck: () => void;
8+
};
49

510
export class LimitedButton extends UiComponent {
611
readonly setting: SettingLimited;
712
readonly element: JQuery<HTMLElement>;
813
readonly checkbox: JQuery<HTMLElement>;
914

10-
constructor(
11-
host: UserScript,
12-
setting: SettingLimited,
13-
handler?: Partial<{ onLimitedCheck: () => void; onLimitedUnCheck: () => void }>
14-
) {
15-
super(host);
15+
constructor(host: UserScript, setting: SettingLimited, options?: Partial<LimitedButtonOptions>) {
16+
super(host, options);
1617

1718
const element = $("<div/>").addClass("ks-text-button");
1819

@@ -27,10 +28,10 @@ export class LimitedButton extends UiComponent {
2728
checkbox.on("change", () => {
2829
if (checkbox.is(":checked") && setting.limited === false) {
2930
setting.limited = true;
30-
handler?.onLimitedCheck?.();
31+
options?.onLimitedCheck?.();
3132
} else if (!checkbox.is(":checked") && setting.limited === true) {
3233
setting.limited = false;
33-
handler?.onLimitedUnCheck?.();
34+
options?.onLimitedUnCheck?.();
3435
}
3536
});
3637

packages/userscript/source/ui/components/ListItem.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { UserScript } from "../../UserScript";
2-
import { UiComponent } from "./UiComponent";
2+
import { UiComponent, UiComponentOptions } from "./UiComponent";
33

4-
export type ListItemOptions = {
4+
export type ListItemOptions = UiComponentOptions & {
55
/**
66
* Should there be additional padding below this element?
77
*/
@@ -18,7 +18,7 @@ export class ListItem extends UiComponent {
1818
* @param options Options for the list item.
1919
*/
2020
constructor(host: UserScript, options?: Partial<ListItemOptions>) {
21-
super(host);
21+
super(host, options);
2222

2323
const element = $("<li/>");
2424
element.addClass("ks-setting");

packages/userscript/source/ui/components/OptionsListItem.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { SettingOptions } from "../../settings/Settings";
22
import { UserScript } from "../../UserScript";
33
import { Fieldset } from "./Fieldset";
44
import { RadioItem } from "./RadioItem";
5-
import { UiComponent } from "./UiComponent";
5+
import { UiComponent, UiComponentOptions } from "./UiComponent";
6+
7+
export type OptionsListItemOptions = UiComponentOptions & {
8+
readonly onCheck: () => void;
9+
readonly readOnly: boolean;
10+
};
611

712
export class OptionsListItem<TSetting extends SettingOptions = SettingOptions> extends UiComponent {
813
readonly fieldset: Fieldset;
@@ -17,20 +22,15 @@ export class OptionsListItem<TSetting extends SettingOptions = SettingOptions> e
1722
* @param host The userscript instance.
1823
* @param label The label on the setting element.
1924
* @param setting The setting this element is linked to.
20-
* @param handler The event handlers for this setting element.
21-
* @param handler.onCheck Will be invoked when the user selects an option.
22-
* @param readOnly Should the user be prevented from changing the value of the input?
25+
* @param options Options for the list item.
2326
*/
2427
constructor(
2528
host: UserScript,
2629
label: string,
2730
setting: TSetting,
28-
handler?: {
29-
onCheck: () => void;
30-
},
31-
readOnly = false
31+
options?: Partial<OptionsListItemOptions>
3232
) {
33-
super(host);
33+
super(host, options);
3434

3535
this.element = $(`<li/>`);
3636

@@ -40,7 +40,10 @@ export class OptionsListItem<TSetting extends SettingOptions = SettingOptions> e
4040
this._options = new Array<RadioItem>();
4141
for (const option of setting.options) {
4242
this._options.push(
43-
new RadioItem(host, setting, option, label, { onCheck: handler?.onCheck, readOnly })
43+
new RadioItem(host, setting, option, label, {
44+
onCheck: options?.onCheck,
45+
readOnly: options?.readOnly,
46+
})
4447
);
4548
}
4649
this.fieldset.addChildren(this._options);

packages/userscript/source/ui/components/SettingsList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class SettingsList extends UiComponent {
4747
* @param host A reference to the host.
4848
* @param options Which tools should be available on the list?
4949
*/
50-
constructor(host: UserScript, options?: SettingsListOptions) {
50+
constructor(host: UserScript, options?: Partial<SettingsListOptions>) {
5151
super(host, options);
5252

5353
const toolOptions = {

0 commit comments

Comments
 (0)