Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
2711 mathias 1
// Héritage
2
WidgetSaisieArbresTetards = function() {
3
	this.serviceAltitudeUrl = null;
4
	this.taxons = {};
2360 jpm 5
}
2711 mathias 6
WidgetSaisieArbresTetards.prototype = new WidgetSaisie();
2360 jpm 7
 
2711 mathias 8
WidgetSaisieArbresTetards.prototype.initEvts = function() {
9
	// super()
10
	WidgetSaisie.prototype.initEvts.call(this);
2360 jpm 11
 
2711 mathias 12
	var lthis = this;
13
	$('body').on('click', '.fermer', function(event) {
14
			event.preventDefault();
15
			lthis.basculerOuvertureFermetureCadre($(this).find('.icone'));
2360 jpm 16
	});
17
 
2711 mathias 18
	this.surChangementTaxonListe();// Vérif lors du chargement de la page
19
	$('#taxon-liste').on('change', this.surChangementTaxonListe.bind(this));
20
	this.surChangementFormation();// Vérif lors du chargement de la page
21
	$('#formation').on('change', this.surChangementFormation.bind(this));
2372 jpm 22
 
2711 mathias 23
	// Gestion des obs
24
	this.configurerMilieux();
25
	this.surChangementNbreObs();
26
};
2372 jpm 27
 
2711 mathias 28
WidgetSaisieArbresTetards.prototype.focusChampFormulaire = function() {
29
	$("#carte-recherche").focus();
30
};
2360 jpm 31
 
2711 mathias 32
WidgetSaisieArbresTetards.prototype.mettreAJourMarkerPosition = function(latLng) {
33
	this.trouverCommune(latLng);
34
	this.trouverAltitude(latLng);
2372 jpm 35
 
36
	var lat = latLng.lat().toFixed(5),
37
		lng = latLng.lng().toFixed(5);
2711 mathias 38
	this.remplirChampLatitude(lat);
39
	this.remplirChampLongitude(lng);
40
	this.remplirChampsLambert93(lat, lng);
41
};
2360 jpm 42
 
