2018-08-09 11:53:59 +02:00
|
|
|
|
import $ from 'jquery';
|
2018-08-22 14:47:56 +02:00
|
|
|
|
import 'select2';
|
2020-01-23 15:12:19 +01:00
|
|
|
|
import { isNumeric } from '@utils';
|
2018-08-09 11:53:59 +02:00
|
|
|
|
|
2020-01-14 18:46:07 +01:00
|
|
|
|
const { api_geo_url, api_adresse_url } = gon.autocomplete || {};
|
|
|
|
|
|
|
|
|
|
const language = {
|
|
|
|
|
errorLoading: function() {
|
|
|
|
|
return 'Les résultats ne peuvent pas être chargés.';
|
|
|
|
|
},
|
|
|
|
|
inputTooLong: function(args) {
|
|
|
|
|
var overChars = args.input.length - args.maximum;
|
|
|
|
|
|
|
|
|
|
return 'Supprimez ' + overChars + ' caractère' + (overChars > 1 ? 's' : '');
|
|
|
|
|
},
|
|
|
|
|
inputTooShort: function(args) {
|
|
|
|
|
var remainingChars = args.minimum - args.input.length;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
'Saisissez au moins ' +
|
|
|
|
|
remainingChars +
|
|
|
|
|
' caractère' +
|
|
|
|
|
(remainingChars > 1 ? 's' : '')
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
loadingMore: function() {
|
|
|
|
|
return 'Chargement de résultats supplémentaires…';
|
|
|
|
|
},
|
|
|
|
|
maximumSelected: function(args) {
|
|
|
|
|
return (
|
|
|
|
|
'Vous pouvez seulement sélectionner ' +
|
|
|
|
|
args.maximum +
|
|
|
|
|
' élément' +
|
|
|
|
|
(args.maximum > 1 ? 's' : '')
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
noResults: function() {
|
|
|
|
|
return 'Aucun résultat trouvé';
|
|
|
|
|
},
|
|
|
|
|
searching: function() {
|
|
|
|
|
return 'Recherche en cours…';
|
|
|
|
|
},
|
|
|
|
|
removeAllItems: function() {
|
|
|
|
|
return 'Supprimer tous les éléments';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const baseOptions = {
|
|
|
|
|
language,
|
|
|
|
|
width: '100%'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const baseAjaxOptions = {
|
|
|
|
|
delay: 250,
|
|
|
|
|
cache: true,
|
|
|
|
|
data({ term: nom }) {
|
|
|
|
|
return {
|
|
|
|
|
nom,
|
|
|
|
|
fields: 'nom,code'
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
processResults(data) {
|
|
|
|
|
return {
|
|
|
|
|
results: data.map(({ nom }) => ({ id: nom, text: nom }))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const regionsOptions = {
|
|
|
|
|
...baseOptions,
|
|
|
|
|
minimumInputLength: 2,
|
|
|
|
|
ajax: { url: `${api_geo_url}/regions`, ...baseAjaxOptions }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const communesOptions = {
|
|
|
|
|
...baseOptions,
|
|
|
|
|
minimumInputLength: 2,
|
|
|
|
|
ajax: { url: `${api_geo_url}/communes`, ...baseAjaxOptions }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const etranger99 = { id: '99 - Étranger', text: '99 - Étranger' };
|
|
|
|
|
const departementsOptions = {
|
|
|
|
|
...baseOptions,
|
2020-01-23 15:12:19 +01:00
|
|
|
|
minimumInputLength: 1,
|
2020-01-14 18:46:07 +01:00
|
|
|
|
ajax: {
|
|
|
|
|
...baseAjaxOptions,
|
|
|
|
|
url: `${api_geo_url}/departements`,
|
2020-01-23 15:12:19 +01:00
|
|
|
|
data({ term }) {
|
|
|
|
|
const data = { fields: 'nom,code' };
|
|
|
|
|
|
|
|
|
|
if (isNumeric(term)) {
|
|
|
|
|
data.code = term.trim().padStart(2, '0');
|
|
|
|
|
} else {
|
|
|
|
|
data.nom = term;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
},
|
2020-01-14 18:46:07 +01:00
|
|
|
|
processResults(data) {
|
|
|
|
|
return {
|
|
|
|
|
results: data
|
|
|
|
|
.map(({ nom, code }) => ({
|
|
|
|
|
id: `${code} - ${nom}`,
|
|
|
|
|
text: `${code} - ${nom}`
|
|
|
|
|
}))
|
|
|
|
|
.concat([etranger99])
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const adresseOptions = {
|
|
|
|
|
...baseOptions,
|
|
|
|
|
minimumInputLength: 2,
|
|
|
|
|
ajax: {
|
|
|
|
|
...baseAjaxOptions,
|
|
|
|
|
url: `${api_adresse_url}/search`,
|
|
|
|
|
data({ term: q }) {
|
|
|
|
|
return {
|
|
|
|
|
q,
|
|
|
|
|
limit: 5
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
processResults(data) {
|
2020-01-27 12:42:04 +01:00
|
|
|
|
let r = data.features.map(({ properties: { label }, geometry }) => ({
|
|
|
|
|
id: label,
|
|
|
|
|
text: label,
|
|
|
|
|
geometry
|
|
|
|
|
}));
|
2020-01-27 16:47:32 +01:00
|
|
|
|
// Allow the user to select an arbitrary address missing from the results,
|
|
|
|
|
// by adding the plain-text query to the list of results.
|
2020-01-27 12:42:04 +01:00
|
|
|
|
r.unshift({ id: data.query, text: data.query });
|
2020-01-14 18:46:07 +01:00
|
|
|
|
return {
|
2020-01-27 12:42:04 +01:00
|
|
|
|
results: r
|
2020-01-14 18:46:07 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const templateOption = ({ text }) =>
|
2019-11-04 14:49:53 +01:00
|
|
|
|
$(
|
2020-01-14 18:46:07 +01:00
|
|
|
|
`<span class="custom-select2-option"><span class="icon person"></span>${text}</span>`
|
2019-11-04 14:49:53 +01:00
|
|
|
|
);
|
|
|
|
|
|
2019-08-01 15:22:37 +02:00
|
|
|
|
addEventListener('ds:page:update', () => {
|
2020-01-14 18:46:07 +01:00
|
|
|
|
$('select.select2').select2(baseOptions);
|
|
|
|
|
$('select.select2.departements').select2(departementsOptions);
|
|
|
|
|
$('select.select2.regions').select2(regionsOptions);
|
|
|
|
|
$('select.select2.communes').select2(communesOptions);
|
|
|
|
|
$('select.select2.adresse').select2(adresseOptions);
|
2018-07-12 11:50:47 +02:00
|
|
|
|
|
2018-09-10 17:52:29 +02:00
|
|
|
|
$('.columns-form select.select2-limited').select2({
|
2020-01-14 18:46:07 +01:00
|
|
|
|
width: '300px',
|
2018-07-12 11:50:47 +02:00
|
|
|
|
placeholder: 'Sélectionnez des colonnes',
|
2020-01-14 18:46:07 +01:00
|
|
|
|
maximumSelectionLength: '5'
|
2018-07-12 11:50:47 +02:00
|
|
|
|
});
|
2018-09-10 17:52:49 +02:00
|
|
|
|
|
|
|
|
|
$('.recipients-form select.select2-limited').select2({
|
2020-01-14 18:46:07 +01:00
|
|
|
|
language,
|
|
|
|
|
width: '300px',
|
2018-09-10 17:52:49 +02:00
|
|
|
|
placeholder: 'Sélectionnez des instructeurs',
|
2020-01-14 18:46:07 +01:00
|
|
|
|
maximumSelectionLength: '30'
|
2018-09-10 17:52:49 +02:00
|
|
|
|
});
|
2019-11-04 14:49:53 +01:00
|
|
|
|
|
|
|
|
|
$('select.select2-limited.select-instructeurs').select2({
|
2020-01-14 18:46:07 +01:00
|
|
|
|
language,
|
2019-11-04 14:49:53 +01:00
|
|
|
|
dropdownParent: $('.instructeur-wrapper'),
|
|
|
|
|
placeholder: 'Saisir l’adresse email de l’instructeur',
|
|
|
|
|
tags: true,
|
|
|
|
|
tokenSeparators: [',', ' '],
|
2020-01-14 18:46:07 +01:00
|
|
|
|
templateResult: templateOption,
|
|
|
|
|
templateSelection: templateOption
|
2019-11-04 14:49:53 +01:00
|
|
|
|
});
|
2018-07-12 11:50:47 +02:00
|
|
|
|
});
|