@@ -23,83 +23,9 @@ var record_type = document.getElementById('record_type').value;
2323var site_id = document . getElementById ( 'site_id' ) . value ;
2424var 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
10531fabric . 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
0 commit comments