Subversion Repositories eFlore/Applications.cel

Rev

Rev 3811 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3638 delphine 1
/**
2
 * Constructeur WidgetSaisie par défaut
3
 */
4
function WidgetSaisie( proprietes ) {
5
	if  ( valOk(proprietes) ) {
6
		this.urlWidgets                        = proprietes.urlWidgets;
7
		this.projet                            = proprietes.projet;
8
		this.idProjet                          = proprietes.idProjet;
9
		this.tagsMotsCles                      = proprietes.tagsMotsCles;
10
		this.mode                              = proprietes.mode;
11
		this.langue                            = proprietes.langue;
12
		this.serviceAnnuaireIdUrl              = proprietes.serviceAnnuaireIdUrl;
13
		this.serviceNomCommuneUrl              = proprietes.serviceNomCommuneUrl;
14
		this.serviceNomCommuneUrlAlt           = proprietes.serviceNomCommuneUrlAlt;
15
		this.debug                             = proprietes.debug;
16
		this.html5                             = proprietes.html5;
17
		this.serviceSaisieUrl                  = proprietes.serviceSaisieUrl;
18
		this.serviceObsUrl                     = proprietes.serviceObsUrl;
19
		this.chargementImageIconeUrl           = proprietes.chargementImageIconeUrl;
20
		this.pasDePhotoIconeUrl                = proprietes.pasDePhotoIconeUrl;
21
		this.autocompletionElementsNbre        = proprietes.autocompletionElementsNbre;
22
		this.serviceAutocompletionNomSciUrl    = proprietes.serviceAutocompletionNomSciUrl;
23
		this.serviceAutocompletionNomSciUrlTpl = proprietes.serviceAutocompletionNomSciUrlTpl;
24
		this.dureeMessage                      = proprietes.dureeMessage;
25
		this.obsMaxNbre                        = proprietes.obsMaxNbre;
26
		this.tagImg                            = proprietes.tagImg;
27
		this.tagObs                            = proprietes.tagObs;
28
		this.obsId                             = proprietes.obsId;
29
		this.nomSciReferentiel                 = proprietes.nomSciReferentiel;
30
		this.especeImposee                     = proprietes.especeImposee;
31
		this.infosEspeceImposee                = proprietes.infosEspeceImposee;
32
		this.referentielImpose                 = proprietes.referentielImpose;
33
		this.isTaxonListe                      = proprietes.isTaxonListe;
34
		this.utils                             = utils;
35
		this.msgs                              = utils.msgs;
36
	}
37
	this.urlRacine            = window.location.origin;
38
	this.obsNbre              = 0;
39
	this.nbObsEnCours         = 1;
40
	this.totalObsATransmettre = 0;
41
	this.nbObsTransmises      = 0;
42
	this.observer             = null;
43
	this.isASL                = false;
44
}
45
WidgetSaisie.prototype = new WidgetsSaisiesCommun();
46
 
47
var valOk = WidgetSaisie.prototype.valOk;
48
 
49
/**
50
 * Initialise le formulaire, les validateurs, les listes de complétion...
51
 */
