Subversion Repositories Sites.obs-saisons.fr

Rev

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

Rev Author Line No. Line
168 aurelien 1
var urlBaseJrest = 'http://localhost/obs_saisons/applications/jrest/';
2
 
54 aurelien 3
/**
4
	************************************************************************************************
5
	************************************************************************************************
6
 
7
	   Fonction permettant de transformer les élements de la classe pliage en accordéon
8
 
9
	************************************************************************************************
10
	************************************************************************************************
11
**/
12
function initialiserElementsPliables() {
13
 
14
	$('.pliage h4').addClass('lien_pliage');
15
 
16
	$('.pliage ul').hide();
17
	$('.pliage > ul:first-child').hide();
188 aurelien 18
 
19
	$('.1er_element_date ul').show();
20
	$('.1er_element_date > ul:first-child').show();
54 aurelien 21
 
22
	$('.lien_pliage').bind('click', function() {
23
 
24
		$(this).nextAll('ul').slideToggle();
25
		return false;
26
	});
27
}
28
 
29
$('.pliage').ready(function() {
30
	initialiserElementsPliables();
31
});
32
 
188 aurelien 33
/**
34
************************************************************************************************
35
************************************************************************************************
54 aurelien 36
 
188 aurelien 37
   Fonctions pour la fiche individu
54 aurelien 38
 
188 aurelien 39
************************************************************************************************
40
************************************************************************************************
41
**/
54 aurelien 42
 
188 aurelien 43
observations = $('.observations_individu');
44
window.alert(dump(observations));
45
observations[0].ready(function() {
46
	window.alert(dump(observations));
47
	$(this).nextAll('ul').show();
48
});
49
 
50
 
54 aurelien 51
/**
52
	************************************************************************************************
53
	************************************************************************************************
54
 
55
	   Fonctions de gestion des onglets du formulaire de saisie d'espèce
56
 
57
	************************************************************************************************
58
	************************************************************************************************
59
**/
60
function initialiserOnglets() {
61
	$(".contenu_onglet").hide();
62
	$("ul.liste_onglets li:first").addClass("active").show();
63
	$(".contenu_onglet:first").show();
64
 
65
	$("ul.liste_onglets li").click(function() {
66
 
67
		$("ul.liste_onglets li").removeClass("active");
68
		$(this).addClass("active");
69
		$(".contenu_onglet").hide();
70
 
71
		var activeTab = $(this).find("a").attr("href");
72
		$(activeTab).fadeIn();
73
		return false;
74
	});
75
}
76
 
77
$('.contenu_onglet').ready(function() {
78
	initialiserOnglets();
79
});
80
 
81
function agrandirZoneCLicBoutonsRadios() {
82
 
83
	$('.formulaire_informations_espece_mini').addClass("element_cliquable");
84
 
85
	$('#form_saisie_espece').children('input[type="submit"]').hide();
86
	$('.formulaire_informations_espece_mini').children('input[type="radio"]').hide();
87
	$('.titre_type').hide();
88
 
89
	$('.formulaire_informations_espece_mini').bind('click',function() {
90
		$(this).children('input[type="radio"]').attr('checked','checked');
91
		$('.formulaire_informations_espece_mini').removeClass('element_clique');
92
		$(this).addClass('element_clique');
93
 
94
		$('#form_saisie_espece').submit();
95
	});
96
}
97
 
98
$('#form_saisie_espece').ready(function() {
99
	agrandirZoneCLicBoutonsRadios();
100
});
101
 
102
 
103
/**
104
	************************************************************************************************
105
	************************************************************************************************
106
 
107
	   Fonctions permettant de transformer les cases du tableau d'évenements en mini formulaire
108
 
109
	************************************************************************************************
110
	************************************************************************************************
111
**/
112
 
113
htmlEnCours = '';
114
elementEnCours = null;
115
 
116
function initialiserLignesCliquables() {
117
 
118
	$('.conteneur_element_modifier').hide();
119
 
120
	$(".date_observation_individu").each(function() {
121
 
122
		$(this).bind('click', function() {
123
 
124
			if($(this).hasClass('element_clique')) {
125
				return false;
126
			} else {
127
 
128
				conteneur_obs = $(this).children('span');
129
				id_formulaire = conteneur_obs.attr('id');
130
 
131
				remplacerElementDateParFormulaireAjax(id_formulaire, $(this));
132
 
133
				return false;
134
			}
135
		});
136
	});
137
}
138
 
