opnsense.js 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
 * Map input fields from given parent tag to structure of named arrays.
 *
 * @param parent tag id in dom
 * @return array
 */
function getFormData(parent) {

    data = {};
10
    $( "#"+parent+"  input,select" ).each(function( index ) {
11 12 13 14 15 16 17 18 19
            node = data ;
            keyparts = $(this).attr('id').split('.');
            for (var i in keyparts) {
                if (!(keyparts[i] in node)) {
                    node[keyparts[i]] = {};
                }
                if (i < keyparts.length - 1 ) {
                    node = node[keyparts[i]];
                } else {
20 21 22 23 24 25 26 27 28 29 30 31
                    if ($(this).is("select")) {
                        // selectbox, collect selected items
                        var tmp_str = "";
                        $(this).children().each(function(index){
                            if ($(this).prop("selected")){
                                if (tmp_str != "") tmp_str = tmp_str + ",";
                                tmp_str = tmp_str + $(this).val();
                            }
                            node[keyparts[i]] = tmp_str;
                        });
                    } else if ($(this).prop("type") == "checkbox") {
                        // checkbox input type
32 33 34 35 36
                        if ($(this).prop("checked")) {
                            node[keyparts[i]] = 1 ;
                        } else {
                            node[keyparts[i]] = 0 ;
                        }
37
                    } else {
38
                        // regular input type
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
                        node[keyparts[i]] = $(this).val();
                    }
                }
            }
    });

    return data;
}

/**
 * bind data to form, using named arrays
 *
 * for example,
 *      data = {'host':{'name':'opnsense'}}
 *      parent = 'general'
 *
 *      will search for an input tag host.name within the parent tag 'general' and fills it with the value 'opnsense'
 *
 * @param parent tag id in dom
 * @param data named array structure
 */
function setFormData(parent,data) {
61 62 63
    //alert( JSON.stringify(data['general']['interfaces']) );

    $( "#"+parent+"  input,select" ).each(function( index ) {
64 65 66 67 68 69 70 71 72
        node = data ;
        keyparts = $(this).attr('id').split('.');
        for (var i in keyparts) {
            if (!(keyparts[i] in node)) {
                break;
            }
            if (i < keyparts.length - 1 ) {
                node = node[keyparts[i]];
            } else {
73 74 75 76 77 78 79 80 81 82 83 84 85
                // data node found, handle per type
                if ($(this).is("select")) {
                    // handle select boxes
                    $(this).empty(); // flush
                    for (var key in node[keyparts[i]]) {
                        if (node[keyparts[i]][key]["selected"] != "0") {
                            $(this).append("<option value='"+key+"' selected>" + node[keyparts[i]][key]["value"] + " </option>");
                        } else {
                            $(this).append("<option value='"+key+"'>" + node[keyparts[i]][key]["value"] + " </option>");
                        }
                    }
                } else if ($(this).prop("type") == "checkbox") {
                    // checkbox type
86 87 88 89 90
                    if (node[keyparts[i]] != 0) {
                        $(this).prop("checked",true) ;
                    } else {
                        $(this).prop("checked",false) ;
                    }
91
                } else {
92
                    // regular input type
93 94 95 96 97 98
                    $(this).val(node[keyparts[i]]);
                }
            }
        }
    });
}
99 100 101 102 103 104 105 106 107 108


/**
 * handle form validations
 * @param parent
 * @param validationErrors
 */
function handleFormValidation(parent,validationErrors) {
    $( "#"+parent+"  input" ).each(function( index ) {
        if (validationErrors != undefined && $(this).attr('id') in validationErrors) {
109
            $("*[for='" + $(this).attr('id') + "']").addClass("has-error");
110 111
            $("span[for='" + $(this).attr('id') + "']").text(validationErrors[$(this).attr('id')]);
        } else {
112
            $("*[for='" + $(this).attr('id') + "']").removeClass("has-error");
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            $("span[for='" + $(this).attr('id') + "']").text("");
        }
    });
}

/**
 * call remote function (post request), wrapper around standard jQuery lib.
 * @param url endpoint url
 * @param sendData input structure
 * @param callback callback function
 */
function ajaxCall(url,sendData,callback) {
    $.ajax({
        type: "POST",
        url: url,
        dataType:"json",
        complete: function(data,status) {
            if ( callback == null ) {
                null;
            } else if ( "responseJSON" in data ) {
                callback(data['responseJSON'],status);
            } else {
135
                callback(data,status);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            }
        },
        data:sendData
    });
}

/**
 * retrieve json type data (GET request) from remote url
 * @param url endpoint url
 * @param sendData input structure
 * @param callback callback function
 */
function ajaxGet(url,sendData,callback) {
    $.ajax({
        type: "GET",
        url: url,
        dataType:"json",
        complete: function(data,status) {
            if ( callback == null ) {
                null;
            } else if ( "responseJSON" in data ) {
                callback(data['responseJSON'],status);
            } else {
                callback({},status);
            }
        },
        data:sendData
    });
}