52
WidgetSaisie.prototype.initForm = function() {
53
	const lthis = this;
54
 
55
	this.initFormConnection();
56
	if ( this.valOk( this.obsId ) ) {
57
		this.chargerInfoObs();
58
	}
59
	if( this.isTaxonListe ) {
60
		this.initFormTaxonListe();
61
	} else {
62
		this.ajouterAutocompletionNoms();
63
	}
64
	// au rafraichissement de la page,
65
	// les input date semblent conserver la valeur entrée précedemment
66
	// c'est voulu après la création d'une obs mais pas quand la page est actualisée
67
	// Déjà tenté: onbeforeunload avec un location.reload(true) n'a pas permis de le faire
68
	$( 'input[type=date]' ).each( function () {
69
		( lthis.valOk( $( this ).data( 'default' ) ) ) ? $( this ).val( $( this ).data( 'default' ) ) : $( this ).val( '' );
70
	});
71
	this.configurerFormValidator();
72
	this.definirReglesFormValidator();
73
 
74
	if( this.especeImposee ) {
75
		$( '#taxon' ).attr( 'disabled', 'disabled' );
76
		$( '#taxon-input-groupe' ).attr( 'title', '' );
77
		// Bricolage cracra pour avoir le nom retenu avec auteur (nom_retenu.libelle ne le mentionne pas)
78
		var infosEspeceImposee = $.parseJSON( this.infosEspeceImposee );
79
			nomRetenuComplet   = infosEspeceImposee.nom_retenu_complet,
80
			debutAnneRefBiblio = nomRetenuComplet.indexOf( ' [' );
81
		if ( -1 !== debutAnneRefBiblio ) {
82
			nomRetenuComplet = nomRetenuComplet.substr( 0, debutAnneRefBiblio );
83
		}
84
		// fin bricolage cracra
85
		var infosAssociee = {
86
			label : infosEspeceImposee.nom_sci_complet,
87
			value : infosEspeceImposee.nom_sci_complet,
88
			nt : infosEspeceImposee.num_taxonomique,
89
			nomSel : infosEspeceImposee.nom_sci,
90
			nomSelComplet : infosEspeceImposee.nom_sci_complet,
91
			numNomSel : infosEspeceImposee.id,
92
			nomRet : nomRetenuComplet,
93
			numNomRet : infosEspeceImposee['nom_retenu.id'],
94
			famille : infosEspeceImposee.famille,
95
			retenu : ( 'false' === infosEspeceImposee.retenu ) ? false : true
96
		};
97
		$( '#taxon' ).data( infosAssociee );
98
	}
99
};
100
 
101
/**
102
 * Initialise les écouteurs d'événements
103
 */
104
WidgetSaisie.prototype.initEvts = function() {
105
	const lthis = this;
106
 
107
	// identité
108
	this.initEvtsConnection();
109
	// on location, initialisation de la géoloc
110
	this.initEvtsGeoloc();
111
	// Sur téléchargement image
112
	this.initEvtsFichier();
113
 
114
	$( '#referentiel' ).on( 'change', this.surChangementReferentiel.bind( this ) );
115
	// Création / Suppression / Transmission des obs
116
	// Défilement des miniatures dans le résumé obs
117
	this.initEvtsObs();
118
	// Alertes et aides
119
	this.initEvtsAlertes();
120
	// message avant de quitter le formulaire
121
	this.confirmerSortie();
122
};
123
 
124
// Identité Observateur par courriel
125
WidgetSaisie.prototype.requeterIdentiteCourriel = function() {
126
	const lthis = this;
127
 
128
	var courriel = $( '#courriel' ).val();
129
	var urlAnnuaire = this.serviceAnnuaireIdUrl + courriel;
130
 
131
	if ( this.valOk( courriel ) ) {
132
		$.ajax({
133
			url : urlAnnuaire,
134
			type : 'GET',
135
			success : function( data, textStatus, jqXHR ) {
136
				if ( lthis.debug ) {
137
					console.log( 'SUCCESS: ' + textStatus );
138
				}
139
				if ( lthis.valOk( data ) && lthis.valOk( data[courriel] ) ) {
140
					var infos = data[courriel];
141
					lthis.surSuccesCompletionCourriel( infos, courriel );
142
				} else {
143
					lthis.surErreurCompletionCourriel();
144
				}
145
			},
146
			error : function( jqXHR, textStatus, errorThrown ) {
147
				if ( lthis.debug ) {
148
					console.log( 'ERREUR: '+ textStatus );
149
				}
150
				lthis.surErreurCompletionCourriel();
151
			},
152
			complete : function( jqXHR, textStatus ) {
153
				if ( lthis.debug ) {
154
					console.log( 'COMPLETE: '+ textStatus );
155
				}
156
			}
157
		});
158
	}
159
};
160
 
161
// se déclanche quand on choisit "Observation sans inscription" mais que le mail entré est incrit à Tela
162
WidgetSaisie.prototype.surSuccesCompletionCourriel = function( infos, courriel ) {
163
	if ( $( '#utilisateur-connecte' ).hasClass( 'hidden' ) ) {// si quelque chose a foiré après actualisation
164
		if ( !this.valOk( $( '#warning-identite' ) ) ) {
165
			$( '#zone-courriel' ).before( '<p id="warning-identite" class="warning"><i class="fas fa-exclamation-triangle"></i> ' + this.msgTraduction( 'courriel-connu' ) + '</p>' );
166
		}
167
		$( '#inscription, #zone-prenom-nom, #zone-courriel-confirmation' ).addClass( 'hidden' );
168
		$( '#prenom, #nom, #courriel_confirmation' ).attr( 'disabled', 'disabled' );
169
		$( '.nav.control-group' ).addClass( 'error' );
170
	}
171
};
172
 
