diff --git a/smaug_dashboard/static/smaugdashboard/js/jquery.smaug.js b/smaug_dashboard/static/smaugdashboard/js/jquery.smaug.js index e744874..5a8a011 100644 --- a/smaug_dashboard/static/smaugdashboard/js/jquery.smaug.js +++ b/smaug_dashboard/static/smaugdashboard/js/jquery.smaug.js @@ -26,6 +26,151 @@ $(cur_node).closest(".form-group.required").removeClass("has-error"); } + /* create dynamic field */ + function createDynamicField(schema, userdata, modal_body) { + if(schema!=null) { + for(var p in schema.properties) { + var property = schema.properties[p]; + /* confirm whether the field is required */ + var required = false; + if($.inArray(p, schema.required) >= 0) { + required = true; + } + + /* form group */ + var form_group = $("
").addClass("form-group"); + if(required) { + form_group.addClass("required"); + } + + /* control label */ + var control_label = $("").addClass("control-label") + .attr("for", "id_"+p) + .html(property.title); + if(required) { + control_label.addClass("required"); + } + form_group.append(control_label); + + /* icon required */ + if(required) { + var icon_required = $("").addClass("hz-icon-required") + .addClass("fa") + .addClass("fa-asterisk"); + form_group.append(icon_required); + } + + /* help icon */ + if(property.hasOwnProperty("description")) { + var help_icon = $("").addClass("help-icon") + .attr("data-toggle", "tooltip") + .attr("data-placement", "top") + .attr("title", "") + .attr("data-original-title", property.description); + var question_circle = $("").addClass("fa") + .addClass("fa-question-circle"); + help_icon.append(question_circle); + form_group.append(help_icon); + } + + /* control wrapper */ + var control_wrapper = $(""); + + if(property.hasOwnProperty("eumn")) { + //drop down list + var dropdownlist_control = $(""); + dropdownlist_control.addClass("form-control"); + dropdownlist_control.attr("id", "id_"+p); + dropdownlist_control.attr("name", p); + + //get drop down list options + for(option in property.eumn) { + var option_control = $("").attr('value', property.eumn[option]) + .html(property.eumn[option]); + dropdownlist_control.append(option_control); + } + + //default value + if(property.hasOwnProperty("default")) { + if(property.default != null) { + dropdownlist_control.val(property.default); + } + } + + //user value + if(userdata != null) { + if(userdata.hasOwnProperty(p)) { + dropdownlist_control.val(userdata[p]); + } + } + control_wrapper.append(dropdownlist_control); + } + else { + switch(property.type) { + case "string": { + //text box + var text_control = $(""); + text_control.addClass("form-control"); + text_control.attr("id", "id_"+p); + text_control.attr("name", p); + + //default value + if(property.hasOwnProperty("default")) { + if(property.default != null) { + text_control.val(property.default); + } + } + + //user value + if(userdata != null) { + if(userdata.hasOwnProperty(p)) { + text_control.val(userdata[p]); + } + } + control_wrapper.append(text_control); + break; + } + case "boolean": { + //check box + var checkbox_control = $(""); + checkbox_control.addClass("form-control"); + checkbox_control.attr("id", "id_"+p); + checkbox_control.attr("name", p); + + //default value + if(property.hasOwnProperty("default")) { + if(property.default&&eval(property.default)) { + checkbox_control.attr("checked",true); + } + } + + //user value + if(userdata != null) { + if(userdata.hasOwnProperty(p)) { + if(userdata[p]) { + checkbox_control.attr("checked", true); + } + else { + checkbox_control.removeAttr("checked"); + } + } + } + control_wrapper.append(checkbox_control); + break; + } + default: { + break; + } + } + } + + /* add control to body */ + form_group.append(control_wrapper); + modal_body.append(form_group); + } + } + } + $.Smaug = { /* get the default resources parameters */ @@ -70,6 +215,97 @@ } } return flag; + }, + + /* Get dynamic field value */ + getDynamicValue: function(form_controls) { + var data = null; + if(form_controls!=null&&form_controls.length>0) { + data = {}; + for(var i = 0; i