139
function initialiserCalendrierFormulaire() {
140
 
141
	anneeEnCours = $("#annee_en_cours").attr("value");
142
 
143
	if(typeof(anneeEnCours)=='undefined'){
144
		dateCourante = new Date();
145
		anneeEnCours = dateCourante.getFullYear();
146
	}
147
 
148
	$(".calendrier").each(function() {
149
		$(this).find("input").datepicker({
150
			disabled: true,
151
			altFormat: 'dd/mm/yyyy',
152
			minDate: '01/01/'+anneeEnCours,
153
			maxDate: '31/12/'+anneeEnCours
154
 
155
		});
156
	});
157
}
158
 
159
$(document).ready(function() {
160
	initialiserCalendrierFormulaire();
161
 
162
});
163
 
90 aurelien 164
function estUneDateValide(peutEtreUneDate) {
165
	var reg= new RegExp("^[0-3][0-9][/]{1}[0-1][0-2][/]{1}[0-9]{4}$","g");
166
	return reg.test(peutEtreUneDate);
167
}
168
 
54 aurelien 169
/** Fonctions de manipulation des éléments du tableau pour les transformer en élements cliquables et calendriers **/
170
function initialiserMiniCalendrierFormulaire(id_element) {
171
 
172
	anneeEnCours = jQuery.trim($("#annee_en_cours").html());
173
 
174
	if(typeof(anneeEnCours)=='undefined'){
175
		dateCourante = new Date();
176
		anneeEnCours = dateCourante.getFullYear();
177
	}
178
 
90 aurelien 179
	$('#'+id_element)
180
	.datepicker({
54 aurelien 181
		disabled: true,
90 aurelien 182
		constrainInput: true,
54 aurelien 183
		altFormat: 'dd/mm/yyyy',
184
		minDate: '01/01/'+anneeEnCours,
90 aurelien 185
		maxDate: '31/12/'+anneeEnCours,
186
		onSelect: function(dateText, inst) {
187
			$('#'+id_element).val(dateText);
188
			donnees_obs = collecterDonneesMiniFormulaire(id_formulaire);
189
			envoyerRequeteAjaxValidationMiniFormulaire(donnees_obs, id_formulaire);
190
		}
54 aurelien 191
	});
90 aurelien 192
 
193
	$('#'+id_element).datepicker( "show" );
194
 
195
	/*$('#'+id_element).keypress(function() {
196
		if(estUneDateValide($('#'+id_element).val())) {
197
			$('#'+id_element).removeClass('erreur_valeur');
198
		} else {
199
			$('#'+id_element).addClass('erreur_valeur');
200
		}
201
 
202
	});*/
54 aurelien 203
}
204
 
205
function remplacerElementDateParFormulaireAjax(id_element, conteneur_selectionne) {
206
 
207
	if(elementEnCours != null) {
208
		elementEnCours.removeClass('element_clique');
209
		elementEnCours.html(htmlEnCours);
210
	}
211
 
212
	elementEnCours = conteneur_selectionne;
213
	htmlEnCours = conteneur_selectionne.html();
214
 
215
	conteneur_obs = $('#'+id_element);
216
	valeur_date = conteneur_obs.html();
217
	valeur_date = valeur_date.replace('-','');
90 aurelien 218
 
54 aurelien 219
	conteneur_selectionne.addClass('element_clique');
90 aurelien 220
	conteneur_selectionne.html('<span class="calendrier"><input type="text" value="'+jQuery.trim(valeur_date)+'" class="calendrier" id="'+id_formulaire+'" size="7" maxlenght="10" /></div>');
54 aurelien 221
	initialiserMiniCalendrierFormulaire(id_formulaire);
222
 
223
}
224
 
225
function remplacerMiniFormulaireParElementDate(id_element) {
226
 
90 aurelien 227
	valeur_date_pour_stade = $('#'+id_element).val();
228
 
229
	/*if(!estUneDateValide(valeur_date_pour_stade)) {
230
		valeur_date_pour_stade = htmlEnCours;
231
	}*/
232
 
54 aurelien 233
	elementEnCours.html('<span id="'+id_element+'">'+valeur_date_pour_stade+' </span>');
234
	elementEnCours.removeClass('element_clique');
235
	elementEnCours = null;
100 aurelien 236
 
54 aurelien 237
}
238
 
239
 
