From bf05eac7677997075bc58d2b70c3e101e2e87560 Mon Sep 17 00:00:00 2001
From: Tyler Schicke <tyler_schicke@brown.edu>
Date: Sun, 7 Apr 2019 17:39:23 -0400
Subject: Refactored schema scripting Changed tree view style to not be global

---
 .../views/collections/CollectionSchemaView.tsx      | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

(limited to 'src/client/views/collections/CollectionSchemaView.tsx')

diff --git a/src/client/views/collections/CollectionSchemaView.tsx b/src/client/views/collections/CollectionSchemaView.tsx
index afe530c1a..b10aaba98 100644
--- a/src/client/views/collections/CollectionSchemaView.tsx
+++ b/src/client/views/collections/CollectionSchemaView.tsx
@@ -86,12 +86,8 @@ export class CollectionSchemaView extends CollectionViewBase {
         )
         let reference = React.createRef<HTMLDivElement>();
         let onItemDown = setupDrag(reference, () => props.doc, (containingCollection: CollectionView) => this.props.removeDocument(props.doc));
-        let applyToDoc = (doc: Document, value: string) => {
-            let script = CompileScript(value, { addReturn: true, params: { this: "Document" } });
-            if (!script.compiled) {
-                return false;
-            }
-            const res = script.run({ this: doc });
+        let applyToDoc = (doc: Document, run: (args?: { [name: string]: any }) => any) => {
+            const res = run({ this: doc });
             if (!res.success) return false;
             const field = res.result;
             if (field instanceof Field) {
@@ -120,13 +116,22 @@ export class CollectionSchemaView extends CollectionViewBase {
                         return field || "";
                     }}
                     SetValue={(value: string) => {
-                        return applyToDoc(props.doc, value);
+                        let script = CompileScript(value, { addReturn: true, params: { this: "Document" } });
+                        if (!script.compiled) {
+                            return false;
+                        }
+                        return applyToDoc(props.doc, script.run);
                     }}
                     OnFillDown={(value: string) => {
+                        let script = CompileScript(value, { addReturn: true, params: { this: "Document" } });
+                        if (!script.compiled) {
+                            return;
+                        }
+                        const run = script.run;
                         //TODO This should be able to be refactored to compile the script once
                         this.props.Document.GetTAsync<ListField<Document>>(this.props.fieldKey, ListField).then((val) => {
                             if (val) {
-                                val.Data.forEach(doc => applyToDoc(doc, value));
+                                val.Data.forEach(doc => applyToDoc(doc, run));
                             }
                         })
                     }}>
-- 
cgit v1.2.3-70-g09d2

