diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js index 3574fffa43948b2f3f8e0ff6b45677704b938874..c45f80d20f320b5b1a2915cbbe4b3812a38149d2 100644 --- a/app/static/js/hosts.js +++ b/app/static/js/hosts.js @@ -12,6 +12,21 @@ $(document).ready(function() { ); } + // Enable / disable item_id field depending on machine_type + // Item can only be assigned for physical hosts + // And check / uncheck random_mac checkbox + function update_machine_type_attributes() { + var machine_type = $("#machine_type_id option:selected").text(); + if( machine_type.startsWith("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(); + } + } + // If random_mac is checked, generate a random address // Empty the field otherwise function fill_mac_address() { @@ -43,29 +58,13 @@ $(document).ready(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); - } + // On register and edit host first page load + if( $("#hostForm").length || $("#editHostForm").length ) { + update_machine_type_attributes(); } - // 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(); - } + update_machine_type_attributes(); }); // Prefill interface name with hostname