2372 jpm 43
proj4.defs([
44
	['EPSG:4326', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'],
45
	['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']
46
]);
2711 mathias 47
WidgetSaisieArbresTetards.prototype.remplirChampsLambert93 = function(lat, lng) {
2372 jpm 48
	// Prendre en compte l'initialisation des projections
49
	var coordinate = {x: lng,y: lat};
50
	proj4(proj4.defs('EPSG:4326'), proj4.defs('EPSG:2154')).forward(coordinate);
51
	$('#l93-x').val(coordinate.x.toFixed(0));
52
	$('#l93-y').val(coordinate.y.toFixed(0));
2711 mathias 53
};
2372 jpm 54
 
2711 mathias 55
WidgetSaisieArbresTetards.prototype.trouverAltitude = function(pos) {
56
	var url_service = this.serviceAltitudeUrl,
57
		urlAltFormatee = url_service.replace('{lat}', pos.lat()).replace('{lon}', pos.lng());
58
	var lthis = this;
59
	$.ajax({
60
		url: urlAltFormatee,
61
		type: 'GET',
62
		dataType: 'jsonp',
63
		beforeSend : function() {
64
			$('#altitude').empty();
65
			$('#dialogue-erreur .alert-txt').empty();
66
		},
67
		success : function(data, textStatus, jqXHR) {
68
			$('#altitude').empty().append(data.altitude);
69
			$('#marqueur-altitude').data('altitude', data.altitude);
70
		},
71
		statusCode : {
72
		    500 : function(jqXHR, textStatus, errorThrown) {
73
				if (lthis.debug) {
74
					$('#dialogue-erreur .alert-txt').append('<p id="msg">Un problème est survenu lors de l\'appel au service fournissant l\'altitude.</p>');
2372 jpm 75
					reponse = jQuery.parseJSON(jqXHR.responseText);
76
					var erreurMsg = '';
77
					if (reponse != null) {
78
						$.each(reponse, function (cle, valeur) {
79
							erreurMsg += valeur + '<br />';
80
						});
81
					}
2360 jpm 82
 
2711 mathias 83
					$('#dialogue-erreur .alert-txt').append('<p class="msg-erreur">Erreur 500 : '+errorThrown+"<br />"+erreurMsg+'</p>');
2372 jpm 84
				}
2711 mathias 85
		    }
86
		},
87
		error : function(jqXHR, textStatus, errorThrown) {
88
			if (lthis.debug) {
89
				$("#dialogue-erreur .alert-txt").append('<p class="msg">Une erreur Ajax est survenue lors de l\'appel au service fournissant l\'altitude.</p>');
90
				reponse = jQuery.parseJSON(jqXHR.responseText);
91
				var erreurMsg = '';
92
				if (reponse != null) {
93
					$.each(reponse, function (cle, valeur) {
94
						erreurMsg += valeur + '<br />';
95
					});
2372 jpm 96
				}
97
 
2711 mathias 98
				$('#dialogue-erreur .alert-txt').append('<p class="msg-erreur">Erreur Ajax : '+errorThrown+' (type : '+textStatus+') <br />'+erreurMsg+'</p>');
2360 jpm 99
			}
100
		},
2711 mathias 101
		complete : function(jqXHR, textStatus) {
102
			var debugMsg = extraireEnteteDebug(jqXHR);
103
			if (debugMsg != '') {
104
				if (lthis.debug) {
105
					$('#dialogue-erreur .alert-txt').append('<pre class="msg-debug msg">Débogage : '+debugMsg+'</pre>');
106
				}
2372 jpm 107
			}
2711 mathias 108
			if ($('#dialogue-erreur .msg').length > 0) {
109
				$('#dialogue-erreur').show();
2360 jpm 110
			}
111
		}
112
	});
2372 jpm 113
};
114
 
2711 mathias 115
WidgetSaisieArbresTetards.prototype.surChangementTaxonListe = function() {
2372 jpm 116
	if ($('#taxon-liste').val() === '?') {
117
		$('#taxon-input-groupe').removeClass('hidden');
118
		$('#taxon').valid();
119
	} else {
120
		$('#taxon-input-groupe').addClass('hidden');
121
	}
2711 mathias 122
};
2372 jpm 123
 
2711 mathias 124
WidgetSaisieArbresTetards.prototype.surChangementFormation = function() {
2372 jpm 125
	if ($('#formation').val() === 'alignement') {
126
		$('#aligne-nbre-groupe').removeClass('hidden');
127
		$('#aligne-nbre').valid();
128
	} else {
129
		$('#aligne-nbre-groupe').addClass('hidden');
130
	}
2711 mathias 131
};
2372 jpm 132
 
2711 mathias 133
WidgetSaisieArbresTetards.prototype.configurerFormValidator = function() {
2360 jpm 134
	$.validator.addMethod(
2372 jpm 135
		'dateCel',
2360 jpm 136
		function (value, element) {
2372 jpm 137
			return value == '' || (/^[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}$/.test(value));
2360 jpm 138
		},
2372 jpm 139
		'Format : jj/mm/aaaa. Date incomplète, utiliser 0, exemple : 00/12/2011.');
2360 jpm 140
 
2372 jpm 141
	$.validator.addMethod(
142
		'aligneNbre',
143
		function (value, element) {
144
			var ok = true;
145
			if ($('#formation').val() === 'alignement') {
146
				ok = (value != '' && /^[0-9]+$/.test(value) && value > 1);
147
			}
148
			return ok;
149
		},
150
		"Veuillez indiquer le nombre d'arbres d'alignement.");
151
 
152
	$.validator.addMethod(
153
		'autreSp',
154
		function (value, element) {
155
			var taxonListe = $('#taxon-liste').val();
156
			return taxonListe !== '?' || (taxonListe === '?' && value != '');
157
		},
158
		"Veuillez sélectionner une espèce ou une indication sur la plante.");
159
 
160
	// Modification des méthodes par défaut de Jquery Validation pour Boostrap 3
161
	$.validator.setDefaults({
162
		ignore: [],// Forcer Jquery Validate à examiner les éléments en "display:none;"
2360 jpm 163
		highlight: function(element) {
2372 jpm 164
			$(element).closest('.form-group').addClass('has-error');
2360 jpm 165
		},
2372 jpm 166
		unhighlight: function(element) {
167
			$(element).closest('.form-group').removeClass('has-error');
168
		},
2360 jpm 169
		success: function(element) {
2372 jpm 170
			$(element).closest('.form-group').removeClass('has-error').addClass('has-success');
2360 jpm 171
 
2372 jpm 172
			if ($(element).attr('id') == 'taxon' && $('#taxon').val() != '') {
2360 jpm 173
				// Si le taxon n'est pas lié au référentiel, on vide le data associé
174
				if ($('#taxon').data('value') != $('#taxon').val()) {
175
					$('#taxon').data('numNomSel', '');
176
					$('#taxon').data('nomRet', '');
177
					$('#taxon').data('numNomRet', '');
178
					$('#taxon').data('nt', '');
179
					$('#taxon').data('famille', '');
180
				}
181
			}
2372 jpm 182
		},
183
		errorElement: 'span',
184
		errorClass: 'help-block',
185
		errorPlacement: function(error, element) {
186
			//console.log(element.attr('name') +'-'+ element.parent('.input-group').length);
187
			if (element.parent('.input-group').length) {
188
				error.insertAfter(element.parent());
189
			} else {
190
				error.insertAfter(element);
191
			}
2360 jpm 192
		}
193
	});
2711 mathias 194
};
2360 jpm 195
 
2711 mathias 196
WidgetSaisieArbresTetards.prototype.definirReglesFormValidator = function() {
2360 jpm 197
	$('#form-observateur').validate({
198
		rules: {
199
			courriel : {
200
				required : true,
201
				email : true},
202
			courriel_confirmation : {
203
				required : true,
204
				equalTo: '#courriel'}
205
		}
206
	});
207
	$('#form-station').validate({
208
		rules: {
209
			latitude : {
210
				range: [-90, 90],
211
				required: true},
212
			longitude : {
213
				range: [-180, 180],
214
				required: true},
2372 jpm 215
			'l93-x': 'required',
2380 jpm 216
			'l93-y': 'required'
2360 jpm 217
		}
218
	});
219
	$('#form-obs').validate({
220
		rules: {
221
			date: {
222
				required: true,
223
				'dateCel' : true},
2372 jpm 224
			'taxon-liste': {required: true},
225
			taxon: {autreSp: true},
226
			certitude: 'required',
227
			arbreTetardFormation: 'required',
228
			arbreTetardAligneNbre: {aligneNbre: true}
2360 jpm 229
		}
230
	});
2711 mathias 231
};
2360 jpm 232
 
2711 mathias 233
WidgetSaisieArbresTetards.prototype.configurerMilieux = function() {
2372 jpm 234
	$('.cb-milieux').on('click', function(event) {
235
		$(this).valid();
236
		event.stopPropagation();
237
	});
2711 mathias 238
};
2360 jpm 239
 
2711 mathias 240
WidgetSaisieArbresTetards.prototype.basculerOuvertureFermetureCadre = function(element) {
2372 jpm 241
	if (element.hasClass('glyphicon-plus-sign')) {
242
		element.removeClass('glyphicon-plus-sign').addClass('glyphicon-minus-sign');
2360 jpm 243
	} else {
2372 jpm 244
		element.removeClass('glyphicon-minus-sign').addClass('glyphicon-plus-sign');
2360 jpm 245
	}
2711 mathias 246
};
2360 jpm 247
 
2711 mathias 248
WidgetSaisieArbresTetards.prototype.basculerAffichageCoord = function() {
2372 jpm 249
	var textActuel = $(this).text(),
250
			textARemplacer = $(this).data('toggle-text');
251
	$(this).text(textARemplacer).data('toggle-text', textActuel);
252
 
253
	if ($(this).hasClass('cacher-coord')) {
254
		$(this).removeClass('cacher-coord').addClass('afficher-coord');
255
		$('#coordonnees-geo').addClass('hidden');
256
	} else {
257
		$(this).removeClass('afficher-coord').addClass('cacher-coord');
258
		$('#coordonnees-geo').removeClass('hidden');
259
	}
260
 
2360 jpm 261
	return false;
2711 mathias 262
};
2360 jpm 263
 
2711 mathias 264
WidgetSaisieArbresTetards.prototype.afficherObs = function() {
2372 jpm 265
	var numNomSel = ($('#taxon-liste').val() == '?') ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
266
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
267
		taxon = ($('#taxon-liste').val() == '?') ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
2711 mathias 268
		referentiel = (numNomSel == undefined) ? '' : '['+ this.nomSciReferentiel +']',
2372 jpm 269
		commune = $('#commune-nom').text(),
270
		codeInsee = $('#commune-code-insee').text(),
271
		lat = $('input[name="latitude"]').val(),
272
		lng = $('input[name="longitude"]').val(),
273
		date = $('#date').val(),
2711 mathias 274
		formation = this.getTextOptionSelectionne('formation'),
2372 jpm 275
		nbreAligne = ($('#aligne-nbre').val() != undefined ? ' (' + $('#aligne-nbre').val() + ')' : ''),
2711 mathias 276
		cavites = this.getTextOptionSelectionne('cavites'),
277
		circonference = this.getTextOptionSelectionne('circonference'),
278
		hauteurTete = this.getTextOptionSelectionne('hauteur-tete'),
2372 jpm 279
		presenceSp = $('#presence-sp').val(),
2711 mathias 280
		taille = this.getTextOptionSelectionne('taille-type'),
281
		entretien = this.getTextOptionSelectionne('entretien'),
282
		etatSanitaire = this.getTextOptionSelectionne('etat-sanitaire'),
283
		milieux = this.getMilieux(),
284
		notes = (nomSpecial ? this.taxons[numNomSel]['nom_fr'] + ".<br />" : '') + $('#notes').val();
2372 jpm 285
 
286
	$('#liste-obs').prepend(
2711 mathias 287
		'<div id="obs'+this.obsNbre+'" class="obs obs'+this.obsNbre+'">'+
2360 jpm 288
				'<div class="well">'+
289
					'<div class="obs-action pull-right has-tooltip" data-placement="bottom" '+
290
						'title="Supprimer cette observation de la liste à transmettre">'+
2711 mathias 291
						'<button class="btn btn-danger supprimer-obs" value="'+this.obsNbre+'" title="'+this.obsNbre+'">'+
2372 jpm 292
							'<span class="glyphicon glyphicon-trash icon-white"></i>'+
2360 jpm 293
						'</button>'+
294
					'</div> '+
2372 jpm 295
					'<div class="row">'+
296
						'<div class="col-md-2 obs-miniatures">'+
2711 mathias 297
							this.ajouterImgMiniatureAuTransfert()+
2360 jpm 298
						'</div>'+
2372 jpm 299
						'<div class="col-md-8">'+
300
							'<ul class="list-unstyled obs-entete">'+
2360 jpm 301
								'<li>'+
2372 jpm 302
									'<span class="nom-sci">' + taxon + '</span> ' +
2711 mathias 303
									this.formaterNumNomSel(numNomSel)+
2372 jpm 304
									'<span class="referentiel-obs">' + referentiel + '</span>' +
305
									' observé à ' +
306
									'<span class="commune">' + commune + '</span> ' +
307
									'(' + codeInsee + ') [' + lat +' / ' + lng + ']' +
308
									' le ' +
309
									'<span class="date">' + date + '</span>' +
310
								'</li>' +
311
							'</ul>'+
312
							'<ul class="list-unstyled obs-details">'+
313
								'<li>' +
314
									'<span>Situation(s) :</span> ' + milieux + ' ; ' +
315
									'<span>Formation :</span> ' + formation + nbreAligne + ' ; ' +
316
									'<span>Cavités :</span> ' + cavites + ' ; ' +
317
									'<span>Circonférence :</span> ' + circonference + ' ; ' +
318
									'<span>Hauteur de la tête :</span> ' + hauteurTete + ' ; ' +
319
								'</li>' +
320
								'<li>' +
321
									'<span>Présences sur l\'arbre :</span> ' + presenceSp + ' ' +
322
								'</li>' +
323
								'<li>' +
324
									'<span>Type taille :</span> ' + taille + ' ; ' +
2391 jpm 325
									'<span>Entretien :</span> ' + entretien + ' ; ' +
2372 jpm 326
									'<span>État sanitaire :</span> ' + etatSanitaire + ' ; ' +
327
								'</li>' +
328
								'<li>' +
329
									'<span>Commentaires :</span> ' + notes +
2360 jpm 330
								'</li>'+
331
							'</ul>'+
332
						'</div>'+
333
					'</div>'+
334
				'</div>'+
335
		'</div>');
2711 mathias 336
};
2360 jpm 337
 
2711 mathias 338
WidgetSaisieArbresTetards.prototype.formaterNumNomSel = function(numNomSel) {
339
	var nn = '';
340
	if (numNomSel == undefined) {
341
		nn = '<span class="alert-error">[non lié au référentiel]</span>';
342
	} else {
343
		nn = '<span class="nn">[nn'+numNomSel+']</span>';
344
	}
345
	return nn;
346
};
347
 
348
WidgetSaisieArbresTetards.prototype.getMilieux = function() {
2372 jpm 349
	var milieuxStr = '',
350
		milieux = [];
351
	$('.cb-milieux:checked').each(function() {
352
		milieux.push($(this).val());
353
	});
354
 
355
	milieuxStr = Array.prototype.slice.call(milieux).join(', ');
356
	return milieuxStr;
2711 mathias 357
};
2372 jpm 358
 
2711 mathias 359
WidgetSaisieArbresTetards.prototype.getTextOptionSelectionne = function(id) {
2372 jpm 360
	return ($('#' + id).val() != undefined ? $('#' + id + ' option:selected').text()  : '');
2711 mathias 361
};
2372 jpm 362
 
2711 mathias 363
// @TODO harmoniser (altitude)
364
WidgetSaisieArbresTetards.prototype.stockerObsData = function() {
2372 jpm 365
	var nomHorsListe = $('#taxon-liste').val() == '?' ? true : false;
366
		nomSpecial = $('#taxon-liste option:selected').hasClass('nom-special'),
367
		numNomSel = nomHorsListe ? $('#taxon').data('numNomSel') : $('#taxon-liste').val(),
368
		nomSel = nomHorsListe ? $('#taxon').val() : $('#taxon-liste option:selected').data('nom-a-sauver'),
2711 mathias 369
		nomRet = nomHorsListe ? $('#taxon').data('nomRet') : this.taxons[numNomSel]['nom_ret'],
370
		numNomRet = nomHorsListe ? $('#taxon').data('numNomRet') : this.taxons[numNomSel]['num_nom_ret'],
371
		numTaxon = nomHorsListe ? $('#taxon').data('nt') : this.taxons[numNomSel]['num_taxon'],
372
		famille = nomHorsListe ? $('#taxon').data('famille') : this.taxons[numNomSel]['famille'],
373
		referentiel = (numNomSel == undefined) ? '' : this.nomSciReferentiel;
2360 jpm 374
 
2711 mathias 375
	$('#liste-obs').data('obsId'+this.obsNbre, {
2372 jpm 376
		'date' : $('#date').val(),
377
		'notes' : $('#notes').val(),
2360 jpm 378
 
2372 jpm 379
		'nom_sel': nomSel,
380
		'num_nom_sel': numNomSel,
381
		'nom_ret': nomRet,
382
		'num_nom_ret': numNomRet,
383
		'num_taxon': numTaxon,
384
		'famille': famille,
385
		'referentiel': referentiel,
2360 jpm 386
 
2372 jpm 387
		'latitude' : $('#latitude').val(),
388
		'longitude' : $('#longitude').val(),
389
		'commune_nom' : $('#commune-nom').text(),
390
		'commune_code_insee' : $('#commune-code-insee').text(),
391
		'altitude': $('#altitude').text(),
392
		'lieudit': $('#lieudit').val(),
2711 mathias 393
		'milieu': this.getMilieux(),
2372 jpm 394
		'certitude': $('#certitude').val(),
395
 
2360 jpm 396
		//Ajout des champs images
2711 mathias 397
		'image_nom' :this. getNomsImgsOriginales(),
2360 jpm 398
 
399
		// Ajout des champs étendus de l'obs
2711 mathias 400
		'obs_etendue': this.getObsChpEtendus()
2360 jpm 401
	});
2711 mathias 402
};