Aller au contenu

« Widget:SinametFormIFrame » : différence entre les versions

De Observatoire du réemploi des PMCB en Charente
Aucun résumé des modifications
Aucun résumé des modifications
 
(5 versions intermédiaires par le même utilisateur non affichées)
Ligne 35 : Ligne 35 :
  <form id="dynamicForm" class="form-grid"></form>
  <form id="dynamicForm" class="form-grid"></form>


  <center><button id="executeBtn" style="margin-top: 2rem;">Exécuter</button></center><br><br>
  <center><button id="executeBtn" style="margin-top: 2rem;">Visualiser</button></center><br>


  <iframe id="monIframe" width="100%" height="600" src="<!--{$envionurl|escape:'html'}-->/include/iframe/<!--{$module|escape:'html'}/?territory=16&typepermis=PCLOG" frameborder="0"></iframe>
  <iframe id="monIframe" width="100%" height="600" src="<!--{$environurl|escape:'html'}-->/include/iframe/<!--{$module|escape:'html'}-->/" frameborder="0" scrolling="no"></iframe>


  <script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
  <script>
  <script>
// https://cors-anywhere.herokuapp.com/
// https://cors-anywhere.herokuapp.com/
   const API_URL = "https://app.sinamet.tech/environ/obsrpmcb16/include/param/or16_general/grf_permis_construire/";  
   const API_URL = "<!--{$environurl|escape:'html'}-->/include/param/<!--{$module|escape:'html'}-->/";  
   const BASE_IFRAME_URL = "https://app.sinamet.tech/environ/obsrpmcb16/include/iframe/or16_general/grf_permis_construire/";
   const BASE_IFRAME_URL = "<!--{$environurl|escape:'html'}-->/include/iframe/<!--{$module|escape:'html'}-->/";


   async function loadForm() {
   async function loadForm() {

Dernière version du 17 août 2025 à 15:54

 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css" />
 <style>
   body {
     font-family: Arial, sans-serif;
     padding: 2rem;
   }
   .form-grid {
     display: grid;
     grid-template-columns: repeat(3, 1fr);
     gap: 1rem;
     align-items: start;
   }
   .form-section-title {
     grid-column: span 3;
     font-weight: bold;
     font-size: 1.2em;
     margin-top: 1em;
   }
   .form-group {
     display: flex;
     flex-direction: column;
   }
   .form-group label {
     margin-bottom: 0.5rem;
   }
   #output {
     margin-top: 2rem;
     font-weight: bold;
     color: green;
     word-break: break-all;
   }
 </style>
<form id="dynamicForm" class="form-grid"></form>
<button id="executeBtn" style="margin-top: 2rem;">Visualiser</button>

<iframe id="monIframe" width="100%" height="600" src="/include/iframe//" frameborder="0" scrolling="no"></iframe>
<script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
<script>

// https://cors-anywhere.herokuapp.com/

 const API_URL = "/include/param//"; 
 const BASE_IFRAME_URL = "/include/iframe//";
 async function loadForm() {
   const response = await fetch(API_URL);
   const data = await response.json();
   const form = document.getElementById("dynamicForm");
     console.log(data);
     const fields = Object.values(data)[0];
     fields.forEach(field => {
     const type = field.typefield;
     if (type === "empty") {
       const div = document.createElement("div");
       form.appendChild(div);
       return;
     }
     if (type === "title") {
       const title = document.createElement("div");
       title.className = "form-section-title";
       title.textContent = field.label;
       form.appendChild(title);
       return;
     }
     const group = document.createElement("div");
     group.className = "form-group";
     const label = document.createElement("label");
     label.textContent = field.label || field.namefield;
     label.htmlFor = field.namefield;
     group.appendChild(label);
     if (type === "text") {
       const input = document.createElement("input");
       input.type = "text";
       input.name = field.namefield;
       input.id = field.namefield;
       input.value = field.defaultvalue || "";
       group.appendChild(input);
     }
     if (type === "select") {
       const select = document.createElement("select");
       select.name = field.namefield;
       select.id = field.namefield;
       const values = field.values || [];
       const returnValues = field.returnvalues || values;
       for (let i = 0; i < values.length; i++) {
         const option = document.createElement("option");
         option.value = returnValues[i];
         option.textContent = values[i];
         select.appendChild(option);
       }
       group.appendChild(select);
       new Choices(select, { shouldSort: false, searchEnabled: true });
     }
     if (type === "checkbox") {
       const checkbox = document.createElement("input");
       checkbox.type = "checkbox";
       checkbox.name = field.namefield;
       checkbox.id = field.namefield;
       checkbox.checked = field.defaultvalue || false;
       group.appendChild(checkbox);
     }
     if (type === "int-slider") {
       const input = document.createElement("input");
       input.type = "range";
       input.name = field.namefield;
       input.id = field.namefield;
       input.min = field.rangevalue[0];
       input.max = field.rangevalue[1];
       input.value = field.defaultvalue || 0;
       const valDisplay = document.createElement("span");
       valDisplay.textContent = input.value + " km";
       input.oninput = () => valDisplay.textContent = input.value + " km";
       group.appendChild(input);
       group.appendChild(valDisplay);
     }
     form.appendChild(group);
   });
 }
 function getFormValues() {
   const form = document.getElementById("dynamicForm");
   const elements = form.elements;
   const result = {};
   for (let el of elements) {
     if (!el.name) continue;
     if (el.type === "checkbox") {
       result[el.name] = el.checked;
     } else {
       result[el.name] = el.value;
     }
   }
   return result;
 }
 function buildUrlFromParams(base, params) {
   const query = Object.entries(params)
     .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
     .join("&");
   return `${base}?${query}`;
 }
 document.getElementById("executeBtn").addEventListener("click", () => {
   const values = getFormValues();
   const url = buildUrlFromParams(BASE_IFRAME_URL, values);
   const iframe = document.getElementById('monIframe');
   if (url && url.startsWith('http')) {
       iframe.src = url;
     } else {
       alert("Veuillez entrer une URL valide (commençant par http)");
     }
   //document.getElementById("output").textContent = "URL générée : " + url;
 });
 loadForm();

</script>