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

Commit

Permalink
feat(limited): Discrete component for limited
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Oct 8, 2022
1 parent d435b17 commit 04eaf18
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 59 deletions.
3 changes: 2 additions & 1 deletion packages/userscript/source/options/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Resource, SpaceBuildings } from "../types";
import { LimitedButton } from "../ui/components/LimitedButton";
import { MaxButton } from "../ui/components/MaxButton";
import { TriggerButton } from "../ui/components/TriggerButton";
import { BonfireItem } from "./BonfireSettings";
Expand All @@ -23,7 +24,7 @@ export class Setting {

export class SettingLimited extends Setting {
limited: boolean;
$limited: JQuery<HTMLElement> | undefined = undefined;
$limited: LimitedButton | undefined = undefined;

constructor(id: string, enabled = false, limited = false) {
super(id, enabled);
Expand Down
4 changes: 2 additions & 2 deletions packages/userscript/source/ui/SettingLimitedMaxUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class SettingLimitedMaxUi {
handler: {
onCheck: () => void;
onUnCheck: () => void;
onLimitedCheck?: () => void;
onLimitedUnCheck?: () => void;
onLimitedCheck: () => void;
onLimitedUnCheck: () => void;
},
delimiter = false,
upgradeIndicator = false
Expand Down
40 changes: 5 additions & 35 deletions packages/userscript/source/ui/SettingLimitedUi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SettingLimited } from "../options/Settings";
import { clog } from "../tools/Log";
import { UserScript } from "../UserScript";
import { LimitedButton } from "./components/LimitedButton";
import { SettingUi } from "./SettingUi";

export class SettingLimitedUi {
Expand Down Expand Up @@ -30,8 +30,8 @@ export class SettingLimitedUi {
handler: {
onCheck: () => void;
onUnCheck: () => void;
onLimitedCheck?: () => void;
onLimitedUnCheck?: () => void;
onLimitedCheck: () => void;
onLimitedUnCheck: () => void;
},
delimiter = false,
upgradeIndicator = false
Expand All @@ -47,38 +47,8 @@ export class SettingLimitedUi {
[]
);

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

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

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

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

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

element.append(input, labelElement);
const limitedButton = new LimitedButton(host, name, setting, handler);
element.append(limitedButton.element);

return element;
}
Expand Down
1 change: 0 additions & 1 deletion packages/userscript/source/ui/SettingMaxUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class SettingMaxUi {
);

const maxButton = new MaxButton(host, name, label, setting);
setting.$max = maxButton;
element.append(maxButton.element);

return element;
Expand Down
12 changes: 3 additions & 9 deletions packages/userscript/source/ui/TradeSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,8 @@ export class TradeSettingsUi extends SettingsSectionUi {
{
onCheck: () => this._host.engine.imessage("status.sub.enable", [i18nName]),
onUnCheck: () => this._host.engine.imessage("status.sub.disable", [i18nName]),
onLimitedCheck: () => {
this._host.updateOptions(() => (option.limited = true));
this._host.engine.imessage("trade.limited", [i18nName]);
},
onLimitedUnCheck: () => {
this._host.updateOptions(() => (option.limited = false));
this._host.engine.imessage("trade.unlimited", [i18nName]);
},
onLimitedCheck: () => this._host.engine.imessage("trade.limited", [i18nName]),
onLimitedUnCheck: () => this._host.engine.imessage("trade.unlimited", [i18nName]),
},
delimiter,
upgradeIndicator
Expand Down Expand Up @@ -297,7 +291,7 @@ export class TradeSettingsUi extends SettingsSectionUi {

for (const [name, option] of objectEntries(this._settings.items)) {
mustExist(option.$enabled).prop("checked", this._settings.items[name].enabled);
mustExist(option.$limited).prop("checked", this._settings.items[name].limited);
mustExist(option.$limited).refreshUi();

mustExist(option.$autumn).prop("checked", this._settings.items[name].autumn);
mustExist(option.$spring).prop("checked", this._settings.items[name].spring);
Expand Down
16 changes: 5 additions & 11 deletions packages/userscript/source/ui/WorkshopSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,10 @@ export class WorkshopSettingsUi extends SettingsSectionUi {
option,
label,
{
onCheck: () => this._host.engine.imessage("status.auto.enable", [label]),
onUnCheck: () => this._host.engine.imessage("status.auto.disable", [label]),
onLimitedCheck: () => {
this._host.updateOptions(() => (option.limited = true));
this._host.engine.imessage("craft.limited", [label]);
},
onLimitedUnCheck: () => {
this._host.updateOptions(() => (option.limited = false));
this._host.engine.imessage("craft.unlimited", [label]);
},
onCheck: () => this._host.engine.imessage("status.sub.enable", [label]),
onUnCheck: () => this._host.engine.imessage("status.sub.disable", [label]),
onLimitedCheck: () => this._host.engine.imessage("craft.limited", [label]),
onLimitedUnCheck: () => this._host.engine.imessage("craft.unlimited", [label]),
},
delimiter,
upgradeIndicator
Expand Down Expand Up @@ -250,7 +244,7 @@ export class WorkshopSettingsUi extends SettingsSectionUi {

for (const [, option] of objectEntries(this._settings.items)) {
mustExist(option.$enabled).prop("checked", option.enabled);
mustExist(option.$limited).prop("checked", option.limited);
mustExist(option.$limited).refreshUi();
mustExist(option.$max).refreshUi();
}
}
Expand Down
48 changes: 48 additions & 0 deletions packages/userscript/source/ui/components/LimitedButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { SettingLimited } from "../../options/Settings";
import { mustExist } from "../../tools/Maybe";
import { UserScript } from "../../UserScript";

export class LimitedButton {
readonly host: UserScript;
readonly setting: SettingLimited;
readonly element: JQuery<HTMLElement>;

constructor(
host: UserScript,
id: string,
setting: SettingLimited,
handler: { onLimitedCheck: () => void; onLimitedUnCheck: () => void }
) {
const element = $("<label/>", {
for: `toggle-limited-${id}`,
text: host.engine.i18n("ui.limit"),
});

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

input.on("change", () => {
if (input.is(":checked") && setting.limited === false) {
setting.limited = true;
host.updateOptions();
handler.onLimitedCheck();
} else if (!input.is(":checked") && setting.limited === true) {
setting.limited = false;
host.updateOptions();
handler.onLimitedUnCheck();
}
});

setting.$limited = this;

this.element = element;
this.host = host;
this.setting = setting;
}

refreshUi() {
mustExist(this.element).prop("checked", this.setting.limited);
}
}

0 comments on commit 04eaf18

Please sign in to comment.