Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
refactor(ui): Move core UI elements out of SettingsSectionUi
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Sep 29, 2022
1 parent 67e9f8d commit da66504
Show file tree
Hide file tree
Showing 16 changed files with 434 additions and 326 deletions.
39 changes: 26 additions & 13 deletions packages/userscript/source/ui/BonfireSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ucfirst } from "../tools/Format";
import { mustExist } from "../tools/Maybe";
import { UserScript } from "../UserScript";
import { SettingsSectionUi } from "./SettingsSectionUi";
import { SettingUi } from "./SettingUi";

export class BonfireSettingsUi extends SettingsSectionUi {
readonly element: JQuery<HTMLElement>;
Expand Down Expand Up @@ -288,7 +289,8 @@ export class BonfireSettingsUi extends SettingsSectionUi {
private _getAdditionOptions(): Array<JQuery<HTMLElement>> {
const header = this._getHeader("Additional options");

const upgradeBuildingsButton = this._getOption(
const upgradeBuildingsButton = SettingUi.make(
this._host,
"buildings",
this._settings.upgradeBuildings,
this._host.i18n("ui.upgrade.buildings"),
Expand All @@ -314,16 +316,24 @@ export class BonfireSettingsUi extends SettingsSectionUi {
const upgradeBuildingsButtons = [];
for (const [upgradeName, upgrade] of objectEntries(this._settings.upgradeBuildings.items)) {
const label = this._host.i18n(`$buildings.${upgradeName}.label`);
const button = this._getOption(`building-${upgradeName}`, upgrade, label, false, false, {
onCheck: () => {
this._host.updateOptions(() => (upgrade.enabled = true));
this._host.imessage("status.auto.enable", [label]);
},
onUnCheck: () => {
this._host.updateOptions(() => (upgrade.enabled = false));
this._host.imessage("status.auto.disable", [label]);
},
});
const button = SettingUi.make(
this._host,
`building-${upgradeName}`,
upgrade,
label,
false,
false,
{
onCheck: () => {
this._host.updateOptions(() => (upgrade.enabled = true));
this._host.imessage("status.auto.enable", [label]);
},
onUnCheck: () => {
this._host.updateOptions(() => (upgrade.enabled = false));
this._host.imessage("status.auto.disable", [label]);
},
}
);

upgradeBuildingsButtons.push({ label: label, button: button });
}
Expand All @@ -347,7 +357,8 @@ export class BonfireSettingsUi extends SettingsSectionUi {
});
upgradeBuildingsButton.append(upgradeBuildingsItemsButton, upgradeBuildingsList);

const nodeTurnOnSteamworks = this._getOption(
const nodeTurnOnSteamworks = SettingUi.make(
this._host,
"_steamworks",
this._settings.turnOnSteamworks,
this._host.i18n("option.steamworks"),
Expand Down Expand Up @@ -389,7 +400,9 @@ export class BonfireSettingsUi extends SettingsSectionUi {
this.setState(this._settings);

mustExist(this._settings.$enabled).prop("checked", this._settings.enabled);
mustExist(this._settings.$trigger)[0].title = this._renderPercentage(this._settings.trigger);
mustExist(this._settings.$trigger)[0].title = SettingsSectionUi.renderPercentage(
this._settings.trigger
);
mustExist(this._settings.upgradeBuildings.$enabled).prop(
"checked",
this._settings.upgradeBuildings.enabled
Expand Down
3 changes: 2 additions & 1 deletion packages/userscript/source/ui/FilterSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ucfirst } from "../tools/Format";
import { mustExist } from "../tools/Maybe";
import { UserScript } from "../UserScript";
import { SettingsSectionUi } from "./SettingsSectionUi";
import { SettingUi } from "./SettingUi";

export class FiltersSettingsUi extends SettingsSectionUi {
readonly element: JQuery<HTMLElement>;
Expand Down Expand Up @@ -115,7 +116,7 @@ export class FiltersSettingsUi extends SettingsSectionUi {
] as const;

const makeButton = (name: FilterItem, option: FilterSettingsItem, label: string) =>
this._getOption(name, option, label, false, false, {
SettingUi.make(this._host, name, option, label, false, false, {
onCheck: () => {
option.enabled = true;
this._host.imessage("filter.enable", [label]);
Expand Down
8 changes: 5 additions & 3 deletions packages/userscript/source/ui/OptionsSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ucfirst } from "../tools/Format";
import { isNil, mustExist } from "../tools/Maybe";
import { UserScript } from "../UserScript";
import { SettingsSectionUi } from "./SettingsSectionUi";
import { SettingTriggerUi } from "./SettingTriggerUi";
import { SettingUi } from "./SettingUi";

export class OptionsSettingsUi extends SettingsSectionUi {
readonly element: JQuery<HTMLElement>;
Expand Down Expand Up @@ -63,8 +65,8 @@ export class OptionsSettingsUi extends SettingsSectionUi {
iname: string
): JQuery<HTMLElement> {
return option.trigger
? this._getOptionWithTrigger(name, option as SettingTrigger, iname)
: this._getOption(name, option, iname);
? SettingTriggerUi.make(this._host, name, option as SettingTrigger, iname)
: SettingUi.make(this._host, name, option, iname);
}

setState(state: OptionsSettings): void {
Expand All @@ -88,7 +90,7 @@ export class OptionsSettingsUi extends SettingsSectionUi {
mustExist(option.$enabled).prop("checked", this._settings.items[name].enabled);

if (!isNil(option.$trigger)) {
option.$trigger[0].title = this._renderPercentage(
option.$trigger[0].title = SettingsSectionUi.renderPercentage(
mustExist(this._settings.items[name].trigger)
);
}
Expand Down
22 changes: 15 additions & 7 deletions packages/userscript/source/ui/ReligionSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { mustExist } from "../tools/Maybe";
import { UnicornItemVariant } from "../types";
import { UserScript } from "../UserScript";
import { SettingsSectionUi } from "./SettingsSectionUi";
import { SettingTriggerUi } from "./SettingTriggerUi";
import { SettingUi } from "./SettingUi";

export class ReligionSettingsUi extends SettingsSectionUi {
readonly element: JQuery<HTMLElement>;
Expand Down Expand Up @@ -227,7 +229,8 @@ export class ReligionSettingsUi extends SettingsSectionUi {
getAdditionOptions(): Array<JQuery<HTMLElement>> {
const nodeHeader = this._getHeader("Additional options");

const nodeAdore = this._getOptionWithTrigger(
const nodeAdore = SettingTriggerUi.make(
this._host,
"adore",
this._settings.adore,
this._host.i18n("option.faith.adore"),
Expand All @@ -245,7 +248,8 @@ export class ReligionSettingsUi extends SettingsSectionUi {
}
);

const nodeAutoPraise = this._getOptionWithTrigger(
const nodeAutoPraise = SettingTriggerUi.make(
this._host,
"autoPraise",
this._settings.autoPraise,
this._host.i18n("option.praise"),
Expand All @@ -263,7 +267,8 @@ export class ReligionSettingsUi extends SettingsSectionUi {
}
);

const nodeBestUnicornBuilding = this._getOption(
const nodeBestUnicornBuilding = SettingUi.make(
this._host,
"bestUnicornBuilding",
this._settings.bestUnicornBuilding,
this._host.i18n("option.faith.best.unicorn"),
Expand Down Expand Up @@ -313,7 +318,8 @@ export class ReligionSettingsUi extends SettingsSectionUi {
//this._host.saveToKittenStorage();
});

const nodeTranscend = this._getOption(
const nodeTranscend = SettingUi.make(
this._host,
"transcend",
this._settings.transcend,
this._host.i18n("option.faith.transcend"),
Expand Down Expand Up @@ -355,17 +361,19 @@ export class ReligionSettingsUi extends SettingsSectionUi {
this.setState(this._settings);

mustExist(this._settings.$enabled).prop("checked", this._settings.enabled);
mustExist(this._settings.$trigger)[0].title = this._renderPercentage(this._settings.trigger);
mustExist(this._settings.$trigger)[0].title = SettingsSectionUi.renderPercentage(
this._settings.trigger
);

mustExist(this._settings.adore.$enabled).prop("checked", this._settings.adore.enabled);
mustExist(this._settings.adore.$trigger)[0].title = this._renderPercentage(
mustExist(this._settings.adore.$trigger)[0].title = SettingsSectionUi.renderPercentage(
this._settings.adore.trigger
);
mustExist(this._settings.autoPraise.$enabled).prop(
"checked",
this._settings.autoPraise.enabled
);
mustExist(this._settings.autoPraise.$trigger)[0].title = this._renderPercentage(
mustExist(this._settings.autoPraise.$trigger)[0].title = SettingsSectionUi.renderPercentage(
this._settings.autoPraise.trigger
);
mustExist(this._settings.bestUnicornBuilding.$enabled).prop(
Expand Down
12 changes: 8 additions & 4 deletions packages/userscript/source/ui/ScienceSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ucfirst } from "../tools/Format";
import { mustExist } from "../tools/Maybe";
import { UserScript } from "../UserScript";
import { SettingsSectionUi } from "./SettingsSectionUi";
import { SettingUi } from "./SettingUi";

export class ScienceSettingsUi extends SettingsSectionUi {
readonly element: JQuery<HTMLElement>;
Expand All @@ -29,7 +30,8 @@ export class ScienceSettingsUi extends SettingsSectionUi {
const element = this._getSettingsPanel(toggleName, label, this._settings, list);
this._settings.$enabled = element.checkbox;

const techsButton = this._getOption(
const techsButton = SettingUi.make(
this._host,
"techs",
this._settings.techs,
this._host.i18n("ui.upgrade.techs"),
Expand All @@ -55,7 +57,7 @@ export class ScienceSettingsUi extends SettingsSectionUi {
const techButtons = [];
for (const [techName, tech] of objectEntries(this._settings.techs.items)) {
const label = this._host.i18n(`$science.${techName}.label`);
const button = this._getOption(`tech-${techName}`, tech, label, false, false, {
const button = SettingUi.make(this._host, `tech-${techName}`, tech, label, false, false, {
onCheck: () => {
this._host.updateOptions(() => (tech.enabled = true));
this._host.imessage("status.auto.enable", [label]);
Expand Down Expand Up @@ -87,7 +89,8 @@ export class ScienceSettingsUi extends SettingsSectionUi {
techsButton.append(techsItemsButton, techsList);

// Set up the more complex policies options.
const policiesButton = this._getOption(
const policiesButton = SettingUi.make(
this._host,
"policies",
this._settings.policies,
this._host.i18n("ui.upgrade.policies"),
Expand Down Expand Up @@ -115,7 +118,8 @@ export class ScienceSettingsUi extends SettingsSectionUi {
const policyLabel = this._host.i18n(
`$policy.${policyName === "authocracy" ? "autocracy" : policyName}.label`
);
const policyButton = this._getOption(
const policyButton = SettingUi.make(
this._host,
`policy-${policyName}`,
policy,
policyLabel,
Expand Down
76 changes: 76 additions & 0 deletions packages/userscript/source/ui/SettingLimitedUi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { SettingLimited } from "../options/Settings";
import { clog } from "../tools/Log";
import { UserScript } from "../UserScript";
import { SettingUi } from "./SettingUi";

export class SettingLimitedUi {
/**
* Create a UI element for an option that can be limited.
* This will result in an element with a checkbox that has a "Limited" label.
*
* @param host The userscript instance.
* @param name A unique name for this option.
* @param option The option model.
* @param label The label for the option.
* @param delimiter Should a delimiter be rendered after this element?
* @param upgradeIndicator Should an indicator be rendered in front of the elemnt,
* to indicate that this is an upgrade of a prior option?
* @param handler Handlers to call when the option is checked or unchecked.
* @param handler.onCheck Is called when the option is checked.
* @param handler.onUnCheck Is called when the option is unchecked.
* @param handler.onLimitedCheck Is called when the "Limited" checkbox is checked.
* @param handler.onLimitedUnCheck Is called when the "Limited" checkbox is unchecked.
* @returns The created element.
*/
static make(
host: UserScript,
name: string,
option: SettingLimited,
label: string,
delimiter = false,
upgradeIndicator = false,
handler: {
onCheck?: () => void;
onUnCheck?: () => void;
onLimitedCheck?: () => void;
onLimitedUnCheck?: () => void;
} = {}
): JQuery<HTMLElement> {
const element = SettingUi.make(host, name, option, label, delimiter, upgradeIndicator, handler);

const labelElement = $("<label/>", {
for: `toggle-limited-${name}`,
text: host.i18n("ui.limit"),
});

const input = $("<input/>", {
id: `toggle-limited-${name}`,
type: "checkbox",
}).data("option", option);
option.$limited = input;

input.on("change", () => {
if (input.is(":checked") && option.limited === false) {
if (handler.onLimitedCheck) {
handler.onLimitedCheck();
return;
}

host.updateOptions(() => (option.limited = true));
clog("Unlogged action item");
} else if (!input.is(":checked") && option.limited === true) {
if (handler.onLimitedUnCheck) {
handler.onLimitedUnCheck();
return;
}

host.updateOptions(() => (option.limited = false));
clog("Unlogged action item");
}
});

element.append(input, labelElement);

return element;
}
}
67 changes: 67 additions & 0 deletions packages/userscript/source/ui/SettingMaxUi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { SettingMax } from "../options/Settings";
import { UserScript } from "../UserScript";
import { SettingsSectionUi } from "./SettingsSectionUi";
import { SettingUi } from "./SettingUi";

export class SettingMaxUi {
/**
* Create a UI element for an option that can have a maximum.
* This will result in an element with a labeled checkbox and a "Max" indicator,
* which controls the respective `max` property in the option model.
*
* @param host The userscript instance.
* @param name A unique name for this option.
* @param option The option model.
* @param label The label for the option.
* @param delimiter Should a delimiter be rendered after this element?
* @param upgradeIndicator Should an indicator be rendered in front of the elemnt,
* to indicate that this is an upgrade of a prior option?
* @param handler Handlers to call when the option is checked or unchecked.
* @param handler.onCheck Is called when the option is checked.
* @param handler.onUnCheck Is called when the option is unchecked.
* @returns The created element.
*/
static make(
host: UserScript,
name: string,
option: SettingMax,
label: string,
delimiter = false,
upgradeIndicator = false,
handler: {
onCheck?: () => void;
onUnCheck?: () => void;
} = {}
): JQuery<HTMLElement> {
const element = SettingUi.make(host, name, option, label, delimiter, upgradeIndicator, handler);

const maxButton = $("<div/>", {
id: `set-${name}-max`,
css: {
cursor: "pointer",
display: "inline-block",
float: "right",
paddingRight: "5px",
},
}).data("option", option);
option.$max = maxButton;

maxButton.on("click", () => {
const value = SettingsSectionUi.promptLimit(
host.i18n("ui.max.set", [label]),
option.max.toString()
);

if (value !== null) {
const limit = SettingsSectionUi.renderLimit(value, host);
host.updateOptions(() => (option.max = value));
maxButton[0].title = limit;
maxButton[0].innerText = host.i18n("ui.max", [limit]);
}
});

element.append(maxButton);

return element;
}
}
Loading

0 comments on commit da66504

Please sign in to comment.