Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 4 additions & 82 deletions netbox_floorplan/static/netbox_floorplan/floorplan/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,83 +23,9 @@ var record_type = document.getElementById('record_type').value;
var site_id = document.getElementById('site_id').value;
var location_id = document.getElementById('location_id').value;

// Wait for DOM to be ready before making htmx calls
document.addEventListener('DOMContentLoaded', function() {
// Check if target elements exist before making htmx requests
if (document.getElementById('rack-card')) {
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, {
source: '#rack-card',
target: '#rack-card',
swap: 'innerHTML',
trigger: 'load'
});
}

if (document.getElementById('unrack-card')) {
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, {
source: '#unrack-card',
target: '#unrack-card',
swap: 'innerHTML',
trigger: 'load'
});
}
});

// Add a click event listener as a backup to handle pagination (?per_page=)
// and multiple result links query parameters (e.g ?page=2)
// Fixes issue where attempting to click on pagination links or sort rack/device table
// resulted in the table being malformed or not loading at all.
// The function works by intercepting clicks on pagination links within the rack or device cards,
// preventing the default action, and then constructing the correct URL (with any query parameters)
// to fetch the updated table data using htmx. It ensures that the correct floorplan_id is
// always included in the request.
// This approach handles both relative URLs (e.g., ?page=2) and incorrect absolute URLs
// (e.g., /plugins/floorplan/floorplans/<floorplan_id>/edit/?page=2) that may be generated by htmx.
// Assistance from Github Copilot used to help create this function
document.addEventListener('click', function(evt) {
// Check if the clicked element is within a rack or device card and is a pagination link
const link = evt.target?.closest('a[href]');
// Check if the link has an htmx request or a relative URL
if (link && link.getAttribute('hx-get')) {
// Get the container element (rack-card or unrack-card)
const container = link.closest('#rack-card, #unrack-card');
if (container) {
// Get the href attribute from the link
const href = link.getAttribute('hx-get') || link.getAttribute('href');

// Check if it's a relative URL or incorrect absolute URL
if (href && (href.startsWith('?') || href.includes('/plugins/floorplan/floorplans/' + obj_pk + '/edit/'))) {
evt.preventDefault();

// Extract query parameters
const url = new URL(href, window.location.origin);
const params = url.searchParams;

// Determine correct endpoint (rack or device)
const baseEndpoint = container.id === 'rack-card' ? '/plugins/floorplan/floorplans/racks/' : '/plugins/floorplan/floorplans/devices/';

// Build correct URL
const correctedUrl = new URL(baseEndpoint, window.location.origin);
correctedUrl.searchParams.set('floorplan_id', obj_pk);

// Add other query parameters (e.g page or per_page) except 'floorplan_id'
params.forEach((value, key) => {
if (key !== 'floorplan_id') {
correctedUrl.searchParams.set(key, value);
}
});

// Make the corrected htmx request to the rack or device page to grab the table data
if (document.getElementById(container.id)) {
htmx.ajax('GET', correctedUrl.toString(), {
target: '#' + container.id,
swap: 'innerHTML'
});
}
}
}
}
});
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, { source: '#rack-card', target: '#rack-card', swap: 'innerHTML', trigger: 'load' })
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, { source: '#unrack-card', target: '#unrack-card', swap: 'innerHTML', trigger: 'load' })


fabric.Object.prototype.set({
Expand Down Expand Up @@ -529,12 +455,8 @@ function delete_floorplan_object() {
}
save_floorplan();
setTimeout(() => {
if (document.getElementById('rack-card')) {
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, { target: '#rack-card', swap: 'innerHTML' });
}
if (document.getElementById('unrack-card')) {
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, { target: '#unrack-card', swap: 'innerHTML' });
}
htmx.ajax('GET', `/plugins/floorplan/floorplans/racks/?floorplan_id=${obj_pk}`, { target: '#rack-card', swap: 'innerHTML' });
htmx.ajax('GET', `/plugins/floorplan/floorplans/devices/?floorplan_id=${obj_pk}`, { target: '#unrack-card', swap: 'innerHTML' });
}, 1500);
});
// Clear the selection after deletion
Expand Down
2 changes: 1 addition & 1 deletion netbox_floorplan/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.0"
__version__ = "0.8.0"