-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_jquery_conditioneditorDnD.html
More file actions
441 lines (393 loc) · 17.3 KB
/
test_jquery_conditioneditorDnD.html
File metadata and controls
441 lines (393 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jQuery UI TreeInTable</title>
<link href="jquery-ui-1.11.4/jquery-ui.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/ise.treeintable.css">
<link rel="stylesheet" type="text/css" href="css/ise.treeintable.css">
<style>
.cpmTable td {
min-width: 365px;
}
.buttonclass {
width: 365px;
}
/*This is the trick to make autocomplete menu popup properly in the dialog */
ul.ui-front {
z-index: 1100 !important;
}
/*
Tthe equationedito editor has this class -- equationditorDialog
Without it, the autocomplete widget will be messed by jquery.ui.dialog _movetotop().
*/
.equationditorDialog{
z-index: 1100 !important;
}
/*This is the trick to make autocomplete menu popup properly in the dialog */
</style>
</head>
<body>
<div id="datastore3" class="datastore"></div>
<div id="treeintable3" class="treeintable">Condition Editor (Drag-and-Drop, Meta-Data driven) </div>
<br/>
<p>
Write you search criteria below and click "Parse Expression To Condition Tree" button.
</p>
<textarea rows="20" cols="50" id="textarea" >(sourceIp=="10.1.2.49")||(priority >= 1 )&& (device !="linksys")</textarea>
<br/><br/>
<input class ="buttonclass" type="button" id="load" value="Parse Expression To Condition Tree" style="width ="/>
<br/><br/>
<input class ="buttonclass" type="button" id="math" value="Get Boolean Expression From Condition Tree" />
<br/><br/>
<input class ="buttonclass" type="button" id="json" value="Get JSON From Condition Tree" />
<br/>
<p>
This widget enables user to configure data search criteria. <br>
User can write down search criteria and let the widget parse the string expression to Condition-Editor-Tree.<br>
It supports drag and drop to arrange conditions. <br>
Validation and configuration are driven by meta-data. Service provider controls which fields/functions are accessible to users.
</p>
<script src="jquery-ui-1.11.4/external/jquery/jquery.js"></script>
<script src="jquery-ui-1.11.4/jquery-ui.js"></script>
<script type="text/javascript" src="js/jquery.ise.datastore.js"></script>
<script type="text/javascript" src="js/jquery.ise.treeintable.js"></script>
<script type="text/javascript" src="js/jquery.ise.conditioneditor.js"></script>
<script type="text/javascript" src="js/esprima.js"></script>
<script type="text/javascript" src="js/jquery.ise.versatilecombobox.js"></script>
<script type="text/javascript" src="js/jquery.ise.equationeditor.js"></script>
<script>
var metadata= {
"fields": [{
"label": "Name",
"value": "Name",
"type": "string"
}, {
"label": "Age",
"value": "Age",
"type": "numerical"
}, {
"label": "SSN",
"value": "SSN",
"type": "string"
}, {
"label": "sourceIp",
"value": "sourceIp",
"type": "string"
}, {
"label": "targetIp",
"value": "targetIp",
"type": "string"
}, {
"label": "amount",
"value": "amount",
"type": "numerical"
}, {
"label": "bonus",
"value": "bonus",
"type": "numerical"
}, {
"label": "registered",
"value": "registered",
"type": "boolean"
}, {
"label": "priority",
"value": "priority",
"type": "numerical"
}, {
"label": "device",
"value": "device",
"type": "string"
}],
"operators": [{
"label": "=",
"value": "=",
"type": "numerical"
}, {
"label": "==",
"value": "==",
"type": "numerical"
}, {
"label": "!=",
"value": "!=",
"type": "numerical"
}, {
"label": ">=",
"value": ">=",
"type": "numerical"
}, {
"label": "<=",
"value": "<=",
"type": "numerical"
}, {
"label": "<",
"value": "<",
"type": "numerical"
}, {
"label": ">",
"value": ">",
"type": "numerical"
}, {
"label": "is",
"value": "is",
"type": "boolean"
}, {
"label": "isNot",
"value": "isNot",
"type": "boolean"
}, {
"label": "matches",
"value": "matches",
"type": "string"
},{
"label": "startsWith",
"value": "startsWith",
"type": "string"
}, {
"label": "endsWith",
"value": "endsWith",
"type": "string"
}, {
"label": "like",
"value": "like",
"type": "string"
}, {
"label": "contains",
"value": "contains",
"type": "string"
}, {
"label": "range",
"value": "range",
"type": "string"
}]
}
var treeintableWidget3= null;
var storeWidget3= null;
$(document).ready(function(){
$("#load").click(function(){
//alert("Value: " + $("#textarea").val());
var mathString = $("#textarea").val();
if (mathString.trim().length==0) return;
try{
var mathJson =esprima.parse(mathString);
//treeintableWidget3.element.children()[0].remove(); //failed in IE and Safari
$(treeintableWidget3.element).empty();
var store3 =$( "<div></div>" )
.appendTo( "body" )
.datastore({
data: mathJson,
// once receives "complete" event from "datastore" object. carry on this function
complete: function( event, data ) {
loadConditionEditor(event, data);
}
});
}catch (e){
alert("Failed to parse " + mathString + "\n" + e);
}
});
$("#math").click(function(){
var mathString = treeintableWidget3.getMathExpression();
alert("Boolean Expresson:\n" + mathString);
});
$("#json").click(function(){
var mathString = treeintableWidget3.getMathExpression();
var mathJson =esprima.parse(mathString);
alert("JSON (you can submit to sever to validate and search data) \n" + JSON.stringify(mathJson, null, " "));
});
});
var loadConditionEditor = function( event, data ) {
console.log( "datastore build complete" );
//instantiate "treeintable" object.
var treeinTableNode =$("#treeintable3").conditioneditor({
store: store3,
metadata: metadata,
checkEquationeditorValidity : function(equationobject){
// override checkConditionValidity(). developer can customize data validation logic
// For example, (sourceIp1=="10.1.2.t")||matches(Name, ok) --> sourceIp1 is not in medatdata, 10.1.2.t is not a valid IP address
var obj ={};
//check if FIELD input is in meta-data
var isFieldInMetaData = this.metadataUtil.isItemInMetaData("fields", "value", equationobject.left);
if (!isFieldInMetaData){
var msg ="[" +equationobject.left + "] is not known. It is not in the metadata list";
obj.message = (obj.message) ? obj.message + " " + msg : msg;
}
// validate if the operator is valid
// For example: matchesXXX(Name, ok) --> "matchesXXX" is not a supported function
var isOperatorValid =this.metadataUtil.isItemInMetaData("operators", "value", equationobject.operator);
var ops = metadata.operators;
if (!isOperatorValid){
var msg = "[" + equationobject.operator + "] is not a supported function. It is not in the metadata list";
obj.message = (obj.message) ? obj.message + " " + msg : msg;
}
var isValueVadid =true;
// you can right code to check equationobject.right is correct
//For example (sourceIp1=="10.1.2.t") 10.1.2.t is not a valid IP address
if ( equationobject.left=="sourceIp" || equationobject.left=="targetIp"){
if (this.metadataUtil.isItemInMetaData("fields", "value", equationobject.right)){
// if user pick "targetIp" in the value combobox. It means [sourceIp] matches [targetIp]. This is a valid case
return null;
}
var ipformat = /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/;
var ipformat2 = /^(25[0-4]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
var ipformat3 = /^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
isValueVadid= ipformat.test(equationobject.right ) && ipformat2.test(equationobject.right )&& ipformat3.test(equationobject.right );
if (!isValueVadid){
var msg ="[" +equationobject.right + "] is not a valid IP address";
obj.message = (obj.message) ? obj.message + " " + msg : msg;
}
}
if (isValueVadid && isFieldInMetaData && isOperatorValid){
return null;
}else{
//obj.row = trNode;
return obj;
}
},
getValueFromEquationEditor(type){
// override getValueFromEquationEditor(), this api controls what value to populate to equation-editor (where user configurs something like "a>b")
var equationeditorWidget = this.equationeditor.data("iseEquationeditor");
if (type=="field"){
return equationeditorWidget.getFieldWidget().selectedValue.value;
}else if (type=="operator"){
return equationeditorWidget.getOperatorWidget().selectedValue.value;
}else if (type=="value"){
return equationeditorWidget.getValueWidget().selectedValue.value;
}
return null;
},
getMathExpressFromEquationDataItem : function(dataItem){
// override getMathExpressFromEquationDataItem(). Develper can control how to generate math-expression
var opLeft = dataItem.left
var opRight = dataItem.right;
var operator = dataItem.operator;
var conditioneditor = this;
var opType =null;
var ops = metadata.operators;
for (var i=0; i< ops.length; i++){
if (operator == ops[i].value){
opType = ops[i].type;
break;
}
}
if (opType && opType !="numerical"){
return operator + "( " + opLeft.name + " ," + opRight.value + ")" ;
}
return dataItem.expression;
},
onEquationEditoronFormCreateComplete:function(conditioneditor, equationeditor, fieldwidget, operatorwidget, valuewidget){
if (this.debug) console.log(" test file conditionEditor -> onEquationEditoronFormCreateComplete()");
//operatorwidget.enable();
//valuewidget.enable();
//I am overriding onFieldInputChange() API.
//When user changes "field" value, we can update "operator" and "value" widget according to specific business logic.
equationeditor.onFieldInputChange = function(widget){
var fieldwidget = equationeditor.getFieldWidget();
var operatorwidget = equationeditor.getOperatorWidget();
var valuewidget = equationeditor.getValueWidget();
if (this.debug) console.log("customized onFieldInputChange() " +JSON.stringify(widget.selectedValue));
var fieldValue = fieldwidget.selectedValue;
if (!fieldValue) return;
// enable operator-combo and valuewidget-combo
var fieldObj = conditioneditor.metadataUtil.getFieldObject(fieldValue, "value");
if (fieldObj){
operatorwidget.reset();
// if (fieldObj.type == "string") --> fill operatorwidget.selectOptions with "startsWith, endsWith..., etc"
operatorwidget.selectOptions = conditioneditor.metadataUtil.getOperatorsByType(fieldObj.type);
operatorwidget.selectOptions= operatorwidget.selectOptions.concat( conditioneditor.metadataUtil.getOperatorsByType("boolean"));
}
if (fieldObj.type != "boolean"){
// say user picks "name" field (a string type field), we want to populate value-combo with all string type fields except "name"
var typeFields = conditioneditor.metadataUtil.getFieldsByType(fieldObj.type);
var idx = typeFields.indexOf(fieldObj);
typeFields.splice(idx, 1);
valuewidget.selectOptions = typeFields;
}
///* We can examine every select field's value and update operator-combo and value-combo accordingly.
if (fieldValue.value == "name"){
// write you business logic
//operatorwidget.selectOptions = ...
//valuewidget.selectOptions = ..
}else{
// write you businese logic ...
}
//*/
operatorwidget.reset();
valuewidget.reset();
};
/*
* Override the API onOperatorInputChange()
* When ever user picks operator value, we can update the value-combo accordingly.
*/
equationeditor.onOperatorInputChange = function(widget){
var fieldwidget = equationeditor.getFieldWidget();
var operatorwidget = equationeditor.getOperatorWidget();
var valuewidget = equationeditor.getValueWidget();
if (conditioneditor.debug) console.log("customized onOperatorInputChange() " +JSON.stringify(widget.selectedValue));
var fieldValue = fieldwidget.selectedValue;
var operatorValue = operatorwidget.selectedValue;
var operatorObj = conditioneditor.metadataUtil.getOperatorObject(operatorValue, "value");
if (operatorObj.type == "boolean"){
var nullFields =[{
"label": "null",
"value": "null"
},
{
"label": "non null",
"value": "non null"
}];
valuewidget.selectOptions = nullFields;
}
valuewidget.reset();
};
/*
* Override the API onValueInputChange()
* When ever user picks operator value, we can update the value-combo accordingly.
*/
equationeditor.onValueInputChange = function(widget){
if (equationeditor.debug) console.log("customized onValueInputChange() " +JSON.stringify(widget.selectedValue));
};
},
debug : true
});
storeWidget3= treeinTableNode.data("iseConditioneditor").store; //"treeintable" old code
treeintableWidget3=treeinTableNode.data("iseConditioneditor");
treeintableWidget3.decorateTablerow=function(cpmTableRow){
//instance level override the class api
//console.log("instance override decorateTablerow()");
};
treeintableWidget3.onRowSelect=function(trNode){
console.log("TreeInColumnable-items onRowSelect()" + trNode.treetableArrayItem);
// for debugging. Test various api
//console.log("Validation Error List\n" + treeintableWidget3.validate());
// console.log("getChildItemIndex(): " + treeintableWidget3.getChildItemIndex(trNode));
// console.log(treeintableWidget3.getParentRowItem(trNode));
// console.log(treeintableWidget3.getSubTree(trNode));
// console.log(treeintableWidget3.getDirectChildren(trNode));
// console.log(treeintableWidget3.getParentRowItemsList(trNode));
// console.log(treeintableWidget3.getChildIndexOfParent(trNode));
// console.log(treeintableWidget3.getPreviousSiblingRowItem(trNode));
// console.log(treeintableWidget3.getNexeSiblingRowItem(trNode));
//console.log("getRootLevelRowItems() " + treeintableWidget3.getRootLevelRowItems());
};
///* You can override conditioneditor widget API like below
treeintableWidget3.getEquationEditorValidationErrorTitle=function(){
return 'Invadid Input';
},
//*/
treeintableWidget3.buildTreeTable();
treeintableWidget3.setTreetableDragAndDrop(); ////<------ enable Drag-and-Drop
}
</script>
<script type="text/javascript">
var store3 =$( "<div></div>" )
//.appendTo( "body" )
.datastore({
url: "data/test_ce.json", // it is /* (priority==5) || (souceIp=="1.2.2.3") && ( device=="router") */
// once receives "complete" event from "datastore" object. carry on this function
complete: function( event, data ) {
loadConditionEditor(event, data);
}
});
</script>
</body>
</html>