Subversion Repositories Sites.obs-saisons.fr

Rev

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