From c3ca0179b8a5672037d27af59faf64a50fe4f097 Mon Sep 17 00:00:00 2001
From: srichman333 <sarah_n_richman@brown.edu>
Date: Mon, 27 Nov 2023 02:05:29 -0500
Subject: schema to dataviz

---
 .../collectionFreeForm/CollectionFreeFormView.tsx  | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

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

diff --git a/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx b/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx
index e350c35cc..04e5ebb2c 100644
--- a/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx
+++ b/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx
@@ -1973,3 +1973,27 @@ ScriptingGlobals.add(function bringToFront() {
 ScriptingGlobals.add(function sendToBack(doc: Doc) {
     SelectionManager.Views().forEach(view => view.CollectionFreeFormView?.bringToFront(view.rootDoc, true));
 });
+ScriptingGlobals.add(function datavizFromSchema(doc: Doc) {
+    SelectionManager.Views().forEach(view => {
+        const keys = Cast(view.rootDoc.schema_columnKeys, listSpec('string'))?.filter(key => key!="text");
+       
+        if (!keys) return;
+        const children = DocListCast(view.rootDoc[Doc.LayoutFieldKey(view.rootDoc)]);
+        let csvRows = [];
+        csvRows.push(keys.join(','));
+        for (let i=0; i < children.length; i++) {
+            let eachRow = [];
+            for (let j=0; j<keys.length; j++){
+                eachRow.push(children[i][keys[j]]);
+            }
+            csvRows.push(eachRow);
+        }
+        const blob = new Blob([csvRows.join('\n')], { type: 'text/csv' });
+        const options = { x:0, y:-300, title: 'schemaTable', _width: 300, _height: 100, type: 'text/csv' };
+        const file = new File([blob], 'schemaTable', options);
+        const loading = Docs.Create.LoadingDocument(file, options);
+        loading.presentation_openInLightbox = true;
+        DocUtils.uploadFileToDoc(file, {}, loading);
+        if (view.ComponentView?.addDocument) view.ComponentView?.addDocument(loading);
+    })
+});
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2

