Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Editor fixes #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions editor/EvaluationRoute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)))
Expand All @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions editor/statics/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3>
</h3>
</div>
<div class="col-6" style="padding-top:5px;padding-bottom:5px; padding-right: 5px">
<button type="button" class="btn btn-danger float-end">Execute</button>
<button type="button" class="btn btn-danger float-end" onclick="executeRule()">Execute</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -51,17 +51,19 @@ <h3>
<div class="card-header border-success">
Data Context
</div>
<div class="card-body">
<div id="jsoneditor" contenteditable="true" style="white-space: pre-wrap; font-family: monospace; border: solid; padding: 15px 15px 15px 15px;" onkeydown="JsonEditorTab(event);" role="textbox"></div>
</div>
<div class="card-footer">
<button type="button" class="btn btn-danger">Delete Data</button>
<div>
<div class="card-body">
<div class="jsoneditor" contenteditable="true" style="white-space: pre-wrap; font-family: monospace; border: solid; padding: 15px 15px 15px 15px;" role="textbox"></div>
</div>
<div class="card-footer">
<button type="button" class="btn btn-danger delete">Delete Data</button>
</div>
</div>
</div>
</div>
<div class="col-12">
<br>
<button type="button" class="btn btn-primary">Add Another Data</button>
<button type="button" class="btn btn-primary" onclick="AddData(event)">Add Another Data</button>
</div>
</div>
<div id="panelResult" class="row-12" style="display: none">
Expand All @@ -71,7 +73,7 @@ <h3>
Execution Result
</div>
<div class="card-body">
<textarea class="flex-box" rows="15" cols="100" readonly></textarea>
<textarea id="response" class="flex-box" rows="15" cols="100" readonly></textarea>
</div>
</div>
</div>
Expand All @@ -82,6 +84,7 @@ <h3>
</div>
</div>
</div>

<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/jquery-3.6.1.min.js"></script>
<script src="js/jquery.mark.js"></script>
Expand Down
78 changes: 57 additions & 21 deletions editor/statics/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
})
})