Skip to content

Commit b5e61f1

Browse files
author
RAJA MARAGANI
committed
modified add rules, and each event show in textarea like as rules
1 parent 1d841e6 commit b5e61f1

File tree

2 files changed

+154
-144
lines changed

2 files changed

+154
-144
lines changed

src/main/resources/static/js/testrules.js

Lines changed: 134 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
// Global vars
22
var frontendServiceUrl;
33
var i = 0;
4+
var ruleTemplate = {
5+
"TemplateName" : "",
6+
"Type" : "",
7+
"TypeRule" : "",
8+
"IdRule" : "",
9+
"StartEvent" : "",
10+
"IdentifyRules" : "",
11+
"MatchIdRules" : {},
12+
"ExtractionRules" : "",
13+
"DownstreamIdentifyRules" : "",
14+
"DownstreamMergeRules" : "",
15+
"DownstreamExtractionRules" : "",
16+
"ArrayMergeOptions" : "",
17+
"HistoryIdentifyRules" : "",
18+
"HistoryExtractionRules" : "",
19+
"HistoryPathRules" : "",
20+
"ProcessRules" : null,
21+
"ProcessFunction" : null
22+
};
423
jQuery(document).ready(
524
function() {
625

@@ -51,41 +70,37 @@ jQuery(document).ready(
5170
function AppViewModel(rulesList) {
5271
var self = this;
5372
self.rulesBindingList = ko.observableArray(rulesList);
54-
self.eventsBindingList = ko.observable();
55-
self.dropdown = ko.observableArray([ "EiffelArtifactCreatedEvent", "EiffelArtifactPublishedEvent", "EiffelConfidenceLevelModifiedEvent",
56-
"EiffelTestCaseStartedEvent", "EiffelTestCaseFinishedEvent", "EiffelCompositionDefinedEvent", "EiffelSourceChangeCreatedEvent",
57-
"EiffelSourceChangeSubmittedEvent" ]);
58-
self.backupdropdown = self.dropdown;
73+
self.eventsBindingList = ko.observableArray([]);
5974
self.parsedToString = function(item) {
6075
return JSON.stringify(item, null, 2);
6176
};
62-
63-
//After adding a rule, this function remove the type from dropdown
64-
self.removeDropdown = function(name) {
65-
var index = self.dropdown.indexOf(name);
66-
if (index !== -1) {
67-
self.dropdown.splice(index, 1);
68-
}
69-
return name;
70-
};
71-
7277
// Removing the rule
73-
self.removeRule = function(name) {
74-
self.rulesBindingList.remove(name);
75-
self.dropdown.push(name.Type);
76-
if(self.rulesBindingList().length == 0){
78+
self.removeRule = function(data, event) {
79+
var context = ko.contextFor(event.target);
80+
self.rulesBindingList.splice(context.$index(), 1);
81+
if (self.rulesBindingList().length == 0) {
7782
$.jGrowl("Deleted all rule types, but we need atleast one Rule type, Here add default rule type", {
7883
sticky : false,
7984
theme : 'Error'
8085
});
81-
self.rulesBindingList.push({
82-
"TemplateName" : "",
83-
"Type" : "EiffelArtifactCreatedEvent"
86+
self.rulesBindingList.push(ruleTemplate);
87+
}
88+
};
8489

90+
// Removing the events
91+
self.removeEvent = function(data, event) {
92+
var context = ko.contextFor(event.target);
93+
self.eventsBindingList.splice(context.$index(), 1);
94+
if (self.eventsBindingList().length == 0) {
95+
self.eventsBindingList.push({});
96+
$.jGrowl("Deleted all events, but we need atleast one event.", {
97+
sticky : false,
98+
theme : 'Error'
8599
});
100+
86101
}
87102
};
88-
103+
89104
//This submit function for finding the aggregated object from the rules and events, This function internally call the ajax call
90105
self.submit = function() {
91106
var events = $("#eventsListID").val();
@@ -94,7 +109,20 @@ jQuery(document).ready(
94109
try {
95110
formRules.push(JSON.parse($(this).val()));
96111
} catch (e) {
97-
$.jGrowl("Invalid json format :\n" + $(this).val(), {
112+
$.jGrowl("Invalid json rule format :\n" + $(this).val(), {
113+
sticky : false,
114+
theme : 'Error'
115+
});
116+
return false;
117+
}
118+
});
119+
120+
var formEvents = [];
121+
$('.formEvents').each(function() {
122+
try {
123+
formEvents.push(JSON.parse($(this).val()));
124+
} catch (e) {
125+
$.jGrowl("Invalid json event format :\n" + $(this).val(), {
98126
sticky : false,
99127
theme : 'Error'
100128
});
@@ -131,116 +159,100 @@ jQuery(document).ready(
131159
complete : function() {
132160
}
133161
};
134-
var eventsValid = isValidJSON(events.toString());
135-
if (!eventsValid) {
136-
alert("Events are not a valid json format");
137-
} else {
162+
138163
var ajaxHttpSender = new AjaxHttpSender();
139164
//console.log(JSON.stringify(JSON.parse('{"listRulesJson":' + JSON.stringify(formRules) + ',"listEventsJson":' + events.toString() + '}')));
140165
ajaxHttpSender.sendAjax(frontendServiceUrl + "/rules/rule-check/aggregation", "POST", JSON.stringify(JSON.parse('{"listRulesJson":'
141-
+ JSON.stringify(formRules) + ',"listEventsJson":' + events.toString() + '}')), callback);
142-
}
166+
+ JSON.stringify(formRules) + ',"listEventsJson":' + JSON.stringify(formEvents) + '}')), callback);
143167
};
144168

145169
// This function for adding rule
146-
self.addRule = function(viewModel, event) {
147-
var newValue = event.target.value;
148-
if (newValue != '') {
149-
self.rulesBindingList.push({
150-
"TemplateName" : "",
151-
"Type" : newValue
152-
153-
});
154-
self.removeDropdown(newValue);
155-
}
156-
}
170+
self.addRule = function() {
171+
self.rulesBindingList.push(ruleTemplate);
172+
};
173+
// This function for adding rule
174+
self.addEvent = function() {
175+
self.eventsBindingList.push({});
176+
};
157177
return self;
158178
}
159179

160-
161180
var vm = new AppViewModel([]);
162181
ko.applyBindings(vm, $("#submitButton")[0]);
163-
vm.rulesBindingList.push({
164-
"TemplateName" : "",
165-
"Type" : "EiffelArtifactCreatedEvent"
182+
vm.rulesBindingList.push(ruleTemplate);
183+
vm.eventsBindingList.push({});
166184

167-
});
168-
vm.removeDropdown("EiffelArtifactCreatedEvent");
169-
ko.applyBindings(vm, $("#testTulesDOMObject")[0]);
185+
ko.applyBindings(vm, $("#testRulesDOMObject")[0]);
186+
ko.applyBindings(vm, $("#testEventsDOMObject")[0]);
170187

171188
//Upload events list json data
172-
$(".container").on("click","button.upload_rules", function(event) {
173-
event.stopPropagation();
174-
event.preventDefault();
175-
176-
function validateJsonAndCreateSubscriptions(subscriptionFile) {
177-
var reader = new FileReader();
178-
reader.onload = function() {
179-
var fileContent = reader.result;
180-
var jsonLintResult = "";
181-
try {
182-
jsonLintResult = jsonlint.parse(fileContent);
183-
} catch (e) {
184-
$.alert("JSON Format Check Failed:\n" + e.name + "\n" + e.message);
185-
return false;
186-
}
187-
$.jGrowl('JSON Format Check Succeeded', {
188-
sticky : false,
189-
theme : 'Notify'
190-
});
191-
192-
var rulesList = JSON.parse(fileContent);
193-
ko.cleanNode($("#testTulesDOMObject")[0]);
194-
ko.cleanNode($("#submitButton")[0]);
195-
$("#testTulesDOMObject").css('min-height', $(".navbar-sidenav").height() - 180);
196-
vm.rulesBindingList.removeAll();
197-
$('.eventsListDisplay > div:gt(0)').remove();
198-
vm.rulesBindingList = ko.observableArray(rulesList);
199-
vm.dropdown([]);
200-
vm.dropdown = ko.observableArray([ "EiffelArtifactCreatedEvent", "EiffelArtifactPublishedEvent",
201-
"EiffelConfidenceLevelModifiedEvent", "EiffelTestCaseStartedEvent", "EiffelTestCaseFinishedEvent",
202-
"EiffelCompositionDefinedEvent", "EiffelSourceChangeCreatedEvent", "EiffelSourceChangeSubmittedEvent" ]);
189+
$(".container").on("click", "button.upload_rules", function(event) {
190+
event.stopPropagation();
191+
event.preventDefault();
203192

204-
ko.applyBindings(vm, $("#testTulesDOMObject")[0]);
205-
ko.applyBindings(vm, $("#submitButton")[0]);
206-
};
207-
reader.readAsText(subscriptionFile);
193+
function validateJsonAndCreateSubscriptions(subscriptionFile) {
194+
var reader = new FileReader();
195+
reader.onload = function() {
196+
var fileContent = reader.result;
197+
var jsonLintResult = "";
198+
try {
199+
jsonLintResult = jsonlint.parse(fileContent);
200+
} catch (e) {
201+
$.alert("JSON Format Check Failed:\n" + e.name + "\n" + e.message);
202+
return false;
208203
}
204+
$.jGrowl('JSON Format Check Succeeded', {
205+
sticky : false,
206+
theme : 'Notify'
207+
});
209208

210-
function createUploadWindow() {
211-
var pom = document.createElement('input');
212-
pom.setAttribute('id', 'uploadFile');
213-
pom.setAttribute('type', 'file');
214-
pom.setAttribute('name', 'upFile');
215-
pom.onchange = function uploadFinished() {
216-
var subscriptionFile = pom.files[0];
217-
validateJsonAndCreateSubscriptions(subscriptionFile);
218-
};
219-
if (document.createEvent) {
220-
var event = document.createEvent('MouseEvents');
221-
event.initEvent('click', true, true);
222-
pom.dispatchEvent(event);
223-
} else {
224-
pom.click();
225-
}
226-
}
209+
var rulesList = JSON.parse(fileContent);
210+
ko.cleanNode($("#testRulesDOMObject")[0]);
211+
ko.cleanNode($("#submitButton")[0]);
212+
$("#testRulesDOMObject").css('min-height', $(".navbar-sidenav").height() - 180);
213+
vm.rulesBindingList.removeAll();
214+
$('.rulesListDisplay > div:gt(0)').remove();
215+
vm.rulesBindingList = ko.observableArray(rulesList);
216+
ko.applyBindings(vm, $("#testRulesDOMObject")[0]);
217+
ko.applyBindings(vm, $("#submitButton")[0]);
218+
};
219+
reader.readAsText(subscriptionFile);
220+
}
227221

228-
function createUploadWindowMSExplorer() {
229-
$('#upload_rules').click();
230-
var file = $('#upload_rules').prop('files')[0];
231-
validateJsonAndCreateSubscriptions(file);
232-
}
222+
function createUploadWindow() {
223+
var pom = document.createElement('input');
224+
pom.setAttribute('id', 'uploadFile');
225+
pom.setAttribute('type', 'file');
226+
pom.setAttribute('name', 'upFile');
227+
pom.onchange = function uploadFinished() {
228+
var subscriptionFile = pom.files[0];
229+
validateJsonAndCreateSubscriptions(subscriptionFile);
230+
};
231+
if (document.createEvent) {
232+
var event = document.createEvent('MouseEvents');
233+
event.initEvent('click', true, true);
234+
pom.dispatchEvent(event);
235+
} else {
236+
pom.click();
237+
}
238+
}
233239

234-
// If MS Internet Explorer -> special handling for creating
235-
// download
236-
// file window.
237-
if (window.navigator.msSaveOrOpenBlob) {
238-
createUploadWindowMSExplorer();
239-
} else {
240-
// HTML5 Download File window handling
241-
createUploadWindow();
242-
}
243-
});
240+
function createUploadWindowMSExplorer() {
241+
$('#upload_rules').click();
242+
var file = $('#upload_rules').prop('files')[0];
243+
validateJsonAndCreateSubscriptions(file);
244+
}
245+
246+
// If MS Internet Explorer -> special handling for creating
247+
// download
248+
// file window.
249+
if (window.navigator.msSaveOrOpenBlob) {
250+
createUploadWindowMSExplorer();
251+
} else {
252+
// HTML5 Download File window handling
253+
createUploadWindow();
254+
}
255+
});
244256

245257
//Upload list of events json data
246258
$(".container").on("click", "button.upload_events", function(event) {
@@ -262,12 +274,13 @@ jQuery(document).ready(
262274
sticky : false,
263275
theme : 'Notify'
264276
});
265-
277+
console.log("I am ghere");
266278
var eventsList = JSON.parse(fileContent);
267-
ko.cleanNode($("#submitButton")[0]);
268-
$("#eventsListID").val(JSON.stringify(eventsList, null, 2));
269-
$(".textareaCustom").css('min-height', $(".navbar-sidenav").height() - 180);
270-
ko.applyBindings(vm, $("#submitButton")[0]);
279+
ko.cleanNode($("#testEventsDOMObject")[0]);
280+
vm.eventsBindingList.removeAll();
281+
$('.eventsListDisplay > div:gt(0)').remove();
282+
vm.eventsBindingList = ko.observableArray(eventsList);
283+
ko.applyBindings(vm, $("#testEventsDOMObject")[0]);
271284
};
272285
reader.readAsText(subscriptionFile);
273286
}
@@ -345,8 +358,7 @@ jQuery(document).ready(
345358
} else {
346359
downloadFile(jsonData, contentType, fileName);
347360
}
348-
}
349-
else{
361+
} else {
350362
$.jGrowl("Data not available for download!", {
351363
sticky : false,
352364
theme : 'Error'

0 commit comments

Comments
 (0)