From 8f0243ec5a53e9c370397ae7ef582bb035e9c9c3 Mon Sep 17 00:00:00 2001
From: Virendra Negi <63040+meetme2meat@users.noreply.github.com>
Date: Wed, 8 Mar 2023 20:32:33 +0530
Subject: [PATCH] make editor work
---
editor/EvaluationRoute.go | 23 +++++++----
editor/statics/index.html | 19 ++++++----
editor/statics/js/index.js | 78 ++++++++++++++++++++++++++++----------
3 files changed, 83 insertions(+), 37 deletions(-)
diff --git a/editor/EvaluationRoute.go b/editor/EvaluationRoute.go
index 4d191143..c7676709 100644
--- a/editor/EvaluationRoute.go
+++ b/editor/EvaluationRoute.go
@@ -19,8 +19,8 @@ type JSONData struct {
}
type EvaluateRequest struct {
- GrlText string `json:"grlText"`
- Input []*JSONData `json:"jsonInput"`
+ GrlText string `json:"grlText"`
+ Input []string `json:"jsonInput"`
}
func InitializeEvaluationRoute(router *mux.HyperMux) {
@@ -41,14 +41,22 @@ func InitializeEvaluationRoute(router *mux.HyperMux) {
dataContext := ast.NewDataContext()
- for _, jd := range evReq.Input {
- jsonByte, err := base64.StdEncoding.DecodeString(jd.JSONData)
+ for _, input := range evReq.Input {
+ jsonByte, err := base64.StdEncoding.DecodeString(input)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
- _, _ = w.Write([]byte(fmt.Sprintf("json data named %s should be sent using base64. got %v", jd.Name, err)))
+ _, _ = w.Write([]byte(fmt.Sprintf("jsonInput data should be sent using base64.", err)))
return
}
- err = dataContext.AddJSON(jd.Name, jsonByte)
+ var jd JSONData
+ err = json.Unmarshal(jsonByte, &jd)
+ if err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ _, _ = w.Write([]byte(fmt.Sprintf("unmarshal json data named got err %v", err)))
+ return
+ }
+
+ err = dataContext.AddJSON(jd.Name, []byte(jd.JSONData))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(fmt.Sprintf("invalid JSON data named %s when add json to context got %v", jd.Name, err)))
@@ -72,7 +80,6 @@ func InitializeEvaluationRoute(router *mux.HyperMux) {
_, _ = w.Write([]byte(fmt.Sprintf("invalid GRL : %s", err.Error())))
return
}
-
eng1 := &engine.GruleEngine{MaxCycle: 5}
kb := lib.NewKnowledgeBaseInstance("Evaluator", "0.0.1")
err = eng1.Execute(dataContext, kb)
@@ -84,7 +91,7 @@ func InitializeEvaluationRoute(router *mux.HyperMux) {
respData := make(map[string]interface{})
for _, keyName := range dataContext.GetKeys() {
- respData[keyName] = dataContext.Get(keyName)
+ respData[keyName] = dataContext.Get(keyName).Value().Interface()
}
resultBytes, err := json.Marshal(respData)
diff --git a/editor/statics/index.html b/editor/statics/index.html
index 14cd61b6..8622189a 100644
--- a/editor/statics/index.html
+++ b/editor/statics/index.html
@@ -18,7 +18,7 @@
-
+
@@ -51,17 +51,19 @@
-
-
-
+
@@ -71,7 +73,7 @@
Execution Result
-
+
@@ -82,6 +84,7 @@
+
diff --git a/editor/statics/js/index.js b/editor/statics/js/index.js
index 11a946f9..70046f8b 100644
--- a/editor/statics/js/index.js
+++ b/editor/statics/js/index.js
@@ -29,13 +29,17 @@ function ShowResult() {
}
function executeRule() {
- let grl = $("#grlText").val();
- let json = $("#jsonText").val()
+ let grl = $("#grleditor").text();
let grlB64 = btoa(grl);
- let jsonB64 = btoa(json);
-
- $.post( "/evaluate", JSON.stringify({"grlText": grlB64, "jsonText": jsonB64}) , function( data, status ) {
- $("#response").val(status + " : " + JSON.stringify(data) );
+ let size = $('div#panelContext').find('div.jsoneditor').length;
+ let jsonBlob = new Array(size);
+ $('div#panelContext').find('div.jsoneditor').each(function(index, editor) {
+ let json = $(editor).text();
+ let jsonB64 = btoa(json);
+ jsonBlob[index] = jsonB64;
+ })
+ $.post( "/evaluate", JSON.stringify({"grlText": grlB64, "jsonInput": jsonBlob}) , function( data, status ) {
+ $("#response").text(status + " : " + JSON.stringify(data) );
}, "json") .fail(function(data) {
$("#response").val( "Status " + data.status + " : " + data.statusText + ". ResponseText : " + data.responseText);
});
@@ -69,24 +73,42 @@ function Mark() {
// do nothing
}
-function JsonEditorTab(e) {
- if (e.keyCode === 9) { // tab key
- e.preventDefault(); // this will prevent us from tabbing out of the editor
+function AddData(e) {
+ var div = document.createElement('div');
+ var divBody = document.createElement('div');
+ divBody.className = "card-body";
+ var editor = document.createElement('div');
+ editor.className = "jsoneditor";
+ editor.setAttribute("contenteditable", true);
+ editor.setAttribute("style", style="white-space: pre-wrap; font-family: monospace; border: solid; padding: 15px 15px 15px 15px;");
+ editor.setAttribute("role","textbox");
+ divBody.appendChild(editor);
+
+ var divFooter = document.createElement('div');
+ divFooter.className = "card-footer";
+ var button = document.createElement('button');
+ button.setAttribute("type","button");
+ button.className="btn btn-danger delete";
+ button.innerHTML = "Delete Data";
+ divFooter.appendChild(button);
+
+ div.appendChild(divBody);
+ div.appendChild(divFooter);
+ $('div#panelContext').find("div.card-header").after(div);
+}
- // now insert four non-breaking spaces for the tab key
- var editor = document.getElementById("jsoneditor");
- var doc = editor.ownerDocument.defaultView;
- var sel = doc.getSelection();
- var range = sel.getRangeAt(0);
+function JsonEditorTab(editor) {
+ var doc = editor.ownerDocument.defaultView;
+ var sel = doc.getSelection();
+ var range = sel.getRangeAt(0);
- var tabNode = document.createTextNode("\u00a0\u00a0\u00a0\u00a0");
- range.insertNode(tabNode);
+ var tabNode = document.createTextNode("\u00a0\u00a0\u00a0\u00a0");
+ range.insertNode(tabNode);
- range.setStartAfter(tabNode);
- range.setEndAfter(tabNode);
- sel.removeAllRanges();
- sel.addRange(range);
- }
+ range.setStartAfter(tabNode);
+ range.setEndAfter(tabNode);
+ sel.removeAllRanges();
+ sel.addRange(range);
}
function GrlEditorTab(e) {
@@ -109,3 +131,17 @@ function GrlEditorTab(e) {
}
}
+$(document).ready(function() {
+ $("div#panelContext").on('click','button.delete', function(event) {
+ event.preventDefault();
+ $(this).parent().parent().remove();
+ })
+
+ $("div#panelContext").on('keydown', 'div.jsoneditor', function(event) {
+ if (event.keyCode === 9) {
+ event.preventDefault();
+ JsonEditorTab(this);
+ }
+ })
+})
+