1,97 → 1,49 |
/** |
* Constructeur WidgetSaisie par défaut |
*/ |
function WidgetSaisie() { |
this.urlBaseAuth = window.location.origin + '/service:annuaire:auth'; |
this.mode = 'prod'; |
this.langue = 'fr'; |
this.obsNbre = 0; |
this.nbObsEnCours = 1; |
function WidgetSaisie( proprietes ) { |
if ( valOk(proprietes) ) { |
this.urlWidgets = proprietes.urlWidgets; |
this.module = proprietes.nomSquelette; |
this.tagProjet = proprietes.tagProjet; |
this.mode = proprietes.mode; |
this.langue = proprietes.langue; |
this.serviceAnnuaireIdUrl = proprietes.serviceAnnuaireIdUrl; |
this.serviceNomCommuneUrl = proprietes.serviceNomCommuneUrl; |
this.serviceNomCommuneUrlAlt = proprietes.serviceNomCommuneUrlAlt; |
this.debug = proprietes.debug; |
this.html5 = proprietes.html5; |
this.serviceSaisieUrl = proprietes.serviceSaisieUrl; |
this.serviceObsUrl = proprietes.serviceObsUrl; |
this.chargementImageIconeUrl = proprietes.chargementImageIconeUrl; |
this.pasDePhotoIconeUrl = proprietes.pasDePhotoIconeUrl; |
this.autocompletionElementsNbre = proprietes.autocompletionElementsNbre; |
this.serviceAutocompletionNomSciUrl = proprietes.serviceAutocompletionNomSciUrl; |
this.serviceAutocompletionNomSciUrlTpl = proprietes.serviceAutocompletionNomSciUrlTpl; |
this.dureeMessage = proprietes.dureeMessage; |
this.obsMaxNbre = proprietes.obsMaxNbre; |
this.tagImg = proprietes.tagImg; |
this.tagObs = proprietes.tagObs; |
this.obsId = proprietes.obsId; |
this.nomSciReferentiel = proprietes.nomSciReferentiel; |
this.especeImposee = proprietes.especeImposee; |
this.infosEspeceImposee = proprietes.infosEspeceImposee; |
this.referentielImpose = proprietes.referentielImpose; |
this.isTaxonListe = proprietes.isTaxonListe; |
this.utils = utils; |
this.msgs = utils.msgs; |
} |
this.urlRacine = window.location.origin; |
this.obsNbre = 0; |
this.nbObsEnCours = 1; |
this.totalObsATransmettre = 0; |
this.nbObsTransmises = 0; |
this.debug = null; |
this.html5 = null; |
this.tagProjet = null; |
this.tagImg = null; |
this.tagObs = null; |
this.separationTagImg = null; |
this.separationTagObs = null; |
this.obsId = null; |
this.serviceSaisieUrl = null; |
this.serviceObsUrl = null; |
this.nomSciReferentiel = null; |
this.especeImposee = false; |
this.infosEspeceImposee = null; |
this.isTaxonListe = false; |
this.autocompletionElementsNbre = null; |
this.referentielImpose = null; |
this.serviceAutocompletionNomSciUrl = null; |
this.serviceAutocompletionNomSciUrlTpl = null; |
this.obsMaxNbre = null; |
this.dureeMessage = null; |
this.serviceAnnuaireIdUrl = null; |
this.serviceNomCommuneUrl = null; |
this.serviceNomCommuneUrlAlt = null; |
this.chargementImageIconeUrl = null; |
this.pasDePhotoIconeUrl = null; |
this.observer = null; |
|
// système de traduction minimaliste |
this.msgs = { |
fr: { |
'format-non-supporte': 'Le format de fichier n\'est pas supporté, les formats acceptés sont', |
'image-deja-chargee' : 'Cette image a déjà été utilisée', |
'date-incomplete': 'Format : jj/mm/aaaa. Date incomplète, utiliser 0, exemple : 00/12/2011.', |
'observations-transmises': 'observations transmises', |
'taxon-ou-image' : 'Veuillez transmettre au moins, soit une image, soit une espèce', |
'supprimer-observation-liste': 'Supprimer cette observation de la liste à transmettre', |
'milieu': 'Milieu', |
'commentaires': 'Commentaires', |
'erreur-inconnue': 'Erreur inconnue', |
'non-lie-au-ref': 'non lié au référentiel', |
'obs-le': 'le', |
'quitter-page' : 'Êtes vous sûr de vouloir quiter la page?\nLes observations saisies mais non transmises seront perdues.', |
'courriel-connu' : 'Un compte existe pour ce courriel, connectez-vous pour saisir votre observation', |
'obs-numero' : 'Observation n°', |
'erreur-ajax' : 'Erreur Ajax', |
'erreur' : 'Erreur', |
'erreur-chargement' : 'Erreur lors du chargement de l\'observation', |
'lieu-obs' : 'observé à', |
'lieu-dit' : 'Lieu-dit', |
'station' : 'Station' |
}, |
en: { |
'format-non-supporte': 'The file format is not supported, the accepted formats are', |
'image-deja-chargee' : 'This image has already been used', |
'date-incomplete': 'Format: dd / mm / yyyy. Incomplete date, use 0, example: 00/12/2011.', |
'observations-transmises': 'observations transmitted', |
'taxon-ou-image' : 'Please transmit at least one image or one species', |
'supprimer-observation-liste': 'Delete this observation from the list to be transmitted', |
'milieu': 'Environment', |
'commentaires': 'Comments', |
'erreur-inconnue': 'Unknown error', |
'non-lie-au-ref': 'unrelated to the referencial ', |
'obs-le': 'the', |
'quitter-page' : 'Are you sure you want to leave the page?\nThe observations entered but not transmitted will be lost.', |
'courriel-connu' : 'An account exists for this email, log in to enter your observation', |
'obs-numero' : 'Observation number ', |
'erreur-ajax' : 'Ajax Error', |
'erreur' : 'Error', |
'erreur-chargement' : 'Error loading the observation', |
'lieu-obs' : 'observed at', |
'lieu-dit' : 'Locality', |
'station' : 'Place' |
} |
}; |
this.nbObsTransmises = 0; |
this.observer = null; |
this.isASL = false; |
} |
WidgetSaisie.prototype = new WidgetsSaisiesCommun(); |
|
/** |
* Initialisation du widget |
*/ |
WidgetSaisie.prototype.init = function() { |
this.initForm(); |
this.initEvts(); |
}; |
var valOk = WidgetSaisie.prototype.valOk; |
|
/** |
* Initialise le formulaire, les validateurs, les listes de complétion... |
99,28 → 51,21 |
WidgetSaisie.prototype.initForm = function() { |
const lthis = this; |
|
this.chargerStatutSSO(); |
|
if ( valeurOk( this.obsId ) ) { |
this.initFormConnection(); |
if ( this.valOk( this.obsId ) ) { |
this.chargerInfoObs(); |
} |
|
if( this.isTaxonListe ) { |
this.surChangementTaxonListe(); |
$( '#taxon-liste' ).on( 'change', lthis.surChangementTaxonListe ); |
$( '#taxon-liste' ).on( 'change', lthis.surChangementValeurTaxon.bind( lthis ) ); |
if ( this.debug ) { |
console.log( 'Selected taxon:' + $( '#taxon-liste option:selected' ).val()); |
} |
this.initFormTaxonListe(); |
} else { |
this.ajouterAutocompletionNoms(); |
} |
// au rafraichissement de la page, |
// les input date semblent conserver la valeur entrée précedemment |
// c'est voulu après la création d'un obs mais pas quand la page est actualisée |
// c'est voulu après la création d'une obs mais pas quand la page est actualisée |
// Déjà tenté: onbeforeunload avec un location.reload(true) n'a pas permis de le faire |
$( 'input[type=date]' ).each( function () { |
( valeurOk( $( this ).data( 'default' ) ) ) ? $( this ).val( $( this ).data( 'default' ) ) : $( this ).val( '' ); |
( lthis.valOk( $( this ).data( 'default' ) ) ) ? $( this ).val( $( this ).data( 'default' ) ) : $( this ).val( '' ); |
}); |
this.configurerFormValidator(); |
this.definirReglesFormValidator(); |
158,232 → 103,23 |
WidgetSaisie.prototype.initEvts = function() { |
const lthis = this; |
|
// Empêcher que le module carto ne bind ses events partout |
$( '#tb-geolocation' ).on( 'submit blur click focus mousedown mouseleave mouseup change', '*', function( event ) { |
event.preventDefault(); |
return false; |
}); |
$( '#tb-geolocation' ).on( 'location' , lthis.locationHandler.bind( lthis ) ); |
|
// identité |
$( '#bouton-inscription' ).attr( 'href', this.urlSiteTb() + 'inscription' ); |
$( '#deconnexion a' ).click( function( event ) { |
event.preventDefault(); |
lthis.deconnecterUtilisateur(); |
}); |
$( '#bouton-anonyme' ).on( 'click', function( event ) { |
arreter( event ); |
$( this ).css({ |
'background-color': 'rgba(0, 159, 184, 0.7)', |
'color': '#fff' |
}); |
$( '#anonyme' ).removeClass( 'hidden' ); |
$( '#courriel' ).focus(); |
}); |
if ( '' === $( '#nom-complet').text() ) { |
$( '#courriel' ).on( 'blur', this.requeterIdentiteCourriel.bind( this ) ); |
$( '#courriel' ).on( 'keypress', this.testerLancementRequeteIdentite.bind( this ) ); |
} |
$( '#prenom' ).on( 'change', this.formaterPrenom ); |
$( '#nom' ).on( 'change', this.formaterNom ); |
$( '#courriel_confirmation' ).on( 'paste', this.bloquerCopierCollerCourriel.bind( this ) ); |
|
this.initEvtsConnection(); |
// on location, initialisation de la géoloc |
this.initEvtsGeoloc(); |
// Sur téléchargement image |
$( '#fichier' ).on( 'change', function ( e ) { |
arreter( e ); |
var options = { |
success: lthis.afficherMiniature.bind( lthis ), // post-submit callback |
dataType: 'xml', // 'xml', 'script', or 'json' (expected server response type) |
resetForm: true // reset the form after successful submit |
}; |
$( '#miniature' ).append( '<img id="miniature-chargement" class="miniature" alt="chargement" src="' + this.chargementImageIconeUrl + '"/>' ); |
this.initEvtsFichier(); |
|
var imgCheminTmp = $( '#fichier' ).val(), |
formatImgOk = lthis.verifierFormat( imgCheminTmp ), |
imgNonDupliquee = lthis.verifierDuplication( imgCheminTmp ); |
|
if( formatImgOk && imgNonDupliquee ) { |
$( '#form-upload' ).ajaxSubmit( options ); |
} else { |
$( '#form-upload' )[0].reset(); |
if ( !formatImgOk ) { |
window.alert( lthis.msgTraduction( 'format-non-supporte' ) + ' ' + $( '#fichier' ).attr( 'accept' ) ); |
} |
if ( !imgNonDupliquee ) { |
window.alert( lthis.msgTraduction( 'image-deja-chargee' ) ); |
} |
} |
return false; |
}); |
|
$( 'body' ).on( 'click', '.effacer-miniature', function() { |
$( this ).parent().remove(); |
}); |
|
$( '#referentiel' ).on( 'change', this.surChangementReferentiel.bind( this ) ); |
|
$( '#ajouter-obs' ).on( 'click', this.ajouterObs.bind( this ) ); |
$( '.obs-nbre' ).on( 'changement', this.surChangementNbreObs.bind( this ) ); |
|
$( 'body' ).on( 'click', '.defilement-miniatures-gauche', function( event ) { |
event.preventDefault(); |
lthis.defilerMiniatures( $( this ) ); |
}); |
$( 'body' ).on( 'click', '.defilement-miniatures-droite', function( event ) { |
event.preventDefault(); |
lthis.defilerMiniatures( $( this ) ); |
}); |
$( 'body' ).on( 'click', '.supprimer-obs', function() { |
var that = this, |
suppObs = lthis.supprimerObs.bind( lthis ); |
// bricolage pour avoir les deux contextes en même temps (objet et elt. du DOM) |
suppObs( that ); |
}); |
|
$( '#transmettre-obs' ).on( 'click', this.transmettreObs.bind( this ) ); |
|
// Alertes et tooltips |
$( '.alert .close' ).on( 'click', this.fermerPanneauAlert ); |
$( '#btn-aide' ).on( 'click', this.basculerAffichageAide ); |
$( '.has-tooltip' ).tooltip( 'enable' ); |
// Création / Suppression / Transmission des obs |
// Défilement des miniatures dans le résumé obs |
this.initEvtsObs(); |
// Alertes et aides |
this.initEvtsAlertes(); |
// message avant de quitter le formulaire |
this.confirmerSortie(); |
}; |
|
// Identité Observateur *******************************************************/ |
/** |
* Interroge le SSO pour connaître le statut de l'utilisateur, et change le menu |
* à droite de la barre en fonction |
*/ |
WidgetSaisie.prototype.chargerStatutSSO = function() { |
const lthis = this; |
var urlAuth = this.urlBaseAuth + '/identite'; |
|
$( '#form-observateur' )[0].reset(); |
|
|
if( 'local' !== this.mode ) { |
this.connexion( urlAuth, true ); |
} else { |
urlAuth = window.location.origin + '/widget:cel:modules/saisie2/test-token.json'; |
$( '#connexion' ).click( function( event ) { |
event.preventDefault(); |
lthis.connexion( urlAuth, true ); |
}); |
} |
}; |
|
/** |
* Déconnecte l'utilisateur du SSO |
*/ |
WidgetSaisie.prototype.deconnecterUtilisateur = function() { |
var urlAuth = this.urlBaseAuth + '/deconnexion'; |
|
if( 'local' === this.mode ) { |
this.definirUtilisateur(); |
window.location.reload(); |
return; |
} |
this.connexion( urlAuth, false ); |
}; |
|
WidgetSaisie.prototype.connexion = function( urlAuth, connexionOnOff ) { |
const lthis = this; |
|
$.ajax({ |
url: urlAuth, |
type: "GET", |
dataType: 'json', |
xhrFields: { |
withCredentials: true |
} |
}) |
.done( function( data ) { |
if( connexionOnOff ) { |
// connecté |
lthis.definirUtilisateur( data.token ); |
} else { |
lthis.definirUtilisateur(); |
window.location.reload(); |
} |
}) |
.fail( function( error ) { |
// @TODO gérer l'affichage de l'erreur, mais pas facile à placer |
// dans l'interface actuelle sans que ce soit moche |
//afficherErreurServeur(); |
}); |
} |
|
WidgetSaisie.prototype.definirUtilisateur = function( jeton ) { |
var idUtilisateur = '', |
nomComplet = '', |
courriel = '', |
pseudo = '', |
prenom = '', |
nom = ''; |
|
// affichage |
if ( undefined !== jeton ) { |
// décodage jeton |
var jetonDecode = this.decoderJeton( jeton ); |
// console.log(jetonDecode); |
|
idUtilisateur = jetonDecode.id; |
nomComplet = jetonDecode.intitule; |
courriel = jetonDecode.sub; |
pseudo = jetonDecode.pseudo; |
prenom = jetonDecode.prenom; |
nom = jetonDecode.nom; |
$( '#courriel, #courriel_confirmation' ).attr( 'disabled', 'disabled' ); |
$( '#prenom' ).attr( 'disabled', 'disabled' ); |
$( '#nom' ).attr( 'disabled', 'disabled' ); |
$( '#bouton-connexion, #creation-compte' ).addClass( 'hidden' ); |
$( '#utilisateur-connecte, #anonyme, #zone-courriel-confirmation, #zone-prenom-nom' ).removeClass( 'hidden' ); |
$( '#date-releve' ).focus(); |
} |
$( '.warning' ).remove(); |
$( '#nom-complet' ).html( nomComplet ); |
$( '#courriel, #courriel_confirmation' ).val( courriel ); |
$( '#profil-utilisateur a' ).attr( 'href', this.urlSiteTb() + 'membres/' + pseudo.toLowerCase().replace( ' ', '-' ) ); |
$( '#id_utilisateur' ).val( idUtilisateur ); |
$( '#prenom' ).val( prenom ); |
$( '#nom' ).val( nom ); |
}; |
|
/** |
* Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE |
* JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple |
*/ |
WidgetSaisie.prototype.decoderJeton = function( jeton ) { |
parts = jeton.split( '.' ); |
payload = parts[1]; |
payload = this.b64d( payload ); |
payload = JSON.parse( payload, true ); |
return payload; |
}; |
|
/** |
* Décodage "url-safe" des chaînes base64 retournées par le SSO (lib jwt) |
*/ |
WidgetSaisie.prototype.b64d = function( input ) { |
var remainder = input.length % 4; |
|
if ( 0 !== remainder ) { |
var padlen = 4 - remainder; |
|
for ( var i = 0; i < padlen; i++ ) { |
input += '='; |
} |
} |
input = input.replace( '-', '+' ); |
input = input.replace( '_', '/' ); |
return atob( input ); |
}; |
|
WidgetSaisie.prototype.urlSiteTb = function() { |
var urlPart = ( 'test' === this.mode ) ? '/test/' : '/'; |
|
return window.location.origin + urlPart; |
}; |
|
// Identité Observateur par courriel |
WidgetSaisie.prototype.requeterIdentiteCourriel = function() { |
const lthis = this; |
391,7 → 127,7 |
var courriel = $( '#courriel' ).val(); |
var urlAnnuaire = this.serviceAnnuaireIdUrl + courriel; |
|
if ( valeurOk( courriel ) ) { |
if ( this.valOk( courriel ) ) { |
$.ajax({ |
url : urlAnnuaire, |
type : 'GET', |
399,7 → 135,7 |
if ( lthis.debug ) { |
console.log( 'SUCCESS: ' + textStatus ); |
} |
if ( valeurOk( data ) && valeurOk( data[courriel] ) ) { |
if ( lthis.valOk( data ) && lthis.valOk( data[courriel] ) ) { |
var infos = data[courriel]; |
lthis.surSuccesCompletionCourriel( infos, courriel ); |
} else { |
424,10 → 160,10 |
// se déclanche quand on choisit "Observation sans inscription" mais que le mail entré est incrit à Tela |
WidgetSaisie.prototype.surSuccesCompletionCourriel = function( infos, courriel ) { |
if ( $( '#utilisateur-connecte' ).hasClass( 'hidden' ) ) {// si quelque chose a foiré après actualisation |
if ( !valeurOk( $( '#warning-identite' ) ) ) { |
if ( !this.valOk( $( '#warning-identite' ) ) ) { |
$( '#zone-courriel' ).before( '<p id="warning-identite" class="warning"><i class="fas fa-exclamation-triangle"></i> ' + this.msgTraduction( 'courriel-connu' ) + '</p>' ); |
} |
$( '#bouton-inscription, #zone-prenom-nom, #zone-courriel-confirmation' ).addClass( 'hidden' ); |
$( '#inscription, #zone-prenom-nom, #zone-courriel-confirmation' ).addClass( 'hidden' ); |
$( '#prenom, #nom, #courriel_confirmation' ).attr( 'disabled', 'disabled' ); |
$( '.nav.control-group' ).addClass( 'error' ); |
} |
438,13 → 174,11 |
$( '#creation-compte, #zone-prenom-nom, #zone-courriel-confirmation' ).removeClass( 'hidden' ); |
$( '#warning-identite' ).remove(); |
$( '.nav.control-group' ).removeClass( 'error' ); |
|
$( '#prenom, #nom, #courriel_confirmation' ).val( '' ); |
$( '#prenom, #nom, #courriel_confirmation' ).removeAttr( 'disabled' ); |
$( '#prenom, #nom, #courriel_confirmation' ).val( '' ).removeAttr( 'disabled' ); |
}; |
|
WidgetSaisie.prototype.testerLancementRequeteIdentite = function( event ) { |
if ( valeurOk( event.which, true, 13 ) ) { |
if ( this.valOk( event.which, true, 13 ) ) { |
this.requeterIdentiteCourriel(); |
event.preventDefault(); |
event.stopPropagation(); |
451,13 → 185,33 |
} |
}; |
|
WidgetSaisie.prototype.reduireVoletIdentite = function() { |
if ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() ) { |
$( '#bouton-connexion, #creation-compte' ).addClass( 'hidden' ); |
$( '#bienvenue').removeClass( 'hidden' ); |
$( '#inscription, #zone-courriel' ).addClass( 'hidden' ); |
if ( lthis.valOk( $( '#nom' ).val() ) && lthis.valOk( $( '#prenom' ).val() ) ) { |
$( '#zone-prenom-nom' ).addClass( 'hidden' ); |
$( '#bienvenue-prenom' ).text( ' ' + $( '#prenom' ).val() ); |
$( '#bienvenue-nom' ).text( ' ' + $( '#nom' ).val() ); |
} else { |
$( '#zone-prenom-nom' ).removeClass( 'hidden' ); |
$( '#bienvenue-prenom,#bienvenue-nom' ).text( '' ); |
} |
} else { |
$( '#bouton-connexion, #creation-compte' ).removeClass( 'hidden' ); |
$( '#bienvenue').addClass( 'hidden' ); |
} |
}; |
|
|
WidgetSaisie.prototype.formaterNom = function() { |
$( this ).val( $( this ).val().toUpperCase() ); |
$( '#nom' ).val( $( '#nom' ).val().toUpperCase() ); |
}; |
|
WidgetSaisie.prototype.formaterPrenom = function() { |
var prenom = new Array(), |
mots = $( this ).val().split( ' ' ), |
mots = $( '#prenom' ).val().split( ' ' ), |
motsLength = mots.length; |
|
for ( var i = 0; i < motsLength; i++ ) { |
478,7 → 232,7 |
prenom.push( motMajuscule ); |
} |
} |
$( this ).val( prenom.join( ' ' ) ); |
$( '#prenom' ).val( prenom.join( ' ' ) ); |
}; |
|
WidgetSaisie.prototype.bloquerCopierCollerCourriel = function() { |
496,7 → 250,7 |
url: urlObs, |
type: 'GET', |
success: function( data, textStatus, jqXHR ) { |
if ( valeurOk( data ) ) { |
if ( lthis.valOk( data ) ) { |
lthis.prechargerForm( data ); |
} else { |
lthis.surErreurChargementInfosObs.bind( lthis ); |
510,220 → 264,99 |
|
// @TODO faire mieux que ça ! |
WidgetSaisie.prototype.surErreurChargementInfosObs = function() { |
alert( this.msgTraduction( 'erreur-chargement' ) ); |
this.activerModale( this.msgTraduction( 'erreur-chargement' ) ); |
}; |
|
WidgetSaisie.prototype.prechargerForm = function( data ) { |
|
$( '#milieu' ).val( data.milieu ); |
|
// $( '#carte-recherche' ).val( data.zoneGeo ); |
// $( '#commune-nom' ).text( data.zoneGeo ); |
|
// if( data.hasOwnProperty( 'codeZoneGeo' ) ) { |
// // TODO: trouver un moyen qui fonctionne lorsqu'on aura d'autres référentiels que INSEE |
// $( '#commune-code-insee' ).text( data.codeZoneGeo.replace( 'INSEE-C:', '' ) ); |
// } |
|
// if( data.hasOwnProperty( 'latitude' ) && data.hasOwnProperty( 'longitude' ) ) { |
// var latLng = new google.maps.LatLng( data.latitude, data.longitude ); |
// this.mettreAJourMarkerPosition( latLng ); |
// this.marker.setPosition( latLng ); |
// this.map.setCenter( latLng ); |
// this.map.setZoom( 16 ); |
// } |
}; |
|
// uniquement utilisé si taxon-liste ******************************************/ |
// Affiche/Cache le champ taxon |
WidgetSaisie.prototype.surChangementTaxonListe = function() { |
if ( valeurOk( $( '#taxon-liste' ).val() ) ) { |
if ( 'autre' !== $( '#taxon-liste' ).val() ) { |
$( '#taxon-input-groupe' ) |
.hide( 200, function () { |
$( this ).addClass( 'hidden' ).show(); |
}) |
.find( '#taxon-autre' ).val( '' ); |
} else { |
$( '#taxon-input-groupe' ) |
.hide() |
.removeClass( 'hidden' ) |
.show(200) |
.find( '#taxon-autre' ) |
.on( 'change', function() { |
if( !valeurOk( $( '#taxon-autre' ).data( 'numNomSel' ) ) ) { |
$( '#taxon' ).val( $( '#taxon-autre' ).val() ); |
$( '#taxon' ).data( 'numNomSel', '' ) |
.data( 'nomRet','' ) |
.data( 'numNomRet', '' ) |
.data( 'nt', '' ) |
.data( 'famille', '' ); |
} |
$( '#taxon' ).trigger( 'change' ); |
}); |
} |
$( '#commune-nom' ).text( data.zoneGeo ); |
if( data.hasOwnProperty( 'codeZoneGeo' ) ) { |
// TODO: trouver un moyen qui fonctionne lorsqu'on aura d'autres référentiels que INSEE |
$( '#commune-insee' ).text( data.codeZoneGeo.replace( 'INSEE-C:', '' ) ); |
} |
}; |
|
WidgetSaisie.prototype.surChangementValeurTaxon = function() { |
var numNomSel = 0; |
|
if( valeurOk( $( '#taxon-liste' ).val() ) ) { |
if( 'autre' === $( '#taxon-liste' ).val() ) { |
this.ajouterAutocompletionNoms(); |
} else { |
var optionRetenue = $( '#taxon-liste' ).find( 'option[value="' + $( '#taxon-liste' ).val() + '"]' ); |
// $( '#taxon' ).val( $( '#taxon-liste' ).val() ); |
$( '#taxon' ).val( $( '#taxon-liste' ).val() ) |
.data( 'value', $( '#taxon-liste' ).val() ) |
.data( 'numNomSel', optionRetenue.data( 'num-nom-sel' ) ) |
.data( 'nomRet', optionRetenue.data( 'nom-ret' ) ) |
.data( 'numNomRet', optionRetenue.data( 'num-nom-ret' ) ) |
.data( 'nt', optionRetenue.data( 'nt' ) ) |
.data( 'famille', optionRetenue.data( 'famille' ) ); |
$( '#taxon' ).trigger( 'change' ); |
|
numNomSel = $( '#taxon' ).data( 'numNomSel' ); |
// Si l'espèce est mal déterminée la certitude est "à déterminer" |
if( !valeurOk( numNomSel ) ) { |
$( '#certitude' ).find( 'option' ).each( function() { |
if ( $( this ).hasClass( 'aDeterminer' ) ) { |
$( this ).attr( 'selected', true ); |
} else { |
$( this ).attr( 'selected', false ); |
} |
}); |
} |
} |
if( data.hasOwnProperty( 'latitude' ) && data.hasOwnProperty( 'longitude' ) ) { |
// $cartoRemplacee = $( '#tb-geolocation' ), |
// suffixe = '', |
// layer = 'osm', |
// zoomInit = 18 |
var typeLocalisation = $( '#top' ).data( 'type-loc' ), |
donnesResetCarto = { |
latitude : data.latitude, |
longitude : data.longitude, |
typeLocalisation : typeLocalisation |
}; |
this.transfererCarto( donnesResetCarto ); |
} |
}; |
|
// Autocompletion taxons ******************************************************/ |
WidgetSaisie.prototype.ajouterAutocompletionNoms = function() { |
// Ajouter Obs ****************************************************************/ |
/** |
* Retourne un Array contenant les valeurs des champs étendus |
*/ |
WidgetSaisie.prototype.getObsChpSpecifiques = function() { |
const lthis = this; |
|
var taxonSelecteur = '#taxon'; |
var champs = new Array(), |
$thisForm = $( '#form-supp' ), |
elements = |
'input[type=text]:not(.collect-other),'+ |
'input[type=checkbox]:checked,'+ |
'input[type=radio]:checked,'+ |
'input[type=email],'+ |
'input[type=number],'+ |
'input[type=range],'+ |
'input[type=date],'+ |
'textarea,'+ |
'.select', |
retour = new Array(); |
|
if ( valeurOk( $( '#taxon-liste' ).val(), true, 'autre' ) ) { |
taxonSelecteur += '-autre'; |
} |
$( taxonSelecteur ).autocomplete({ |
source: function( requete, add ) { |
// la variable de requête doit être vidée car sinon le parametre "term" est ajouté |
requete = ''; |
if( valeurOk( $( '#referentiel' ).val(), false, 'autre' ) ) { |
var url = lthis.getUrlAutocompletionNomsSci(); |
$.getJSON( url, requete, function( data ) { |
var suggestions = lthis.traiterRetourNomsSci( data ); |
add( suggestions ); |
}) |
.fail( function() { |
$( '#certitude' ).find( 'option' ).each( function() { |
if ( $( this ).hasClass( 'aDeterminer' ) ) { |
$( this ).attr( 'selected', true ); |
} else { |
$( this ).attr( 'selected', false ); |
} |
}); |
}); |
$( elements, $thisForm ).each( function() { |
if ( lthis.valOk( $( this ).val() ) && ( lthis.valOk( $( this ).attr( 'name' ) ) || lthis.valOk( $( this ).data( 'name' ) ) ) ) { |
var valeur = $( this ).val(), |
cle = ( lthis.valOk( $( this ).attr( 'name' ) ) ) ? $( this ).attr( 'name' ) : $( this ).data( 'name' ); |
if ( cle in champs ) { |
champs[cle] += ';' + valeur; |
} else { |
champs[cle] = valeur; |
} |
}, |
html: true |
} |
}); |
$( taxonSelecteur ).on( 'autocompleteselect', this.surAutocompletionTaxon ); |
}; |
|
WidgetSaisie.prototype.getUrlAutocompletionNomsSci = function() { |
var taxonSelecteur = '#taxon'; |
|
if ( valeurOk( $( '#taxon-liste' ).val(), true, 'autre' ) ) { |
taxonSelecteur += '-autre'; |
for ( var key in champs ) { |
retour.push({ 'cle' : key , 'valeur' : champs[key] }); |
} |
|
var mots = $( taxonSelecteur ).val(); |
var url = this.serviceAutocompletionNomSciUrlTpl.replace( '{referentiel}', this.nomSciReferentiel ); |
|
url = url.replace( '{masque}', mots ); |
return url; |
}; |
|
WidgetSaisie.prototype.traiterRetourNomsSci = function( data ) { |
var taxonSelecteur = '#taxon'; |
|
if ( valeurOk ( $( '#taxon-liste' ).val(), true, 'autre' ) ) { |
taxonSelecteur += '-autre'; |
if ( this.valOk( $( '#coord-lineaire' ).val() ) ) { |
retour.push({ 'cle' : 'coordonnees-rue-ou-lineaire' , 'valeur' : $( '#coord-lineaire' ).val() }); |
} |
var suggestions = []; |
if ( undefined != data.resultat ) { |
$.each( data.resultat, function( i, val ) { |
val.nn = i; |
var nom = { |
label : '', |
value : '', |
nt : 0, |
nomSel : '', |
nomSelComplet : '', |
numNomSel : 0, |
nomRet : '', |
numNomRet : 0, |
famille : '', |
retenu : false |
}; |
if ( suggestions.length >= this.autocompletionElementsNbre ) { |
nom.label = '...'; |
nom.value = $( taxonSelecteur ).val(); |
suggestions.push( nom ); |
return false; |
} else { |
nom.label = val.nom_sci_complet; |
nom.value = val.nom_sci_complet; |
nom.nt = val.num_taxonomique; |
nom.nomSel = val.nom_sci; |
nom.nomSelComplet = val.nom_sci_complet; |
nom.numNomSel = val.nn; |
nom.nomRet = val.nom_retenu_complet; |
nom.numNomRet = val['nom_retenu.id']; |
nom.famille = val.famille; |
// Tester dans ce sens, permet de considérer 'absent' comme 'false' => est-ce opportun ? |
// en tout cas c'est harmonisé avec le CeL |
nom.retenu = ( 'true' == val.retenu ); |
|
suggestions.push( nom ); |
} |
}); |
} |
return suggestions; |
return retour; |
}; |
|
WidgetSaisie.prototype.surAutocompletionTaxon = function( event, ui ) { |
if ( valeurOk( ui ) ) { |
$( '#taxon' ).val( ui.item.value ); |
$( '#taxon' ).data( 'value', ui.item.value ) |
.data( 'numNomSel', ui.item.numNomSel ) |
.data( 'nomRet', ui.item.nomRet ) |
.data( 'numNomRet', ui.item.numNomRet ) |
.data( 'nt', ui.item.nt ) |
.data( 'famille', ui.item.famille ); |
if ( ui.item.retenu ) { |
$( '#taxon' ).addClass( 'ns-retenu' ); |
} else { |
$( '#taxon' ).removeClass( 'ns-retenu' ); |
} |
// Si l'espèce est mal déterminée la certitude est "à déterminer" |
if( !valeurOk( $( '#taxon' ).data( 'numNomSel' ) ) ) { |
$( '#certitude' ).find( 'option' ).each( function() { |
if ( $( this ).hasClass( 'aDeterminer' ) ) { |
WidgetSaisie.prototype.reinitialiserForm = function() { |
this.supprimerMiniatures(); |
if( !this.especeImposee ) { |
$( '#taxon' ).val( '' ); |
$( '#taxon' ).data( 'numNomSel', '' ) |
.data( 'nomRet','' ) |
.data( 'numNomRet', '' ) |
.data( 'nt', '' ) |
.data( 'famille', '' ); |
if( this.isTaxonListe ) { |
$( '#taxon-liste' ).find( 'option' ).each( function() { |
if ( $( this ).hasClass( 'choisir' ) ) { |
$( this ).attr( 'selected', true ); |
} else { |
$( this ).attr( 'selected', false ); |
} |
}); |
$( '#taxon-input-groupe' ).addClass( 'hidden' ); |
$('#taxon-autre').val(''); |
} |
} |
$( '#taxon' ).trigger( 'change' ); |
if ( this.valOk( $( '#form-supp' ) ) ) { |
$( '#form-supp' ).validate().resetForm(); |
} |
}; |
|
|
// Géolocalisation *************************************************************/ |
/** |
* Fonction handler de l'évenement location du module tb-geoloc |
731,10 → 364,10 |
WidgetSaisie.prototype.locationHandler = function( location ) { |
var locDatas = location.originalEvent.detail; |
|
if ( valeurOk( locDatas ) ) { |
if ( this.valOk( locDatas ) ) { |
console.log( locDatas ); |
var altitude = ( valeurOk( locDatas.elevation ) ) ? locDatas.elevation : ''; |
var pays = ( valeurOk( locDatas.osmCountryCode ) ) ? locDatas.osmCountryCode.toUpperCase() : 'FR'; |
var altitude = ( this.valOk( locDatas.elevation ) ) ? locDatas.elevation : ''; |
var pays = ( this.valOk( locDatas.osmCountryCode ) ) ? locDatas.osmCountryCode.toUpperCase() : 'FR'; |
var latitude = ''; |
var longitude = ''; |
var coordLineaire = ''; |
741,30 → 374,30 |
var nomCommune = ''; |
var communeInsee = ''; |
|
if ( valeurOk( locDatas.geometry.coordinates ) ) { |
if ( this.valOk( locDatas.geometry.coordinates ) ) { |
if ( 'Point' === locDatas.geometry.type ) { |
if ( valeurOk( locDatas.geometry.coordinates[0] ) ) { |
if ( this.valOk( locDatas.geometry.coordinates[0] ) ) { |
longitude = locDatas.geometry.coordinates[0]; |
} |
if ( valeurOk( locDatas.geometry.coordinates[1] ) ) { |
if ( this.valOk( locDatas.geometry.coordinates[1] ) ) { |
latitude = locDatas.geometry.coordinates[1]; |
} |
} else if ( 'LineString' === locDatas.geometry.type ) {// on a besoin que d'un point de la rue |
coordLineaire = JSON.stringify( locDatas.geometry.coordinates ); |
if ( valeurOk( locDatas.geometry.coordinates[0][0] ) ) { |
if ( this.valOk( locDatas.geometry.coordinates[0][0] ) ) { |
longitude = locDatas.geometry.coordinates[0][0]; |
} |
if ( valeurOk( locDatas.geometry.coordinates[0][1] ) ){ |
if ( this.valOk( locDatas.geometry.coordinates[0][1] ) ){ |
latitude = locDatas.geometry.coordinates[0][1]; |
} |
} |
} |
if ( valeurOk( locDatas.inseeData ) ) { |
if ( this.valOk( locDatas.inseeData ) ) { |
nomCommune = locDatas.inseeData.nom; |
communeInsee = ( valeurOk( locDatas.inseeData.code ) ) ? locDatas.inseeData.code : ''; |
} else if ( valeurOk( locDatas.locality ) ) { |
communeInsee = ( this.valOk( locDatas.inseeData.code ) ) ? locDatas.inseeData.code : ''; |
} else if ( this.valOk( locDatas.locality ) ) { |
nomCommune = locDatas.locality; |
} else if ( valeurOk( locDatas.locality ) ) { |
} else if ( this.valOk( locDatas.locality ) ) { |
nomCommune = locDatas.osmCounty; |
} |
$( '#coord-lineaire' ).val( coordLineaire ); |
774,7 → 407,7 |
$( '#commune-insee' ).val( communeInsee ); |
$( '#altitude' ).val( altitude ); |
$( '#pays' ).val( pays ); |
if ( valeurOk( $( '#latitude' ).val() ) && valeurOk( $( '#longitude' ).val() ) ) { |
if ( this.valOk( $( '#latitude' ).val() ) && this.valOk( $( '#longitude' ).val() ) ) { |
$( '#geoloc' ).closest( '.control-group' ).removeClass( 'error' ); |
} else { |
$( '#geoloc' ).closest( '.control-group' ).addClass( 'error' ); |
785,69 → 418,6 |
} |
|
// Form Validator *************************************************************/ |
WidgetSaisie.prototype.configurerFormValidator = function() { |
const lthis = this; |
|
$.validator.addMethod( |
'dateCel', |
function ( value, element ) { |
return ( valeurOk( value ) && ( /^(?:[0-9]{4}-[0-9]{2}-[0-9]{2})|(?:[0-9]{2}\/[0-9]{2}\/[0-9]{4})$/.test( value ) ) ); |
}, |
lthis.msgTraduction( 'date-incomplete' ) |
); |
$.validator.addMethod( |
'userEmailOk', |
function ( value, element ) { |
return ( valeurOk( value ) ); |
}, |
'' |
); |
$.validator.addMethod( |
'minMaxOk', |
function ( value, element, param ) { |
$.validator.messages.minMaxOk = lthis.validerMinMax( element ).message; |
return lthis.validerMinMax( element ).cond; |
}, |
$.validator.messages.minMaxOk |
); |
$.validator.addMethod( |
'listFields', |
function ( value, element ) { |
return ( valeurOk( value ) ); |
}, |
'' |
); |
$.extend( $.validator.defaults, { |
errorElement: 'span', |
onfocusout: function( element ) { |
if( valeurOk( element.id, false, 'taxon' ) ) { |
if ( $( element ).valid() ) { |
$( element ).closest( '.control-group' ).removeClass( 'error' ); |
} else { |
$( element ).closest( '.control-group' ).addClass( 'error' ); |
} |
} |
}, |
onkeyup : function( element ) { |
if( valeurOk( element.id, false, 'taxon' ) ) { |
if ( $( element ).valid() ) { |
$( element ).closest( '.control-group' ).removeClass( 'error' ); |
} else { |
$( element ).closest( '.control-group' ).addClass( 'error' ); |
} |
} |
}, |
unhighlight: function( element ) { |
if( valeurOk( element.id, false, 'taxon' ) ) { |
$( element ).closest( '.control-group' ).removeClass( 'error' ); |
} |
}, |
highlight: function( element ) { |
$( element ).closest( '.control-group' ).addClass( 'error' ); |
} |
}); |
}; |
|
WidgetSaisie.prototype.chpEtendusValidation = function() { |
const lthis = this; |
|
876,13 → 446,13 |
|
$( elements, $thisForm ).each( function() { |
for( var fieldsClass = 0; spefieldsCount > fieldsClass; fieldsClass++ ) { |
if ( valeurOk( $( this ).attr( 'required' ) ) && $( this ).hasClass( speFields[fieldsClass] ) && !valeurOk( chpSuppValidation.rules[ dataName ] ) ) { |
if ( lthis.valOk( $( this ).attr( 'required' ) ) && $( this ).hasClass( speFields[fieldsClass] ) && !lthis.valOk( chpSuppValidation.rules[ dataName ] ) ) { |
|
var dataName = $( this ).data( 'name' ); |
|
namesListFields.push( dataName ); |
chpSuppValidation.rules[ dataName ] = { required : true }; |
if ( valeurOk( $( '.other', $( this ) ) ) ) { |
if ( lthis.valOk( $( '.other', $( this ) ) ) ) { |
picked = ( 'select' === speFields[fieldsClass] ) ? ':selected' : ':checked'; |
chpSuppValidation.rules[ 'collect-other-' + dataName.replace( '[]', '' ) ] = { |
required : '#other-' + dataName.replace( '[]', '' ) + picked, |
895,11 → 465,11 |
errors[dataName] = '.' + speFields[fieldsClass]; |
} |
} |
if ( valeurOk( $( this ).attr( 'name' ) ) && valeurOk ( $( this ).attr( 'required' ) ) && 0 > $.inArray( $( this ).attr( 'name' ) , namesListFields ) ) { |
if ( lthis.valOk( $( this ).attr( 'name' ) ) && lthis.valOk ( $( this ).attr( 'required' ) ) && 0 > $.inArray( $( this ).attr( 'name' ) , namesListFields ) ) { |
chpSuppValidation.rules[ $( this ).attr( 'name' ) ] = { required : true, minlength: 1 }; |
if( |
( valeurOk( $( this ).attr( 'type' ), true, 'number' ) || valeurOk( $( this ).attr( 'type' ), true, 'range' ) ) && |
( valeurOk( $( this )[0].min ) || valeurOk( $( this )[0].max ) ) |
( lthis.valOk( $( this ).attr( 'type' ), true, 'number' ) || lthis.valOk( $( this ).attr( 'type' ), true, 'range' ) ) && |
( lthis.valOk( $( this )[0].min ) || lthis.valOk( $( this )[0].max ) ) |
) { |
chpSuppValidation.rules[ $( this ).attr('name') ]['minMaxOk'] = true; |
chpSuppValidation.messages[ $( this ).attr('name') ] = lthis.validerMinMax( $( this )[0] ).message; |
906,9 → 476,9 |
} |
} |
}); |
if ( valeurOk( chpSuppValidation.rules ) ) { |
if ( this.valOk( chpSuppValidation.rules ) ) { |
$.each( chpSuppValidation.rules, function( key ) { |
if ( !valeurOk( chpSuppValidation.messages[key] ) ) { |
if ( !lthis.valOk( chpSuppValidation.messages[key] ) ) { |
chpSuppValidation.messages[key] = 'Ce champ est requis :\nVeuillez entrer une valeur valide.'; |
} |
}); |
920,7 → 490,6 |
}; |
|
WidgetSaisie.prototype.validerMinMax = function( element ) { |
|
var mMCond = new Boolean(), |
minCond = parseFloat( element.value ) >= parseFloat( element.min ), |
maxCond = parseFloat( element.value ) <= parseFloat( element.max ), |
928,8 → 497,8 |
returnMnMx = { cond : true , message : '' }; |
|
if( |
( valeurOk( element.type, true, 'number' ) || valeurOk( element.type, true, 'range' ) ) && |
( valeurOk( element.min ) || valeurOk( element.max ) ) |
( this.valOk( element.type, true, 'number' ) || this.valOk( element.type, true, 'range' ) ) && |
( this.valOk( element.min ) || this.valOk( element.max ) ) |
) { |
if ( element.min && element.max ) { |
messageMnMx += ' comprise entre ' + element.min + ' et ' + element.max; |
948,9 → 517,9 |
|
}; |
|
|
WidgetSaisie.prototype.definirReglesFormValidator = function() { |
const lthis = this; |
|
var formSuppValidation = this.chpEtendusValidation(); |
|
$( '#form-supp' ).validate({ |
957,13 → 526,13 |
onclick : function( element ) { |
if ( |
( |
valeurOk( element.type, true, 'checkbox' ) || |
valeurOk( element.type, true, 'radio' ) |
lthis.valOk( element.type, true, 'checkbox' ) || |
lthis.valOk( element.type, true, 'radio' ) |
) && |
( |
!valeurOk( $( '.' + $( element ).attr( 'name' ).replace( '[]', '' ) + ':checked' ) ) || |
valeurOk( $( '.' + $( element ).attr( 'name' ).replace( '[]', '' ) + ':not(.other):checked' ) ) || |
!valeurOk( $( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ) ) || |
!lthis.valOk( $( '.' + $( element ).attr( 'name' ).replace( '[]', '' ) + ':checked' ) ) || |
lthis.valOk( $( '.' + $( element ).attr( 'name' ).replace( '[]', '' ) + ':not(.other):checked' ) ) || |
!lthis.valOk( $( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ) ) || |
$( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ).is( ':checked' ) || |
( |
$( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ).is( ':checked' ) && |
1011,8 → 580,8 |
}); |
// Validation Taxon si pas de miniature |
$( '#taxon' ).on( 'change', function() { |
var images = valeurOk( $( '#miniatures .miniature' ) ); |
lthis.validerTaxonImage( valeurOk( $( this ).val() ), images ); |
var images = lthis.valOk( $( '#miniatures .miniature' ) ); |
lthis.validerTaxonImage( lthis.valOk( $( this ).val() ), images ); |
}); |
// Validation miniatures avec MutationObserver |
this.surPresenceAbsenceMiniature(); |
1047,7 → 616,7 |
} |
} |
}); |
$( '#connexion,#bouton-inscription,#bouton-anonyme' ).click( function( event ) { |
$( '#connexion,#inscription,#bouton-anonyme' ).on( 'click', function( event ) { |
$( '.nav.control-group' ).removeClass( 'error' ); |
}); |
}; |
1079,7 → 648,7 |
// Fonction callback à éxécuter quand une mutation est observée |
var callback = function( mutationsList ) { |
for( var mutation of mutationsList ) { |
var taxon = valeurOk( $( '#taxon' ).val() ); |
var taxon = lthis.valOk( $( '#taxon' ).val() ); |
|
images = ( 0 < mutation.target.childElementCount ); |
lthis.validerTaxonImage( taxon, images ); |
1091,23 → 660,24 |
this.observer.observe( targetNode, { childList: true } ); |
}; |
|
WidgetSaisie.prototype.validerFormulaire = function() { |
var observateur = ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() ) |
WidgetSaisie.prototype.validerForm = function() { |
const lthis = this; |
|
var observateur = ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() ); |
var obs = $( '#form-observation' ).valid(); |
var geoloc = ( valeurOk( $( '#latitude' ).val() ) && valeurOk( $( '#longitude' ).val() ) ) ; |
var images = valeurOk( $( '#miniatures .miniature' ) ); |
var taxon = valeurOk( $( '#taxon' ).val() ); |
var geoloc = ( this.valOk( $( '#latitude' ).val() ) && this.valOk( $( '#longitude' ).val() ) ) ; |
var images = this.valOk( $( '#miniatures .miniature' ) ); |
var taxon = this.valOk( $( '#taxon' ).val() ); |
// validation et panneau taxon/images |
var taxonOuImage = this.validerTaxonImage( taxon, images ); |
// console.log(taxonOuImage); |
var chpsSupp = new Boolean(); |
if ( valeurOk( $( '#form-supp' ) ) ) { |
if ( this.valOk( $( '#form-supp' ) ) ) { |
chpsSupp = ( function () { |
var otherFlag = $( '#form-supp' ).valid(); |
if( valeurOk( $( '.other', $( '#form-supp' ) ) ) ) { |
if( lthis.valOk( $( '.other', $( '#form-supp' ) ) ) ) { |
$( '.other', $( '#form-supp' ) ).each( function() { |
var picked = ( $( this ).data( 'element' ) !== 'select' ) ? ':checked' : ':selected'; |
if ( $( this ).is( picked ) && valeurOk( $( this ).val(), true, 'other' ) ) { |
if ( $( this ).is( picked ) && lthis.valOk( $( this ).val(), true, 'other' ) ) { |
otherFlag = false; |
} |
}); |
1136,87 → 706,6 |
return ( observateur && obs && geoloc && taxonOuImage && chpsSupp ); |
}; |
|
// Fichier Images *************************************************************/ |
/** |
* Affiche la miniature d'une image temporaire (formulaire) qu'on a ajoutée à l'obs |
*/ |
WidgetSaisie.prototype.afficherMiniature = function( reponse ) { |
if ( this.debug ) { |
var debogage = $( 'debogage', reponse ).text(); |
//console.log( 'Débogage upload : '+debogage); |
} |
|
var message = $( 'message', reponse ).text(); |
|
if ( valeurOk( message ) ) { |
$( '#miniature-msg' ).append( message ); |
} else { |
$( '#miniatures' ).append( this.creerWidgetMiniature( reponse ) ); |
} |
$( '#ajouter-obs' ).removeAttr( 'disabled' ); |
}; |
|
/** |
* Crée la miniature d'une image temporaire (formulaire), avec le bouton pour l'effacer |
*/ |
WidgetSaisie.prototype.creerWidgetMiniature = function( reponse ) { |
var miniatureUrl = $( 'miniature-url', reponse ).text(); |
var imgNom = $( 'image-nom', reponse ).text(); |
var html = |
'<div class="miniature mb-3 mr-3">'+ |
'<img class="miniature-img" class="miniature img-rounded" alt="' + imgNom + '" src="' + miniatureUrl + '"/>'+ |
'<a class="effacer-miniature"><i class="far fa-trash-alt"></i></a>'+ |
'</div>'; |
|
return html; |
}; |
|
/** |
* Retourne true si l'extension de l'image 'nom' est .jpg ou .jpeg |
*/ |
WidgetSaisie.prototype.verifierFormat = function( nom ) { |
var parts = nom.split( '.' ); |
|
extension = parts[ parts.length - 1 ]; |
return ( 'jpeg' === extension.toLowerCase() || 'jpg' === extension.toLowerCase() ); |
}; |
|
/** |
* Check les miniatures déjà téléchargées |
* renvoie false si le même nom est rencontré 2 fois |
* renvoie true sinon |
*/ |
WidgetSaisie.prototype.verifierDuplication = function( cheminTmp ) { |
var parts = cheminTmp.split( '\\' ), |
nomImage = parts[ parts.length - 1 ], |
thisSrcParts = [], |
thisNomImage = '', |
nonDupliquee = true; |
|
$( 'img.miniature-img,img.miniature' ).each( function() { |
// vérification avec alt de l'image |
if ( valeurOk( $( this ).attr ( 'alt' ), true, nomImage ) ) { |
nonDupliquee = false; |
return false;// Pas besoin de poursuivre la boucle |
} else { // sinon vérifie aussi avec l'adresse (src) de l'image |
thisSrcParts = $( this ).attr( 'src' ).split( '/' ); |
thisNomImage = thisSrcParts[ thisSrcParts.length - 1 ].replace( '_min', '' ); |
if ( valeurOk( thisNomImage, true, nomImage ) ) { |
nonDupliquee = false; |
return false; |
} |
} |
}); |
return nonDupliquee; |
}; |
|
/** |
* Efface une miniature (formulaire) |
*/ |
WidgetSaisie.prototype.supprimerMiniature = function( miniature ) { |
miniature.parents( '.miniature' ).remove(); |
}; |
|
// Referentiel ****************************************************************/ |
// N'est pas utilisé en cas de taxon-liste |
WidgetSaisie.prototype.surChangementReferentiel = function() { |
1225,739 → 714,3 |
$( '#taxon' ).val( '' ); |
$( '#taxon' ).data( 'numNomSel', '' ); |
}; |
|
// Ajouter Obs ****************************************************************/ |
/** |
* Ajoute une observation saisie dans le formulaire à la liste des observations à transmettre |
*/ |
WidgetSaisie.prototype.ajouterObs = function() { |
// Fermeture automatique des dialogue de transmission de données |
// @WARNING TEST |
$( '#dialogue-obs-transaction-ko' ).addClass( 'hidden' ); |
$( '#dialogue-obs-transaction-ok' ).addClass( 'hidden' ); |
if ( this.validerFormulaire() ) { |
this.masquerPanneau( '#dialogue-form-invalide' ); |
this.obsNbre = this.obsNbre + 1; |
$( '.obs-nbre' ).text( this.obsNbre ); |
$( '.obs-nbre' ).triggerHandler( 'changement' ); |
this.afficherObs(); |
this.stockerObsData(); |
this.supprimerMiniatures(); |
if( !this.especeImposee ) { |
$( '#taxon' ).val( '' ); |
$( '#taxon' ).data( 'numNomSel', '' ) |
.data( 'nomRet','' ) |
.data( 'numNomRet', '' ) |
.data( 'nt', '' ) |
.data( 'famille', '' ); |
if( this.isTaxonListe ) { |
$( '#taxon-liste' ).find( 'option' ).each( function() { |
if ( $( this ).hasClass( 'choisir' ) ) { |
$( this ).attr( 'selected', true ); |
} else { |
$( this ).attr( 'selected', false ); |
} |
}); |
$( '#taxon-input-groupe' ).addClass( 'hidden' ); |
$('#taxon-autre').val(''); |
} |
} |
if ( valeurOk( $( '#form-supp' ) ) ) { |
$( '#form-supp' ).validate().resetForm(); |
// if ( valeurOk( $( '#form-supp' ) ) ) { |
} |
$( '#barre-progression-upload' ).attr( 'aria-valuemax', this.obsNbre ); |
$( '#barre-progression-upload .sr-only' ).text( '0/' + this.obsNbre + ' ' + this.msgTraduction( 'observations-transmises' ) ); |
} else { |
this.afficherPanneau( '#dialogue-form-invalide' ); |
} |
}; |
|
/** |
* Affiche une observation dans la liste des observations à transmettre |
*/ |
WidgetSaisie.prototype.afficherObs = function() { |
var numNomSel = $( '#taxon' ).data( 'numNomSel' ), |
taxon = $( '#taxon' ).val(), |
referentiel = '', |
commune = '', |
inseeLatitudeLongitude = '', |
lieudit = '', |
station = '', |
milieu = '', |
commentaires = ''; |
|
if ( valeurOk( numNomSel ) ) { |
referentiel = '<span class="referentiel-obs">' + '[' + this.nomSciReferentiel + ']' + '</span>'; |
} |
if ( valeurOk( $( '#commune-nom' ).val() ) ) { |
commune = ' ' + this.msgTraduction( 'lieu-obs' ) + ' <span class="commune">' + $( '#commune-nom' ).val() + '</span> '; |
} |
if ( valeurOk( $( '#commune-insee' ).val() ) && valeurOk( $( '#latitude' ).val() ) && valeurOk( $( '#longitude' ).val() ) ) { |
inseeLatitudeLongitude = '(INSEE Commune:' + $('#commune-insee').val() + ') [' + $( '#latitude' ).val() + ' / ' + $( '#longitude' ).val() + ']'; |
} |
if ( valeurOk( $( '#lieudit' ).val() ) ) { |
lieudit = '<span>' + this.msgTraduction( 'lieu-dit' ) + ' :</span> ' + $( '#lieudit' ).val() + ' '; |
} |
if ( valeurOk( $( '#station' ).val() ) ) { |
station = '<span>' + this.msgTraduction( 'station' ) + ' :</span> ' + $( '#station' ).val() + ' '; |
} |
if ( valeurOk( $( '#milieu' ).val() ) ) { |
milieu = '<span>' + this.msgTraduction( 'milieu' ) + ' :</span> ' + $( '#milieu' ).val() + ' '; |
} |
if ( valeurOk( $( '#notes' ).val() ) ) { |
commentaires = this.msgTraduction( 'commentaires' ) + ' : <span>' + $( '#notes' ).val() + '</span>' + ' '; |
} |
|
var responsivDiff1 = '', |
responsivDiff2 = '', |
responsivDiff3 = '', |
responsivDiff4 = '', |
responsivDiff5 = '', |
responsivDiff6 = ''; |
if ( window.matchMedia( '(min-width: 576px)' ).matches ) { |
/* La largeur minimum de l'affichage est 600 px inclus */ |
responsivDiff1 = ' droite'; |
responsivDiff2 = '<div></div>'; |
responsivDiff3 = '<div class="row">'; |
responsivDiff4 = ' col-md-2 col-sm-4'; |
responsivDiff5 = ' class="col-md-9 col-sm-7"'; |
responsivDiff6 = '</div>'; |
} |
|
|
$( '#liste-obs' ).prepend( |
'<div id="obs' + this.obsNbre + '" class="obs obs' + this.obsNbre + ' mb-2">'+ |
|
'<div '+ |
'class="obs-action" '+ |
'title="' + this.msgTraduction( 'supprimer-observation-liste' ) + '"'+ |
'>'+ |
'<button class="btn btn-danger supprimer-obs' + responsivDiff1 + '" value="'+ this.obsNbre + '" title="' + this.msgTraduction( 'obs-numero' ) + this.obsNbre + '">'+ |
'<i class="far fa-trash-alt"></i>'+ |
'</button>'+ |
responsivDiff2 + |
'</div> '+ |
|
responsivDiff3 + |
'<div class="thumbnail' + responsivDiff4 + '">'+ |
this.ajouterImgMiniatureAuTransfert()+ |
'</div>'+ |
'<div' + responsivDiff5 + '>'+ |
'<ul class="unstyled">'+ |
'<li>'+ |
'<span class="nom-sci">' + taxon + '</span> '+ |
this.ajouterNumNomSel( numNomSel ) + |
referentiel + |
commune + |
inseeLatitudeLongitude + |
' ' + this.msgTraduction( 'obs-le' ) + ' ' + |
'<span class="date">' + this.fournirDate( $( '#date_releve' ).val() ) + '</span>'+ |
'</li>'+ |
'<li>'+ |
lieudit + |
station + |
milieu + |
'</li>'+ |
'<li>'+ |
commentaires + |
'</li>'+ |
'</ul>'+ |
'</div>'+ |
responsivDiff6+ |
'</div>' |
); |
|
$( '#zone-liste-obs' ).removeClass( 'hidden' ); |
}; |
|
/** |
* Ajoute une boîte de miniatures avec défilement des images, |
* pour une obs de la liste des obs à transmettre |
*/ |
WidgetSaisie.prototype.ajouterImgMiniatureAuTransfert = function() { |
var html = |
'<div class="defilement-miniatures">'+ |
'<figure class="centre">'+ |
'<img class="miniature align-middle" alt="Aucune photo" src="' + this.pasDePhotoIconeUrl + '" width="80%" />'+ |
'</figure>'+ |
'</div>', |
miniatures = '', |
premiere = true, |
centre = ''; |
defilVisible = ''; |
|
if ( valeurOk( $( '#miniatures img' ) ) ) { |
$( '#miniatures img' ).each( function() { |
|
var imgVisible = ( premiere ) ? 'miniature-selectionnee' : 'miniature-cachee', |
css = ( $( this ).hasClass( 'b64' ) ) ? 'miniature b64' : 'miniature', |
src = $( this ).attr( 'src' ), |
alt = $( this ).attr( 'alt' ), |
miniature = '<img class="' + css + ' ' + imgVisible + ' align-middle" alt="' + alt + '"src="' + src + '" width="80%" />'; |
|
premiere = false; |
|
|
miniatures += miniature; |
}); |
if ( 1 === $( '#miniatures img' ).length ) { |
centre = 'centre'; |
defilVisible = ' defilement-miniatures-cache'; |
} |
html = |
'<div class="defilement-miniatures">'+ |
'<a href="#" class="defilement-miniatures-gauche mr-1' + defilVisible + '"><i class="fas fa-chevron-circle-left"></i></a>'+ |
'<figure class="' + centre + '">'+ |
miniatures+ |
'</figure>'+ |
'<a href="#" class="defilement-miniatures-droite ml-1' + defilVisible + '"><i class="fas fa-chevron-circle-right"></i></a>'+ |
'</div>'; |
} |
return html; |
}; |
|
WidgetSaisie.prototype.fournirDate = function( dateObs ) { |
if ( /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/.test( dateObs ) ) { |
return dateObs; |
} else if ( /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test( dateObs ) ) { |
var dateArray = dateObs.split( '-' ); |
return dateArray[2] + '/' + dateArray[1] + '/' + dateArray[0] |
} else { |
console.log( 'erreur date : ' + dateObs ) |
} |
} |
|
WidgetSaisie.prototype.ajouterNumNomSel = function( numNomSel ) { |
var nn = '<span class="nn">[nn' + numNomSel + ']</span>'; |
if ( !valeurOk( numNomSel ) ) { |
nn = '<span class="alert-error">[' + this.msgTraduction( 'non-lie-au-ref' ) + ']</span>'; |
} |
return nn; |
}; |
|
|
WidgetSaisie.prototype.stockerObsData = function() { |
const lthis = this; |
|
var numNomSel = $( '#taxon' ).data( 'numNomSel' ), |
nomSel = $( '#taxon' ).val(), |
nomRet = $( '#taxon' ).data( 'nomRet' ), |
numNomRet = $( '#taxon' ).data( 'numNomRet' ), |
numTaxon = $( '#taxon' ).data( 'nt' ), |
famille = $( '#taxon' ).data( 'famille' ), |
referentiel = ( !valeurOk( numNomSel ) ) ? 'autre' : this.nomSciReferentiel; |
|
$( '#liste-obs' ).data( 'obsId' + this.obsNbre, { |
'num_nom_sel' : numNomSel, |
'nom_sel' : nomSel, |
'nom_ret' : nomRet, |
'num_nom_ret' : numNomRet, |
'num_taxon' : numTaxon, |
'famille' : famille, |
'referentiel' : referentiel, |
'certitude' : $( '#certitude' ).val(), |
'date' : lthis.fournirDate( $( '#date_releve' ).val() ), |
'notes' : $( '#notes' ).val().trim(), |
'pays' : $( '#pays' ).val(), |
'commune_nom' : $( '#commune-nom' ).val(), |
'commune_code_insee' : $( '#commune-insee' ).val(), |
'latitude' : $( '#latitude' ).val(), |
'longitude' : $( '#longitude' ).val(), |
'altitude' : $( '#altitude' ).val(), |
'lieudit' : $( '#lieudit' ).val(), |
'station' : $( '#station' ).val(), |
'milieu' : $( '#milieu' ).val(), |
//Ajout des champs images |
'image_nom' : lthis.getNomsImgsOriginales(), |
'image_b64' : lthis.getB64ImgsOriginales(), |
// Ajout des champs étendus de l'obs |
'obs_etendue' : lthis.getObsChpEtendus() |
}); |
}; |
|
WidgetSaisie.prototype.getNomsImgsOriginales = function() { |
var noms = new Array(); |
|
$( '.miniature-img' ).each( function() { |
noms.push( $( this ).attr( 'alt' ) ); |
}); |
return noms; |
}; |
|
WidgetSaisie.prototype.getB64ImgsOriginales = function() { |
var b64 = new Array(); |
|
$( '.miniature-img' ).each( function() { |
if ( $( this ).hasClass( 'b64' ) ) { |
b64.push( $( this ).attr( 'src' ) ); |
} else if ( $( this ).hasClass( 'b64-canvas' ) ) { |
b64.push( $( this ).data( 'b64' ) ); |
} |
}); |
return b64; |
}; |
|
/** |
* Retourne un Array contenant les valeurs des champs étendus |
*/ |
WidgetSaisie.prototype.getObsChpEtendus = function() { |
var champs = new Array(), |
$thisForm = $( '#form-supp' ), |
elements = |
'input[type=text]:not(.collect-other),'+ |
'input[type=checkbox]:checked,'+ |
'input[type=radio]:checked,'+ |
'input[type=email],'+ |
'input[type=number],'+ |
'input[type=range],'+ |
'input[type=date],'+ |
'textarea,'+ |
'.select', |
retour = new Array(); |
|
$( elements, $thisForm ).each( function() { |
if ( valeurOk( $( this ).val() ) && ( valeurOk( $( this ).attr( 'name' ) ) || valeurOk( $( this ).data( 'name' ) ) ) ) { |
var valeur = $( this ).val(), |
cle = ( valeurOk( $( this ).attr( 'name' ) ) ) ? $( this ).attr( 'name' ) : $( this ).data( 'name' ); |
if ( cle in champs ) { |
champs[cle] += ';' + valeur; |
} else { |
champs[cle] = valeur; |
} |
} |
}); |
for ( var key in champs ) { |
retour.push({ 'cle' : key , 'valeur' : champs[key] }); |
} |
if ( valeurOk( $( '#coord-lineaire' ).val() ) ) { |
retour.push({ 'cle' : 'coordonnees-rue-ou-lineaire' , 'valeur' : $( '#coord-lineaire' ).val() }); |
} |
return retour; |
}; |
|
/** |
* Efface toutes les miniatures (formulaire) |
*/ |
WidgetSaisie.prototype.supprimerMiniatures = function() { |
// Déconnection MutationObserver miniatures |
// Sinon on a une erreur avant la création d'une nouvelle obs |
this.observer.disconnect(); |
$( '#miniatures' ).empty(); |
// Validation miniatures reprend à 0 pour une nouvelle obs |
this.surPresenceAbsenceMiniature(); |
$( '#miniature-msg' ).empty(); |
}; |
|
WidgetSaisie.prototype.surChangementNbreObs = function() { |
if ( 0 === this.obsNbre ) { |
$( '#transmettre-obs' ).attr( 'disabled', 'disabled' ); |
} else if ( 0 < this.obsNbre && this.obsNbre < this.obsMaxNbre ) { |
$( '#transmettre-obs' ).removeAttr( 'disabled' ); |
$( '#ajouter-obs' ).removeAttr( 'disabled' ); |
} else if ( this.obsNbre >= this.obsMaxNbre ) { |
$( '#ajouter-obs' ).attr( 'disabled', 'disabled' ); |
this.afficherPanneau( '#dialogue-bloquer-creer-obs' ); |
} |
}; |
|
WidgetSaisie.prototype.defilerMiniatures = function( element ) { |
var miniatureSelectionne = element.siblings( 'figure' ).find( 'img.miniature-selectionnee' ); |
|
miniatureSelectionne.removeClass( 'miniature-selectionnee' ); |
miniatureSelectionne.addClass( 'miniature-cachee' ); |
|
var miniatureAffichee = miniatureSelectionne; |
|
if( element.hasClass( 'defilement-miniatures-gauche' ) ) { |
if( 0 !== miniatureSelectionne.prev( '.miniature' ).length ) { |
miniatureAffichee = miniatureSelectionne.prev( '.miniature' ); |
} else { |
miniatureAffichee = miniatureSelectionne.siblings( '.miniature' ).last(); |
} |
} else { |
if( 0 !== miniatureSelectionne.next('.miniature').length ) { |
miniatureAffichee = miniatureSelectionne.next( '.miniature' ); |
} else { |
miniatureAffichee = miniatureSelectionne.siblings( '.miniature' ).first(); |
} |
} |
miniatureAffichee.addClass( 'miniature-selectionnee' ); |
miniatureAffichee.removeClass( 'miniature-cachee' ); |
}; |
|
WidgetSaisie.prototype.supprimerObs = function( selector ) { |
var obsId = $( selector ).val(); |
|
// Problème avec IE 6 et 7 |
if ( 'Supprimer' === obsId ) { |
obsId = $( selector ).attr( 'title' ); |
} |
this.supprimerObsParId( obsId ); |
}; |
|
WidgetSaisie.prototype.supprimerObsParId = function( obsId ) { |
this.obsNbre = this.obsNbre - 1; |
$( '.obs-nbre' ).text( this.obsNbre ); |
$( '.obs-nbre' ).triggerHandler( 'changement' ); |
$( '.obs' + obsId ).remove(); |
obsId = parseInt( obsId ); |
|
var listObsData = $( '#liste-obs' ).data(), |
exId = 0, |
indexObs = '', |
exIndexObs = ''; |
|
for ( var id = obsId; id <= ( this.obsNbre + 1 ); id++ ) { |
exId = parseInt( id ) + 1; |
indexObs = 'obsId' + id; |
exIndexObs = 'obsId' + exId; |
$( '#liste-obs' ).removeData( indexObs ); |
if ( valeurOk( listObsData[ exIndexObs ] ) ) { |
$( '#liste-obs' ).data( indexObs, listObsData[ exIndexObs ] ); |
} |
$( '#obs' + exId ) |
.attr( 'id', 'obs' + id ) |
.removeClass( 'obs' + exId ) |
.addClass( 'obs' + id ) |
.find( '.supprimer-obs' ) |
.attr( 'title', 'Observation n°' + id ) |
.val( id ); |
if ( parseInt( id ) !== this.obsNbre ) { |
id = parseInt( id ); |
} |
} |
}; |
|
WidgetSaisie.prototype.transmettreObs = function() { |
var observations = $( '#liste-obs' ).data(); |
|
if ( this.debug ) { |
console.log( observations ); |
} |
if ( !valeurOk( typeof observations, true, 'object' ) ) { |
this.afficherPanneau( '#dialogue-zero-obs' ); |
} else { |
this.nbObsEnCours = 1; |
this.nbObsTransmises = 0; |
this.totalObsATransmettre = $.map( observations, function( n, i ) { |
return i; |
}).length; |
this.depilerObsPourEnvoi(); |
} |
return false; |
}; |
|
WidgetSaisie.prototype.depilerObsPourEnvoi = function() { |
var observations = $( '#liste-obs' ).data(); |
|
// la boucle est factice car on utilise un tableau |
// dont on a besoin de n'extraire que le premier élément |
// or javascript n'a pas de méthode cross browsers pour extraire les clés |
// TODO: utiliser var.keys quand ça sera plus répandu |
// ou bien utiliser un vrai tableau et pas un objet |
for ( var obsNum in observations ) { |
var obsATransmettre = { |
'projet' : this.tagProjet, |
'tag-obs' : this.tagObs, |
'tag-img' : this.tagImg |
}; |
var utilisateur = { |
id_utilisateur : $( '#id_utilisateur' ).val(), |
prenom : $( '#prenom' ).val(), |
nom : $( '#nom' ).val(), |
courriel : $( '#courriel' ).val() |
}; |
|
obsATransmettre['utilisateur'] = utilisateur; |
obsATransmettre[obsNum] = observations[obsNum]; |
|
var idObsNumerique = obsNum.replace( 'obsId', '' ); |
|
if( '' !== idObsNumerique ) { |
this.envoyerObsAuCel( idObsNumerique, obsATransmettre ); |
} |
break; |
} |
}; |
|
WidgetSaisie.prototype.envoyerObsAuCel = function( idObs, observation ) { |
const lthis = this; |
|
var erreurMsg = ''; |
|
$.ajax({ |
url : lthis.serviceSaisieUrl, |
type : 'POST', |
data : observation, |
dataType : 'json', |
beforeSend : function() { |
$( '#dialogue-obs-transaction-ko' ).addClass( 'hidden' ); |
$( '#dialogue-obs-transaction-ok' ).addClass( 'hidden' ); |
$( '.alert-txt' ).empty(); |
$( '.alert-txt .msg-erreur' ).remove(); |
$( '.alert-txt .msg-debug' ).remove(); |
$( '#chargement' ).removeClass( 'hidden' ); |
}, |
success : function( data, textStatus, jqXHR ) { |
// mise à jour du nombre d'obs à transmettre |
// et suppression de l'obs |
lthis.supprimerObsParId( idObs ); |
lthis.nbObsEnCours++; |
// mise à jour du statut |
lthis.mettreAJourProgression(); |
if( 0 < lthis.obsNbre ) { |
// dépilement de la suivante |
lthis.depilerObsPourEnvoi(); |
} |
}, |
statusCode : { |
500 : function( jqXHR, textStatus, errorThrown ) { |
erreurMsg += lthis.msgTraduction( 'erreur' ) + ' 500 :\ntype : ' + textStatus + ' ' + errorThrown + '\n'; |
} |
}, |
error : function( jqXHR, textStatus, errorThrown ) { |
erreurMsg += lthis.msgTraduction( 'erreur-ajax' ) + ' :\ntype : ' + textStatus + ' ' + errorThrown + '\n'; |
try { |
reponse = jQuery.parseJSON( jqXHR.responseText ); |
if ( null !== reponse ) { |
$.each( reponse, function( cle, valeur ) { |
erreurMsg += valeur + '\n'; |
}); |
} |
} catch( e ) { |
erreurMsg += lthis.msgTraduction( 'erreur-inconnue' ) + ' : ' + jqXHR.responseText; |
} |
}, |
complete : function( jqXHR, textStatus ) { |
var debugMsg = extraireEnteteDebug( jqXHR ); |
|
if ( '' !== erreurMsg ) { |
if ( lthis.debug ) { |
$( '#dialogue-obs-transaction-ko .alert-txt' ).append( '<pre class="msg-erreur">' + erreurMsg + '</pre>' ); |
$( '#dialogue-obs-transaction-ko .alert-txt' ).append( '<pre class="msg-debug">Débogage : ' + debugMsg + '</pre>' ); |
} |
|
var hrefCourriel = 'mailto:cel_remarques@tela-botanica.org?'+ |
'subject=Dysfonctionnement du widget de saisie ' + lthis.tagProjet+ |
'&body=' + erreurMsg + '%0D%0ADébogage :%0D%0A' + debugMsg; |
|
$( '#obs' + idObs + ' div div' ).addClass( 'obs-erreur' ); |
$( '#dialogue-obs-transaction-ko .alert-txt' ).append( |
$( '#tpl-transmission-ko' ).clone() |
.find( '.courriel-erreur' ) |
.attr( 'href', hrefCourriel ) |
.end() |
.html() |
); |
$( '#dialogue-obs-transaction-ko' ).removeClass( 'hidden' ); |
$( '#chargement' ).addClass( 'hidden' ); |
lthis.initialiserBarreProgression; |
} else { |
if ( lthis.debug ) { |
$( '#dialogue-obs-transaction-ok .alert-txt' ).append( '<pre class="msg-debug">Débogage : ' + debugMsg + '</pre>' ); |
} |
if( 0 === lthis.obsNbre ) { |
setTimeout( function() { |
$( '#chargement' ).addClass( 'hidden' ); |
$( '#dialogue-obs-transaction-ok .alert-txt' ).append( $( '#tpl-transmission-ok' ).clone().html() ); |
$( '#dialogue-obs-transaction-ok' ).removeClass( 'hidden' ); |
lthis.initialiserObs.bind( lthis ); |
}, 1500 ); |
} |
} |
} |
}); |
}; |
|
WidgetSaisie.prototype.mettreAJourProgression = function() { |
this.nbObsTransmises++; |
|
var pct = ( this.nbObsTransmises / this.totalObsATransmettre ) * 100; |
|
$( '#barre-progression-upload' ).attr( 'aria-valuenow', this.nbObsTransmises ); |
$( '#barre-progression-upload' ).css( 'width', pct + '%' ); |
$( '#barre-progression-upload .sr-only' ).text( this.nbObsTransmises + '/' + this.totalObsATransmettre + ' ' + this.msgTraduction( 'observations-transmises' ) ); |
if( 0 === this.obsNbre ) { |
$( '.progress' ).removeClass( 'active' ); |
$( '.progress' ).removeClass( 'progress-bar-striped' ); |
} |
}; |
|
WidgetSaisie.prototype.initialiserBarreProgression = function() { |
$( '#barre-progression-upload' ).attr( 'aria-valuenow', 0 ); |
$( '#barre-progression-upload' ).css( 'width', '0%' ); |
$( '#barre-progression-upload .sr-only' ).text( '0/0 ' + this.msgTraduction( 'observations-transmises' ) ); |
$( '.progress' ).addClass( 'active' ); |
$( '.progress' ).addClass( 'progress-bar-striped' ); |
}; |
|
WidgetSaisie.prototype.initialiserObs = function() { |
this.obsNbre = 0; |
this.nbObsTransmises = 0; |
this.nbObsEnCours = 0; |
this.totalObsATransmettre = 0; |
this.initialiserBarreProgression(); |
$( '.obs-nbre' ).text( this.obsNbre ); |
$( '.obs-nbre' ).triggerHandler( 'changement' ); |
$( '#liste-obs' ).removeData(); |
$( '.obs' ).remove(); |
$( '#dialogue-bloquer-creer-obs' ).addClass( 'hidden' ); |
}; |
|
WidgetSaisie.prototype.afficherPanneau = function( selecteur ) { |
$( selecteur ) |
.removeClass( 'hidden') |
.hide() |
.show( 600 ) |
.delay( this.dureeMessage ) |
.hide( 600 ); |
}; |
|
WidgetSaisie.prototype.masquerPanneau = function( selecteur ) { |
$( selecteur ).addClass( 'hidden' ); |
}; |
|
WidgetSaisie.prototype.fermerPanneauAlert = function() { |
$( this ).parentsUntil( '.zone-alerte', '.alert' ).addClass( 'hidden' ); |
}; |
|
/** |
* Si la langue est définie dans this.langue, et si des messages sont définis |
* dans this.msgs, tente de trouver le message dont la clé est [cle] dans la |
* langue en cours. S'il n'est pas trouvé, retourne la version française (par |
* défaut); si celle-ci n'exite pas, retourne "N/A". |
*/ |
WidgetSaisie.prototype.msgTraduction = function( cle ) { |
var msg = 'N/A'; |
|
if ( this.msgs ) { |
if ( this.langue in this.msgs && cle in this.msgs[this.langue] ) { |
msg = this.msgs[this.langue][cle]; |
} else if ( cle in this.msgs['fr'] ) { |
msg = this.msgs['fr'][cle]; |
} |
} |
return msg; |
}; |
|
// lib hors objet -- |
|
/** |
* Permet à la fois de vérifier qu'une valeur ou objet existe et n'est pas vide |
* et de comparer à une autre valeur : |
* Vérifie qu'une variable ou objet n'est pas : vide, null, undefined, NaN |
* Si comparer est défini on le compare à valeur en fonction de sensComparaison |
* Un booléen est une variable valide : on retourne true |
* @param { string || number || object || undefined } valeur |
* @param { boolean } sensComparaison : true = rechercher, false = refuser |
* @param { string || number || object || undefined || boolean } comparer :valeur à comparer |
* @returns {boolean} |
*/ |
function valeurOk( valeur, sensComparaison = true, comparer = undefined ) { |
if ( 'boolean' !== typeof valeur ) { |
switch( typeof valeur ) { |
case 'string' : |
retour = ( '' !== valeur ); |
break; |
case 'number' : |
retour = ( NaN !== valeur ); |
break; |
case 'object' : |
retour = ( null !== valeur && undefined !== valeur && !$.isEmptyObject( valeur ) ); |
if ( undefined !== valeur.length ) { |
retour = ( retour && 0 < valeur.length ); |
} |
break; |
case 'undefined' : |
default : |
retour = false; |
} |
if ( retour && comparer !== undefined ) { |
var resultComparaison = ( comparer === valeur ); |
retour = ( sensComparaison ) ? resultComparaison : !resultComparaison ; |
} |
return retour; |
} else { |
// Un booléen est une valeur valable |
return true; |
} |
} |
|
/** |
* Stope l'évènement courant quand on clique sur un lien. |
* Utile pour Chrome, Safari... |
*/ |
function arreter( evenement ) { |
if ( evenement.stopPropagation ) { |
evenement.stopPropagation(); |
} |
if ( evenement.preventDefault ) { |
evenement.preventDefault(); |
} |
return false; |
} |
|
/** |
* Extrait les données de désinsectisation d'une requête AJAX de jQuery |
* @param jqXHR |
* @returns {String} |
*/ |
function extraireEnteteDebug( jqXHR ) { |
var msgDebug = ''; |
|
if ( '' !== jqXHR.getResponseHeader( 'X-DebugJrest-Data' ) ) { |
var debugInfos = jQuery.parseJSON( jqXHR.getResponseHeader( 'X-DebugJrest-Data' ) ); |
|
if ( null !== debugInfos ) { |
$.each( debugInfos, function( cle, valeur ) { |
msgDebug += valeur + '\n'; |
}); |
} |
} |
return msgDebug; |
} |
|
/* |
* jQuery UI Autocomplete HTML Extension |
* |
* Copyright 2010, Scott González (http://scottgonzalez.com) |
* Dual licensed under the MIT or GPL Version 2 licenses. |
* |
* http://github.com/scottgonzalez/jquery-ui-extensions |
* |
* Adaptation par Aurélien Peronnet pour la mise en gras des noms de taxons valides |
*/ |
( function( $ ) { |
var proto = $.ui.autocomplete.prototype, |
initSource = proto._initSource; |
|
WidgetSaisie.prototype.filter = function( array, term ) { |
var matcher = new RegExp( $.ui.autocomplete.escapeRegex( term ), 'i' ); |
|
return $.grep( array, function( value ) { |
return matcher.test( $( '<div>' ).html( value.label || value.value || value ).text() ); |
}); |
} |
$.extend( proto, { |
_initSource: function() { |
if ( this.options.html && $.isArray( this.options.source ) ) { |
this.source = function( request, response ) { |
response( filter( this.options.source, request.term ) ); |
}; |
} else { |
initSource.call( this ); |
} |
}, |
_renderItem: function( ul, item) { |
if ( item.retenu ) { |
item.label = '<strong>' + item.label + '</strong>'; |
} |
|
return $( '<li></li>' ) |
.data( 'item.autocomplete', item ) |
.append( $( '<a></a>' )[ ( this.options.html ) ? 'html' : 'text' ]( item.label ) ) |
.appendTo( ul ); |
} |
}); |
})( jQuery ); |