Subversion Repositories eFlore/Applications.cel

Rev

Rev 2949 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2943 delphine 1
// Héritage
2
WidgetSaisieArbresRemarquables = function() {
3
	this.serviceAltitudeUrl = null;
4
	this.taxons = {};
5
}
6
WidgetSaisieArbresRemarquables.prototype = new WidgetSaisie();
7
 
8
WidgetSaisieArbresRemarquables.prototype.initEvts = function() {
9
	// super()
10
	WidgetSaisie.prototype.initEvts.call(this);
11
 
12
	var lthis = this;
13
	$('body').on('click', '.fermer', function(event) {
14
			event.preventDefault();
15
			lthis.basculerOuvertureFermetureCadre($(this).find('.icone'));
16
	});
17
 
2944 mathias 18
	// multi cases à cocher dans un menu déroulant : clic confortable !
19
	$('.btn-group.dropdown ul li input[type="checkbox"], .btn-group.dropdown ul li label').on('click', function(event) {
20
		$(this).valid();
21
		event.stopPropagation();
22
	});
23
 
2943 delphine 24
	this.surChangementTaxonListe();// Vérif lors du chargement de la page
25
	$('#taxon-liste').on('change', this.surChangementTaxonListe.bind(this));
26
	this.surChangementSituation();// Vérif lors du chargement de la page
2944 mathias 27
	$('#situation-autre').on('change', this.surChangementSituation.bind(this));
2943 delphine 28
	this.surChangementEsthetique();// Vérif lors du chargement de la page
2944 mathias 29
	$('#esthetique-autre').on('change', this.surChangementEsthetique.bind(this));
30
	this.surChangementRemarquable();// Vérif lors du chargement de la page
31
	$('.cb-remarquable').on('change', this.surChangementRemarquable.bind(this));
2943 delphine 32
 
33
	// Gestion des obs
34
	this.surChangementNbreObs();
35
};
36
 
37
WidgetSaisieArbresRemarquables.prototype.focusChampFormulaire = function() {
38
	$("#carte-recherche").focus();
39
};
40
 
41
WidgetSaisieArbresRemarquables.prototype.mettreAJourMarkerPosition = function(latLng) {
42
	this.trouverCommune(latLng);
43
	this.trouverAltitude(latLng);
44
 
45
	var lat = latLng.lat().toFixed(5),
46
		lng = latLng.lng().toFixed(5);
47
	this.remplirChampLatitude(lat);
48
	this.remplirChampLongitude(lng);
49
	this.remplirChampsLambert93(lat, lng);
50
};
51
 
