From e7b56c4501ebbcbb6d767fdba5c0cd98862c3cdc Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Fri, 15 Dec 2017 14:57:32 +0100
Subject: [PATCH] Improve register new host form

- disable item field for virtual hosts
- pre-fill interface name with hostname
---
 app/static/js/hosts.js                  | 19 +++++++++++++++++++
 app/templates/_helpers.html             |  3 +--
 app/templates/networks/create_host.html |  2 +-
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js
index 8b45519..8594adc 100644
--- a/app/static/js/hosts.js
+++ b/app/static/js/hosts.js
@@ -24,6 +24,25 @@ $(document).ready(function() {
     update_available_ips();
   });
 
+  // Enable / disable item_id field depending on type
+  // Item can only be assigned for physical hosts
+  $("#type").on('change', function() {
+    var host_type = $(this).val();
+    if( host_type == "Physical" ) {
+      $("#item_id").prop("disabled", false);
+    } else {
+      $("#item_id").val("");
+      $("#item_id").prop("disabled", true);
+    }
+  });
+
+  // Prefill interface name with hostname
+  $("#name").keyup(function(event) {
+    var hostname = $(this).val();
+    console.log(hostname);
+    $("#interface_name").val(hostname);
+  });
+
   var hosts_table =  $("#hosts_table").DataTable({
     "ajax": function(data, callback, settings) {
       $.getJSON(
diff --git a/app/templates/_helpers.html b/app/templates/_helpers.html
index 30a990c..acc9431 100644
--- a/app/templates/_helpers.html
+++ b/app/templates/_helpers.html
@@ -18,7 +18,6 @@
 
 {% macro render_field(field) -%}
   {% set field_class = kwargs.pop('class_', '') + ' form-control' %}
-  {% set readonly = kwargs.pop('readonly', False) %}
   {% if field.errors %}
     {% set field_class = field_class + ' is-invalid' %}
   {% endif %}
@@ -26,7 +25,7 @@
   <div class="form-group row">
     {{ field.label(class_="col-sm-2 col-form-label") }}
     <div class="col-sm-10">
-      {{ field(class_=field_class, readonly=readonly) }}
+      {{ field(class_=field_class, **kwargs) }}
       {% if field.description %}
         <small class="form-text text-muted">
           {{ field.description|safe }}
diff --git a/app/templates/networks/create_host.html b/app/templates/networks/create_host.html
index db52aec..a60b7c7 100644
--- a/app/templates/networks/create_host.html
+++ b/app/templates/networks/create_host.html
@@ -20,7 +20,7 @@
     {{ render_field(form.name, class_="text-lowercase") }}
     {{ render_field(form.type) }}
     {{ render_field(form.description) }}
-    {{ render_field(form.item_id) }}
+    {{ render_field(form.item_id, disabled=True) }}
     {{ render_field(form.network_id) }}
     {{ render_field(form.ip) }}
     {{ render_field(form.interface_name, class_="text-lowercase") }}
-- 
GitLab