240
/** Fonctions d'envoi et de récupération des élements du formulaire ajax **/
241
function envoyerRequeteAjaxValidationMiniFormulaire(donnees_obs, id_formulaire) {
242
 
100 aurelien 243
	url_page_courante = document.URL;
244
	url_page_courante = url_page_courante.replace('module=Individu','module=Observation');
245
	url_page_courante = url_page_courante.replace('action=afficherListeIndividu','action=validerFormulaireModificationObservationAjax');
246
	url_ajax = url_page_courante.replace('action=validerFormulaireSaisieIndividu','action=validerFormulaireModificationObservationAjax');
247
 
248
	$.post(url_ajax, donnees_obs, function(obj_retour) {
249
 
54 aurelien 250
		if(obj_retour.reponse === 'OK') {
251
			remplacerMiniFormulaireParElementDate(id_formulaire);
252
		}
253
	});
254
}
255
 
256
function collecterDonneesMiniFormulaire(id_formulaire) {
257
 
258
	valeur_date_pour_stade = $('#'+id_formulaire).val();
259
 
260
	if(valeur_date_pour_stade == null || jQuery.trim(valeur_date_pour_stade) == '') {
261
		return false ;
262
	}
263
 
264
	id_individu_stade_obs = id_formulaire.split('_');
265
 
266
	stade_obs = 'observation_'+id_individu_stade_obs[3];
267
	date_annee_en_cours = jQuery.trim($("#annee_en_cours").html());
268
 
269
	donnees_obs = { "id_individu": id_individu_stade_obs[1],
90 aurelien 270
					"annee_en_cours": date_annee_en_cours
54 aurelien 271
					};
272
 
90 aurelien 273
	identifiant_observation = 'observation_'+id_individu_stade_obs[3];
54 aurelien 274
 
90 aurelien 275
	donnees_obs[identifiant_observation] = valeur_date_pour_stade;
276
 
54 aurelien 277
	return donnees_obs;
278
}
279
 
280
 
281
$('#saisie_liste_evenements').ready(function() {
282
	initialiserLignesCliquables();
283
});
284
 
285
 
286
 
287
 
288
/**
289
	************************************************************************************************
290
	************************************************************************************************
291
 
292
	    Fonctions concernant la carte permettant de pointer d'afficher l'emplacement des stations
293
 
294
	************************************************************************************************
295
	************************************************************************************************
296
**/
297
var map;
298
var marker;
100 aurelien 299
var liste_localite_en_cours;
300
var indice_commune_en_cours;
301
var liste_auto_completion_a_le_focus;
302
var timerRequeteAutocompletion;
303
var timerAffichageAutocompletion;
54 aurelien 304
 
100 aurelien 305
 
306
function cacherElementsRafraichissables() {
307
	$('.rafraichissable input').attr('disabled', 'disabled');
308
	$('.rafraichissable input[type="text"]').addClass("chargement");
309
}
310
 
311
function montrerElementsRafraichissables() {
312
	$('.rafraichissable input').removeAttr('disabled');
313
	$('.chargement').removeClass("chargement");
314
}
315
 
316
function ajouterAutoCompletionCommune() {
317
 
318
	$('input#station_commune').after('<div class="conteneur_suggestions"></div>');
319
	$('input#station_commune').parent().addClass('autocompletion');
320
	$('input#station_commune').attr('autocomplete','off');
321
	$('.conteneur_suggestions').hide();
322
	rendreListeAutoCompletionInteractive('input#station_commune');
323
}
324
 
54 aurelien 325
function ajouterListenerFormulaireSaisieLatLon() {
100 aurelien 326
 
54 aurelien 327
	$('input#localiser_lat_lon').click(function() {
328
		verifierEtLocaliserCoordonnees();
329
	});
330
}
331
 
332
function carteEstEnSaisie() {
333
	return ($('#conteneur_form_liens_lat_lon').length > 0);
334
}
335
 
336
function verifierEtLocaliserCoordonnees() {
337
 
338
	lat ;
339
	lon;
340
 
341
	if(carteEstEnSaisie()) {
342
		lat = $('#station_lat').val();
343
		lon = $('#station_lon').val();
100 aurelien 344
 
345
		obtenirInformationsPourCoordonnees(lat, lon);
54 aurelien 346
	} else {
347
		var lat = jQuery.trim($('#station_lat').html());
348
		var lon = jQuery.trim($('#station_lon').html());
349
	}
350
 
351
	if(jQuery.trim(lat) == '' || jQuery.trim(lon) == '') {
352
		return;
353
	}
354
 
355
	if(!isNaN(lat) && lat.length > 0 && !isNaN(lon) && lon.length > 0) {
356
 
357
	} else {
358
		window.alert("coordonnées invalides");
359
	}
360
 
361
	var positionMarker = new google.maps.LatLng(lat, lon);
362
 
363
	marker.setPosition(positionMarker);
364
	map.setCenter(positionMarker);
100 aurelien 365
	map.setZoom(12);
54 aurelien 366
}
367
 