52
proj4.defs([
53
	['EPSG:4326', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'],
54
	['EPSG:2154', '+title=RGF93 / Lambert-93 +proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs']
55
]);
56
WidgetSaisieArbresRemarquables.prototype.remplirChampsLambert93 = function(lat, lng) {
57
	// Prendre en compte l'initialisation des projections
58
	var coordinate = {x: lng,y: lat};
59
	proj4(proj4.defs('EPSG:4326'), proj4.defs('EPSG:2154')).forward(coordinate);
60
	$('#l93-x').val(coordinate.x.toFixed(0));
61
	$('#l93-y').val(coordinate.y.toFixed(0));
62
};
63
 
64
WidgetSaisieArbresRemarquables.prototype.trouverAltitude = function(pos) {
65
	var url_service = this.serviceAltitudeUrl,
66
		urlAltFormatee = url_service.replace('{lat}', pos.lat()).replace('{lon}', pos.lng());
67
	var lthis = this;
68
	$.ajax({
69
		url: urlAltFormatee,
70
		type: 'GET',
71
		dataType: 'jsonp',
72
		beforeSend : function() {
73
			$('#altitude').empty();
74
			$('#dialogue-erreur .alert-txt').empty();
75
		},
76
		success : function(data, textStatus, jqXHR) {
77
			$('#altitude').empty().append(data.altitude);
78
			$('#marqueur-altitude').data('altitude', data.altitude);
79
		},
80
		statusCode : {
81
		    500 : function(jqXHR, textStatus, errorThrown) {
82
				if (lthis.debug) {
83
					$('#dialogue-erreur .alert-txt').append('<p id="msg">Un problème est survenu lors de l\'appel au service fournissant l\'altitude.</p>');
84
					reponse = jQuery.parseJSON(jqXHR.responseText);
85
					var erreurMsg = '';
86
					if (reponse != null) {
87
						$.each(reponse, function (cle, valeur) {
88
							erreurMsg += valeur + '<br />';
89
						});
90
					}
91
 
92
					$('#dialogue-erreur .alert-txt').append('<p class="msg-erreur">Erreur 500 : '+errorThrown+"<br />"+erreurMsg+'</p>');
93
				}
94
		    }
95
		},
96
		error : function(jqXHR, textStatus, errorThrown) {
97
			if (lthis.debug) {
98
				$("#dialogue-erreur .alert-txt").append('<p class="msg">Une erreur Ajax est survenue lors de l\'appel au service fournissant l\'altitude.</p>');
99
				reponse = jQuery.parseJSON(jqXHR.responseText);
100
				var erreurMsg = '';
101
				if (reponse != null) {
102
					$.each(reponse, function (cle, valeur) {
103
						erreurMsg += valeur + '<br />';
104
					});
105
				}
106
 
107
				$('#dialogue-erreur .alert-txt').append('<p class="msg-erreur">Erreur Ajax : '+errorThrown+' (type : '+textStatus+') <br />'+erreurMsg+'</p>');
108
			}
109
		},
110
		complete : function(jqXHR, textStatus) {
111
			var debugMsg = extraireEnteteDebug(jqXHR);
112
			if (debugMsg != '') {
113
				if (lthis.debug) {
114
					$('#dialogue-erreur .alert-txt').append('<pre class="msg-debug msg">Débogage : '+debugMsg+'</pre>');
115
				}
116
			}
117
			if ($('#dialogue-erreur .msg').length > 0) {
118
				$('#dialogue-erreur').show();
119
			}
120
		}
121
	});
122
};
123
 
124
WidgetSaisieArbresRemarquables.prototype.surChangementTaxonListe = function() {
125
	if ($('#taxon-liste').val() === '?') {
126
		$('#taxon-input-groupe').removeClass('hidden');
127
		$('#taxon').valid();
128
	} else {
129
		$('#taxon-input-groupe').addClass('hidden');
130
	}
131
};
132
 
133
WidgetSaisieArbresRemarquables.prototype.surChangementEsthetique = function() {
2944 mathias 134
	var cochee = $('#esthetique-autre').prop("checked");
135
	if (cochee) {
136
		$('#autre-esthetique-id').removeClass('hidden');
2943 delphine 137
	} else {
2944 mathias 138
		$('#autre-esthetique-id').addClass('hidden');
2943 delphine 139
	}
140
};
141
 
142
WidgetSaisieArbresRemarquables.prototype.surChangementSituation = function() {
2944 mathias 143
	var cochee = $('#situation-autre').prop("checked");
144
	if (cochee) {
2943 delphine 145
		$('#autre-situation-id').removeClass('hidden');
146
	} else {
147
		$('#autre-situation-id').addClass('hidden');
148
	}
149
};
150
 
2944 mathias 151
// Activation de la saisie si on clique sur un type de champ
152
WidgetSaisieArbresRemarquables.prototype.surChangementRemarquable = function() {
153
	$('.cb-remarquable').each(function() {
154
		var id = $(this).prop('id'),
155
			cochee = $(this).prop("checked"),
156
			idCommentaire = id + '-commentaire',
157
			champCommentaire = $('#' + idCommentaire);
158
		if (cochee) {
159
			champCommentaire.removeClass('hidden');
160
		} else {
161
			champCommentaire.addClass('hidden');
162
		}
163
	});
164
};
165
 
2943 delphine 166
WidgetSaisieArbresRemarquables.prototype.configurerFormValidator = function() {
167
	$.validator.addMethod(
168
		'dateCel',
169
		function (value, element) {
170
			return value == '' || (/^[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}$/.test(value));
171
		},
172
		'Format : jj/mm/aaaa. Date incomplète, utiliser 0, exemple : 00/12/2011.');
173
 
174
	$.validator.addMethod(
175
		'autreSp',
176
		function (value, element) {
177
			var taxonListe = $('#taxon-liste').val();
178
			return taxonListe !== '?' || (taxonListe === '?' && value != '');
179
		},
180
		"Veuillez sélectionner une espèce ou une indication sur la plante.");
181
 
182
	// Modification des méthodes par défaut de Jquery Validation pour Boostrap 3
183
	$.validator.setDefaults({
184
		ignore: [],// Forcer Jquery Validate à examiner les éléments en "display:none;"
185
		highlight: function(element) {
186
			$(element).closest('.form-group').addClass('has-error');
187
		},
188
		unhighlight: function(element) {
189
			$(element).closest('.form-group').removeClass('has-error');
190
		},
191
		success: function(element) {
192
			$(element).closest('.form-group').removeClass('has-error').addClass('has-success');
193
 
194
			if ($(element).attr('id') == 'taxon' && $('#taxon').val() != '') {
195
				// Si le taxon n'est pas lié au référentiel, on vide le data associé
196
				if ($('#taxon').data('value') != $('#taxon').val()) {
197
					$('#taxon').data('numNomSel', '');
198
					$('#taxon').data('nomRet', '');
199
					$('#taxon').data('numNomRet', '');
200
					$('#taxon').data('nt', '');
201
					$('#taxon').data('famille', '');
202
				}
203
			}
204
		},
205
		errorElement: 'span',
206
		errorClass: 'help-block',
207
		errorPlacement: function(error, element) {
208
			//console.log(element.attr('name') +'-'+ element.parent('.input-group').length);
209
			if (element.parent('.input-group').length) {
210
				error.insertAfter(element.parent());
211
			} else {
212
				error.insertAfter(element);
213
			}
214
		}
215
	});
216
};
217
 
2947 mathias 218
// surcharge fonction validerFormulaire()
219
WidgetSaisieArbresRemarquables.prototype.validerFormulaire = function() {
220
	observateur = $("#form-observateur").valid();
221
	situation = $("#form-situation").valid();
222
	criteres = $("#form-criteres").valid();
2948 delphine 223
	remarquable = $("#form-remarquable").valid();
2958 mathias 224
	remarquabilite = $("#form-remarquabilite").valid();
2947 mathias 225
	station = $("#form-station").valid();
226
	obs = $("#form-obs").valid();
2948 delphine 227
	return (observateur && station && obs && criteres && situation &&remarquable);
2947 mathias 228
};
229
 
2943 delphine 230
WidgetSaisieArbresRemarquables.prototype.definirReglesFormValidator = function() {
231
	$('#form-observateur').validate({
232
		rules: {
233
			courriel : {
234
				required : true,
235
				email : true},
236
			courriel_confirmation : {
237
				required : true,
238
				equalTo: '#courriel'}
239
		}
240
	});
241
	$('#form-station').validate({
242
		rules: {
2945 delphine 243
			date: {
244
				required: true,
245
				'dateCel' : true},
2943 delphine 246
			latitude : {
247
				range: [-90, 90],
248
				required: true},
249
			longitude : {
250
				range: [-180, 180],
251
				required: true},
252
			'l93-x': 'required',
253
			'l93-y': 'required'
254
		}
255
	});
256
	$('#form-obs').validate({
257
		rules: {
258
			'taxon-liste': {required: true},
259
			taxon: {autreSp: true},
260
			certitude: 'required',
2945 delphine 261
			arbreRemarquableClasseCirconference: 'required',
262
			arbreRemarquableSante: 'required',
263
			arbreRemarquablePresenceCavite: 'required'
2943 delphine 264
		}
265
	});
2947 mathias 266
	$('#form-situation').validate({
267
		rules: {
2948 delphine 268
			"situation-ch" : {
2947 mathias 269
            	required : true,
270
            	minlength : 1
271
            }
272
		},
273
		errorPlacement: function(error, element) {
2948 delphine 274
			error.insertBefore(element.parent());
2947 mathias 275
			error.insertAfter(element.closest('div.btn-group.dropdown'));
276
		}
277
	});
2958 mathias 278
	$('#form-remarquabilite').validate({
2945 delphine 279
		rules: {
280
			arbreRemarquableRemarquabilite: 'required'
281
		}
282
	});
2948 delphine 283
	$('#form-remarquable').validate({
284
		rules: {
285
			"remarquable-ch" : {
286
            	required : true,
287
            	minlength : 1
288
            }
289
		},
290
		errorPlacement: function(error, element) {
291
			error.insertBefore(element.parent());
292
			error.insertAfter(element.closest('div.control-group'));
293
		}
294
	});
2943 delphine 295
};
296
 
297
 
298
 
299
WidgetSaisieArbresRemarquables.prototype.basculerOuvertureFermetureCadre = function(element) {
300
	if (element.hasClass('glyphicon-plus-sign')) {
301
		element.removeClass('glyphicon-plus-sign').addClass('glyphicon-minus-sign');
302
	} else {
303
		element.removeClass('glyphicon-minus-sign').addClass('glyphicon-plus-sign');
304
	}
305
};
306
 
307
WidgetSaisieArbresRemarquables.prototype.basculerAffichageCoord = function() {
308
	var textActuel = $(this).text(),
309
			textARemplacer = $(this).data('toggle-text');
310
	$(this).text(textARemplacer).data('toggle-text', textActuel);
311
 
312
	if ($(this).hasClass('cacher-coord')) {
313
		$(this).removeClass('cacher-coord').addClass('afficher-coord');
314
		$('#coordonnees-geo').addClass('hidden');
315
	} else {
316
		$(this).removeClass('afficher-coord').addClass('cacher-coord');
317
		$('#coordonnees-geo').removeClass('hidden');
318
	}
319
 
320
	return false;
321
};
322
 
323
WidgetSaisieArbresRemarquables.prototype.afficherObs = function() {
324
	var numNomSel = ($('#taxon-liste').val() == '?') ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
325
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
326
		taxon = ($('#taxon-liste').val() == '?') ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
327
		referentiel = (numNomSel == undefined) ? '' : '['+ this.nomSciReferentiel +']',
328
		commune = $('#commune-nom').text(),
329
		codeInsee = $('#commune-code-insee').text(),
330
		lat = $('input[name="latitude"]').val(),
331
		lng = $('input[name="longitude"]').val(),
332
		date = $('#date').val(),
2945 delphine 333
		certitude = $('#certitude').val(),
334
		circonference = $('#circonference').val(),
335
		hauteur = $('#hauteurNbr').val(),
336
		hauteurPrec = ($('#hauteurPrec').val() != "" ? ' (' + $('#hauteurPrec').val() + ')' : ''),
337
		age = $('#age').val(),
338
		sante = $('#sante').val(),
339
		cavites = $('#cavites').val(),
340
		historique = $('#historique').val(),
2948 delphine 341
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + ".<br />" : '') + $('#notes').val(),
342
		remarquabilite= $('#remarquabilite').val();
2943 delphine 343
 
2948 delphine 344
	var situation = [];
345
	// récupération des cases à cocher multiples dans un tableau pour l'abondance
346
	$('input[name="situation-ch"]:checked').each(function() {
347
		situation.push(' '+$(this).val());
348
	});
349
	var remarquable = [];
350
	// récupération des cases à cocher multiples dans un tableau pour l'abondance
351
	$('input[name="remarquable-ch"]:checked').each(function() {
352
		remarquable.push(' '+$(this).val());
353
	});
354
	var esthetique = [];
355
	// récupération des cases à cocher multiples dans un tableau pour l'abondance
356
	$('input[name="esthetique-ch"]:checked').each(function() {
357
		esthetique.push(' '+$(this).val());
358
	});
2943 delphine 359
 
360
	$('#liste-obs').prepend(
361
		'<div id="obs'+this.obsNbre+'" class="obs obs'+this.obsNbre+'">'+
362
				'<div class="well">'+
363
					'<div class="obs-action pull-right has-tooltip" data-placement="bottom" '+
364
						'title="Supprimer cette observation de la liste à transmettre">'+
365
						'<button class="btn btn-danger supprimer-obs" value="'+this.obsNbre+'" title="'+this.obsNbre+'">'+
366
							'<span class="glyphicon glyphicon-trash icon-white"></i>'+
367
						'</button>'+
368
					'</div> '+
369
					'<div class="row">'+
370
						'<div class="col-md-2 obs-miniatures">'+
371
							this.ajouterImgMiniatureAuTransfert()+
372
						'</div>'+
373
						'<div class="col-md-8">'+
374
							'<ul class="list-unstyled obs-entete">'+
375
								'<li>'+
376
									'<span class="nom-sci">' + taxon + '</span> ' +
377
									this.formaterNumNomSel(numNomSel)+
2945 delphine 378
									'<span class="referentiel-obs">' + referentiel +' ('+certitude+') </span>' +
2943 delphine 379
									' observé à ' +
380
									'<span class="commune">' + commune + '</span> ' +
381
									'(' + codeInsee + ') [' + lat +' / ' + lng + ']' +
382
									' le ' +
383
									'<span class="date">' + date + '</span>' +
384
								'</li>' +
385
							'</ul>'+
386
							'<ul class="list-unstyled obs-details">'+
387
								'<li>' +
2945 delphine 388
									'<span>Circonférence :</span> ' + circonference + ' ; ' +
389
									'<span>Hauteur :</span> ' + hauteur + 'm'+ hauteurPrec+'; ' +
390
									'<span>Âge :</span> ' + age + ' ; ' +
391
									'<span>Santé :</span> ' + sante + ' ; ' +
2943 delphine 392
									'<span>Cavités :</span> ' + cavites + ' ; ' +
2948 delphine 393
									'<span>Situation :</span> ' + situation + ' ; ' +
2943 delphine 394
								'</li>' +
395
								'<li>' +
2948 delphine 396
									'<span>Intérêt esthetique :</span> ' + esthetique + ' ' +
397
									'<span>Intérêt historique :</span> ' + historique + ' ' +
398
									'<span>Raison(s) remarquabilité :</span> ' + remarquable + ' ' +
399
									'<span>Degré de remarquabilité  :</span> ' + remarquabilite + ' ' +
2943 delphine 400
								'</li>' +
401
								'<li>' +
2945 delphine 402
									'<span>Notes :</span> ' + notes +
2943 delphine 403
								'</li>'+
404
							'</ul>'+
405
						'</div>'+
406
					'</div>'+
407
				'</div>'+
408
		'</div>');
409
};
410
 