173
// se déclanche quand on choisit "Observation sans inscription" et qu'effectivement le mail n'est pas connu de Tela
174
WidgetSaisie.prototype.surErreurCompletionCourriel = function() {
175
	$( '#creation-compte, #zone-prenom-nom, #zone-courriel-confirmation' ).removeClass( 'hidden' );
176
	$( '#warning-identite' ).remove();
177
	$( '.nav.control-group' ).removeClass( 'error' );
178
	$( '#prenom, #nom, #courriel_confirmation' ).val( '' ).removeAttr( 'disabled' );
179
};
180
 
181
WidgetSaisie.prototype.testerLancementRequeteIdentite = function( event ) {
182
	if ( this.valOk( event.which, true, 13 ) ) {
183
		this.requeterIdentiteCourriel();
184
		event.preventDefault();
185
		event.stopPropagation();
186
	}
187
};
188
 
189
WidgetSaisie.prototype.reduireVoletIdentite = function() {
190
	if ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() ) {
191
		$( '#bouton-connexion, #creation-compte' ).addClass( 'hidden' );
192
		$( '#bienvenue').removeClass( 'hidden' );
193
		$( '#inscription, #zone-courriel' ).addClass( 'hidden' );
194
		if ( lthis.valOk( $( '#nom' ).val() ) && lthis.valOk( $( '#prenom' ).val() ) ) {
195
			$( '#zone-prenom-nom' ).addClass( 'hidden' );
196
			$( '#bienvenue-prenom' ).text( ' ' + $( '#prenom' ).val() );
197
			$( '#bienvenue-nom' ).text( ' ' + $( '#nom' ).val() );
198
		} else {
199
			$( '#zone-prenom-nom' ).removeClass( 'hidden' );
200
			$( '#bienvenue-prenom,#bienvenue-nom' ).text( '' );
201
		}
202
	} else {
203
		$( '#bouton-connexion, #creation-compte' ).removeClass( 'hidden' );
204
		$( '#bienvenue').addClass( 'hidden' );
205
	}
206
};
207
 
208
 
209
WidgetSaisie.prototype.formaterNom = function() {
210
	$( '#nom' ).val( $( '#nom' ).val().toUpperCase() );
211
};
212
 
213
WidgetSaisie.prototype.formaterPrenom = function() {
214
	var prenom     = new Array(),
215
		mots       = $( '#prenom' ).val().split( ' ' ),
216
		motsLength = mots.length;
217
 
218
	for ( var i = 0; i < motsLength; i++ ) {
219
		var mot = mots[i];
220
		if ( 0 <= mot.indexOf( '-' ) ) {
221
			var prenomCompose = new Array(),
222
				motsComposes = mot.split( '-' )
223
				motsComposesLength = motsComposes.length;
224
			for ( var j = 0; j < motsComposesLength; j++ ) {
225
				var motSimple    = motsComposes[j],
226
					motMajuscule = motSimple.charAt(0).toUpperCase() + motSimple.slice(1);
227
 
228
				prenomCompose.push( motMajuscule );
229
			}
230
			prenom.push( prenomCompose.join( '-' ) );
231
		} else {
232
			var motMajuscule = mot.charAt(0).toUpperCase() + mot.slice(1);
233
			prenom.push( motMajuscule );
234
		}
235
	}
236
	$( '#prenom' ).val( prenom.join( ' ' ) );
237
};
238
 
239
WidgetSaisie.prototype.bloquerCopierCollerCourriel = function() {
240
	this.afficherPanneau( '#dialogue-bloquer-copier-coller' );
241
	return false;
242
};
243
 
244
// Préchargement des infos-obs ************************************************/
245
WidgetSaisie.prototype.chargerInfoObs = function() {
246
	const lthis = this;
247
 
248
	var urlObs = this.serviceObsUrl + '/' + this.obsId;
249
 
250
	$.ajax({
251
		url: urlObs,
252
		type: 'GET',
253
		success: function( data, textStatus, jqXHR ) {
254
			if ( lthis.valOk( data ) ) {
255
				lthis.prechargerForm( data );
256
			} else {
257
				lthis.surErreurChargementInfosObs.bind( lthis );
258
			}
259
		},
260
		error: function( jqXHR, textStatus, errorThrown ) {
261
			lthis.surErreurChargementInfosObs();
262
		}
263
	});
264
};
265
 
