JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.angularjs.org/1.0.0/angular-1.0.0.js"></script>
JavaScript
var app = angular.module('myApp', []);
function buildExpression(list) {
var map = {}, node, roots = {}, i;
for (i = 0; i < list.length; i += 1) {
list[i].childNodes = []; // initialize the children
}
var obj = {type : "NODE_OP", childNodes : []}
for (i = 0; i < 2; i++) {
obj.value = list[i].connector;
node = list[i];
if (node.parentId !== 0) {
if (filters[i + 1])
createNode(i, node, roots.childNodes, list[i]);
else {
roots.childNodes[1] = {type : "NODE_CONST", value : node.value, childNodes : []};
}
} else {
roots = {type : "NODE_OP", value : list[i].connector, childNodes : []}
if (filters.length == 1) {
roots = {type : "NODE_CONST", value : list[i].value, childNodes : []}
} else {
roots.childNodes.push({type : "NODE_CONST", value : node.value, childNodes : []});
if (list.length > 1) roots.childNodes.push(obj);
}
}
}
return roots;
}
function createNode(i, node, childern, listObj) {
if (childern[1].childNodes.length != 2) {
node.type = "NODE_CONST"
childern[1].childNodes.push({type : "NODE_CONST", value : node.value, childNodes : []});
if (filters[i + 1]) {
if (!filters[i + 2]) {
childern[1].childNodes.push({type : "NODE_CONST", value : filters[i + 1].value, childNodes : []})
} else {
childern[1].childNodes.push({type : "NODE_OP", value : listObj.connector, childNodes : []})
node = filters[i + 1];
createNode(i + 1, node, childern[1].childNodes, filters[i]);
}
}
}
}
var filters=[{
"type": "item",
"parentId": 0,
"value": "$F{Filter1}",
"connector": "AND",
},{
"type": "item",
"parentId": 1,
"value": "$F{Filter2}",
"connector": "AND",
},{
"type": "item",
"parentId": 2,
"value": "$F{Filter3}",
"connector": "AND",
},{
"type": "item",
"parentId": 3,
"value": "$F{Filter4}",
"connector": "AND",
},{
"type": "item",
"parentId": 4,
"value": "$F{Filter5}",
"connector": "AND",
},{
"type": "item",
"parentId": 5,
"value": "$F{Filter6}",
"connector":...