411
WidgetSaisieArbresRemarquables.prototype.formaterNumNomSel = function(numNomSel) {
412
	var nn = '';
413
	if (numNomSel == undefined) {
414
		nn = '<span class="alert-error">[non lié au référentiel]</span>';
415
	} else {
416
		nn = '<span class="nn">[nn'+numNomSel+']</span>';
417
	}
418
	return nn;
419
};
420
 
421
WidgetSaisieArbresRemarquables.prototype.getTextOptionSelectionne = function(id) {
422
	return ($('#' + id).val() != undefined ? $('#' + id + ' option:selected').text()  : '');
423
};
424
 
425
// @TODO harmoniser (altitude)
426
WidgetSaisieArbresRemarquables.prototype.stockerObsData = function() {
427
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
428
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
429
		numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
430
		nomSel = nomHorsListe ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
431
		nomRet = nomHorsListe ? $('#taxon').data('nomRet') : this.taxons[numNomSel]['nom_ret'],
432
		numNomRet = nomHorsListe ? $('#taxon').data('numNomRet') : this.taxons[numNomSel]['num_nom_ret'],
433
		numTaxon = nomHorsListe ? $('#taxon').data('nt') : this.taxons[numNomSel]['num_taxon'],
434
		famille = nomHorsListe ? $('#taxon').data('famille') : this.taxons[numNomSel]['famille'],
435
		referentiel = (numNomSel == undefined) ? '' : this.nomSciReferentiel;
436
 
437
	$('#liste-obs').data('obsId'+this.obsNbre, {
438
		'date' : $('#date').val(),
439
		'notes' : $('#notes').val(),
440
 
441
		'nom_sel': nomSel,
442
		'num_nom_sel': numNomSel,
443
		'nom_ret': nomRet,
444
		'num_nom_ret': numNomRet,
445
		'num_taxon': numTaxon,
446
		'famille': famille,
447
		'referentiel': referentiel,
448
 
449
		'latitude' : $('#latitude').val(),
450
		'longitude' : $('#longitude').val(),
451
		'commune_nom' : $('#commune-nom').text(),
452
		'commune_code_insee' : $('#commune-code-insee').text(),
453
		'altitude': $('#altitude').text(),
454
		'lieudit': $('#lieudit').val(),
455
		'certitude': $('#certitude').val(),
456
 
457
		//Ajout des champs images
458
		'image_nom' :this. getNomsImgsOriginales(),
459
 
460
		// Ajout des champs étendus de l'obs
461
		'obs_etendue': this.getObsChpEtendus()
462
	});
463
};
464
 