266
// @TODO faire mieux que ça !
267
WidgetSaisie.prototype.surErreurChargementInfosObs = function() {
268
	this.activerModale( this.msgTraduction( 'erreur-chargement' ) );
269
};
270
 
271
WidgetSaisie.prototype.prechargerForm = function( data ) {
272
	$( '#milieu' ).val( data.milieu );
273
	$( '#commune-nom' ).text( data.zoneGeo );
274
	if( data.hasOwnProperty( 'codeZoneGeo' ) ) {
275
	  // TODO: trouver un moyen qui fonctionne lorsqu'on aura d'autres référentiels que INSEE
276
	  $( '#commune-insee' ).text( data.codeZoneGeo.replace( 'INSEE-C:', '' ) );
277
	}
278
 
279
	if( data.hasOwnProperty( 'latitude' ) && data.hasOwnProperty( 'longitude' ) ) {
280
		// $cartoRemplacee = $( '#tb-geolocation' ),
281
		// suffixe = '',
282
		// layer = 'osm',
283
		// zoomInit = 18
284
		var typeLocalisation = $( '#top' ).data( 'type-loc' ),
285
			donnesResetCarto = {
286
			latitude         : data.latitude,
287
			longitude        : data.longitude,
288
			typeLocalisation : typeLocalisation
289
		};
290
		this.transfererCarto( donnesResetCarto );
291
	}
292
};
293
 
294
// Ajouter Obs ****************************************************************/
295
/**
296
 * Retourne un Array contenant les valeurs des champs étendus
297
 */
298
WidgetSaisie.prototype.getObsChpSpecifiques = function() {
299
	const lthis = this;
300
 
301
	var champs    = new Array(),
302
		$thisForm = $( '#form-supp' ),
303
		elements  =
304
			'input[type=text]:not(.collect-other),'+
305
			'input[type=checkbox]:checked,'+
306
			'input[type=radio]:checked,'+
307
			'input[type=email],'+
308
			'input[type=number],'+
309
			'input[type=range],'+
310
			'input[type=date],'+
311
			'textarea,'+
312
			'.select',
313
		retour    = new Array();
314
 
315
	$( elements, $thisForm ).each( function() {
316
		if ( lthis.valOk( $( this ).val() ) && ( lthis.valOk( $( this ).attr( 'name' ) ) || lthis.valOk( $( this ).data( 'name' ) ) ) ) {
317
			var valeur = $( this ).val(),
318
				cle    = ( lthis.valOk( $( this ).attr( 'name' ) ) ) ? $( this ).attr( 'name' ) : $( this ).data( 'name' );
319
			if ( cle in champs ) {
320
				champs[cle] += ';' + valeur;
321
			} else {
322
				champs[cle] = valeur;
323
			}
324
		}
325
	});
326
	for ( var key in champs ) {
327
		retour.push({ 'cle' : key , 'valeur' : champs[key] });
328
	}
329
	if ( this.valOk( $( '#coord-lineaire' ).val() ) ) {
330
		retour.push({ 'cle' : 'coordonnees-rue-ou-lineaire' , 'valeur' : $( '#coord-lineaire' ).val() });
331
	}
332
	return retour;
333
};
334
 
335
WidgetSaisie.prototype.reinitialiserForm = function() {
336
	this.supprimerMiniatures();
337
	if( !this.especeImposee ) {
338
		$( '#taxon' ).val( '' );
339
		$( '#taxon' ).data( 'numNomSel', '' )
340
			.data( 'nomRet','' )
341
			.data( 'numNomRet', '' )
342
			.data( 'nt', '' )
343
			.data( 'famille', '' );
344
		if( this.isTaxonListe ) {
345
			$( '#taxon-liste' ).find( 'option' ).each( function() {
346
				if ( $( this ).hasClass( 'choisir' ) ) {
347
					$( this ).attr( 'selected', true );
348
				} else {
349
					$( this ).attr( 'selected', false );
350
				}
351
			});
352
			$( '#taxon-input-groupe' ).addClass( 'hidden' );
353
			$('#taxon-autre').val('');
354
		}
355
	}
356
	if ( this.valOk( $( '#form-supp' ) ) ) {
357
		$( '#form-supp' ).validate().resetForm();
358
	}
359
};
360
 
