Subversion Repositories eFlore/Applications.cel

Rev

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

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