Răsfoiți Sursa

Merge pull request #13874 from pv2b/choices-css-rewrite

Refactor row coloring logic and simplify mark planned/connected toggle implementation
Daniel Sheppard 1 an în urmă
părinte
comite
07e2cf0ad2

+ 5 - 32
netbox/dcim/tables/devices.py

@@ -51,34 +51,6 @@ def get_cabletermination_row_class(record):
     return ''
     return ''
 
 
 
 
-def get_interface_row_class(record):
-    if not record.enabled:
-        return 'danger'
-    elif record.is_virtual:
-        return 'primary'
-    return get_cabletermination_row_class(record)
-
-
-def get_interface_state_attribute(record):
-    """
-    Get interface enabled state as string to attach to <tr/> DOM element.
-    """
-    if record.enabled:
-        return 'enabled'
-    else:
-        return 'disabled'
-
-
-def get_interface_connected_attribute(record):
-    """
-    Get interface disconnected state as string to attach to <tr/> DOM element.
-    """
-    if record.mark_connected or record.cable:
-        return 'connected'
-    else:
-        return 'disconnected'
-
-
 #
 #
 # Device roles
 # Device roles
 #
 #
@@ -706,11 +678,12 @@ class DeviceInterfaceTable(InterfaceTable):
             'cable', 'connection',
             'cable', 'connection',
         )
         )
         row_attrs = {
         row_attrs = {
-            'class': get_interface_row_class,
             'data-name': lambda record: record.name,
             'data-name': lambda record: record.name,
-            'data-enabled': get_interface_state_attribute,
-            'data-type': lambda record: record.type,
-            'data-connected': get_interface_connected_attribute
+            'data-enabled': lambda record: "enabled" if record.enabled else "disabled",
+            'data-virtual': lambda record: "true" if record.is_virtual else "false",
+            'data-mark-connected': lambda record: "true" if record.mark_connected else "false",
+            'data-cable-status': lambda record: record.cable.status if record.cable else "",
+            'data-type': lambda record: record.type
         }
         }
 
 
 
 

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
netbox/project-static/dist/netbox-dark.css


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
netbox/project-static/dist/netbox-light.css


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
netbox/project-static/dist/netbox-print.css


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
netbox/project-static/dist/netbox.js


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
netbox/project-static/dist/netbox.js.map


+ 10 - 26
netbox/project-static/src/buttons/connectionToggle.ts

@@ -7,10 +7,10 @@ import { isTruthy, apiPatch, hasError, getElements } from '../util';
  *
  *
  * @param element Connection Toggle Button Element
  * @param element Connection Toggle Button Element
  */
  */
-function toggleConnection(element: HTMLButtonElement): void {
+function setConnectionStatus(element: HTMLButtonElement, status: string): void {
+  // Get the button's row to change its data-cable-status attribute
+  const row = element.parentElement?.parentElement as HTMLTableRowElement;
   const url = element.getAttribute('data-url');
   const url = element.getAttribute('data-url');
-  const connected = element.classList.contains('connected');
-  const status = connected ? 'planned' : 'connected';
 
 
   if (isTruthy(url)) {
   if (isTruthy(url)) {
     apiPatch(url, { status }).then(res => {
     apiPatch(url, { status }).then(res => {
@@ -19,34 +19,18 @@ function toggleConnection(element: HTMLButtonElement): void {
         createToast('danger', 'Error', res.error).show();
         createToast('danger', 'Error', res.error).show();
         return;
         return;
       } else {
       } else {
-        // Get the button's row to change its styles.
-        const row = element.parentElement?.parentElement as HTMLTableRowElement;
-        // Get the button's icon to change its CSS class.
-        const icon = element.querySelector('i.mdi, span.mdi') as HTMLSpanElement;
-        if (connected) {
-          row.classList.remove('success');
-          row.classList.add('info');
-          element.classList.remove('connected', 'btn-warning');
-          element.classList.add('btn-info');
-          element.title = 'Mark Installed';
-          icon.classList.remove('mdi-lan-disconnect');
-          icon.classList.add('mdi-lan-connect');
-        } else {
-          row.classList.remove('info');
-          row.classList.add('success');
-          element.classList.remove('btn-success');
-          element.classList.add('connected', 'btn-warning');
-          element.title = 'Mark Installed';
-          icon.classList.remove('mdi-lan-connect');
-          icon.classList.add('mdi-lan-disconnect');
-        }
+        // Update cable status in DOM
+        row.setAttribute('data-cable-status', status);
       }
       }
     });
     });
   }
   }
 }
 }
 
 
 export function initConnectionToggle(): void {
 export function initConnectionToggle(): void {
-  for (const element of getElements<HTMLButtonElement>('button.cable-toggle')) {
-    element.addEventListener('click', () => toggleConnection(element));
+  for (const element of getElements<HTMLButtonElement>('button.mark-planned')) {
+    element.addEventListener('click', () => setConnectionStatus(element, 'planned'));
+  }
+  for (const element of getElements<HTMLButtonElement>('button.mark-installed')) {
+    element.addEventListener('click', () => setConnectionStatus(element, 'connected'));
   }
   }
 }
 }