361
// Géolocalisation *************************************************************/
362
/**
363
 * Fonction handler de l'évenement location du module tb-geoloc
364
 */
365
WidgetSaisie.prototype.locationHandler = function( location ) {
366
	var locDatas = location.originalEvent.detail;
367
 
368
	if ( this.valOk( locDatas ) ) {
369
		console.log( locDatas );
370
		var geometry      = JSON.stringify( locDatas.geometry );
371
		var altitude      = ( this.valOk( locDatas.elevation ) ) ? locDatas.elevation : '';
372
		var pays          = ( this.valOk( locDatas.osmCountryCode ) ) ? locDatas.osmCountryCode.toUpperCase() : 'FR';
373
		var latitude      = '';
374
		var longitude     = '';
375
		var coordLineaire = '';
376
		var nomCommune    = '';
377
		var communeInsee  = '';
378
 
379
		if ( this.valOk( locDatas.geometry.coordinates ) ) {
380
			if ( 'Point' === locDatas.geometry.type ) {
381
				if ( this.valOk( locDatas.geometry.coordinates[0] ) ) {
382
					longitude = locDatas.geometry.coordinates[0];
383
				}
384
				if ( this.valOk( locDatas.geometry.coordinates[1] ) ) {
385
					latitude = locDatas.geometry.coordinates[1];
386
				}
387
			} else if ( 'LineString' === locDatas.geometry.type ) {// on a besoin que d'un point de la rue
388
				coordLineaire = JSON.stringify( locDatas.geometry.coordinates );
389
				if ( this.valOk( locDatas.geometry.coordinates[0][0] ) ) {
390
					longitude = locDatas.geometry.coordinates[0][0];
391
				}
392
				if ( this.valOk( locDatas.geometry.coordinates[0][1] ) ){
393
					latitude = locDatas.geometry.coordinates[0][1];
394
				}
395
			}
396
		}
397
		if ( this.valOk( locDatas.inseeData ) ) {
398
			nomCommune = locDatas.inseeData.nom;
399
			communeInsee = ( this.valOk( locDatas.inseeData.code ) ) ? locDatas.inseeData.code : '';
400
		} else if ( this.valOk( locDatas.locality ) ) {
401
			nomCommune = locDatas.locality;
402
		} else if ( this.valOk( locDatas.locality ) ) {
403
			nomCommune = locDatas.osmCounty;
404
		}
405
		$( '#geometry' ).val( geometry );
406
		$( '#coord-lineaire' ).val( coordLineaire );
407
		$( '#latitude' ).val( latitude );
408
		$( '#longitude' ).val( longitude );
409
		$( '#commune-nom' ).val( nomCommune );
410
		$( '#commune-insee' ).val( communeInsee );
411
		$( '#altitude' ).val( altitude );
412
		$( '#pays' ).val( pays );
413
		if ( this.valOk( $( '#latitude' ).val() ) && this.valOk( $( '#longitude' ).val() ) ) {
414
			$( '#geoloc' ).closest( '.control-group' ).removeClass( 'error' );
415
		} else {
416
			$( '#geoloc' ).closest( '.control-group' ).addClass( 'error' );
417
		}
418
	} else {
419
		console.log( 'Error location' );
420
	}
421
}
422
 
