osm-labo/assets/app.js

274 lines
8.5 KiB
JavaScript
Raw Normal View History

/*
* Welcome to your app's main JavaScript file!
*
* We recommend including the built version of this JavaScript file
* (and its CSS file) in your base layout (base.html.twig).
*/
// any CSS you import will output into a single css file (app.css in this case)
import './styles/app.css';
2025-06-21 10:26:55 +02:00
// start the Stimulus application
// import './bootstrap';
2025-06-06 13:26:44 +02:00
import './utils.js';
import './opening_hours.js';
import './josm.js';
2025-06-06 23:28:35 +02:00
import './edit.js';
2025-05-28 17:05:34 +02:00
2025-06-21 10:26:55 +02:00
import Chart from 'chart.js/auto';
2025-06-21 11:28:31 +02:00
import ChartDataLabels from 'chartjs-plugin-datalabels';
import { genererCouleurPastel, setupCitySearch, handleAddCityFormSubmit, enableLabourageForm, getLabourerUrl, adjustListGroupFontSize } from './utils.js';
import Tablesort from 'tablesort';
2025-06-21 10:26:55 +02:00
window.Chart = Chart;
2025-06-21 11:28:31 +02:00
window.genererCouleurPastel = genererCouleurPastel;
window.setupCitySearch = setupCitySearch;
window.handleAddCityFormSubmit = handleAddCityFormSubmit;
window.getLabourerUrl = getLabourerUrl;
Chart.register(ChartDataLabels);
function initDashboardChart() {
const chartCanvas = document.getElementById('statsBubbleChart');
if (!chartCanvas) {
return;
}
if (!chartCanvas.dataset.stats || chartCanvas.dataset.stats.length <= 2) { // <= 2 pour ignorer '[]'
console.log("Les données du graphique sont vides ou absentes, le graphique ne sera pas affiché.");
return;
}
const statsData = JSON.parse(chartCanvas.dataset.stats);
if (statsData && statsData.length > 0) {
const bubbleChartData = statsData.map(stat => {
const population = parseInt(stat.population, 10);
const placesCount = parseInt(stat.placesCount, 10);
const ratio = population > 0 ? (placesCount / population) * 1000 : 0;
return {
x: population,
y: ratio,
r: Math.sqrt(placesCount) * 2,
label: stat.name,
completion: stat.completionPercent || 0
};
});
new Chart(chartCanvas.getContext('2d'), {
type: 'bubble',
data: {
datasets: [{
label: 'Villes',
data: bubbleChartData,
backgroundColor: bubbleChartData.map(d => `rgba(255, 99, 132, ${d.completion / 100})`),
borderColor: 'rgba(255, 99, 132, 1)',
}]
},
options: {
responsive: true,
plugins: {
datalabels: {
anchor: 'center',
align: 'center',
color: '#000',
font: {
weight: 'bold'
},
formatter: (value, context) => {
return context.dataset.data[context.dataIndex].label;
}
},
legend: {
display: false
},
tooltip: {
callbacks: {
label: (context) => {
const d = context.raw;
return [
`${d.label}`,
`Population: ${d.x.toLocaleString()}`,
`Lieux / 1000 hab: ${d.y.toFixed(2)}`,
`Total lieux: ${Math.round(Math.pow(d.r / 2, 2))}`,
`Complétion: ${d.completion}%`
];
}
}
}
},
scales: {
x: {
type: 'logarithmic',
title: {
display: true,
text: 'Population (échelle log)'
}
},
y: {
title: {
display: true,
text: 'Commerces pour 1000 habitants'
}
}
}
}
});
}
}
2025-05-28 17:05:34 +02:00
// Attendre le chargement du DOM
document.addEventListener('DOMContentLoaded', () => {
console.log('DOMContentLoaded');
2025-06-01 19:52:56 +02:00
2025-06-07 00:23:36 +02:00
2025-05-28 17:05:34 +02:00
const randombg = genererCouleurPastel();
// Appliquer la couleur au body
2025-06-07 00:23:36 +02:00
document.querySelectorAll('body, .edit-land, .body-landing').forEach(element => {
element.style.backgroundColor = randombg;
});
2025-05-28 17:05:34 +02:00
2025-05-28 17:15:47 +02:00
// Gestion du bouton pour afficher tous les champs
const btnShowAllFields = document.querySelector('#showAllFields');
if (btnShowAllFields) {
console.log('btnShowAllFields détecté');
btnShowAllFields.addEventListener('click', () => {
// Sélectionner tous les inputs dans le formulaire
const form = document.querySelector('form');
if (form) {
2025-06-07 00:23:36 +02:00
// Sélectionner tous les inputs sauf #validation_messages
const hiddenInputs = form.querySelectorAll('#advanced_tags');
2025-05-28 17:15:47 +02:00
hiddenInputs.forEach(input => {
input.classList.toggle('d-none');
});
}
});
}
2025-05-28 17:27:48 +02:00
const btnClosedCommerce = document.querySelector('#closedCommerce');
if (btnClosedCommerce) {
btnClosedCommerce.addEventListener('click', () => {
if (!confirm('Êtes-vous sûr de vouloir signaler ce commerce comme fermé ?')) {
return;
}
window.location.href = '/closed_commerce/' + document.getElementById('app_public_closed_commerce').value;
});
}
2025-05-28 17:15:47 +02:00
openingHoursFormManager.init();
// Vérifier si l'élément avec l'ID 'userChangesHistory' existe avant d'appeler la fonction
if (document.getElementById('userChangesHistory')) {
listChangesets();
} else {
console.log('userChangesHistory non trouvé');
}
2025-05-29 16:50:25 +02:00
document.querySelectorAll('input[type="text"]').forEach(input => {
input.addEventListener('blur', updateCompletionProgress);
});
const form = document.querySelector('form')
if (form) {
form.addEventListener('submit', check_validity);
updateCompletionProgress()
}
2025-06-04 00:46:46 +02:00
updateCompletionProgress();
2025-05-29 16:50:25 +02:00
2025-06-21 11:28:31 +02:00
// Activer le tri sur tous les tableaux désignés
document.querySelectorAll('.js-sort-table').forEach(table => {
new Tablesort(table);
});
2025-05-29 17:49:35 +02:00
// Focus sur le premier champ texte au chargement
const firstTextInput = document.querySelector('input.form-control');
if (firstTextInput) {
firstTextInput.focus();
console.log('focus sur le premier champ texte', firstTextInput);
} else {
console.log('pas de champ texte trouvé');
}
2025-05-29 16:50:25 +02:00
2025-05-29 17:49:35 +02:00
parseCuisine();
2025-05-29 16:50:25 +02:00
// Tri automatique des tableaux
2025-06-21 11:28:31 +02:00
// const tables = document.querySelectorAll('table');
// tables.forEach(table => {
// table.classList.add('js-sort-table');
// });
2025-05-28 17:05:34 +02:00
2025-06-05 15:09:28 +02:00
// Modifier la fonction de recherche existante
const searchInput = document.getElementById('app_admin_labourer');
const suggestionList = document.getElementById('suggestionList');
if (searchInput && suggestionList) {
let timeoutId;
searchInput.addEventListener('input', () => {
clearTimeout(timeoutId);
const query = searchInput.value.trim();
if (query.length < 2) {
suggestionList.innerHTML = '';
return;
}
timeoutId = setTimeout(async () => {
const suggestions = await searchInseeCode(query);
suggestionList.innerHTML = '';
if (suggestions.length === 0) {
const li = document.createElement('li');
li.style.cssText = `
padding: 8px 12px;
color: #666;
font-style: italic;
`;
li.textContent = 'Aucun résultat trouvé';
suggestionList.appendChild(li);
return;
}
2025-06-05 15:09:28 +02:00
suggestions.forEach(suggestion => {
const li = document.createElement('li');
li.style.cssText = `
padding: 8px 12px;
cursor: pointer;
border-bottom: 1px solid #eee;
`;
li.textContent = suggestion.label;
li.addEventListener('mouseenter', () => {
li.style.backgroundColor = '#f0f0f0';
});
li.addEventListener('mouseleave', () => {
li.style.backgroundColor = 'white';
});
li.addEventListener('click', () => {
searchInput.value = suggestion.insee;
suggestionList.innerHTML = '';
labourer();
});
suggestionList.appendChild(li);
});
}, 300);
});
}
2025-06-21 11:28:31 +02:00
enableLabourageForm();
initDashboardChart();
adjustListGroupFontSize('.list-group-item');
});