scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
145 lines • 4.86 kB
JavaScript
import { AbsAlert } from "scriptable-abstract";
import { SystemState } from "../../system";
import { MockTextField } from "./text-field";
const DEFAULT_STATE = {
title: "",
message: "",
actions: [],
textFields: [],
destructiveActionIndex: -1,
cancelActionIndex: -1,
presentedResult: 0
};
class MockAlert extends AbsAlert {
constructor() {
super(DEFAULT_STATE);
}
get title() {
return this.state.title;
}
set title(value) {
if (value === null) throw new Error("Title cannot be null");
this.setState({ title: value });
}
get message() {
return this.state.message;
}
set message(value) {
if (value === null) throw new Error("Message cannot be null");
this.setState({ message: value });
}
get actions() {
return [...this.state.actions];
}
get textFields() {
return new Proxy([...this.state.textFields], {
get(target, prop) {
if (typeof prop === "string" && !isNaN(Number(prop))) {
const index = Number(prop);
if (index < 0 || index >= target.length) {
throw new Error("Invalid text field index");
}
}
return Reflect.get(target, prop);
}
});
}
get destructiveAction() {
return this.state.destructiveActionIndex >= 0 ? this.state.actions[this.state.destructiveActionIndex] : void 0;
}
get cancelAction() {
return this.state.cancelActionIndex >= 0 ? this.state.actions[this.state.cancelActionIndex] : void 0;
}
addAction(title) {
if (!title) throw new Error("Action text cannot be empty");
const actions = [...this.state.actions];
const insertIndex = Math.min(
this.state.cancelActionIndex >= 0 ? this.state.cancelActionIndex : actions.length,
this.state.destructiveActionIndex >= 0 ? this.state.destructiveActionIndex : actions.length
);
actions.splice(insertIndex, 0, title);
const newState = { actions };
if (this.state.cancelActionIndex >= insertIndex) {
newState.cancelActionIndex = this.state.cancelActionIndex + 1;
}
if (this.state.destructiveActionIndex >= insertIndex) {
newState.destructiveActionIndex = this.state.destructiveActionIndex + 1;
}
this.setState(newState);
}
addDestructiveAction(title) {
if (!title) throw new Error("Action text cannot be empty");
const actions = [...this.state.actions];
const insertIndex = this.state.cancelActionIndex >= 0 ? this.state.cancelActionIndex : actions.length;
actions.splice(insertIndex, 0, title);
const newState = {
actions,
destructiveActionIndex: insertIndex
};
if (this.state.cancelActionIndex >= insertIndex) {
newState.cancelActionIndex = this.state.cancelActionIndex + 1;
}
this.setState(newState);
}
addCancelAction(title) {
if (!title) throw new Error("Action text cannot be empty");
const actions = [...this.state.actions, title];
this.setState({
actions,
cancelActionIndex: actions.length - 1
});
}
addTextField(placeholder, text) {
const textField = new MockTextField();
if (placeholder) textField.placeholder = placeholder;
if (text) textField.text = text;
const textFields = [...this.state.textFields, textField];
this.setState({ textFields });
return textField;
}
addSecureTextField(placeholder, text) {
const textField = new MockTextField();
textField.isSecure = true;
if (placeholder) textField.placeholder = placeholder;
if (text) textField.text = text;
const textFields = [...this.state.textFields, textField];
this.setState({ textFields });
return textField;
}
textFieldValue(index) {
if (index < 0 || index >= this.state.textFields.length) return "";
return this.state.textFields[index]?.text ?? "";
}
setPresentedResult(result) {
this.setState({ presentedResult: result });
}
async presentAlert() {
const systemResponse = SystemState.getAlertResponse();
if (systemResponse !== void 0) return systemResponse;
if (this.state.presentedResult !== 0) return this.state.presentedResult;
if (this.state.actions.length === 0) return -1;
return this.state.presentedResult;
}
async presentSheet() {
return this.presentAlert();
}
async present() {
return this.presentAlert();
}
setState(newState) {
if (newState === null || newState === void 0) {
throw new Error("State cannot be null or undefined");
}
if ("title" in newState && (newState.title === null || newState.title === void 0)) {
throw new Error("Title cannot be null or undefined");
}
if ("message" in newState && (newState.message === null || newState.message === void 0)) {
throw new Error("Message cannot be null or undefined");
}
return super.setState(newState);
}
}
export {
MockAlert
};
//# sourceMappingURL=alert.js.map