Skip to content

Commit 609b168

Browse files
Fix rules/events uploading problem in Chrome and IE (#90)
* Fix uploading test rules and events problem in Chrome * Fix typeMismatchError when uploading rules and events in IE
1 parent b0ce41d commit 609b168

File tree

1 file changed

+22
-47
lines changed

1 file changed

+22
-47
lines changed

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

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jQuery(document).ready(
8282
self.rulesBindingList.splice(context.$index(), 1);
8383
if (self.rulesBindingList().length == 0) {
8484
window.logMessages("Deleted all rule types, but we need atleast one Rule type, Here add default rule type");
85-
self.rulesBindingList.push(ruleTemplate);
85+
self.rulesBindingList.push(JSON.parse(JSON.stringify(ruleTemplate)));
8686
}
8787
};
8888

@@ -169,44 +169,13 @@ jQuery(document).ready(
169169

170170
var vm = new AppViewModel([]);
171171
ko.applyBindings(vm, $("#submitButton")[0]);
172-
vm.rulesBindingList.push(ruleTemplate);
172+
vm.rulesBindingList.push(JSON.parse(JSON.stringify(ruleTemplate)));
173173
vm.eventsBindingList.push({});
174174

175175
ko.applyBindings(vm, $("#testRulesDOMObject")[0]);
176176
ko.applyBindings(vm, $("#testEventsDOMObject")[0]);
177177

178-
function validateRulesJsonAndCreateSubscriptions(subscriptionFile) {
179-
var reader = new FileReader();
180-
reader.onload = function() {
181-
var fileContent = reader.result;
182-
var jsonLintResult = "";
183-
try {
184-
jsonLintResult = jsonlint.parse(fileContent);
185-
} catch (e) {
186-
window.logMessages("JSON Format Check Failed:\n" + e.name + "\n" + e.message);
187-
return false;
188-
}
189-
$.jGrowl('JSON Format Check Succeeded', {
190-
sticky : false,
191-
theme : 'Notify'
192-
});
193-
194-
var rulesList = JSON.parse(fileContent);
195-
ko.cleanNode($("#testRulesDOMObject")[0]);
196-
ko.cleanNode($("#submitButton")[0]);
197-
$("#testRulesDOMObject").css('min-height', $(".navbar-sidenav").height() - 180);
198-
vm.rulesBindingList.removeAll();
199-
$('.rulesListDisplay > div:gt(0)').remove();
200-
vm.rulesBindingList = ko.observableArray(rulesList);
201-
ko.applyBindings(vm, $("#testRulesDOMObject")[0]);
202-
ko.applyBindings(vm, $("#submitButton")[0]);
203-
closeTooltip();
204-
loadTooltip();
205-
};
206-
reader.readAsText(subscriptionFile);
207-
}
208-
209-
function validateEventsJsonAndCreateSubscriptions(subscriptionFile) {
178+
function validateJSONAndUpload(subscriptionFile, isRules) {
210179
var reader = new FileReader();
211180
reader.onload = function() {
212181
var fileContent = reader.result;
@@ -221,30 +190,36 @@ jQuery(document).ready(
221190
sticky : false,
222191
theme : 'Notify'
223192
});
224-
var eventsList = JSON.parse(fileContent);
225-
ko.cleanNode($("#testEventsDOMObject")[0]);
226-
vm.eventsBindingList.removeAll();
227-
$('.eventsListDisplay > div:gt(0)').remove();
228-
vm.eventsBindingList = ko.observableArray(eventsList);
229-
ko.applyBindings(vm, $("#testEventsDOMObject")[0]);
230-
closeTooltip();
231-
loadTooltip();
193+
194+
var list = JSON.parse(fileContent);
195+
196+
if (isRules == true) {
197+
vm.rulesBindingList([]);
198+
vm.rulesBindingList(list);
199+
} else {
200+
vm.eventsBindingList([]);
201+
vm.eventsBindingList(list);
202+
}
203+
232204
};
233-
reader.readAsText(subscriptionFile);
205+
206+
if (subscriptionFile != null){
207+
reader.readAsText(subscriptionFile);
208+
}
234209
}
235210

236211
//Set onchange event on the input element "uploadRulesFile" and "uploadEventsFile"
237212
var pomRules = document.getElementById('uploadRulesFile');
238213
pomRules.onchange = function uploadFinished() {
239214
var subscriptionFile = pomRules.files[0];
240-
validateRulesJsonAndCreateSubscriptions(subscriptionFile);
215+
validateJSONAndUpload(subscriptionFile, true);
241216
$(this).val("");
242217
};
243218

244219
var pomEvents = document.getElementById('uploadEventsFile');
245220
pomEvents.onchange = function uploadFinished() {
246221
var subscriptionFile = pomEvents.files[0];
247-
validateEventsJsonAndCreateSubscriptions(subscriptionFile);
222+
validateJSONAndUpload(subscriptionFile, false);
248223
$(this).val("");
249224
};
250225

@@ -266,7 +241,7 @@ jQuery(document).ready(
266241
function createUploadWindowMSExplorer() {
267242
$('#upload_rules').click();
268243
var file = $('#upload_rules').prop('files')[0];
269-
validateRulesJsonAndCreateSubscriptions(file);
244+
validateJSONAndUpload(file, true);
270245
}
271246

272247
// HTML5 Download File window handling
@@ -291,7 +266,7 @@ jQuery(document).ready(
291266
function createUploadWindowMSExplorer() {
292267
$('#upload_events').click();
293268
var file = $('#upload_events').prop('files')[0];
294-
validateEventsJsonAndCreateSubscriptions(file);
269+
validateJSONAndUpload(file, false);
295270
}
296271

297272

0 commit comments

Comments
 (0)