var tiers = [];
var defaultDatasource = "_mmx_default";
var ruleTypes = [
"loadByInterval",
"loadByPeriod",
"dropByInterval",
"dropByPeriod",
"JSON"
];
function makeRuleDiv(rule) {
var retVal = "
";
if (rule === null || typeof rule === "undefined") {
retVal += makeRuleComponents("") + makeRuleBody();
} else {
retVal += makeRuleComponents(rule.type) + makeRuleBody(rule);
}
retVal += "
";
return retVal;
}
function makeRuleComponents(type) {
var components = "X ";
components +=
"" +
"type " +
"" +
" ";
$.each(ruleTypes, function(index, ruleType) {
if (ruleType === type) {
components += "" + ruleType + " ";
} else {
components += "" + ruleType + " ";
}
});
components +=
" " +
" ";
return components;
}
function makeRuleBody(rule) {
var retVal = "";
if (rule !== null && typeof rule !== "undefined") {
switch (rule.type) {
case "loadByInterval":
retVal += makeLoadByInterval(rule);
break;
case "loadByPeriod":
retVal += makeLoadByPeriod(rule);
break;
case "dropByInterval":
retVal += makeDropByInterval(rule);
break;
case "dropByPeriod":
retVal += makeDropByPeriod(rule);
break;
case "JSON":
retVal += makeJSON();
break;
default:
break;
}
}
retVal += " "
return retVal;
}
function makeLoadByInterval(rule) {
return "interval " +
"replicants " +
makeTiersDropdown(rule)
;
}
function makeLoadByPeriod(rule) {
return "period " +
"replicants " +
makeTiersDropdown(rule)
;
}
function makeDropByInterval(rule) {
return "interval ";
}
function makeDropByPeriod(rule) {
return "period ";
}
function makeJSON() {
return "JSON ";
}
function makeTiersDropdown(rule) {
var retVal = "tier "
$.each(tiers, function(index, tier) {
if (rule.tier === tier) {
retVal += "" + tier + " ";
} else {
retVal += "" + tier + " ";
}
});
retVal += " ";
return retVal;
}
function getRules() {
var selected = $('#datasources option:selected').text();
if (selected !== "") {
$.getJSON("/info/rules/" + selected, function(data) {
$('#rules_list').empty();
if (!$.isEmptyObject(data)) {
$.each(data, function(index, rule) {
$('#rules_list').append(makeRuleDiv(rule));
});
}
});
}
}
function domToRule(domRule) {
var ruleType = $($(domRule).find(".rule_dropdown_types:first")).val();
var inputs = $($(domRule).find(".rule_body:first")).children(":not(span)");
// Special case for free form JSON
if (ruleType === "JSON") {
return $.parseJSON($(inputs.children("input:first")).val());
}
var rule = {};
rule.type = ruleType;
$.each(inputs, function(index, input) {
var name = $(input).attr("name");
rule[name] = $(input).val();
});
return rule;
}
$(document).ready(function() {
$("button").button();
$("#rules_list").sortable();
$("#error_dialog").dialog({
autoOpen: false,
modal:true,
resizeable: false,
buttons: {
Ok : function() {
$(this).dialog("close");
}
}
});
$("#cancel_dialog").dialog({
autoOpen: false,
modal:true,
resizeable: false,
buttons: {
Yes : function() {
getRules();
$(this).dialog("close");
},
No: function() {
$(this).dialog("close");
}
}
});
$("#update_dialog").dialog({
autoOpen: false,
modal:true,
resizeable: false,
buttons: {
Yes : function() {
var rules = [];
$.each($("#rules_list").children(), function(index, domRule) {
rules.push(domToRule(domRule));
});
var selected = $('#datasources option:selected').text();
$.ajax({
type: 'POST',
url:'/info/rules/' + selected,
data: JSON.stringify(rules),
contentType:"application/json; charset=utf-8",
dataType:"json",
error: function(xhr, status, error) {
$("#update_dialog").dialog("close");
$("#error_dialog").html(xhr.responseText);
$("#error_dialog").dialog("open");
},
success: function(data, status, xhr) {
$("#update_dialog").dialog("close");
}
});
},
Cancel: function() {
$(this).dialog("close");
}
}
});
$.getJSON("/info/tiers", function(theTiers) {
tiers = theTiers;
});
$.getJSON("/info/db/datasources", function(data) {
$.each(data, function(index, datasource) {
$('#datasources').append($(' ').attr("value", datasource).text(datasource));
});
$('#datasources').append($(' ').attr("value", defaultDatasource).text(defaultDatasource));
});
$("#datasources").change(function(event) {
getRules();
$("#rules").show();
});
$(".rule_dropdown_types").live("change", function(event) {
var newRule = {
"type" : $(event.target).val()
};
var ruleBody = $(event.target).parent('.rule_dropdown').next('.rule_body');
ruleBody.replaceWith(makeRuleBody(newRule));
});
$(".delete_rule").live("click", function(event) {
$(event.target).parent(".rule").remove();
});
$("#create_new_rule").click(function (event) {
$('#rules_list').prepend(makeRuleDiv());
});
$("#cancel").click(function() {
$("#cancel_dialog").dialog("open");
});
$('#update').click(function (){
$("#update_dialog").dialog("open")
});
});