From c1b7b40e5d8f73474bec21d028a5e0282922e138 Mon Sep 17 00:00:00 2001 From: Konrad Hyzy Date: Fri, 30 Oct 2020 11:34:30 +0100 Subject: [PATCH 01/28] =?UTF-8?q?Fix=20Choisissez=20un=20motif=20de=20d?= =?UTF-8?q?=C3=A9placement=20-=20en=20dark=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/js/form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/form.js b/src/js/form.js index c78ba791..fee83cd2 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -103,7 +103,7 @@ const createReasonFieldset = (reasonsData) => { const appendToFieldset = appendTo(fieldset) const legendAttrs = { - className: 'legend titre 3 ', + className: 'legend titre-3 ', innerHTML: 'Choisissez un motif de déplacement', } const legend = createElement('legend', legendAttrs) From 54219121b1c8dbc1e3a02e2521d476c0bd9ee8d2 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 13:58:46 +0100 Subject: [PATCH 02/28] Add param support --- src/form-data.json | 2 ++ src/js/form.js | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/form-data.json b/src/form-data.json index 1d43b2d6..ddbf257a 100644 --- a/src/form-data.json +++ b/src/form-data.json @@ -2,6 +2,7 @@ [ { "key": "firstname", + "alias": "prenom", "type": "text", "contentType": "firstname", "label": "Prénom", @@ -10,6 +11,7 @@ }, { "key": "lastname", + "alias": "nom", "type": "text", "contentType": "lastname", "label": "Nom", diff --git a/src/js/form.js b/src/js/form.js index fee83cd2..42b6d5c8 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -6,6 +6,8 @@ import formData from '../form-data.json' import { $, appendTo, createElement } from './dom-utils' +const params = new URLSearchParams(window.location.search) + const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) const p = createElement('p', { className: 'msg-info', innerHTML: 'Tous les champs sont obligatoires.' }) @@ -53,6 +55,11 @@ const createFormGroup = ({ type, } + if (params.get(name)) { + inputAttrs.value = params.get(name) + if (name.includes('date') || name.includes('heure')) inputAttrs.value = new Date(params.get(name)).toISOString().slice(0, 10) + } + console.log(inputAttrs.value) const input = createElement('input', inputAttrs) const validityAttrs = { @@ -141,10 +148,12 @@ export function createForm () { .filter(field => !field.isHidden) .map((field, index) => { + // Permet de changer la clé devant être présente dans l'URI en ajoutant la propriété 'alias' + const name = field.alias || field.key const formGroup = createFormGroup({ autofocus: index === 0, ...field, - name: field.key, + name, }) return formGroup From 06c681cefc8e448cc46695468dab28f35de0a19d Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 15:52:47 +0100 Subject: [PATCH 03/28] Switch to URI fragments & Add aliases --- src/form-data.json | 8 ++++++++ src/js/form.js | 17 ++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/form-data.json b/src/form-data.json index ddbf257a..a3a92484 100644 --- a/src/form-data.json +++ b/src/form-data.json @@ -20,6 +20,7 @@ }, { "key": "birthday", + "alias": "date-de-naissance", "type": "text", "contentType": "birthday", "label": "Date de naissance", @@ -32,6 +33,7 @@ [ { "key": "placeofbirth", + "alias": "lieu-de-naissance", "type": "text", "contentType": "cityofbirth", "label": "Lieu de naissance", @@ -40,6 +42,7 @@ }, { "key": "address", + "alias": "adresse", "type": "text", "contentType": "address", "label": "Adresse", @@ -48,6 +51,7 @@ }, { "key": "city", + "alias": "ville", "type": "text", "contentType": "city", "label": "Ville", @@ -56,6 +60,7 @@ }, { "key": "zipcode", + "alias": "code-postal", "type": "number", "contentType": "zipcode", "label": "Code Postal", @@ -86,6 +91,7 @@ }, { "key": "datesortie", + "alias": "date", "type": "date", "contentType": "datesortie", "label": "Date de sortie", @@ -94,6 +100,7 @@ }, { "key": "heuresortie", + "alias": "heure", "type": "time", "contentType": "heuresortie", "label": "Heure de sortie", @@ -101,6 +108,7 @@ }, { "key": "reason", + "alias": "raison", "type": "list", "items": [ { diff --git a/src/js/form.js b/src/js/form.js index 42b6d5c8..05320f05 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -6,7 +6,16 @@ import formData from '../form-data.json' import { $, appendTo, createElement } from './dom-utils' -const params = new URLSearchParams(window.location.search) +// Nettoie les URI fragments +// eslint-disable-next-line prefer-const +let hashParams = {} +window.location.hash.substr(1).split(';').forEach(elem => { + elem = decodeURI(elem) + const I = elem.indexOf(':') + const parts = [elem.slice(0, I), elem.slice(I + 1)] + hashParams[parts[0]] = parts[1] +}) +console.log(hashParams) const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) @@ -55,11 +64,9 @@ const createFormGroup = ({ type, } - if (params.get(name)) { - inputAttrs.value = params.get(name) - if (name.includes('date') || name.includes('heure')) inputAttrs.value = new Date(params.get(name)).toISOString().slice(0, 10) + if (hashParams[name]) { + inputAttrs.value = hashParams[name] } - console.log(inputAttrs.value) const input = createElement('input', inputAttrs) const validityAttrs = { From 8703b26f2ee3b93021cf45a536b2c504a9398a49 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 16:06:18 +0100 Subject: [PATCH 04/28] Remove log I forgot to remove this console.log() --- src/js/form.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/js/form.js b/src/js/form.js index 05320f05..59540308 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -15,7 +15,6 @@ window.location.hash.substr(1).split(';').forEach(elem => { const parts = [elem.slice(0, I), elem.slice(I + 1)] hashParams[parts[0]] = parts[1] }) -console.log(hashParams) const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) @@ -64,7 +63,7 @@ const createFormGroup = ({ type, } - if (hashParams[name]) { + if (hashParams[name] && !name.toUpperCase().includes('CREATION')) { inputAttrs.value = hashParams[name] } const input = createElement('input', inputAttrs) From 73b591c316b6667846af08ed531e06ef106d578a Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 16:44:11 +0100 Subject: [PATCH 05/28] Remove useless security The 'creation time' vars didn't need a guard since they were not evaluated --- src/js/form.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/js/form.js b/src/js/form.js index f5798d3e..8c152208 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -63,9 +63,7 @@ const createFormGroup = ({ type, } - if (hashParams[name] && !name.toUpperCase().includes('CREATION')) { - inputAttrs.value = hashParams[name] - } + if (hashParams[name]) inputAttrs.value = hashParams[name] const input = createElement('input', inputAttrs) const validityAttrs = { From fb28302c3abc4e247ed7ab008ffd60917812aed4 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 17:15:30 +0100 Subject: [PATCH 06/28] Add support for reasons --- src/form-data.json | 1 + src/js/form.js | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/form-data.json b/src/form-data.json index 40edfcc3..7bffb6cc 100644 --- a/src/form-data.json +++ b/src/form-data.json @@ -133,6 +133,7 @@ }, { "code": "sport_animaux", + "alias": "sport-animaux", "label": "Déplacements brefs, dans la limite d'une heure quotidienne et dans un rayon maximal d'un kilomètre autour du domicile, liés soit à l'activité physique individuelle des personnes, à l'exclusion de toute pratique sportive collective et de toute proximité avec d'autres personnes, soit à la promenade avec les seules personnes regroupées dans un même domicile, soit aux besoins des animaux de compagnie ;" }, { diff --git a/src/js/form.js b/src/js/form.js index 8c152208..0cde4538 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -13,6 +13,9 @@ window.location.hash.substr(1).split(';').forEach(elem => { elem = decodeURI(elem) const I = elem.indexOf(':') const parts = [elem.slice(0, I), elem.slice(I + 1)] + if (parts[0] === 'raisons') { + parts[1] = parts[1].split(',') + } hashParams[parts[0]] = parts[1] }) @@ -96,7 +99,8 @@ const createReasonField = (reasonData) => { value: reasonData.code, } const inputReason = createElement('input', inputReasonAttrs) - + const reasonName = reasonData.alias || reasonData.code + if (hashParams.raisons?.includes(reasonName) && !inputReason.checked) inputReason.click() const labelAttrs = { innerHTML: reasonData.label, className: 'form-checkbox-label', for: id } const label = createElement('label', labelAttrs) From 3ff2bfc026476543fa6449f087fe56ca1300e5d1 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 17:40:40 +0100 Subject: [PATCH 07/28] Change syntax from #key:a,b; to #key=a,b& to look more like URL params --- src/js/form.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/js/form.js b/src/js/form.js index 0cde4538..0afdcbd2 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -9,15 +9,16 @@ import { $, appendTo, createElement } from './dom-utils' // Nettoie les URI fragments // eslint-disable-next-line prefer-const let hashParams = {} -window.location.hash.substr(1).split(';').forEach(elem => { +window.location.hash.substr(1).split('&').forEach(elem => { elem = decodeURI(elem) - const I = elem.indexOf(':') - const parts = [elem.slice(0, I), elem.slice(I + 1)] + const parts = elem.split('=') if (parts[0] === 'raisons') { parts[1] = parts[1].split(',') } hashParams[parts[0]] = parts[1] + if (parts[0] === 'auto') hashParams.auto = true }) +console.log(hashParams) const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) From e93fd95a7c28c87498e727413f84da0a58fdf1f1 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 17:42:06 +0100 Subject: [PATCH 08/28] Remove log --- src/js/form.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/js/form.js b/src/js/form.js index 0afdcbd2..f3653999 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -18,7 +18,6 @@ window.location.hash.substr(1).split('&').forEach(elem => { hashParams[parts[0]] = parts[1] if (parts[0] === 'auto') hashParams.auto = true }) -console.log(hashParams) const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) From b0a6fc7a339d844649a99a98bcb2f2d8e2f3f234 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 17:44:09 +0100 Subject: [PATCH 09/28] Add auto-download capability Not working yet, date and hour cannot be set --- src/js/form.js | 5 +++++ src/js/main.js | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/js/form.js b/src/js/form.js index f3653999..3c081caa 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -172,3 +172,8 @@ export function createForm () { const reasonFieldset = createReasonFieldset(reasonsData) appendToForm([...createTitle(), ...formFirstPart, reasonFieldset]) } + +export function autoDownload () { + if (!hashParams.auto) return + document.getElementById('generate-btn').click() +} \ No newline at end of file diff --git a/src/js/main.js b/src/js/main.js index 48fc43a2..91c95535 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -7,9 +7,10 @@ import './check-updates' import { prepareForm } from './form-util' import { warnFacebookBrowserUserIfNecessary } from './facebook-util' import { addVersion } from './util' -import { createForm } from './form' +import { createForm, autoDownload } from './form' warnFacebookBrowserUserIfNecessary() createForm() prepareForm() addVersion(process.env.VERSION) +autoDownload() \ No newline at end of file From 7d986eda4e13b2c25ca63034a0893317eed0fc26 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 17:53:09 +0100 Subject: [PATCH 10/28] Add new line at end of file Yes, a whole commit just for that. --- src/js/form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/form.js b/src/js/form.js index 3c081caa..ce67a7ac 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -176,4 +176,4 @@ export function createForm () { export function autoDownload () { if (!hashParams.auto) return document.getElementById('generate-btn').click() -} \ No newline at end of file +} From 6e454c52e8e69fe4c2b0664bde6fff4973317f49 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 18:34:11 +0100 Subject: [PATCH 11/28] Correct id problem Plus add some end-of-file empty line :) --- src/js/form.js | 15 +++++++-------- src/js/main.js | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/js/form.js b/src/js/form.js index ce67a7ac..9d86e3d6 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -35,15 +35,17 @@ const createFormGroup = ({ min, maxlength, minlength, - name, + key, + alias, pattern, placeholder = '', type = 'text', }) => { + const name = alias || key const formGroup = createElement('div', { className: 'form-group' }) const labelAttrs = { - for: `field-${name}`, - id: `field-${name}-label`, + for: `field-${key}`, + id: `field-${key}-label`, innerHTML: label, } const labelEl = createElement('label', labelAttrs) @@ -53,13 +55,13 @@ const createFormGroup = ({ autocomplete, autofocus, className: 'form-control', - id: `field-${name}`, + id: `field-${key}`, inputmode, min, max, minlength, maxlength, - name, + name: key, pattern, placeholder, required: true, @@ -154,12 +156,9 @@ export function createForm () { .filter(field => !field.isHidden) .map((field, index) => { - // Permet de changer la clé devant être présente dans l'URI en ajoutant la propriété 'alias' - const name = field.alias || field.key const formGroup = createFormGroup({ autofocus: index === 0, ...field, - name, }) return formGroup diff --git a/src/js/main.js b/src/js/main.js index 91c95535..6b151e85 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -13,4 +13,4 @@ warnFacebookBrowserUserIfNecessary() createForm() prepareForm() addVersion(process.env.VERSION) -autoDownload() \ No newline at end of file +autoDownload() From 6ce031f41576d91f8c97f6533c4bd5b753f51a32 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Fri, 30 Oct 2020 19:09:51 +0100 Subject: [PATCH 12/28] Simplify code By using URLSearchParams --- src/js/form.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/js/form.js b/src/js/form.js index 9d86e3d6..d29037ed 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -8,16 +8,7 @@ import { $, appendTo, createElement } from './dom-utils' // Nettoie les URI fragments // eslint-disable-next-line prefer-const -let hashParams = {} -window.location.hash.substr(1).split('&').forEach(elem => { - elem = decodeURI(elem) - const parts = elem.split('=') - if (parts[0] === 'raisons') { - parts[1] = parts[1].split(',') - } - hashParams[parts[0]] = parts[1] - if (parts[0] === 'auto') hashParams.auto = true -}) +let params = new URLSearchParams(window.location.hash.substr(1)) const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) @@ -68,7 +59,9 @@ const createFormGroup = ({ type, } - if (hashParams[name]) inputAttrs.value = hashParams[name] + // Remplit en fonction des params + if (params.has(name)) inputAttrs.value = params.get(name) + const input = createElement('input', inputAttrs) const validityAttrs = { @@ -101,11 +94,13 @@ const createReasonField = (reasonData) => { value: reasonData.code, } const inputReason = createElement('input', inputReasonAttrs) - const reasonName = reasonData.alias || reasonData.code - if (hashParams.raisons?.includes(reasonName) && !inputReason.checked) inputReason.click() const labelAttrs = { innerHTML: reasonData.label, className: 'form-checkbox-label', for: id } const label = createElement('label', labelAttrs) + // Remplit en fonction des params + const reasonName = reasonData.alias || reasonData.code + if (params.get('raisons')?.split(',').includes(reasonName) && !inputReason.checked) inputReason.click() + appendToReason([inputReason, label]) return formReason } @@ -173,6 +168,6 @@ export function createForm () { } export function autoDownload () { - if (!hashParams.auto) return + if (!params.get('auto')) return document.getElementById('generate-btn').click() } From 4a463f48a48cd54c5ba3774ef11c2cf7ca0d3585 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sat, 31 Oct 2020 08:52:30 +0100 Subject: [PATCH 13/28] Reorganize & Add date/hour support All the autoFill operations are now in form-util.js. form.js is now as it was before the 1st commit --- src/js/form-util.js | 37 +++++++++++++++++++++++++++++++++++-- src/js/form.js | 17 ----------------- src/js/main.js | 5 +++-- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 9e50f166..27900c5d 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -3,6 +3,8 @@ import { addSlash, getFormattedDate } from './util' import pdfBase from '../certificate.pdf' import { generatePdf } from './pdf-util' +const params = new URLSearchParams(window.location.hash.substr(1)) + const conditions = { '#field-firstname': { length: 1, @@ -53,11 +55,14 @@ function validateAriaFields () { .includes(true) } -export function setReleaseDateTime (releaseDateInput) { - const loadedDate = new Date() +export function setReleaseDateTime (releaseDateInput, loadedDate = new Date()) { releaseDateInput.value = getFormattedDate(loadedDate) } +export function setReleaseHourTime (releaseTimeInput, loadedDate = new Date()) { + releaseTimeInput.value = loadedDate.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }) +} + export function getProfile (formInputs) { const fields = {} for (const field of formInputs) { @@ -156,6 +161,34 @@ export function prepareForm () { const reasonFieldset = $('#reason-fieldset') const reasonAlert = reasonFieldset.querySelector('.msg-alert') const releaseDateInput = $('#field-datesortie') + const releaseHourInput = $('#field-heuresortie') setReleaseDateTime(releaseDateInput) + setReleaseHourTime(releaseHourInput, new Date(Date.now() + 300_000)) prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar) } + +export function fill () { + const formData = require('../form-data') + // Remplit les fields du groupe 1 + formData.flat(1) + .filter(field => field.key !== 'reason') + .filter(field => !field.isHidden) + .forEach(data => { + const name = data.alias || data.key + const field = $('#field-' + data.key) + if (params.has(name)) field.value = params.get(name) + }) + // Remplit les raisons + formData + .flat(1) + .find(field => field.key === 'reason') + .items.forEach(item => { + const name = item.alias || item.code + const field = $('#checkbox-' + item.code) + if (params.get('raisons')?.split(',').includes(name) && !field.checked) field.click() + }) +} + +export function autoDownload () { + if (params.get('auto')) return document.getElementById('generate-btn').click() +} diff --git a/src/js/form.js b/src/js/form.js index d29037ed..6afdeebe 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -6,10 +6,6 @@ import formData from '../form-data.json' import { $, appendTo, createElement } from './dom-utils' -// Nettoie les URI fragments -// eslint-disable-next-line prefer-const -let params = new URLSearchParams(window.location.hash.substr(1)) - const createTitle = () => { const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' }) const p = createElement('p', { className: 'msg-info', innerHTML: 'Tous les champs sont obligatoires.' }) @@ -32,7 +28,6 @@ const createFormGroup = ({ placeholder = '', type = 'text', }) => { - const name = alias || key const formGroup = createElement('div', { className: 'form-group' }) const labelAttrs = { for: `field-${key}`, @@ -59,9 +54,6 @@ const createFormGroup = ({ type, } - // Remplit en fonction des params - if (params.has(name)) inputAttrs.value = params.get(name) - const input = createElement('input', inputAttrs) const validityAttrs = { @@ -97,10 +89,6 @@ const createReasonField = (reasonData) => { const labelAttrs = { innerHTML: reasonData.label, className: 'form-checkbox-label', for: id } const label = createElement('label', labelAttrs) - // Remplit en fonction des params - const reasonName = reasonData.alias || reasonData.code - if (params.get('raisons')?.split(',').includes(reasonName) && !inputReason.checked) inputReason.click() - appendToReason([inputReason, label]) return formReason } @@ -166,8 +154,3 @@ export function createForm () { const reasonFieldset = createReasonFieldset(reasonsData) appendToForm([...createTitle(), ...formFirstPart, reasonFieldset]) } - -export function autoDownload () { - if (!params.get('auto')) return - document.getElementById('generate-btn').click() -} diff --git a/src/js/main.js b/src/js/main.js index 6b151e85..f11eda92 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -4,13 +4,14 @@ import '../css/main.css' import './icons' import './check-updates' -import { prepareForm } from './form-util' +import { prepareForm, fill, autoDownload } from './form-util' import { warnFacebookBrowserUserIfNecessary } from './facebook-util' import { addVersion } from './util' -import { createForm, autoDownload } from './form' +import { createForm } from './form' warnFacebookBrowserUserIfNecessary() createForm() prepareForm() addVersion(process.env.VERSION) +fill() autoDownload() From 15e8fbf28cd10c24a5fe62e17de8c012c86cf48e Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sat, 31 Oct 2020 09:08:26 +0100 Subject: [PATCH 14/28] Correct autoDownload Now working --- src/js/form-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 27900c5d..840ce7bc 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -190,5 +190,5 @@ export function fill () { } export function autoDownload () { - if (params.get('auto')) return document.getElementById('generate-btn').click() + if (params.has('auto')) $('#generate-btn').click() } From 80a0d9eb92cc37044c4d41189e8bb8869c0e59ee Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sat, 31 Oct 2020 09:15:34 +0100 Subject: [PATCH 15/28] Delete console.log Was already there before. --- src/js/form-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 840ce7bc..acf36241 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -132,7 +132,7 @@ export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonA return } - console.log(getProfile(formInputs), reasons) + // console.log(getProfile(formInputs), reasons) const pdfBlob = await generatePdf(getProfile(formInputs), reasons, pdfBase) From 1ff87976907e3dc004d87ee4dee94bb15c1d448d Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sat, 31 Oct 2020 15:53:28 +0100 Subject: [PATCH 16/28] Rename Func & Group changes fill() => followParams() All the changes are now in form-util.js (fields params + autoDownload) in 1 function --- src/js/form-util.js | 31 +++++++++++++++++-------------- src/js/main.js | 5 ++--- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index acf36241..1e7560ce 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -166,10 +166,13 @@ export function prepareForm () { setReleaseHourTime(releaseHourInput, new Date(Date.now() + 300_000)) prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar) } - -export function fill () { +/** + * Modifie les entrées du formulaire en fonction des paramètres spécifiés sous forme d'URI fragments + */ +export function followParams () { const formData = require('../form-data') - // Remplit les fields du groupe 1 + + // Remplit les entrées du formulaire formData.flat(1) .filter(field => field.key !== 'reason') .filter(field => !field.isHidden) @@ -178,17 +181,17 @@ export function fill () { const field = $('#field-' + data.key) if (params.has(name)) field.value = params.get(name) }) - // Remplit les raisons - formData - .flat(1) - .find(field => field.key === 'reason') - .items.forEach(item => { - const name = item.alias || item.code - const field = $('#checkbox-' + item.code) - if (params.get('raisons')?.split(',').includes(name) && !field.checked) field.click() - }) -} -export function autoDownload () { + // Définit l'objet contenant les raisons et son nom + const reasonsObj = formData.flat(1).find(field => field.key === 'reason') + const reasonsObjName = reasonsObj.alias || reasonsObj.key + // Coche les raisons + reasonsObj.items.forEach(data => { + const name = data.alias || data.code + const field = $('#checkbox-' + data.code) + if (params.get(reasonsObjName)?.split(',').includes(name) && !field.checked) field.click() + }) + + // Génère automatiquement le PDF si besoin if (params.has('auto')) $('#generate-btn').click() } diff --git a/src/js/main.js b/src/js/main.js index f11eda92..6fa107ef 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -4,7 +4,7 @@ import '../css/main.css' import './icons' import './check-updates' -import { prepareForm, fill, autoDownload } from './form-util' +import { prepareForm, followParams } from './form-util' import { warnFacebookBrowserUserIfNecessary } from './facebook-util' import { addVersion } from './util' import { createForm } from './form' @@ -13,5 +13,4 @@ warnFacebookBrowserUserIfNecessary() createForm() prepareForm() addVersion(process.env.VERSION) -fill() -autoDownload() +followParams() From 148763c8aa0dd2caa6182241bc0cc873ccb311b8 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sat, 31 Oct 2020 16:17:39 +0100 Subject: [PATCH 17/28] Rename reasons alias Add s --- src/form-data.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/form-data.json b/src/form-data.json index 7bffb6cc..94201353 100644 --- a/src/form-data.json +++ b/src/form-data.json @@ -108,7 +108,7 @@ }, { "key": "reason", - "alias": "raison", + "alias": "raisons", "type": "list", "items": [ { From 9e22490657949e209f549be39e590133a2585c65 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sat, 31 Oct 2020 16:23:28 +0100 Subject: [PATCH 18/28] Simplify reasons params eval code --- src/js/form-util.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 1e7560ce..6b7c24cb 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -182,14 +182,12 @@ export function followParams () { if (params.has(name)) field.value = params.get(name) }) - // Définit l'objet contenant les raisons et son nom - const reasonsObj = formData.flat(1).find(field => field.key === 'reason') - const reasonsObjName = reasonsObj.alias || reasonsObj.key // Coche les raisons + const reasonsObj = formData.flat(1).find(field => field.key === 'reason') reasonsObj.items.forEach(data => { const name = data.alias || data.code const field = $('#checkbox-' + data.code) - if (params.get(reasonsObjName)?.split(',').includes(name) && !field.checked) field.click() + if (params.get(reasonsObj.alias || 'raisons' || reasonsObj.key)?.split(',').includes(name) && !field.checked) field.click() }) // Génère automatiquement le PDF si besoin From 1cbfe866571435ef445346ff2d07c7cee6ad4b87 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sun, 1 Nov 2020 09:41:21 +0100 Subject: [PATCH 19/28] URI is now synced with inputs Now, inputs are automatically written in URI. --- src/js/form-util.js | 19 ++++++++++++++++--- src/js/main.js | 3 ++- src/js/util.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 6b7c24cb..29a3008b 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -1,8 +1,10 @@ import { $, $$, downloadBlob } from './dom-utils' -import { addSlash, getFormattedDate } from './util' +import { addSlash, getFormattedDate, setParam } from './util' import pdfBase from '../certificate.pdf' import { generatePdf } from './pdf-util' +const formData = require('../form-data') + const params = new URLSearchParams(window.location.hash.substr(1)) const conditions = { @@ -170,8 +172,6 @@ export function prepareForm () { * Modifie les entrées du formulaire en fonction des paramètres spécifiés sous forme d'URI fragments */ export function followParams () { - const formData = require('../form-data') - // Remplit les entrées du formulaire formData.flat(1) .filter(field => field.key !== 'reason') @@ -193,3 +193,16 @@ export function followParams () { // Génère automatiquement le PDF si besoin if (params.has('auto')) $('#generate-btn').click() } + +export function listenToInputChanges () { + const keys = Object.keys(conditions).map(value => value.substr('#field-'.length)) + keys.forEach(key => { + const data = formData.flat(1) + .filter(field => field.key !== 'reason') + .filter(field => !field.isHidden) + .find(field => field.key === key) + const name = data.alias || data.key + const input = document.getElementById('field-' + data.key) + input.addEventListener('input', (e) => setParam(name, e.target.value)) + }) +} diff --git a/src/js/main.js b/src/js/main.js index 6fa107ef..23bdbd51 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -4,7 +4,7 @@ import '../css/main.css' import './icons' import './check-updates' -import { prepareForm, followParams } from './form-util' +import { prepareForm, followParams, listenToInputChanges } from './form-util' import { warnFacebookBrowserUserIfNecessary } from './facebook-util' import { addVersion } from './util' import { createForm } from './form' @@ -14,3 +14,4 @@ createForm() prepareForm() addVersion(process.env.VERSION) followParams() +listenToInputChanges() diff --git a/src/js/util.js b/src/js/util.js index 00b19eaf..cbbac7ff 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -33,3 +33,38 @@ export function addVersion (version) { 'version', ).innerHTML = `${new Date().getFullYear()} - ${version}` } + +export function setParam (key, value) { + const oldParams = window.location.hash.substr(1) + + // Remplace les params si existants + let newParams = oldParams.split('&').map(part => { + if (part.startsWith(`${key}=`)) { + return (key + '=' + value) + } + return part + }) + + // Crée un param si inexistant + if (!newParams.includes(key + '=' + value)) { + newParams.push(key + '=' + value) + } + + // Supprime les doublons de l'URI + let alreadySet = false + newParams = newParams.filter((val) => { + if (val.startsWith(key + '=')) { + if (alreadySet) return false + alreadySet = true + } + return true + }) + + // Supprime elem 0 si nul (pour éviter les #& au lieu de #) + while (newParams[0] === '') newParams.shift() + + if (!value) newParams = newParams.filter(param => (!param.startsWith(key))) + + window.location.hash = '#' + newParams.join('&') + return newParams +} From 5681bef99998f76e3018543d83444981e83a1194 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sun, 1 Nov 2020 09:48:57 +0100 Subject: [PATCH 20/28] Add comment --- src/js/form-util.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/js/form-util.js b/src/js/form-util.js index 29a3008b..5c4df655 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -195,6 +195,7 @@ export function followParams () { } export function listenToInputChanges () { + // Champs const keys = Object.keys(conditions).map(value => value.substr('#field-'.length)) keys.forEach(key => { const data = formData.flat(1) From c13598eb01519534742cb999e37b09c3378b9861 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sun, 1 Nov 2020 13:40:43 +0100 Subject: [PATCH 21/28] Reverse 'name' rename Changed the 'name' var back to 'name', not 'key' --- src/js/form.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/js/form.js b/src/js/form.js index 6afdeebe..cb14601d 100644 --- a/src/js/form.js +++ b/src/js/form.js @@ -22,16 +22,15 @@ const createFormGroup = ({ min, maxlength, minlength, - key, - alias, + name, // here the 'key' value pattern, placeholder = '', type = 'text', }) => { const formGroup = createElement('div', { className: 'form-group' }) const labelAttrs = { - for: `field-${key}`, - id: `field-${key}-label`, + for: `field-${name}`, + id: `field-${name}-label`, innerHTML: label, } const labelEl = createElement('label', labelAttrs) @@ -41,13 +40,13 @@ const createFormGroup = ({ autocomplete, autofocus, className: 'form-control', - id: `field-${key}`, + id: `field-${name}`, inputmode, min, max, minlength, maxlength, - name: key, + name, pattern, placeholder, required: true, @@ -142,6 +141,7 @@ export function createForm () { const formGroup = createFormGroup({ autofocus: index === 0, ...field, + name: field.key, }) return formGroup From 3dce1b17d4cefbeef1e05d1d8a8b3cc4929359c7 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sun, 1 Nov 2020 13:52:45 +0100 Subject: [PATCH 22/28] Move default hour changes to another branch They had nothing to do with the URI params stuff. --- src/js/form-util.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 5c4df655..f8e215ab 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -57,14 +57,11 @@ function validateAriaFields () { .includes(true) } -export function setReleaseDateTime (releaseDateInput, loadedDate = new Date()) { +export function setReleaseDateTime (releaseDateInput) { + const loadedDate = new Date() releaseDateInput.value = getFormattedDate(loadedDate) } -export function setReleaseHourTime (releaseTimeInput, loadedDate = new Date()) { - releaseTimeInput.value = loadedDate.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }) -} - export function getProfile (formInputs) { const fields = {} for (const field of formInputs) { @@ -163,9 +160,7 @@ export function prepareForm () { const reasonFieldset = $('#reason-fieldset') const reasonAlert = reasonFieldset.querySelector('.msg-alert') const releaseDateInput = $('#field-datesortie') - const releaseHourInput = $('#field-heuresortie') setReleaseDateTime(releaseDateInput) - setReleaseHourTime(releaseHourInput, new Date(Date.now() + 300_000)) prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar) } /** From e19b9fa01fb739529658921612049050916c65d8 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Sun, 1 Nov 2020 15:33:58 +0100 Subject: [PATCH 23/28] Add reasons sync support And simplified code --- src/js/form-util.js | 40 +++++++++++++++++++++++++++++----------- src/js/util.js | 7 ++++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index f8e215ab..f63495f3 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -1,11 +1,11 @@ import { $, $$, downloadBlob } from './dom-utils' -import { addSlash, getFormattedDate, setParam } from './util' +import { addSlash, getFormattedDate, setParam, getParam } from './util' import pdfBase from '../certificate.pdf' import { generatePdf } from './pdf-util' const formData = require('../form-data') -const params = new URLSearchParams(window.location.hash.substr(1)) +let params = new URLSearchParams(window.location.hash.substr(1)) const conditions = { '#field-firstname': { @@ -191,14 +191,32 @@ export function followParams () { export function listenToInputChanges () { // Champs - const keys = Object.keys(conditions).map(value => value.substr('#field-'.length)) - keys.forEach(key => { - const data = formData.flat(1) - .filter(field => field.key !== 'reason') - .filter(field => !field.isHidden) - .find(field => field.key === key) - const name = data.alias || data.key - const input = document.getElementById('field-' + data.key) - input.addEventListener('input', (e) => setParam(name, e.target.value)) + formData.flat(1) + .filter(field => field.key !== 'reason') + .filter(field => !field.isHidden) + .forEach(data => { + const name = data.alias || data.key + const input = document.getElementById('field-' + data.key) + input.addEventListener('input', (e) => { + setParam(name, e.target.value) + params = new URLSearchParams(window.location.hash.substr(1)) + }) + }) + + // Raisons + const reasonsObj = formData.flat(1).find(field => field.key === 'reason') + reasonsObj.items.forEach(data => { + const name = data.alias || data.code + const checkbox = $('#checkbox-' + data.code) + checkbox.addEventListener('click', (e) => { + let reasons = getParam(reasonsObj.alias || 'raisons')?.split(',') + if (!reasons) return setParam(reasonsObj.alias || 'raisons', name) + if (checkbox.checked && !reasons.includes(name)) { + reasons.push(name) + } else if (!checkbox.checked && reasons.includes(name)) { + reasons = reasons.filter(elem => elem !== name) + } + setParam(reasonsObj.alias || 'raisons', reasons.toString()) + }) }) } diff --git a/src/js/util.js b/src/js/util.js index cbbac7ff..2ed32722 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -66,5 +66,10 @@ export function setParam (key, value) { if (!value) newParams = newParams.filter(param => (!param.startsWith(key))) window.location.hash = '#' + newParams.join('&') - return newParams +} + +export function getParam (key) { + const params = window.location.hash.substr(1).split('&') + const param = params?.find((val) => val.startsWith(key + '=')) + return param?.substr((key + '=').length) } From d1d1e7e13a9bb0364f83458e07fed87c7bb81c95 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Mon, 2 Nov 2020 09:57:31 +0100 Subject: [PATCH 24/28] Add ability to watch URI hash changes Based on @naholyr 's changes :100: --- src/js/form-util.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index f63495f3..81b97594 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -166,7 +166,8 @@ export function prepareForm () { /** * Modifie les entrées du formulaire en fonction des paramètres spécifiés sous forme d'URI fragments */ -export function followParams () { +export function followParams (watch = true) { + const auto = params.has('auto') // Remplit les entrées du formulaire formData.flat(1) .filter(field => field.key !== 'reason') @@ -186,7 +187,14 @@ export function followParams () { }) // Génère automatiquement le PDF si besoin - if (params.has('auto')) $('#generate-btn').click() + if (auto) $('#generate-btn').click() + + if (!watch || auto) return false + + window.addEventListener('hashchange', () => { + params = new URLSearchParams(window.location.hash.substr(1)) + followParams(false) + }) } export function listenToInputChanges () { @@ -219,4 +227,5 @@ export function listenToInputChanges () { setParam(reasonsObj.alias || 'raisons', reasons.toString()) }) }) + params = new URLSearchParams(window.location.hash.substr(1)) } From ee8b5c449a0e5543c5d31f2bf7408e5263c43619 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Mon, 2 Nov 2020 09:58:46 +0100 Subject: [PATCH 25/28] Add special function to clean params Based on a part of the setParam function; now changes the order of the params --- src/js/form-util.js | 3 ++- src/js/util.js | 60 +++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 81b97594..a0c1c422 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -1,5 +1,5 @@ import { $, $$, downloadBlob } from './dom-utils' -import { addSlash, getFormattedDate, setParam, getParam } from './util' +import { addSlash, getFormattedDate, setParam, getParam, cleanParams } from './util' import pdfBase from '../certificate.pdf' import { generatePdf } from './pdf-util' @@ -206,6 +206,7 @@ export function listenToInputChanges () { const name = data.alias || data.key const input = document.getElementById('field-' + data.key) input.addEventListener('input', (e) => { + window.location.hash = cleanParams() setParam(name, e.target.value) params = new URLSearchParams(window.location.hash.substr(1)) }) diff --git a/src/js/util.js b/src/js/util.js index 2ed32722..c3e70b49 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -1,4 +1,5 @@ import QRCode from 'qrcode' +const formData = require('../form-data.json') export function generateQR (text) { const opts = { @@ -38,7 +39,7 @@ export function setParam (key, value) { const oldParams = window.location.hash.substr(1) // Remplace les params si existants - let newParams = oldParams.split('&').map(part => { + const newParams = oldParams.split('&').map(part => { if (part.startsWith(`${key}=`)) { return (key + '=' + value) } @@ -50,22 +51,12 @@ export function setParam (key, value) { newParams.push(key + '=' + value) } - // Supprime les doublons de l'URI - let alreadySet = false - newParams = newParams.filter((val) => { - if (val.startsWith(key + '=')) { - if (alreadySet) return false - alreadySet = true - } - return true - }) - - // Supprime elem 0 si nul (pour éviter les #& au lieu de #) - while (newParams[0] === '') newParams.shift() + // Nettoie les paramètres + const newParamsStr = cleanParams('#' + newParams.join('&')) - if (!value) newParams = newParams.filter(param => (!param.startsWith(key))) - - window.location.hash = '#' + newParams.join('&') + // Définit + window.location.hash = newParamsStr + return newParamsStr } export function getParam (key) { @@ -73,3 +64,40 @@ export function getParam (key) { const param = params?.find((val) => val.startsWith(key + '=')) return param?.substr((key + '=').length) } + +export function cleanParams (params = window.location.hash) { + const oldParams = params.substr(1) + let newParams = oldParams.split('&') + + // Supprime les doublons + const alreadySeen = [] + newParams = newParams.filter(param => { + if (!param?.includes('=')) return false + const split = param.split('=') + const key = split[0] + const value = split[1] + if (!value) return false + if (alreadySeen.includes(key)) return false + alreadySeen.push(key) + return true + }) + + // Met dans l'ordre + const rightOrder = formData.flat(1).filter(data => !data.isHidden).map(data => data.alias || data.key) + console.log(rightOrder) + newParams = rightOrder.map((elem, index) => { + const peer = newParams.find((param) => { + return param.split('=')[0] === elem + }) + if (!peer) return false + console.log('peer', peer) + return elem + '=' + peer.split('=')[1] + }) + + // Supprime les cadavres + newParams = newParams.filter((elem) => elem) + + // Retourne les params nettoyés + console.log(newParams) + return '#' + newParams.join('&') +} From e40ba7a23cface8a490946c3713e0e617b56725d Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Mon, 2 Nov 2020 15:28:44 +0100 Subject: [PATCH 26/28] Remove console.log --- src/js/util.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/js/util.js b/src/js/util.js index c3e70b49..c287cc86 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -84,13 +84,11 @@ export function cleanParams (params = window.location.hash) { // Met dans l'ordre const rightOrder = formData.flat(1).filter(data => !data.isHidden).map(data => data.alias || data.key) - console.log(rightOrder) newParams = rightOrder.map((elem, index) => { const peer = newParams.find((param) => { return param.split('=')[0] === elem }) if (!peer) return false - console.log('peer', peer) return elem + '=' + peer.split('=')[1] }) @@ -98,6 +96,5 @@ export function cleanParams (params = window.location.hash) { newParams = newParams.filter((elem) => elem) // Retourne les params nettoyés - console.log(newParams) return '#' + newParams.join('&') } From 34e3a5e63a9caa6b8f70bca52ca0cb8d2af679d2 Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Tue, 3 Nov 2020 12:59:52 +0100 Subject: [PATCH 27/28] Corrections --- src/js/form-util.js | 3 +-- src/js/util.js | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 658adf88..5e9ea668 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -1,7 +1,7 @@ import removeAccents from 'remove-accents' import { $, $$, downloadBlob } from './dom-utils' -import { addSlash, getFormattedDate, setParam, getParam, cleanParams } from './util' +import { addSlash, getFormattedDate, setParam, getParam } from './util' import pdfBase from '../certificate.pdf' import { generatePdf } from './pdf-util' @@ -216,7 +216,6 @@ export function listenToInputChanges () { const name = data.alias || data.key const input = document.getElementById('field-' + data.key) input.addEventListener('input', (e) => { - window.location.hash = cleanParams() setParam(name, e.target.value) params = new URLSearchParams(window.location.hash.substr(1)) }) diff --git a/src/js/util.js b/src/js/util.js index c287cc86..a82f3ee6 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -72,7 +72,7 @@ export function cleanParams (params = window.location.hash) { // Supprime les doublons const alreadySeen = [] newParams = newParams.filter(param => { - if (!param?.includes('=')) return false + if (!param?.includes('=') && param !== 'auto') return false const split = param.split('=') const key = split[0] const value = split[1] @@ -93,7 +93,7 @@ export function cleanParams (params = window.location.hash) { }) // Supprime les cadavres - newParams = newParams.filter((elem) => elem) + newParams = newParams.filter((elem) => !!elem) // Retourne les params nettoyés return '#' + newParams.join('&') From 09ac3ed0aa47cc22646e1fba77fca3203482565c Mon Sep 17 00:00:00 2001 From: a2br <62328077+a2br@users.noreply.github.com> Date: Tue, 3 Nov 2020 13:51:36 +0100 Subject: [PATCH 28/28] Correct reasons' relation with URI --- src/js/form-util.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/js/form-util.js b/src/js/form-util.js index 5e9ea668..3c338696 100644 --- a/src/js/form-util.js +++ b/src/js/form-util.js @@ -193,13 +193,17 @@ export function followParams (watch = true) { reasonsObj.items.forEach(data => { const name = data.alias || data.code const field = $('#checkbox-' + data.code) - if (params.get(reasonsObj.alias || 'raisons' || reasonsObj.key)?.split(',').includes(name) && !field.checked) field.click() + if (params.get(reasonsObj.alias || 'raisons' || reasonsObj.key)?.split(',').includes(name)) { + if (!field.checked) field.click() + } else { + if (field.checked) field.click() + } }) // Génère automatiquement le PDF si besoin if (auto) $('#generate-btn').click() - if (!watch || auto) return false + if (!watch /* || auto */) return false window.addEventListener('hashchange', () => { params = new URLSearchParams(window.location.hash.substr(1))