loader.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Petit loader pour charger les modules en environnement sans bundler
  2. (function() {
  3. function loadScript(src, cb) {
  4. var s = document.createElement('script');
  5. s.src = src;
  6. s.onload = cb;
  7. s.async = false;
  8. document.head.appendChild(s);
  9. }
  10. // Attendre que le DOM et Bootstrap soient prêts
  11. function whenReady(callback) {
  12. if (document.readyState === 'loading') {
  13. document.addEventListener('DOMContentLoaded', callback);
  14. } else {
  15. callback();
  16. }
  17. }
  18. whenReady(function() {
  19. // Attendre que Bootstrap soit disponible (quelques ms si nécessaire)
  20. function waitForBootstrap(callback, attempts) {
  21. attempts = attempts || 0;
  22. if (window.bootstrap && window.bootstrap.Dropdown) {
  23. callback();
  24. } else if (attempts < 20) {
  25. setTimeout(function() {
  26. waitForBootstrap(callback, attempts + 1);
  27. }, 50);
  28. } else {
  29. console.warn('[loader.js] Bootstrap non disponible après 1s, chargement sans attendre');
  30. callback();
  31. }
  32. }
  33. waitForBootstrap(function() {
  34. // Charger d'abord les helpers core, puis UI, puis editor
  35. loadScript('/assets/js/core/dom-ready.js', function() {
  36. loadScript('/assets/js/ui/header.js', function() {
  37. // header initialisé
  38. // Forcer une réinitialisation des dropdowns après un court délai
  39. setTimeout(function() {
  40. if (window.initDropdownsManually) {
  41. window.initDropdownsManually();
  42. }
  43. }, 100);
  44. });
  45. // Charger l'éditeur seulement si la page contient un canvas de carte
  46. if (document.getElementById('mapCanvas')) {
  47. loadScript('/assets/js/editor/map-editor.js', function() {
  48. // map-editor chargé
  49. });
  50. }
  51. });
  52. });
  53. });
  54. })();