Skip to content

Commit fd3d397

Browse files
committed
Subscription handling updated
1 parent 0069821 commit fd3d397

File tree

3 files changed

+87
-139
lines changed

3 files changed

+87
-139
lines changed

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

Lines changed: 72 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -554,108 +554,23 @@ jQuery(document).ready(function() {
554554
});
555555
// /END ## upload_subscriptions #################################################
556556

557-
// /Start ## Add Condition ##############################################
558-
/*
559-
$('div.modal-content').on( 'click', 'button.add_condition', function (event) {
560557

561-
event.stopPropagation();
562-
event.preventDefault();
563-
564-
/*var condition = {
565-
"conditions" : [
566-
{
567-
"jmespath" : ko.observable("")
568-
}
569-
]
570-
}
571-
*
572-
var condition = {
573-
"jmespath" : ko.observable("test")
574-
}
575-
576-
577-
578-
vm.subscription()[0].requirements()[0].conditions().push(new jmespath_model({"jmespath": ko.observable("cc")}));
579-
580-
vm.subscription()[0].requirements()[0].conditions().push({"jmespath": ko.observable("cc")});
581-
582-
583-
584-
console.log(ko.toJSON(vm.subscription()[0].requirements()[0].conditions()));
585-
586-
vm.subscription.valueHasMutated();
587-
588-
589-
//var vm = new SubscriptionViewModel();
590-
//ko.applyBindings(vm);
591-
592-
// ko.cleanNode(document.getElementById(element_id))
593-
// ko.applyBindings(viewModel, document.getElementById(element_id))
594-
595-
});*/
596-
// /Stop ## Add Condition ################################################
597-
598-
599-
// /Start ## Delete Condition ##############################################
600-
/* $('div.modal-content').on( 'click', 'button.condition_delete', function (event) {
601-
602-
603-
event.stopPropagation();
604-
event.preventDefault();
605-
606-
var context = ko.contextFor(event.target);
607-
var indexToRemove = context.$index();
608-
609-
// Removing Requirement(Condition), based on index position, from Requirement form in Add_Subscription window.
610-
if (indexToRemove > 0 ){
611-
vm.subscription()[0].requirements.splice(indexToRemove,1);
612-
}
613-
else {
614-
$.alert("You need to have atleast one Condition.");
615-
}
616-
});*/
617-
// /Stop ## Delete Condition ################################################
618-
619-
620558
// /Start ## Add Subscription ########################################
621559
$('.container').on( 'click', 'button.btn.btn-success.add_subscription', function (event) {
622560

623561
event.stopPropagation();
624562
event.preventDefault();
625-
626-
// Clear observable array
627-
vm.subscription([]);
628563

629-
// Map JSON to Model and observableArray
630-
var mappedPackageInfo = $.map(default_json_empty, function (item) {
631-
632-
// Removing old Requirements and conditions from previous Add_subscription window.
633-
item.requirements.splice(1,item.requirements.length - 1);
634-
635-
// Defining Observable on all parameters in Requirements array(which is defined as ObservableArray)
636-
item.requirements[0].conditions[0] = {"jmespath" : ko.observable("")};
637-
item.requirements[0].type = ko.observable("");
638564

639-
return new subscription_model(item);
640-
});
641-
642-
// Load data into observable array
643-
vm.subscription(mappedPackageInfo);
565+
json_obj_clone = JSON.parse(JSON.stringify(default_json_empty));
644566

645-
save_method = 'add';
567+
populate_json(json_obj_clone, "add");
646568

647-
$('.form-group').removeClass('has-error'); // clear error class
648-
$('.help-block').empty(); // clear error string
649-
650-
$('#modal_form').modal('show'); // show bootstrap modal
651-
$('.modal-title').text('Add Subscription'); // Set Title to Bootstrap modal title
652569

653570
});
654571
// /Stop ## Add Subscription ############################################
655572

656573