2944 mathias 465
/**
466
 * Redéfinition pour les champs complexes
467
 */
468
WidgetSaisieArbresRemarquables.prototype.getObsChpEtendus = function() {
2948 delphine 469
	var champs = [],
470
		situation = [],
471
		esthetique = [],
472
		remarquable = [];
2943 delphine 473
 
2944 mathias 474
	$('.obs-chp-etendu').each(function() {
475
		var valeur = $(this).val(),
476
			cle = $(this).attr('name'),
477
			label = $(this).data('label');
478
		if (valeur != '') {
479
			var chpEtendu = {cle: cle, label: label, valeur: valeur};
480
			champs.push(chpEtendu);
481
		}
482
	});
2948 delphine 483
 
2949 delphine 484
	$('input.cb-situation:checked').each(function() {
2948 delphine 485
		var valeur = $(this).val();
486
		if (valeur == 'Autre') {
487
			if ($('#autre-situation').val() != '') {
488
				situation.push(valeur+' :'+$('#autre-situation').val());
489
			} else {
490
				situation.push(valeur);
491
			}
492
		} else if (valeur != '') {
493
			situation.push(valeur);
494
		}
495
	});
496
	var chpEtendu = {cle: 'arbreRemarquableSituation', label: 'contexte/situation', valeur: situation.join('; ')};
497
	champs.push(chpEtendu);
498
 
2949 delphine 499
	$('input.cb-remarquable:checked').each(function() {
2948 delphine 500
		var valeur = $(this).val(),
501
			comm = $(this).attr('id')+'-commentaire',
502
			commid = $('#'+comm).val();
2958 mathias 503
		if (commid != '' && typeof commid !== "undefined") {
2948 delphine 504
			remarquable.push(valeur+' :'+commid);
505
		} else {
506
			remarquable.push(valeur);
507
		}
508
	});
509
	var chpEtendu = {cle: 'arbreRemarquableRemarquable', label: 'remarquable', valeur: remarquable.join('; ')};
510
	champs.push(chpEtendu);
511
 
2949 delphine 512
	$('input.cb-esthetique:checked').each(function() {
2948 delphine 513
		var valeur = $(this).val();
514
		if (valeur == 'Autre') {
515
			if ($('#autre-esthetique').val() != '') {
516
				esthetique.push(valeur+' :'+$('#autre-esthetique').val());
517
			} else {
518
				esthetique.push(valeur);
519
			}
520
		} else if (valeur != '') {
521
			esthetique.push(valeur);
522
		}
523
	});
524
	var chpEtendu = {cle: 'arbreRemarquableEsthetique', label: 'Intérêt esthétique', valeur: esthetique.join('; ')};
525
	champs.push(chpEtendu);
526
 
2944 mathias 527
	return champs;
528
};