+ 38 - 1
netbox/project-static/styles/netbox.scss

@@ -1075,4 +1075,41 @@ html {
       display: none;
       display: none;
     }
     }
   }
   }
-}
+  
+  // Apply row colours to interface lists
+  &[data-netbox-url-name='device_interfaces'] {
+    tr[data-cable-status=connected] {
+      background-color: rgba(map.get($theme-colors, "green"), 0.15);
+    }
+  
+    tr[data-cable-status=planned] {
+      background-color: rgba(map.get($theme-colors, "blue"), 0.15);
+    }
+  
+    tr[data-cable-status=decommissioning] {
+      background-color: rgba(map.get($theme-colors, "yellow"), 0.15);
+    }
+  
+    tr[data-mark-connected=true] {
+      background-color: rgba(map.get($theme-colors, "success"), 0.15);
+    }
+  
+    tr[data-virtual=true] {
+      background-color: rgba(map.get($theme-colors, "primary"), 0.15);
+    }
+  
+    tr[data-enabled=disabled] {
+      background-color: rgba(map.get($theme-colors, "danger"), 0.15);
+    }
+  
+    // Only show the correct button depending on the cable status
+    tr[data-cable-status=connected] button.mark-installed {
+      display: none;
+    }
+  
+    tr:not([data-cable-status=connected]) button.mark-planned {
+      display: none;
+    }
+  
+  }
+}

+ 6 - 9
netbox/templates/dcim/inc/cable_toggle_buttons.html

@@ -1,12 +1,9 @@
 {% load i18n %}
 {% load i18n %}
 {% if perms.dcim.change_cable %}
 {% if perms.dcim.change_cable %}
-    {% if cable.status == 'connected' %}
-        <button type="button" class="btn btn-warning btn-sm cable-toggle connected" title="{% trans "Mark Planned" %}" data-url="{% url 'dcim-api:cable-detail' pk=cable.pk %}">
-            <i class="mdi mdi-lan-disconnect" aria-hidden="true"></i>
-        </button>
-    {% else %}
-        <button type="button" class="btn btn-info btn-sm cable-toggle" title="{% trans "Mark Installed" %}" data-url="{% url 'dcim-api:cable-detail' pk=cable.pk %}">
-            <i class="mdi mdi-lan-connect" aria-hidden="true"></i>
-        </button>
-    {% endif %}
+    <button type="button" class="btn btn-warning btn-sm mark-planned" title="{% trans "Mark Planned" %}" data-url="{% url 'dcim-api:cable-detail' pk=cable.pk %}">
+    <i class="mdi mdi-lan-disconnect" aria-hidden="true"></i>
+    </button>
+    <button type="button" class="btn btn-info btn-sm mark-installed" title="{% trans "Mark Installed" %}" data-url="{% url 'dcim-api:cable-detail' pk=cable.pk %}">
+    <i class="mdi mdi-lan-connect" aria-hidden="true"></i>
+    </button>
 {% endif %}
 {% endif %}

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff