From 1a22bbce68cd8925398fd50ac615e21f275006ce Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Fri, 6 Apr 2018 22:42:02 +0200
Subject: [PATCH] Fix MAC selection on register new host

On page load we have to check the machine type to enable / disable
item_id and check / uncheck the random MAC
---
 app/static/js/hosts.js | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js
index 3574fff..c45f80d 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
-- 
GitLab