opnsense.js 7.58 KB
Newer Older
1
/**
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *    Copyright (C) 2015 Deciso B.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *    shared components
 *
 */

 /**
 *
35 36 37 38 39 40 41
 * Map input fields from given parent tag to structure of named arrays.
 *
 * @param parent tag id in dom
 * @return array
 */
function getFormData(parent) {

42
    var data = {};
43
    $( "#"+parent+"  input,#"+parent+" select" ).each(function( index ) {
44
        if ($(this).prop('id') == undefined) {
45 46 47
            // we need an id.
            return;
        }
48 49 50 51 52 53
        var node = data ; // target node
        var sourceNode = $(this); // document node to fetch data from
        var keyparts = sourceNode.prop('id').split('.');
        $.each(keyparts,function(indx,keypart){
            if (!(keypart in node)) {
                node[keypart] = {};
54
            }
55 56
            if (indx < keyparts.length - 1 ) {
                node = node[keypart];
57
            } else {
58
                if (sourceNode.is("select")) {
59 60
                    // selectbox, collect selected items
                    var tmp_str = "";
61
                    sourceNode.children().each(function(index){
Jos Schellevis's avatar
Jos Schellevis committed
62
                        if ($(this).prop("selected")){
63
                            if (tmp_str != "") tmp_str = tmp_str + ",";
64
                            tmp_str = tmp_str + $(this).val();
65
                        }
66
                    });
67 68
                    node[keypart] = tmp_str;
                } else if (sourceNode.prop("type") == "checkbox") {
69
                    // checkbox input type
70 71
                    if (sourceNode.prop("checked")) {
                        node[keypart] = 1 ;
72
                    } else {
73
                        node[keypart] = 0 ;
74
                    }
75 76
                } else {
                    // regular input type
77
                    node[keypart] = sourceNode.val();
78 79
                }
            }
80
        });
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    });

    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) {
99
    $( "#"+parent+"  input,#"+parent+" select,#"+parent+" span" ).each(function( index ) {
100
        if ($(this).prop('id') == undefined) {
101 102 103
            // we need an id.
            return;
        }
104
        var node = data ;
105
        var targetNode = $(this); // document node to fetch data to
106
        var keyparts = $(this).prop('id').split('.');
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        $.each(keyparts,function(indx,keypart){
            if (keypart in node) {
                if (indx < keyparts.length - 1 ) {
                    node = node[keypart];
                } else {
                    // data node found, handle per type
                    if (targetNode.is("select")) {
                        // handle select boxes
                        targetNode.empty(); // flush
                        $.each(node[keypart],function(indxItem, keyItem){
                            if (keyItem["selected"] != "0") {
                                targetNode.append("<option value='"+indxItem+"' selected>" + keyItem["value"] + " </option>");
                            } else {
                                targetNode.append("<option value='"+indxItem+"'>" + keyItem["value"] + " </option>");
                            }
                        });
                    } else if (targetNode.prop("type") == "checkbox") {
                        // checkbox type
                        if (node[keypart] != 0) {
                            targetNode.prop("checked",true) ;
127
                        } else {
128
                            targetNode.prop("checked",false) ;
129
                        }
130 131 132 133 134
                    } else if (targetNode.is("span")) {
                        if (node[keypart] != null) {
                            targetNode.text("");
                            targetNode.append($.parseHTML(String(node[keypart])));
                        }
135
                    } else {
136 137
                        // regular input type
                        targetNode.val(node[keypart]);
138
                    }
139 140
                }
            }
141
        });
142 143
    });
}
144 145 146 147 148 149 150 151


/**
 * handle form validations
 * @param parent
 * @param validationErrors
 */
function handleFormValidation(parent,validationErrors) {
152
    $( "#"+parent+"  input,#"+parent+" select" ).each(function( index ) {
153 154 155
        if (validationErrors != undefined && $(this).prop('id') in validationErrors) {
            $("*[for='" + $(this).prop('id') + "']").addClass("has-error");
            $("span[for='" + $(this).prop('id') + "']").text(validationErrors[$(this).prop('id')]);
156
        } else {
157 158
            $("*[for='" + $(this).prop('id') + "']").removeClass("has-error");
            $("span[for='" + $(this).prop('id') + "']").text("");
159 160 161 162
        }
    });
}

163 164 165 166 167 168 169 170
/**
 * clear form validations
 * @param parent
 */
function clearFormValidation(parent) {
    handleFormValidation(parent, {});
}

171 172 173 174 175
/**
 * call remote function (post request), wrapper around standard jQuery lib.
 * @param url endpoint url
 * @param sendData input structure
 * @param callback callback function
176
 * @return deferred object
177 178
 */
function ajaxCall(url,sendData,callback) {
179
    return $.ajax({
180 181 182 183 184 185 186 187 188
        type: "POST",
        url: url,
        dataType:"json",
        complete: function(data,status) {
            if ( callback == null ) {
                null;
            } else if ( "responseJSON" in data ) {
                callback(data['responseJSON'],status);
            } else {
189
                callback(data,status);
190 191 192 193 194 195 196 197 198 199 200
            }
        },
        data:sendData
    });
}

/**
 * retrieve json type data (GET request) from remote url
 * @param url endpoint url
 * @param sendData input structure
 * @param callback callback function
201
 * @return deferred object
202 203
 */
function ajaxGet(url,sendData,callback) {
204
    return $.ajax({
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        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
    });
}