Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sweet URI fragments support #50

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c1b7b40
Fix Choisissez un motif de déplacement - en dark mode
lpmtsf Oct 30, 2020
af33a79
Merge pull request #39 from knrdhz/fixDarkMode
betalabmi Oct 30, 2020
5421912
Add param support
a2br Oct 30, 2020
06c681c
Switch to URI fragments
a2br Oct 30, 2020
8703b26
Remove log
a2br Oct 30, 2020
9c65013
Merge branch 'main' into param-support
a2br Oct 30, 2020
73b591c
Remove useless security
a2br Oct 30, 2020
fb28302
Add support for reasons
a2br Oct 30, 2020
3ff2bfc
Change syntax
a2br Oct 30, 2020
e93fd95
Remove log
a2br Oct 30, 2020
b0a6fc7
Add auto-download capability
a2br Oct 30, 2020
7d986ed
Add new line at end of file
a2br Oct 30, 2020
6e454c5
Correct id problem
a2br Oct 30, 2020
6ce031f
Simplify code
a2br Oct 30, 2020
4a463f4
Reorganize & Add date/hour support
a2br Oct 31, 2020
15e8fbf
Correct autoDownload
a2br Oct 31, 2020
80a0d9e
Delete console.log
a2br Oct 31, 2020
1ff8797
Rename Func & Group changes
a2br Oct 31, 2020
148763c
Rename reasons alias
a2br Oct 31, 2020
9e22490
Simplify reasons params eval code
a2br Oct 31, 2020
1cbfe86
URI is now synced with inputs
a2br Nov 1, 2020
5681bef
Add comment
a2br Nov 1, 2020
c13598e
Reverse 'name' rename
a2br Nov 1, 2020
3dce1b1
Move default hour changes to another branch
a2br Nov 1, 2020
e19b9fa
Add reasons sync support
a2br Nov 1, 2020
d1d1e7e
Add ability to watch URI hash changes
a2br Nov 2, 2020
ee8b5c4
Add special function to clean params
a2br Nov 2, 2020
e40ba7a
Remove console.log
a2br Nov 2, 2020
362d686
Update branch
a2br Nov 2, 2020
4fa887b
Resolve merge conflicts by removing local changes
a2br Nov 3, 2020
34e3a5e
Corrections
a2br Nov 3, 2020
09ac3ed
Correct reasons' relation with URI
a2br Nov 3, 2020
f83c916
Merge branch 'main' into param-support
a2br Nov 4, 2020
174b1f8
Merge branch 'main' into param-support
a2br Dec 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/form-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[
{
"key": "firstname",
"alias": "prenom",
a2br marked this conversation as resolved.
Show resolved Hide resolved
"type": "text",
"contentType": "firstname",
"label": "Prénom",
Expand All @@ -10,6 +11,7 @@
},
{
"key": "lastname",
"alias": "nom",
"type": "text",
"contentType": "lastname",
"label": "Nom",
Expand All @@ -18,6 +20,7 @@
},
{
"key": "birthday",
"alias": "date-de-naissance",
"type": "text",
"contentType": "birthday",
"label": "Date de naissance",
Expand All @@ -30,6 +33,7 @@
[
{
"key": "placeofbirth",
"alias": "lieu-de-naissance",
"type": "text",
"contentType": "cityofbirth",
"label": "Lieu de naissance",
Expand All @@ -38,6 +42,7 @@
},
{
"key": "address",
"alias": "adresse",
"type": "text",
"contentType": "address",
"label": "Adresse",
Expand All @@ -46,6 +51,7 @@
},
{
"key": "city",
"alias": "ville",
"type": "text",
"contentType": "city",
"label": "Ville",
Expand All @@ -54,6 +60,7 @@
},
{
"key": "zipcode",
"alias": "code-postal",
"type": "number",
"contentType": "zipcode",
"label": "Code Postal",
Expand Down Expand Up @@ -84,6 +91,7 @@
},
{
"key": "datesortie",
"alias": "date",
"type": "date",
"contentType": "datesortie",
"label": "Date de sortie",
Expand All @@ -92,13 +100,15 @@
},
{
"key": "heuresortie",
"alias": "heure",
"type": "time",
"contentType": "heuresortie",
"label": "Heure de sortie",
"autocomplete": ""
},
{
"key": "reason",
"alias": "raisons",
"type": "list",
"items": [
{
Expand All @@ -123,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 ;"
},
{
Expand Down
75 changes: 73 additions & 2 deletions src/js/form-util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { $, $$, downloadBlob } from './dom-utils'
import { addSlash, getFormattedDate } from './util'
import { addSlash, getFormattedDate, setParam, getParam, cleanParams } from './util'
import pdfBase from '../certificate.pdf'
import { generatePdf } from './pdf-util'

const formData = require('../form-data')

let params = new URLSearchParams(window.location.hash.substr(1))

const conditions = {
'#field-firstname': {
length: 1,
Expand Down Expand Up @@ -127,7 +131,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)

Expand Down Expand Up @@ -159,3 +163,70 @@ export function prepareForm () {
setReleaseDateTime(releaseDateInput)
prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar)
}
/**
* Modifie les entrées du formulaire en fonction des paramètres spécifiés sous forme d'URI fragments
*/
export function followParams (watch = true) {
const auto = params.has('auto')
// Remplit les entrées du formulaire
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)
})

// 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(reasonsObj.alias || 'raisons' || reasonsObj.key)?.split(',').includes(name) && !field.checked) field.click()
})

// Génère automatiquement le PDF si besoin
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 () {
// Champs
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) => {
window.location.hash = cleanParams()
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())
})
})
params = new URLSearchParams(window.location.hash.substr(1))
}
3 changes: 1 addition & 2 deletions src/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const createFormGroup = ({
min,
maxlength,
minlength,
name,
name, // here the 'key' value
pattern,
placeholder = '',
type = 'text',
Expand Down Expand Up @@ -85,7 +85,6 @@ const createReasonField = (reasonData) => {
value: reasonData.code,
}
const inputReason = createElement('input', inputReasonAttrs)

const labelAttrs = { innerHTML: reasonData.label, className: 'form-checkbox-label', for: id }
const label = createElement('label', labelAttrs)

Expand Down
4 changes: 3 additions & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../css/main.css'

import './icons'
import './check-updates'
import { prepareForm } from './form-util'
import { prepareForm, followParams, listenToInputChanges } from './form-util'
import { warnFacebookBrowserUserIfNecessary } from './facebook-util'
import { addVersion } from './util'
import { createForm } from './form'
Expand All @@ -13,3 +13,5 @@ warnFacebookBrowserUserIfNecessary()
createForm()
prepareForm()
addVersion(process.env.VERSION)
followParams()
listenToInputChanges()
68 changes: 68 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import QRCode from 'qrcode'
const formData = require('../form-data.json')

export function generateQR (text) {
const opts = {
Expand Down Expand Up @@ -33,3 +34,70 @@ 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
const 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)
}

// Nettoie les paramètres
const newParamsStr = cleanParams('#' + newParams.join('&'))

// Définit
window.location.hash = newParamsStr
return newParamsStr
}

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)
}

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)
a2br marked this conversation as resolved.
Show resolved Hide resolved
return elem + '=' + peer.split('=')[1]
})

// Supprime les cadavres
newParams = newParams.filter((elem) => elem)

// Retourne les params nettoyés
console.log(newParams)
a2br marked this conversation as resolved.
Show resolved Hide resolved
return '#' + newParams.join('&')
}