423
// Form Validator *************************************************************/
424
WidgetSaisie.prototype.chpEtendusValidation = function() {
425
	const lthis = this;
426
 
427
	var $thisForm = $( '#form-supp' ),
428
		elements  =
429
			'.checkbox,'+
430
			'.radio,'+
431
			'.checkboxes,'+
432
			'.select,'+
433
			'textarea,'+
434
			'input[type=text]:not(.collect-other),'+
435
			'input[type=email],'+
436
			'input[type=number],'+
437
			'input[type=range],'+
438
			'input[type=date]',
439
		speFields = ['checkbox','radio','checkboxes','select'],
440
		spefieldsCount = speFields.length,
441
		chpSuppValidation = {
442
			rules : {},
443
			messages : {},
444
			minmax : []
445
		},
446
		errors = {},
447
		namesListFields = [],
448
		picked = '';
449
 
450
	$( elements, $thisForm ).each( function() {
451
		for( var fieldsClass = 0; spefieldsCount > fieldsClass; fieldsClass++ ) {
452
			if ( lthis.valOk( $( this ).attr( 'required' ) ) && $( this ).hasClass( speFields[fieldsClass] ) && !lthis.valOk( chpSuppValidation.rules[ dataName ] ) ) {
453
 
454
				var dataName = $( this ).data( 'name' );
455
 
456
				namesListFields.push( dataName );
457
				chpSuppValidation.rules[ dataName ] = { required : true };
458
				if ( lthis.valOk( $( '.other', $( this ) ) ) ) {
459
					picked = ( 'select' === speFields[fieldsClass] ) ? ':selected' : ':checked';
460
					chpSuppValidation.rules[ 'collect-other-' + dataName.replace( '[]', '' ) ] = {
461
						required : '#other-' + dataName.replace( '[]', '' ) + picked,
462
						minlength: 1
463
					};
464
					chpSuppValidation.messages[ 'collect-other-' + dataName.replace( '[]', '' ) ] = false;
465
				}
466
				chpSuppValidation.rules[ dataName ]['listFields'] = true;
467
				chpSuppValidation.messages[ dataName ] = 'Ce champ est requis :\nVeuillez choisir une option, ou entrer une valeur autre valide.';
468
				errors[dataName] = '.' + speFields[fieldsClass];
469
			}
470
		}
471
		if ( lthis.valOk( $( this ).attr( 'name' ) ) && lthis.valOk ( $( this ).attr( 'required' ) ) && 0 > $.inArray( $( this ).attr( 'name' ) , namesListFields ) ) {
472
			chpSuppValidation.rules[ $( this ).attr( 'name' ) ] = { required : true, minlength: 1 };
473
			if(
474
				( lthis.valOk( $( this ).attr( 'type' ), true, 'number' ) || lthis.valOk( $( this ).attr( 'type' ), true, 'range' ) ) &&
475
				( lthis.valOk( $( this )[0].min ) || lthis.valOk( $( this )[0].max ) )
476
			) {
477
				chpSuppValidation.rules[ $( this ).attr('name') ]['minMaxOk'] = true;
478
				chpSuppValidation.messages[ $( this ).attr('name') ] = lthis.validerMinMax( $( this )[0] ).message;
479
			}
480
		}
481
	});
482
	if ( this.valOk( chpSuppValidation.rules ) ) {
483
		$.each( chpSuppValidation.rules, function( key ) {
484
			if ( !lthis.valOk( chpSuppValidation.messages[key] ) ) {
485
				chpSuppValidation.messages[key] = 'Ce champ est requis :\nVeuillez entrer une valeur valide.';
486
			}
487
		});
488
		if ( 0 < Object.keys( errors ).length ) {
489
			chpSuppValidation['errors'] = errors;
490
		}
491
	}
492
	return chpSuppValidation;
493
};
494
 
495
WidgetSaisie.prototype.validerMinMax = function( element ) {
496
	var mMCond      = new Boolean(),
497
		minCond     = parseFloat( element.value ) >= parseFloat( element.min ),
498
		maxCond     = parseFloat( element.value ) <= parseFloat( element.max ),
499
		messageMnMx = 'La valeur entrée doit être',
500
		returnMnMx  = { cond : true , message : '' };
501
 
502
	if(
503
		( this.valOk( element.type, true, 'number' ) || this.valOk( element.type, true, 'range' ) ) &&
504
		( this.valOk( element.min ) || this.valOk( element.max ) )
505
	) {
506
		if ( element.min && element.max ) {
507
			messageMnMx += ' comprise entre ' + element.min + ' et ' + element.max;
508
			mnMxCond     = ( minCond && maxCond );
509
		} else if ( element.min ) {
510
			messageMnMx += ' supérieure à ' + element.min;
511
			mnMxCond     = minCond;
512
		} else {
513
			messageMnMx += ' inférieure à ' + element.max;
514
			mnMxCond     = maxCond;
515
		}
516
		returnMnMx.cond    = mnMxCond;
517
		returnMnMx.message = messageMnMx;
518
	}
519
	return returnMnMx;
520
 
521
};
522
 