100 aurelien 368
function obtenirInformationsPourCoordonnees(lat, lon) {
369
 
370
	lat = jQuery.trim(lat);
371
	lon = jQuery.trim(lon);
372
 
373
	if(isNaN(lat) || lat.length <= 0 || isNaN(lon) || lon.length <= 0) {
374
		return;
375
	}
376
 
377
	cacherElementsRafraichissables();
378
 
168 aurelien 379
	$.get(urlBaseJrest+'OdsCommune/informationsPourCoordonnees/?lat='+lat+'&lon='+lon, function(data) {
100 aurelien 380
 
381
		infos_localites = jQuery.parseJSON(data);
382
		$('#station_alt').val(infos_localites.alt);
383
 
384
		if(jQuery.trim($('#station_commune').val()) == '') {
385
			$('#station_commune').val(infos_localites.commune);
386
		}
387
 
388
		montrerElementsRafraichissables();
389
 
390
	});
391
 
392
}
393
 
394
function obtenirInformationsPourCommune(nom_commune) {
395
 
396
	if(jQuery.trim(nom_commune) == '') {
397
		$('.conteneur_suggestions').hide();
398
		return;
399
	}
400
 
168 aurelien 401
	$.get(urlBaseJrest+'OdsCommune/informationsPourCommune/?commune='+nom_commune, function(data) {
100 aurelien 402
 
403
		infos_localites = jQuery.parseJSON(data);
404
		afficherListeAutoCompletion(infos_localites);
405
	});
406
}
407
 
408
function afficherListeAutoCompletion(tableau_localites) {
409
 
410
	liste_localite_en_cours = tableau_localites;
411
 
412
	html_liste_localite = '<ul class="liste_suggestions">';
413
 
414
	for(i = 0; i< tableau_localites.length; i++) {
415
		html_liste_localite += '<li id="commune_'+i+'" class="element_auto_completion_commune">'+tableau_localites[i]['commune']+'  ('+tableau_localites[i]['dpt']+')</li>';
416
	}
417
 
418
	html_liste_localite += '</ul>';
419
 
420
	$('.conteneur_suggestions').html(html_liste_localite);
421
 
422
	$('.element_auto_completion_commune').hover(function() {
423
		indice = $(this).attr('id').split('_')[1];
424
		mettreEnSurbrillanceCommune(indice);
425
	});
426
 
427
	$('.element_auto_completion_commune').click(function() {
428
		indice = $(this).attr('id').split('_')[1];
429
		selectionnerCommune(indice);
430
	});
431
 
432
	if(tableau_localites.length > 0) {
433
		$('.conteneur_suggestions').show();
434
 
435
		mettreEnSurbrillanceCommune(0);
436
	}
437
}
438
 
439
function mettreEnSurbrillanceCommune(indice) {
440
 
441
	if(indice_commune_en_cours != null) {
442
		$('#commune_'+indice_commune_en_cours).removeClass('element_selectionne');
443
	}
444
	$('#commune_'+indice).addClass('element_selectionne');
445
	indice_commune_en_cours = indice;
446
 
447
	reprogrammerTimerPourCacherListeAutoCompletion();
448
}
449
 
450
function rendreListeAutoCompletionInteractive(selecteur) {
451
 
452
	$(selecteur).keyup(function(event) {
453
		if(event.which == 8 || (event.which >= 48 && event.which <= 90)) {
454
 
455
			reprogrammerTimerPourCacherListeAutoCompletion();
456
			reprogrammerTimerPourLancerRequeteCommune();
457
		}
458
	});
459
 
460
	$(selecteur).keydown(function(event) {
461
 
462
		reprogrammerTimerPourCacherListeAutoCompletion();
463
 
464
		// entree
465
		if(event.which==13){
466
			selectionnerCommune(indice_commune_en_cours);
467
			event.preventDefault();
468
		}
469
 
470
		// haut
471
		if(event.which == 38){
472
			if(indice_commune_en_cours > 0) {
473
				mettreEnSurbrillanceCommune(indice_commune_en_cours - 1);
474
			}
475
		}
476
 
477
		// bas
478
		if(event.which == 40){
479
			if(indice_commune_en_cours < liste_localite_en_cours.length - 1) {
480
				mettreEnSurbrillanceCommune(indice_commune_en_cours + 1);
481
			}
482
		}
483
	});
484
 
485
	$('.conteneur_suggestions').blur(function() {
486
		$('.conteneur_suggestions').hide();
487
	});
488
}
489
 
