$(document).ready(function() { function set_default_ip() { // Retrieve the first available IP for the selected network // and update the IP field var network_id = $("#network_id").val(); $.getJSON( $SCRIPT_ROOT + "/network/_retrieve_first_available_ip/" + network_id, function(json) { $("#ip").val(json.data); } ); } // If random_mac is checked, generate a random address // Empty the field otherwise function fill_mac_address() { if( $("#random_mac").prop("checked") ) { $.getJSON( $SCRIPT_ROOT + "/network/_generate_random_mac", function(json) { $("#mac_address").val(json.data.mac); $("#mac_address").prop("readonly", true); } ); } else { $("#mac_address").val(""); $("#mac_address").prop("readonly", false); } } // Set the default IP on first page load for: // - register new host // - add interface // Do NOT replace the IP on edit interface page load! // (we have to keep the existing IP) if( $("#hostForm").length || $("#interfaceForm").length ) { set_default_ip(); } // Set the default IP when changing network $("#network_id").on('change', function() { set_default_ip(); }); // Enable / disable item_id on edit host first page load if( $("#editHostForm").length ) { var machine_type = $("#machine_type_id option:selected").text(); if( machine_type == "Physical" ) { $("#item_id").prop("disabled", false); } else { $("#item_id").prop("disabled", true); } } // Enable / disable item_id field depending on machine_type // Item can only be assigned for physical hosts // And check / uncheck random_mac checkbox $("#machine_type_id").on('change', function() { var machine_type = $("#machine_type_id option:selected").text(); if( machine_type == "Physical" ) { $("#item_id").prop("disabled", false); $("#random_mac").prop("checked", false).change(); } else { $("#item_id").val(""); $("#item_id").prop("disabled", true); $("#random_mac").prop("checked", true).change(); } }); // Prefill interface name with hostname $("#name").keyup(function(event) { var hostname = $(this).val(); $("#interface_name").val(hostname); }); // Fill MAC address on page load if( $("#random_mac").length ) { fill_mac_address(); } // Fill or clear MAC address depending on random_mac checkbox $("#random_mac").on('change', function() { fill_mac_address(); }); // Force the first interface to have the hostname as name // This only applies to the hostForm (not when editing or adding interfaces) $("#hostForm input[name=interface_name]").prop("readonly", true); var hosts_table = $("#hosts_table").DataTable({ "ajax": function(data, callback, settings) { $.getJSON( $SCRIPT_ROOT + "/network/_retrieve_hosts", function(json) { callback(json); }); }, "pagingType": "full_numbers", "pageLength": 20, "lengthMenu": [[20, 50, 100, -1], [20, 50, 100, "All"]], "columnDefs": [ { "targets": [0], "render": function(data, type, row) { // render funtion to create link to Item view page if ( data === null ) { return data; } var url = $SCRIPT_ROOT + "/network/hosts/view/" + data; return '<a href="'+ url + '">' + data + '</a>'; } } ] }); });