523
WidgetSaisie.prototype.definirReglesFormValidator = function() {
524
	const lthis = this;
525
 
526
	var formSuppValidation = this.chpEtendusValidation();
527
 
528
	$( '#form-supp' ).validate({
529
		onclick : function( element ) {
530
			if (
531
				(
532
					lthis.valOk( element.type, true, 'checkbox' ) ||
533
					lthis.valOk( element.type, true, 'radio' )
534
				) &&
535
				(
536
					!lthis.valOk( $( '.' + $( element ).attr( 'name' ).replace( '[]', '' ) + ':checked' ) ) ||
537
					lthis.valOk( $( '.' + $( element ).attr( 'name' ).replace( '[]', '' ) + ':not(.other):checked' ) ) ||
538
					!lthis.valOk( $( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ) ) ||
539
					$( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ).is( ':checked' ) ||
540
					(
541
						$( '#other-' + $( element ).attr( 'name' ).replace( '[]', '' ) ).is( ':checked' ) &&
542
						$( element ).closest( '.control-group' ).hasClass('error')
543
					)
544
				)
545
			) {
546
				$( element ).valid();
547
				if ( $( element ).valid() ) {
548
					$( element ).closest( '.control-group' ).removeClass( 'error' );
549
					$( element ).next( $( 'span.error' ) ).remove();
550
				} else {
551
					$( element ).closest( '.control-group' ).addClass( 'error' );
552
				}
553
			}
554
			return false;
555
		},
556
		rules : formSuppValidation.rules,
557
		messages : formSuppValidation.messages,
558
		errorPlacement : function( error , element ) {
559
			if ( 0 < Object.keys( formSuppValidation.errors ).length ) {
560
				var errorsKeys = Object.keys( formSuppValidation.errors ),
561
					thisKey = '',
562
					errorsFlag = true;
563
				for ( i = 0 ; i < errorsKeys.length ; i++ ) {
564
					thisKey = errorsKeys[i];
565
					if( $( element ).attr( 'name' ) === thisKey ) {
566
						$( formSuppValidation.errors[thisKey] ).append( error );
567
						errorsFlag = false;
568
					}
569
				}
570
				if ( errorsFlag ) {
571
					error.insertAfter( element );
572
				}
573
			} else {
574
				error.insertAfter( element );
575
			}
576
		}
577
	});
578
	$( '#form-supp .select' ).change( function() {
579
		$( this ).valid();
580
	});
581
	$( 'input[type=date]' ).on( 'input', function() {
582
		$( this ).valid();
583
	});
584
	// Validation Taxon si pas de miniature
585
	$( '#taxon' ).on( 'change', function() {
586
		var images = lthis.valOk( $( '#miniatures .miniature' ) );
587
		lthis.validerTaxonImage( lthis.valOk( $( this ).val() ), images );
588
	});
589
	// Validation miniatures avec MutationObserver
590
	this.surPresenceAbsenceMiniature();
591
	$( '#form-observation' ).validate({
592
		rules : {
593
			date_releve : {
594
				required : true,
595
				'dateCel' : true
596
			},
597
			latitude : {
598
				required : true,
599
				minlength : 1,
600
				range : [-90, 90]
601
			},
602
			longitude : {
603
				required : true,
604
				minlength : 1,
605
				range : [-180, 180]
606
			}
607
		}
608
	});
609
	$( '#form-observateur' ).validate({
610
		rules : {
611
			courriel : {
612
				required : true,
613
				email : true,
614
				'userEmailOk' : true
615
			},
616
			courriel_confirmation : {
617
				required : true,
618
				equalTo : '#courriel'
619
			}
620
		}
621
	});
622
	$( '#connexion,#inscription,#bouton-anonyme' ).on( 'click', function( event ) {
623
		$( '.nav.control-group' ).removeClass( 'error' );
624
	});
625
};
626
 