657-
658-
659574
// /Start ## Reload Datatables ###########################################
660575
function reload_table()
661576
{
@@ -680,61 +595,83 @@ jQuery(document).ready(function() {
680595
},
681596
success : function (data, textStatus) {
682597

683-
var returnData = [data];
684-
if (returnData.length > 0) {
598+
populate_json(data, "edit");
599+
600+
},
601+
error : function (XMLHttpRequest, textStatus, errorThrown) {
602+
$.jGrowl("Error: " + XMLHttpRequest.responseText, {
603+
sticky : true,
604+
theme : 'Error'
605+
});
606+
},
607+
complete : function () {
608+
}
609+
};
685610

686-
// Map JSON to Model and observableArray
687-
var mappedPackageInfo = $.map(returnData, function (item) {
611+
// Perform AJAX
612+
var ajaxHttpSender = new AjaxHttpSender();
613+
ajaxHttpSender.sendAjax(backendServiceUrl + "/subscriptions/"+id, "GET", null, callback);
688614

689-
// Defining Observable on all parameters in Requirements array(which is defined as ObservableArray)
615+
});
616+
// /Stop ## Edit Subscription ###########################################
690617

691-
for (i=0; i < item[0].requirements.length; i++) {
692618

693-
var conditions_array = [];
619+
// /Start ## pupulate JSON ###########################################
620+
function populate_json(data, save_method_in)
621+
{
622+
var returnData = [data];
623+
if (returnData.length > 0) {
694624

695-
for (k = 0; k < item[0].requirements[i].conditions.length; k++) {
696625

697-
var jmespath_temp = item[0].requirements[i].conditions[k].jmespath;
626+
vm.subscription([]);
698627

699-
conditions_array.push(new jmespath_model({"jmespath": ko.observable(jmespath_temp)}));
628+
// Map JSON to Model and observableArray
629+
var mappedPackageInfo = $.map(returnData, function (item) {
700630

701-
}
631+
// Defining Observable on all parameters in Requirements array(which is defined as ObservableArray)
702632

703-
item[0].requirements[i] = new conditions_model(conditions_array);
633+
for (i=0; i < item[0].requirements.length; i++) {
704634

705-
}
706-
return new subscription_model(item[0]);
635+
var conditions_array = [];
707636

637+
for (k = 0; k < item[0].requirements[i].conditions.length; k++) {
708638

709-
});
639+
var jmespath_temp = item[0].requirements[i].conditions[k].jmespath;
710640

711-
// Load data into observable array
712-
vm.subscription(mappedPackageInfo);
641+
conditions_array.push(new jmespath_model({"jmespath": ko.observable(jmespath_temp)}));
713642

643+
}
714644

715-
$('#modal_form').modal('show');
716-
$('.modal-title').text('Subscription: ' + data.subscriptionName);
645+
item[0].requirements[i] = new conditions_model(conditions_array);
717646

718-
save_method = 'edit';
719647
}
648+
return new subscription_model(item[0]);
720649

721-
},
722-
error : function (XMLHttpRequest, textStatus, errorThrown) {
723-
$.jGrowl("Error: " + XMLHttpRequest.responseText, {
724-
sticky : true,
725-
theme : 'Error'
726-
});
727-
},
728-
complete : function () {
729-
}
730-
};
731650

732-
// Perform AJAX
733-
var ajaxHttpSender = new AjaxHttpSender();
734-
ajaxHttpSender.sendAjax(backendServiceUrl + "/subscriptions/"+id, "GET", null, callback);
651+
});
735652

736-
});
737-
// /Stop ## Edit Subscription ###########################################
653+
// Load data into observable array
654+
vm.subscription(mappedPackageInfo);
655+
656+
657+
$('#modal_form').modal('show');
658+
659+
if(data === "edit")
660+
{
661+
title_ = 'Subscription: ' + data.subscriptionName
662+
663+
}else
664+
{
665+
title_ = 'Add Subscription';
666+
}
667+
668+
$('.modal-title').text(title_);
669+
670+
save_method = save_method_in;
671+
}
672+
673+
}
674+
// /Stop ## pupulate JSON ###########################################
738675

739676

740677
// /Start ## Save Subscription ##########################################
@@ -783,17 +720,21 @@ jQuery(document).ready(function() {
783720
return;
784721
}
785722

786-
/*
723+
787724
var requirementsArray = vm.subscription()[0].requirements();
788725
for (i=0; i < requirementsArray.length; i++){
789-
if (requirementsArray[i].conditions[0].jmespath() == "") {
790-
$.jGrowl("Error: jmepath field must have a value", {
791-
sticky : true,
792-
theme : 'Error'
793-
});
794-
return;
795-
}
796-
}*/
726+
var conditionsArray = requirementsArray[i].conditions();
727+
for (k=0; k < conditionsArray.length; k++) {
728+
var test_me = ko.toJSON(conditionsArray[k].jmespath());
729+
if (test_me === '""') {
730+
$.jGrowl("Error: jmepath field must have a value", {
731+
sticky: true,
732+
theme: 'Error'
733+
});
734+
return;
735+
}
736+
}
737+
}
797738
//END: Check of other subscription fields values
798739

799740
var id = ko.toJSON(vm.subscription()[0].subscriptionName).trim();

src/main/resources/static/resources/subscription_templates.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"jmespath" : ""
1515
}
1616
],
17-
"type" : ""
1817
}
1918
],
2019
"subscriptionName" : ""

