diff --git a/loko/assets/static/assets/inspection_map.js b/loko/assets/static/assets/inspection_map.js index 02c4a3d..bbb3f11 100644 --- a/loko/assets/static/assets/inspection_map.js +++ b/loko/assets/static/assets/inspection_map.js @@ -1165,7 +1165,14 @@ } function handleAssetClick(feature) { - const props = feature.properties || {}; + const props = Object.assign({}, feature.properties || {}); + if (!props.id && !props.object_id && feature.id) { + props.id = feature.id; + props.object_id = feature.id; + } + if (!props.object_id && props.id) { + props.object_id = props.id; + } openInspectionModal(props); } @@ -1356,7 +1363,6 @@ modal.show(); } - window.openEditInspectionFromMap = function() { if (!window.currentMapInspectionId) return; const mapModalEl = document.getElementById('mapInspectionDetailModal'); @@ -1377,8 +1383,14 @@ if (!modalEl) return; let ctId = props.content_type_id || props.ct_id; - if (!ctId && (props.model_name === 'parkingspot' || props.spot_type || props.asset_type === 'parkingspot' || props.thematic === 'parking')) { - ctId = window.PARKING_SPOT_CT_ID; + if (!ctId) { + if (props.model_name === 'signpanel' || props.asset_type === 'signpanel') { + ctId = window.SIGN_PANEL_CT_ID; + } else if (props.model_name === 'signpole' || props.asset_type === 'signpole') { + ctId = window.SIGN_POLE_CT_ID; + } else if (props.model_name === 'parkingspot' || props.spot_type || props.asset_type === 'parkingspot' || props.thematic === 'parking') { + ctId = window.PARKING_SPOT_CT_ID; + } } const objId = props.object_id || props.id || props.pk || ''; document.getElementById('insp_content_type_id').value = ctId || ''; @@ -1414,16 +1426,43 @@ // Réinitialisation de l'accumulateur de photos/fichiers accumulatedInspectionFiles = []; + window.accumulatedInspectionFiles = accumulatedInspectionFiles; const previewContainer = document.getElementById('insp_photos_preview_container'); if (previewContainer) previewContainer.innerHTML = ''; + notifyPhotosUpdated(); const modal = new bootstrap.Modal(modalEl); modal.show(); }; let accumulatedInspectionFiles = []; + window.accumulatedInspectionFiles = accumulatedInspectionFiles; let inspCameraStream = null; + function notifyPhotosUpdated() { + try { + window.dispatchEvent(new CustomEvent('inspection-photos-updated', { + detail: { + files: accumulatedInspectionFiles, + photos: accumulatedInspectionFiles.filter(f => f && f.type && f.type.startsWith('image/')) + } + })); + } catch (e) { + console.warn('Error dispatching inspection-photos-updated event:', e); + } + } + + window.getInspectionPhotos = function() { + return (accumulatedInspectionFiles || []).filter(f => f && f.type && f.type.startsWith('image/')); + }; + + window.addInspectionPhoto = function(file) { + if (file && !accumulatedInspectionFiles.includes(file)) { + accumulatedInspectionFiles.push(file); + renderPhotoPreviews(); + } + }; + function initInspectionPhotoHandler() { const btnCamera = document.getElementById('insp_btn_continuous_camera'); const btnGallery = document.getElementById('insp_btn_gallery_files'); @@ -1571,10 +1610,10 @@ function renderPhotoPreviews() { const previewContainer = document.getElementById('insp_photos_preview_container'); - if (!previewContainer) return; - previewContainer.innerHTML = ''; + if (previewContainer) previewContainer.innerHTML = ''; accumulatedInspectionFiles.forEach((file, index) => { + if (!previewContainer) return; const col = document.createElement('div'); col.className = 'col-4 col-sm-3 position-relative mt-2'; @@ -1614,7 +1653,11 @@ col.appendChild(card); previewContainer.appendChild(col); }); + + window.renderInspPhotosPreview = renderPhotoPreviews; + notifyPhotosUpdated(); } + window.renderInspPhotosPreview = renderPhotoPreviews; } let newSignPhotos = []; @@ -1854,16 +1897,39 @@ body: formData }); - const data = await res.json(); + let data; + const contentType = res.headers.get('content-type') || ''; + if (contentType.includes('application/json')) { + data = await res.json(); + } else { + const errorText = await res.text(); + throw new Error(`Erreur serveur (${res.status}) : ${errorText.substring(0, 120)}`); + } + if (data.success) { modal.hide(); alert(data.message || 'Panneaux créés avec succès !'); + + // Basculer automatiquement sur la thématique Signalisation + localStorage.setItem(STORAGE_THEMATIC_KEY, 'sign'); + localStorage.removeItem(STORAGE_CATEGORY_KEY); + updateUIElements('sign', ''); + + if (data.lat && data.lon && map) { + const lat = parseFloat(data.lat); + const lon = parseFloat(data.lon); + if (!isNaN(lat) && !isNaN(lon)) { + map.flyTo({ center: [lon, lat], zoom: 19 }); + } + } + loadAssets(); + loadInspectionsHistory(); } else { alert('Erreur : ' + (data.message || 'Impossible de créer les panneaux.')); } } catch(err) { - alert('Erreur réseau : ' + err.message); + alert('Erreur lors de la création : ' + err.message); } finally { btnSubmit.disabled = false; btnSubmit.innerHTML = ' Créer et Enregistrer'; diff --git a/loko/assets/views/sign.py b/loko/assets/views/sign.py index 2488c95..af9edbe 100644 --- a/loko/assets/views/sign.py +++ b/loko/assets/views/sign.py @@ -526,6 +526,8 @@ def sign_assets_geojson(request): "id": obj.id, "geometry": geometry, "properties": { + "id": obj.id, + "object_id": obj.id, "code": obj.code, "name": obj.code, "category": obj.category.get_name() if obj.category else None, diff --git a/loko/inspections/templates/inspections/components/asset_fields/_signpanel.html b/loko/inspections/templates/inspections/components/asset_fields/_signpanel.html index d66f380..5d230e4 100644 --- a/loko/inspections/templates/inspections/components/asset_fields/_signpanel.html +++ b/loko/inspections/templates/inspections/components/asset_fields/_signpanel.html @@ -15,35 +15,84 @@