490
function reprogrammerTimerPourCacherListeAutoCompletion() {
491
 
492
	if(timerAffichageAutocompletion != null) {
493
		window.clearTimeout(timerAffichageAutocompletion);
494
	}
495
 
496
	timerAffichageAutocompletion = window.setTimeout(function() {
497
		$('.conteneur_suggestions').hide();
498
	}, 2000);
499
}
500
 
501
function reprogrammerTimerPourLancerRequeteCommune() {
502
 
503
	if(timerRequeteAutocompletion != null) {
504
		window.clearTimeout(timerRequeteAutocompletion);
505
	}
506
 
507
	timerRequeteAutocompletion = window.setTimeout(function() {
508
		obtenirInformationsPourCommune($('input#station_commune').val());
509
	}, 350);
510
}
511
 
512
function selectionnerCommune(indice) {
513
 
514
	infos_commune = liste_localite_en_cours[indice];
515
 
516
	$('input#station_commune').val(infos_commune['commune']);
517
	$('input#station_lat').val(infos_commune['lat']);
518
	$('input#station_lon').val(infos_commune['lon']);
168 aurelien 519
	$('input#station_code_insee').val(infos_commune['code_insee']);
100 aurelien 520
 
521
	verifierEtLocaliserCoordonnees();
522
 
523
	$('.conteneur_suggestions').hide();
524
}
525
 
54 aurelien 526
function mettreAJourValeursFormulaire(latlon) {
527
 
528
	latlon = latlon.toString().split(',');
529
 
100 aurelien 530
	lat = latlon[0].replace('(', '');
531
	lon = latlon[1].replace(')', '');
532
 
54 aurelien 533
	$('#station_lat').val(latlon[0].replace('(', ''));
534
	$('#station_lon').val(latlon[1].replace(')', ''));
100 aurelien 535
 
536
	obtenirInformationsPourCoordonnees(lat, lon);
54 aurelien 537
}
538
 
539
function initialiserCarte() {
540
 
541
	if($('#map_canvas').length == 0) {
542
		return;
543
	}
544
 
545
	var latlng = new google.maps.LatLng(47.0504, 2.2347);
546
	var myOptions = {
547
		zoom: 6,
548
		center: latlng,
549
		mapTypeId: google.maps.MapTypeId.HYBRID
550
	};
551
 
552
	map = new google.maps.Map(document.getElementById("map_canvas"),
553
    	myOptions);
554
 
555
	marker = new google.maps.Marker({
556
	      position: latlng,
557
	      title:""
558
	});
559
 
560
	if (carteEstEnSaisie()) {
561
 
562
			marker.setDraggable(true);
563
 
100 aurelien 564
			google.maps.event.addListener(marker, 'dragend', function(event) {
54 aurelien 565
				mettreAJourValeursFormulaire(marker.getPosition());
566
			});
567
			$('#conteneur_form_liens_lat_lon').ready(function() {
568
				verifierEtLocaliserCoordonnees();
569
				ajouterListenerFormulaireSaisieLatLon();
100 aurelien 570
				ajouterAutoCompletionCommune();
54 aurelien 571
			});
572
	} else {
573
		marker.setDraggable(false);
574
		$('#conteneur_liens_lat_lon').ready(function() {
575
			verifierEtLocaliserCoordonnees();
576
		});
577
	}
578
 
579
	marker.setClickable(true);
580
 
581
	// To add the marker to the map, call setMap();
582
	marker.setMap(map);
583
}
584
 
585
$('#map_canvas').ready(function() {
586
	initialiserCarte();
100 aurelien 587
});
588
 
589
 
590
/**
591
************************************************************************************************
592
************************************************************************************************
593
 
594
    Fonctions de debug permattant d'afficher les objets javascript à la manière de print_r
595
 
596
************************************************************************************************
597
************************************************************************************************
598
**/
599
 
600
function dump(arr,level) {
601
	var dumped_text = "";
602
	if(!level) level = 0;
603
 
604
	//The padding given at the beginning of the line.
605
	var level_padding = "";
606
	for(var j=0;j<level+1;j++) level_padding += "    ";
607
 
608
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
609
	 for(var item in arr) {
610
	  var value = arr[item];
611
 
612
	  if(typeof(value) == 'object') { //If it is an array,
613
	   dumped_text += level_padding + "'" + item + "' ...\n";
614
	   dumped_text += dump(value,level+1);
615
	  } else {
616
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
617
	  }
618
	 }
619
	} else { //Stings/Chars/Numbers etc.
620
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
621
	}
622
	return dumped_text;
623
}