src/main/resources/templates/index.html

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333

3434
</head>
3535
<body>
36+
3637
<div class="hidden" id="backendServiceUrl" th:text="${backendServiceUrl}"></div>
3738
<div class="hidden" id="subscriptionTemplate" th:text="${subscriptionTemplate}"></div>
3839

3940
<script type="text/javascript" src="resources\subscription_templates.js"></script>
4041
<script type="text/javascript" src="js/main.js"></script>
4142

42-
<div class="container">
43+
<div class="container">
4344
<h1 style="font-size:20pt">Eiffel Intelligence Subscription Handling (<span th:text="${backendServiceUrl}"></span>)
4445
<button data-toggle="tooltip" title="EI connection status check is refreshed continuesly. Click button to force check status." type="button" id="btnEIConnection" class="btn btnEIConnectionStatus"><i class="glyphicon"></i>EI Backend Status</button>
4546
<button data-toggle="tooltip" title="Eiffel intelligence instance information" type="button" id="btnInfo" class="btn btnEIInstanceInfo"><i class="glyphicon"></i> EI Backend Instance Info</button>
@@ -134,7 +135,9 @@ <h3 class="modal-title text-center">Subscription Form</h3>
134135
</div>
135136
</div>
136137

137-
<!-- ko foreach: { data: requirements, as: 'requirements_item' } -->
138+
<img width="16" alt="Information" src="assets/images/information.png" title="Info: 'AND' is used between Conditions in Requirement groups, and 'OR' is used between Requirement groups"/>
139+
140+
<!-- ko foreach: { data: requirements, as: 'requirements_item' } -->
138141
<div class="form-group">
139142
<label class="control-label col-md-3">Requirements</label>
140143
<div class="col-md-9">
@@ -143,7 +146,6 @@ <h3 class="modal-title text-center">Subscription Form</h3>
143146
<div class="form-group">
144147
<label class="control-label col-md-3">Conditions</label>
145148
<div class="col-md-9">
146-
<p data-bind="text: $parentContext.$index() + ':' + $index()"></p>
147149
<textarea data-toggle="tooltip" title="Write a Subscription Rule(JmePath)" data-bind="textInput:$data.jmespath()" name="jmespath" placeholder="jmespath" class="form-control" type="text" />
148150
<button data-bind="click: function(data, event) { $root.delete_condition(data, event, requirements_item , $index(), $parentContext.$index() ); }, clickBubble: false" data-toggle="tooltip" title="Delete condition" class="btn btn-danger float-right" ><i class="glyphicon glyphicon-trash"></i>Delete</button>
149151
<span class="help-block"></span>
@@ -155,15 +157,21 @@ <h3 class="modal-title text-center">Subscription Form</h3>
155157

156158
</div>
157159

158-
<button data-bind="click: function(data, event) { $root.add_condition(data, event, $index()); }, clickBubble: false" data-toggle="tooltip" title="Add a new condition" class="btn btn-success float-right"><i class="glyphicon glyphicon-plus"></i> Add Condition</button>
160+
<div class="form-group">
161+
<label class="control-label col-md-3"></label>
162+
<div class="col-md-9">
163+
<button data-bind="click: function(data, event) { $root.add_condition(data, event, $index()); }, clickBubble: false" data-toggle="tooltip" title="Add a new condition" class="btn btn-success float-right"><i class="glyphicon glyphicon-plus"></i> Add Condition</button>
164+
<span class="help-block"></span>
165+
</div>
166+
</div>
159167

160168

161169

162170

163171
</div>
164172
<!-- /ko -->
165173
<div class="container">
166-
<button data-bind="click: $root.add_requirement, clickBubble: false" data-toggle="tooltip" title="Add a new requirement" class="btn btn-success float-right"><i class="glyphicon glyphicon-plus"></i> Add Requirement</button>
174+
<button data-bind="click: $root.add_requirement, clickBubble: false" data-toggle="tooltip" title="Add a new requirement" class="btn btn-success float-left"><i class="glyphicon glyphicon-plus float-right"></i> Add Requirement</button>
167175
</div>
168176

169177

@@ -188,12 +196,12 @@ <h3 class="modal-title text-center">Subscription Form</h3>
188196
<!-- End Bootstrap modal -->
189197

190198

199+
<!--
191200
<code class="hint mt15">DEBUG INFO</code>
192201
<div data-bind = "visible: subscription().length > 0" >
193202
<p data-bind="text: ko.toJSON(subscription())"></p>
194-
<!--<p data-bind="text: ko.toJSON(subscription().requirements)"></p>-->
195203
</div>
196-
204+
-->
197205

198206

199207

0 commit comments

Comments
 (0)