aboutsummaryrefslogtreecommitdiff
path: root/src/fields/ScriptField.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/fields/ScriptField.ts')
-rw-r--r--src/fields/ScriptField.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/fields/ScriptField.ts b/src/fields/ScriptField.ts
new file mode 100644
index 000000000..24c1d9b3a
--- /dev/null
+++ b/src/fields/ScriptField.ts
@@ -0,0 +1,64 @@
+import { Field, FieldId } from "./Field";
+import { Types } from "../server/Message";
+import { CompileScript, ScriptOptions, CompiledScript } from "../client/util/Scripting";
+import { Server } from "../client/Server";
+
+export interface ScriptData {
+ script: string;
+ options: ScriptOptions;
+}
+
+export class ScriptField extends Field {
+ readonly script: CompiledScript;
+
+ constructor(script: CompiledScript, id?: FieldId, save: boolean = true) {
+ super(id);
+
+ this.script = script;
+
+ if (save) {
+ Server.UpdateField(this);
+ }
+ }
+
+ static FromJson(id: string, data: ScriptData): ScriptField {
+ const script = CompileScript(data.script, data.options);
+ if (!script.compiled) {
+ throw new Error("Can't compile script");
+ }
+ return new ScriptField(script, id, false);
+ }
+
+ ToScriptString() {
+ return "new ScriptField(...)";
+ }
+
+ GetValue() {
+ return this.script;
+ }
+
+ TrySetValue(): boolean {
+ throw new Error("Script fields currently can't be modified");
+ }
+
+ UpdateFromServer() {
+ throw new Error("Script fields currently can't be updated");
+ }
+
+ ToJson(): { _id: string, type: Types, data: ScriptData } {
+ const { options, originalScript } = this.script;
+ return {
+ _id: this.Id,
+ type: Types.Script,
+ data: {
+ script: originalScript,
+ options
+ },
+ };
+ }
+
+ Copy(): Field {
+ //Script fields are currently immutable, so we can fake copy them
+ return this;
+ }
+} \ No newline at end of file