Skip to content

Commit 2c5c0f5

Browse files
committed
Merge changes from netbox-community#85 and remove my changes
1 parent 48c0c23 commit 2c5c0f5

File tree

2 files changed

+17
-84
lines changed

2 files changed

+17
-84
lines changed

netbox_floorplan/static/netbox_floorplan/floorplan/edit.js

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,83 +23,9 @@ var record_type = document.getElementById('record_type').value;
2323
var site_id = document.getElementById('site_id').value;
2424
var location_id = document.getElementById('location_id').value;
2525

26-
// Wait for DOM to be ready before making htmx calls
27-
document.addEventListener('DOMContentLoaded', function() {
28-
// Check if target elements exist before making htmx requests
29-
if (document.getElementById('rack-card')) {
30-
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, {
31-
source: '#rack-card',
32-
target: '#rack-card',
33-
swap: 'innerHTML',
34-
trigger: 'load'
35-
});
36-
}
37-
38-
if (document.getElementById('unrack-card')) {
39-
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, {
40-
source: '#unrack-card',
41-
target: '#unrack-card',
42-
swap: 'innerHTML',
43-
trigger: 'load'
44-
});
45-
}
46-
});
4726

48-
// Add a click event listener as a backup to handle pagination (?per_page=)
49-
// and multiple result links query parameters (e.g ?page=2)
50-
// Fixes issue where attempting to click on pagination links or sort rack/device table
51-
// resulted in the table being malformed or not loading at all.
52-
// The function works by intercepting clicks on pagination links within the rack or device cards,
53-
// preventing the default action, and then constructing the correct URL (with any query parameters)
54-
// to fetch the updated table data using htmx. It ensures that the correct floorplan_id is
55-
// always included in the request.
56-
// This approach handles both relative URLs (e.g., ?page=2) and incorrect absolute URLs
57-
// (e.g., /plugins/floorplan/floorplans/<floorplan_id>/edit/?page=2) that may be generated by htmx.
58-
// Assistance from Github Copilot used to help create this function
59-
document.addEventListener('click', function(evt) {
60-
// Check if the clicked element is within a rack or device card and is a pagination link
61-
const link = evt.target?.closest('a[href]');
62-
// Check if the link has an htmx request or a relative URL
63-
if (link && link.getAttribute('hx-get')) {
64-
// Get the container element (rack-card or unrack-card)
65-
const container = link.closest('#rack-card, #unrack-card');
66-
if (container) {
67-
// Get the href attribute from the link
68-
const href = link.getAttribute('hx-get') || link.getAttribute('href');
69-
70-
// Check if it's a relative URL or incorrect absolute URL
71-
if (href && (href.startsWith('?') || href.includes('/plugins/floorplan/floorplans/' + obj_pk + '/edit/'))) {
72-
evt.preventDefault();
73-
74-
// Extract query parameters
75-
const url = new URL(href, window.location.origin);
76-
const params = url.searchParams;
77-
78-
// Determine correct endpoint (rack or device)
79-
const baseEndpoint = container.id === 'rack-card' ? '/plugins/floorplan/floorplans/racks/' : '/plugins/floorplan/floorplans/devices/';
80-
81-
// Build correct URL
82-
const correctedUrl = new URL(baseEndpoint, window.location.origin);
83-
correctedUrl.searchParams.set('floorplan_id', obj_pk);
84-
85-
// Add other query parameters (e.g page or per_page) except 'floorplan_id'
86-
params.forEach((value, key) => {
87-
if (key !== 'floorplan_id') {
88-
correctedUrl.searchParams.set(key, value);
89-
}
90-
});
91-
92-
// Make the corrected htmx request to the rack or device page to grab the table data
93-
if (document.getElementById(container.id)) {
94-
htmx.ajax('GET', correctedUrl.toString(), {
95-
target: '#' + container.id,
96-
swap: 'innerHTML'
97-
});
98-
}
99-
}
100-
}
101-
}
102-
});
27+
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, { source: '#rack-card', target: '#rack-card', swap: 'innerHTML', trigger: 'load' })
28+
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, { source: '#unrack-card', target: '#unrack-card', swap: 'innerHTML', trigger: 'load' })
10329

10430

10531
fabric.Object.prototype.set({
@@ -529,12 +455,8 @@ function delete_floorplan_object() {
529455
}
530456
save_floorplan();
531457
setTimeout(() => {
532-
if (document.getElementById('rack-card')) {
533-
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, { target: '#rack-card', swap: 'innerHTML' });
534-
}
535-
if (document.getElementById('unrack-card')) {
536-
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, { target: '#unrack-card', swap: 'innerHTML' });
537-
}
458+
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, { target: '#rack-card', swap: 'innerHTML' });
459+
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, { target: '#unrack-card', swap: 'innerHTML' });
538460
}, 1500);
539461
});
540462
// Clear the selection after deletion

netbox_floorplan/tables.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from netbox.tables import NetBoxTable
44
from .models import Floorplan, FloorplanImage
5+
from functools import cached_property
56

67
from dcim.models import Rack, Device
78

@@ -32,8 +33,8 @@ class Meta(NetBoxTable.Meta):
3233

3334

3435
class FloorplanRackTable(NetBoxTable):
35-
3636
name = tables.LinkColumn()
37+
embedded = True
3738

3839
role = tables.TemplateColumn(
3940
# Show the role name if it exists, otherwise show "None" on the edit_floorplan view
@@ -59,6 +60,11 @@ class FloorplanRackTable(NetBoxTable):
5960
</div>
6061
""", orderable=False)
6162

63+
@cached_property
64+
def htmx_url(self):
65+
# no need to check for embedded as this table is always embedded
66+
return "/plugins/floorplan/floorplans/racks/"
67+
6268
class Meta(NetBoxTable.Meta):
6369
model = Rack
6470
# Show the Rack name, role, and U-height in the table
@@ -70,8 +76,8 @@ class Meta(NetBoxTable.Meta):
7076

7177

7278
class FloorplanDeviceTable(NetBoxTable):
73-
7479
name = tables.LinkColumn()
80+
embedded = True
7581

7682
actions = tables.TemplateColumn(template_code="""
7783
<div class="btn-group" role="group">
@@ -80,6 +86,11 @@ class FloorplanDeviceTable(NetBoxTable):
8086
</div>
8187
""", orderable=False)
8288

89+
@cached_property
90+
def htmx_url(self):
91+
# no need to check for embedded as this table is always embedded
92+
return "/plugins/floorplan/floorplans/devices/"
93+
8394
class Meta(NetBoxTable.Meta):
8495
model = Device
8596
fields = ('pk', 'name', 'device_type')

0 commit comments

Comments
 (0)