274 lines
No EOL
9.5 KiB
JavaScript
274 lines
No EOL
9.5 KiB
JavaScript
|
|
|
|
const map = new maplibregl.Map({
|
|
style: window.MAP_STYLE_LIGHT,
|
|
center: [feature_lon, feature_lat],
|
|
zoom: 17,
|
|
pitch: 45,
|
|
bearing: -17.6,
|
|
container: 'map',
|
|
antialias: true
|
|
});
|
|
|
|
map.on('load', () => {
|
|
|
|
const location_data = {
|
|
type: 'FeatureCollection',
|
|
features: [
|
|
{
|
|
type: 'Feature',
|
|
geometry: feature_geom,
|
|
properties: {
|
|
name: '...'
|
|
}
|
|
}
|
|
]
|
|
};
|
|
|
|
// Add the circle as a GeoJSON source
|
|
map.addSource('location', {
|
|
type: 'geojson',
|
|
data: location_data
|
|
});
|
|
|
|
map.addLayer({
|
|
'id': 'location',
|
|
'type': 'fill',
|
|
'source': 'location',
|
|
'paint': {
|
|
'fill-color': '#0094ff',
|
|
'fill-opacity': 0.7,
|
|
}
|
|
});
|
|
|
|
const bounds = turf.bbox(location_data);
|
|
map.fitBounds(bounds, {
|
|
duration: 10
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const fileInput = document.getElementById('fileInput');
|
|
const fileContainer = document.getElementById('fileFieldsContainer');
|
|
|
|
// Configuration pour l'ancien système (fallback)
|
|
const documentTypeChoices = {
|
|
'as_built': 'As Built',
|
|
'cme': 'CME',
|
|
'pis': 'PIS',
|
|
'inspection_ce': 'Inspection G.C.',
|
|
'inspection_em': 'Inspection E.M.',
|
|
'other': 'Autre'
|
|
};
|
|
|
|
// Récupérer la configuration du nouveau système de gestion documentaire
|
|
let documentConfig = { use_managed_documents: false, can_add_unique_tag_documents: false, config_url: null };
|
|
let availableTags = [];
|
|
let availableFolders = [];
|
|
|
|
try {
|
|
const configEl = document.getElementById('documentConfigData');
|
|
if (configEl) {
|
|
documentConfig = JSON.parse(configEl.textContent);
|
|
}
|
|
} catch (e) {
|
|
console.log('Document config not found, using legacy system');
|
|
}
|
|
|
|
// Charger la configuration complète via l'API
|
|
async function loadDocumentConfig() {
|
|
if (!documentConfig.config_url) return;
|
|
|
|
try {
|
|
const response = await fetch(documentConfig.config_url);
|
|
const data = await response.json();
|
|
|
|
documentConfig.use_managed_documents = data.use_managed_documents;
|
|
documentConfig.can_add_unique_tag_documents = data.can_add_unique_tag_documents;
|
|
availableTags = data.tags || [];
|
|
availableFolders = data.folders || [];
|
|
|
|
// Exposer globalement pour le gestionnaire de fichiers
|
|
window.availableTags = availableTags;
|
|
window.availableFolders = availableFolders;
|
|
} catch (e) {
|
|
console.log('Failed to load document config:', e);
|
|
}
|
|
}
|
|
|
|
// Charger la config au démarrage
|
|
loadDocumentConfig();
|
|
|
|
function renderFileFields() {
|
|
if (!fileContainer) return;
|
|
|
|
fileContainer.innerHTML = '';
|
|
|
|
const files = Array.from(fileInput.files);
|
|
files.forEach((file, index) => {
|
|
const row = document.createElement('div');
|
|
row.classList.add('d-flex', 'align-items-center', 'gap-3', 'mb-2', 'p-2', 'border', 'rounded');
|
|
|
|
// Nom du fichier (en lecture seule)
|
|
const fileName = document.createElement('span');
|
|
fileName.classList.add('flex-grow-1', 'text-truncate');
|
|
fileName.style.maxWidth = '200px';
|
|
fileName.textContent = file.name;
|
|
fileName.title = file.name;
|
|
row.appendChild(fileName);
|
|
|
|
if (documentConfig.use_managed_documents && availableTags && availableTags.length > 0) {
|
|
// Nouveau système: select pour le tag
|
|
const tagSelect = document.createElement('select');
|
|
tagSelect.name = `tag_${index}`;
|
|
tagSelect.classList.add('form-select', 'w-auto');
|
|
tagSelect.style.minWidth = '150px';
|
|
|
|
const defaultOption = document.createElement('option');
|
|
defaultOption.value = '';
|
|
defaultOption.textContent = '-- Choisir un tag --';
|
|
tagSelect.appendChild(defaultOption);
|
|
|
|
availableTags.forEach(tag => {
|
|
const option = document.createElement('option');
|
|
option.value = tag.slug;
|
|
option.textContent = tag.name;
|
|
option.dataset.isUnique = tag.is_unique;
|
|
// Désactiver les tags uniques si l'utilisateur n'a pas les permissions
|
|
if (tag.is_unique && !documentConfig.can_add_unique_tag_documents) {
|
|
option.disabled = true;
|
|
option.textContent += ' (non autorisé)';
|
|
}
|
|
tagSelect.appendChild(option);
|
|
});
|
|
row.appendChild(tagSelect);
|
|
|
|
// Select pour le répertoire (affiché uniquement pour les tags non-uniques)
|
|
const folderSelect = document.createElement('select');
|
|
folderSelect.name = `folder_${index}`;
|
|
folderSelect.classList.add('form-select', 'w-auto', 'd-none');
|
|
folderSelect.style.minWidth = '180px';
|
|
|
|
if (availableFolders && availableFolders.length > 0) {
|
|
availableFolders.forEach(folder => {
|
|
const option = document.createElement('option');
|
|
option.value = folder.id;
|
|
option.textContent = folder.path;
|
|
if (folder.is_root) {
|
|
option.selected = true;
|
|
}
|
|
folderSelect.appendChild(option);
|
|
});
|
|
}
|
|
row.appendChild(folderSelect);
|
|
|
|
// Afficher/masquer le sélecteur de répertoire selon le tag
|
|
tagSelect.addEventListener('change', function() {
|
|
const selectedOption = tagSelect.options[tagSelect.selectedIndex];
|
|
const isUnique = selectedOption.dataset.isUnique === 'true' || selectedOption.dataset.isUnique === 'True';
|
|
|
|
if (isUnique || tagSelect.value === '') {
|
|
folderSelect.classList.add('d-none');
|
|
} else {
|
|
folderSelect.classList.remove('d-none');
|
|
}
|
|
});
|
|
|
|
} else {
|
|
// Ancien système: select type de document
|
|
const select = document.createElement('select');
|
|
select.name = `document_type_${index}`;
|
|
select.classList.add('form-select', 'w-25');
|
|
for (const [value, label] of Object.entries(documentTypeChoices)) {
|
|
const option = document.createElement('option');
|
|
option.value = value;
|
|
option.textContent = label;
|
|
if (value === 'other') {
|
|
option.selected = true;
|
|
}
|
|
select.appendChild(option);
|
|
}
|
|
row.appendChild(select);
|
|
}
|
|
|
|
// Bouton de suppression
|
|
const deleteBtn = document.createElement('button');
|
|
deleteBtn.type = 'button';
|
|
deleteBtn.classList.add('btn', 'btn-outline-danger', 'btn-sm');
|
|
deleteBtn.innerHTML = '<i class="bi bi-x-lg"></i>';
|
|
deleteBtn.onclick = () => {
|
|
files.splice(index, 1);
|
|
const dt = new DataTransfer();
|
|
files.forEach(f => dt.items.add(f));
|
|
fileInput.files = dt.files;
|
|
renderFileFields(); // re-render
|
|
};
|
|
row.appendChild(deleteBtn);
|
|
|
|
fileContainer.appendChild(row);
|
|
});
|
|
|
|
// Total
|
|
const countInput = document.createElement('input');
|
|
countInput.type = 'hidden';
|
|
countInput.name = 'file_count';
|
|
countInput.value = files.length;
|
|
fileContainer.appendChild(countInput);
|
|
}
|
|
|
|
if (fileInput) {
|
|
fileInput.addEventListener('change', renderFileFields);
|
|
}
|
|
|
|
|
|
const deleteDocumentModal = document.getElementById('documentDeleteModal');
|
|
if (deleteDocumentModal) {
|
|
deleteDocumentModal.addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const fileName = button.getAttribute('data-filename');
|
|
const deleteUrl = button.getAttribute('data-delete-url');
|
|
|
|
const fileNameContainer = document.getElementById('deleteDocumentFileName');
|
|
const deleteForm = document.getElementById('deleteDocumentForm');
|
|
|
|
fileNameContainer.textContent = fileName;
|
|
deleteForm.action = deleteUrl;
|
|
});
|
|
}
|
|
|
|
historyModal = document.getElementById('documentHistoryModal');
|
|
if (historyModal) {
|
|
document.getElementById('documentHistoryModal').addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const type = button.getAttribute('data-document-type');
|
|
const tagSlug = button.getAttribute('data-tag-slug');
|
|
const locationId = button.getAttribute('data-location-id');
|
|
const useManaged = button.getAttribute('data-use-managed') === 'true';
|
|
|
|
let url;
|
|
if (useManaged && tagSlug) {
|
|
// Nouveau système de gestion documentaire
|
|
url = `${document_history_url}?structure_id=${locationId}&tag_slug=${tagSlug}`;
|
|
} else {
|
|
// Ancien système
|
|
url = `${document_history_url}?structure_id=${locationId}&document_type=${type}`;
|
|
}
|
|
|
|
fetch(url)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
document.getElementById('documentHistoryContent').innerHTML = data.html;
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
document.addEventListener('click', (e) => {
|
|
const btn = e.target.closest('[data-bs-toggle="collapse"]');
|
|
if (!btn) return;
|
|
const target = document.querySelector(btn.getAttribute('data-bs-target'));
|
|
if (!target) return;
|
|
target.addEventListener('shown.bs.collapse', () => btn.textContent = '▾', {once:true});
|
|
target.addEventListener('hidden.bs.collapse', () => btn.textContent = '▸', {once:true});
|
|
}); |