From 1e6aba1e7f726456996e739cbaffe644aea8df9a Mon Sep 17 00:00:00 2001 From: Degra02 Date: Sat, 2 Aug 2025 08:12:06 +0200 Subject: [PATCH] level clone wip --- src/c3nav/editor/static/editor/js/editor.js | 68 +++++++++++++++------ 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/src/c3nav/editor/static/editor/js/editor.js b/src/c3nav/editor/static/editor/js/editor.js index 0e3f5a59..ec3ad12d 100644 --- a/src/c3nav/editor/static/editor/js/editor.js +++ b/src/c3nav/editor/static/editor/js/editor.js @@ -940,6 +940,12 @@ editor = { editor._geometries_layer.addTo(editor.map); editor._highlight_layer.addTo(editor.map); editor._loading_geometry = false; + + // Initialize clone floor functionality now that geometries are loaded + if (editor.cloneFloor && editor.cloneFloor.init) { + editor.cloneFloor.init(); + } + if (editor._bounds_layer === null && editor._geometries_layer.getLayers().length) editor._bounds_layer = editor._geometries_layer; if (editor._next_zoom && editor._bounds_layer !== null) { editor.map.flyToBounds((editor._bounds_layer.getBounds !== undefined) ? editor._bounds_layer.getBounds() : [editor._bounds_layer.getLatLng(), editor._bounds_layer.getLatLng()], { @@ -2516,8 +2522,14 @@ editor.cloneFloor = { isSelectionMode: false, init: function() { + // This will be called after geometries are loaded // Check if clone floor elements exist in the template if ($('#clone-floor-btn').length > 0) { + // Unbind any existing handlers first + $('#clone-floor-btn').off('click'); + $('#execute-clone-btn').off('click'); + $('#cancel-clone-btn').off('click'); + // Bind click event to the button that's already in the template $('#clone-floor-btn').click(editor.cloneFloor.toggleSelectionMode); @@ -2549,17 +2561,24 @@ editor.cloneFloor = { $('#clone-floor-selector').show(); editor.cloneFloor.updateSelectedCount(); - // Add click handlers to geometry items + // Add click handlers directly to geometry layers if (editor._geometries_layer) { let layerCount = 0; editor._geometries_layer.eachLayer(function(layer) { if (layer.feature && layer.feature.properties) { + // Add click handler for selection layer.on('click', editor.cloneFloor.onItemClick); - layer.setStyle({cursor: 'pointer'}); + + // Make layer visually selectable + const currentStyle = layer.options || {}; + layer.setStyle(Object.assign({}, currentStyle, { + cursor: 'pointer', + opacity: Math.max(currentStyle.opacity || 0, 0.5) + })); layerCount++; } }); - console.log('Clone floor: Added click handlers to', layerCount, 'layers'); + console.log('Clone floor: Made', layerCount, 'geometries selectable'); } else { console.log('Clone floor: No geometries layer found'); } @@ -2575,28 +2594,33 @@ editor.cloneFloor = { $('#clone-floor-btn').html(' Clone to Floor').removeClass('btn-warning').addClass('btn-info'); $('#clone-floor-selector').hide(); - // Remove click handlers and reset styles + // Remove click handlers and reset styles for all geometry layers if (editor._geometries_layer) { editor._geometries_layer.eachLayer(function(layer) { if (layer.feature && layer.feature.properties) { + // Remove click handler layer.off('click', editor.cloneFloor.onItemClick); - layer.setStyle({cursor: 'default'}); + + // Reset to original style + layer.setStyle(editor._get_geometry_style(layer.feature)); } }); } // Re-enable map editing editor.map.doubleClickZoom.enable(); - - // Reset visual selection - editor.cloneFloor.updateVisualSelection(); }, onItemClick: function(e) { if (!editor.cloneFloor.isSelectionMode) return; - e.originalEvent.stopPropagation(); - e.originalEvent.preventDefault(); + // Prevent default behavior and stop propagation + if (e.originalEvent) { + e.originalEvent.stopPropagation(); + e.originalEvent.preventDefault(); + } + L.DomEvent.stopPropagation(e); + L.DomEvent.preventDefault(e); const layer = e.target; const feature = layer.feature; @@ -2605,13 +2629,14 @@ editor.cloneFloor = { if (!feature || !feature.properties) { console.log('Clone floor: No feature or properties found'); - return; + return false; } const itemId = feature.properties.id; const itemType = feature.properties.type; console.log('Clone floor: Item ID:', itemId, 'Type:', itemType); + console.log('Clone floor: Full feature properties:', JSON.stringify(feature.properties, null, 2)); // Check if item is already selected const existingIndex = editor.cloneFloor.selectedItems.findIndex( @@ -2633,6 +2658,8 @@ editor.cloneFloor = { editor.cloneFloor.updateSelectedCount(); editor.cloneFloor.updateVisualSelection(); + + return false; // Prevent further event propagation }, updateSelectedCount: function() { @@ -2661,8 +2688,10 @@ editor.cloneFloor = { layer.setStyle({ stroke: true, color: '#ff0000', - weight: 3, - fillOpacity: 0.7 + weight: 4, + opacity: 1, + fillOpacity: 0.7, + fillColor: '#ff0000' }); } } @@ -2703,8 +2732,11 @@ editor.cloneFloor = { keep_sync: keepSync }; - // Make API call + // Debug: Log detailed request data console.log('Clone floor: Making API call with data:', requestData); + console.log('Clone floor: Selected items details:', JSON.stringify(editor.cloneFloor.selectedItems, null, 2)); + console.log('Clone floor: Source level ID:', currentLevelId); + console.log('Clone floor: Target level ID:', parseInt(targetLevelId)); // Use the raw fetch API with better error handling c3nav_api.authenticated().then(function() { @@ -2734,9 +2766,11 @@ editor.cloneFloor = { }) .then(function(data) { console.log('Clone floor: API response data:', data); + console.log('Clone floor: API response type:', typeof data); + console.log('Clone floor: API response keys:', Object.keys(data)); if (data.success) { - alert(`Successfully cloned ${data.cloned_items.length} items: ${data.message}`); + alert(`Successfully cloned ${data.cloned_items?.length || 0} items: ${data.message}`); editor.cloneFloor.cancelSelection(); } else { alert(`Clone failed: ${data.message}`); @@ -2754,8 +2788,4 @@ editor.cloneFloor = { if ($('#sidebar').length) { editor.init(); - // Initialize clone floor functionality after editor is ready - setTimeout(function() { - editor.cloneFloor.init(); - }, 1000); }