627
WidgetSaisie.prototype.validerTaxonImage = function( taxon = false, images = false ) {
628
	var taxonOuImage = ( images || taxon );
629
	if ( images || taxon ) {
630
		this.masquerPanneau( '#dialogue-taxon-or-image' );
631
		$( '#bloc-taxon' ).removeClass( 'error' )
632
			.find( 'span.error' ).hide();
633
		$( '#fichier' ).parent( 'label.label-file' ).removeClass( 'error' );
634
		$( '#photos-conteneur').removeClass( 'error' ).find( 'span.error' ).hide();
635
	} else {
636
		this.afficherPanneau( '#dialogue-taxon-or-image' );
637
		$( '#bloc-taxon' ).addClass( 'error' )
638
			.find( 'span.error' ).show();
639
		$( '#fichier' ).parent( 'label.label-file' ).addClass( 'error' );
640
		$( '#photos-conteneur').addClass( 'error' ).find( 'span.error' ).show();
641
	}
642
	return ( images || taxon );
643
};
644
 
645
WidgetSaisie.prototype.surPresenceAbsenceMiniature = function() {
646
	const lthis = this;
647
 
648
	// voir : https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/disconnect
649
	// Selectionne le noeud dont les mutations seront observées
650
	var targetNode = document.getElementById( 'miniatures' );
651
	// Fonction callback à éxécuter quand une mutation est observée
652
	var callback = function( mutationsList ) {
653
		for( var mutation of mutationsList ) {
654
			var taxon = lthis.valOk( $( '#taxon' ).val() );
655
 
656
			images = ( 0 < mutation.target.childElementCount );
657
			lthis.validerTaxonImage( taxon, images );
658
		}
659
	};
660
	// Créé une instance de l'observateur lié à la fonction de callback
661
	this.observer = new MutationObserver( callback );
662
	// Commence à observer le noeud cible pour les mutations précédemment configurées
663
	this.observer.observe( targetNode, { childList: true } );
664
};
665
 
666
WidgetSaisie.prototype.validerForm = function() {
667
	const lthis = this;
668
 
669
	var observateur  = ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() );
670
	var obs          = $( '#form-observation' ).valid();
671
	var geoloc       = ( this.valOk( $( '#latitude' ).val() ) && this.valOk( $( '#longitude' ).val() ) ) ;
672
	var images       = this.valOk( $( '#miniatures .miniature' ) );
673
	var taxon        = this.valOk( $( '#taxon' ).val() );
674
	// validation et panneau taxon/images
675
	var taxonOuImage = this.validerTaxonImage( taxon, images );
676
	var chpsSupp     = new Boolean();
677
	if ( this.valOk( $( '#form-supp' ) ) ) {
678
		chpsSupp = ( function () {
679
			var otherFlag = $( '#form-supp' ).valid();
680
			if( lthis.valOk( $( '.other', $( '#form-supp' ) ) ) ) {
681
				$( '.other', $( '#form-supp' ) ).each( function() {
682
					var picked = ( $( this ).data( 'element' ) !== 'select' ) ? ':checked' : ':selected';
683
						if ( $( this ).is( picked ) && lthis.valOk( $( this ).val(), true, 'other' ) ) {
684
							otherFlag = false;
685
						}
686
				});
687
			}
688
			return otherFlag;
689
		})();
690
	} else {
691
		chpsSupp = true;
692
	}
693
	// panneau geoloc
694
	if ( geoloc ) {
695
		this.masquerPanneau( '#dialogue-geoloc-ko' );
696
		$( '#geoloc-datas' ).closest( '.control-group' ).removeClass( 'error' );
697
	} else{
698
		this.afficherPanneau( '#dialogue-geoloc-ko' );
699
		$( '#geoloc-datas' ).closest( '.control-group' ).addClass( 'error' );
700
	}
701
	// panneau observateur
702
	if ( observateur ) {
703
		this.masquerPanneau( '#dialogue-utilisateur-non-identifie' );
704
		$( '.nav.control-group' ).removeClass( 'error' );
705
	} else {
706
		this.afficherPanneau( '#dialogue-utilisateur-non-identifie' );
707
		$( '.nav.control-group' ).addClass( 'error' );
708
	}
709
	return ( observateur && obs && geoloc && taxonOuImage && chpsSupp );
710
};
711
 
712
// Referentiel ****************************************************************/
713
// N'est pas utilisé en cas de taxon-liste
714
WidgetSaisie.prototype.surChangementReferentiel = function() {
715
	this.nomSciReferentiel = $( '#referentiel' ).val();
716
	//réinitialise taxon.val
717
	$( '#taxon' ).val( '' );
718
	$( '#taxon' ).data( 'numNomSel', '' );
719
};